Belle II Software development
LogFile.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include "daq/slc/system/LogFile.h"
9
10#include <daq/slc/base/ConfigFile.h>
11#include <daq/slc/base/StringUtil.h>
12
13#include <iostream>
14#include <sstream>
15#include <cstdio>
16
17#include <sys/stat.h>
18#include <cstdlib>
19#include <daq/slc/system/LockGuard.h>
20
21using namespace Belle2;
22
23bool LogFile::g_stderr = true;
24bool LogFile::g_opened = false;
25std::string LogFile::g_filepath;
26std::string LogFile::g_linkpath;
27std::ofstream LogFile::g_stream;
28unsigned int LogFile::g_filesize = 0;
29Mutex LogFile::g_mutex;
30LogFile::Priority LogFile::g_threshold;
31std::string LogFile::g_filename;
32Date LogFile::g_date;
33
34LogFile::Priority LogFile::getPriority(const std::string& str)
35{
36 const std::string s = StringUtil::toupper(str);
37 if (s == "DEBUG") {
38 return DEBUG;
39 } else if (s == "INFO") {
40 return INFO;
41 } else if (s == "NOTICE") {
42 return NOTICE;
43 } else if (s == "WARNING") {
44 return WARNING;
45 } else if (s == "ERROR") {
46 return ERROR;
47 } else if (s == "FATAL") {
48 return FATAL;
49 }
50 return UNKNOWN;
51}
52
53void LogFile::open(const std::string& filename, Priority threshold)
54{
55 if (!g_opened) {
56 ConfigFile config("slowcontrol");
57 std::string path = config.get("log.dir");
58 //if (path.size() == 0) path = config.get("logfile.dir");
59 system(("mkdir -p " + path + "/" + filename).c_str());
60 g_filename = filename;
61 g_date = Date();
62 g_filepath = path + StringUtil::form("/%s/%s.log", filename.c_str(), g_date.toString("%Y.%m.%d"));
63 g_linkpath = path + StringUtil::form("/%s/latest.log", filename.c_str());
64 //"/latest.log";
65 g_threshold = threshold;
66 g_opened = true;
67 open();
68 }
69}
70
71void LogFile::open()
72{
73 if (!g_opened) return;
74 struct stat st;
75 if (stat(g_filepath.c_str(), &st) == 0) {
76 g_filesize = st.st_size;
77 g_stream.open(g_filepath.c_str(), std::ios::out | std::ios::app);
78 } else {
79 g_filesize = 0;
80 g_stream.open(g_filepath.c_str(), std::ios::out);
81 }
82 debug("/* ---------- log file opened ---------- */");
83 debug("log file : %s (%d) ", g_filepath.c_str(), g_filesize);
84 std::string cmd = "ln -sf " + g_filepath + " " + g_linkpath;
85 system(cmd.c_str());
86 debug("sym link : %s", g_linkpath.c_str());
87}
88
89void LogFile::close()
90{
91 if (!g_opened) return;
92 g_stream.close();
93 g_opened = false;
94}
95
96void LogFile::debug(const std::string& msg, ...)
97{
98 va_list ap;
99 // cppcheck-suppress va_start_referencePassed ; the format is taken by const reference throughout this API
100 va_start(ap, msg);
101 put_impl(msg, DEBUG, ap);
102 va_end(ap);
103}
104
105void LogFile::info(const std::string& msg, ...)
106{
107 va_list ap;
108 // cppcheck-suppress va_start_referencePassed ; the format is taken by const reference throughout this API
109 va_start(ap, msg);
110 put_impl(msg, INFO, ap);
111 va_end(ap);
112}
113
114void LogFile::notice(const std::string& msg, ...)
115{
116 va_list ap;
117 // cppcheck-suppress va_start_referencePassed ; the format is taken by const reference throughout this API
118 va_start(ap, msg);
119 put_impl(msg, NOTICE, ap);
120 va_end(ap);
121}
122
123void LogFile::warning(const std::string& msg, ...)
124{
125 va_list ap;
126 // cppcheck-suppress va_start_referencePassed ; the format is taken by const reference throughout this API
127 va_start(ap, msg);
128 put_impl(msg, WARNING, ap);
129 va_end(ap);
130}
131
132void LogFile::error(const std::string& msg, ...)
133{
134 va_list ap;
135 // cppcheck-suppress va_start_referencePassed ; the format is taken by const reference throughout this API
136 va_start(ap, msg);
137 put_impl(msg, ERROR, ap);
138 va_end(ap);
139}
140
141void LogFile::fatal(const std::string& msg, ...)
142{
143 va_list ap;
144 // cppcheck-suppress va_start_referencePassed ; the format is taken by const reference throughout this API
145 va_start(ap, msg);
146 put_impl(msg, FATAL, ap);
147 va_end(ap);
148}
149
150
151void LogFile::put(Priority priority, const std::string& msg, ...)
152{
153 va_list ap;
154 // cppcheck-suppress va_start_referencePassed ; the format is taken by const reference throughout this API
155 va_start(ap, msg);
156 put_impl(msg, priority, ap);
157 va_end(ap);
158}
159
160int LogFile::put_impl(const std::string& msg, Priority priority, va_list ap)
161{
162 LockGuard lockGuard(g_mutex);
163 if (g_threshold > priority) {
164 return 0;
165 }
166 Date date;
167 if (g_date.getDay() != date.getDay()) {
168 g_stream.close();
169 open();
170 }
171 std::stringstream ss;
172 ss << "[" << date.toString();
173 std::string color = "\x1b[49m\x1b[39m";
174 switch (priority) {
175 case DEBUG: color = "\x1b[49m\x1b[39m"; ss << "] [DEBUG] "; break;
176 case INFO: color = "\x1b[49m\x1b[32m"; ss << "] [INFO] "; break;
177 case NOTICE: color = "\x1b[49m\x1b[34m"; ss << "] [NOTICE] "; break;
178 case WARNING: color = "\x1b[49m\x1b[35m"; ss << "] [WARNING] "; break;
179 case ERROR: color = "\x1b[49m\x1b[31m"; ss << "] [ERROR] "; break;
180 case FATAL: color = "\x1b[41m\x1b[37m"; ss << "] [FATAL] "; break;
181 default: ss << "] [UNKNOWN] "; break;
182 }
183 static char* s = new char[1024 * 1024 * 5];
184 vsnprintf(s, 1024 * 1024 * 5, msg.c_str(), ap);
185 ss << s << std::endl;
186 std::string str = ss.str();
187 std::cerr << color << str << "\x1b[49m\x1b[39m";
188 if (g_opened) {
189 g_stream << str;
190 g_stream.flush();
191 g_filesize += str.size();
192 }
193 return (int) str.size();
194}
Abstract base class for different kinds of events.