8#include "daq/slc/system/LogFile.h"
10#include <daq/slc/base/ConfigFile.h>
11#include <daq/slc/base/StringUtil.h>
19#include <daq/slc/system/LockGuard.h>
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;
34LogFile::Priority LogFile::getPriority(
const std::string& str)
36 const std::string s = StringUtil::toupper(str);
39 }
else if (s ==
"INFO") {
41 }
else if (s ==
"NOTICE") {
43 }
else if (s ==
"WARNING") {
45 }
else if (s ==
"ERROR") {
47 }
else if (s ==
"FATAL") {
53void LogFile::open(
const std::string& filename, Priority threshold)
57 std::string path = config.get(
"log.dir");
59 system((
"mkdir -p " + path +
"/" + filename).c_str());
60 g_filename = filename;
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());
65 g_threshold = threshold;
73 if (!g_opened)
return;
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);
80 g_stream.open(g_filepath.c_str(), std::ios::out);
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;
86 debug(
"sym link : %s", g_linkpath.c_str());
91 if (!g_opened)
return;
96void LogFile::debug(
const std::string& msg, ...)
100 put_impl(msg, DEBUG, ap);
104void LogFile::info(
const std::string& msg, ...)
108 put_impl(msg, INFO, ap);
112void LogFile::notice(
const std::string& msg, ...)
116 put_impl(msg, NOTICE, ap);
120void LogFile::warning(
const std::string& msg, ...)
124 put_impl(msg, WARNING, ap);
128void LogFile::error(
const std::string& msg, ...)
132 put_impl(msg, ERROR, ap);
136void LogFile::fatal(
const std::string& msg, ...)
140 put_impl(msg, FATAL, ap);
145void LogFile::put(Priority priority,
const std::string& msg, ...)
149 put_impl(msg, priority, ap);
153int LogFile::put_impl(
const std::string& msg, Priority priority, va_list ap)
156 if (g_threshold > priority) {
160 if (g_date.getDay() != date.getDay()) {
164 std::stringstream ss;
165 ss <<
"[" << date.toString();
166 std::string color =
"\x1b[49m\x1b[39m";
168 case DEBUG: color =
"\x1b[49m\x1b[39m"; ss <<
"] [DEBUG] ";
break;
169 case INFO: color =
"\x1b[49m\x1b[32m"; ss <<
"] [INFO] ";
break;
170 case NOTICE: color =
"\x1b[49m\x1b[34m"; ss <<
"] [NOTICE] ";
break;
171 case WARNING: color =
"\x1b[49m\x1b[35m"; ss <<
"] [WARNING] ";
break;
172 case ERROR: color =
"\x1b[49m\x1b[31m"; ss <<
"] [ERROR] ";
break;
173 case FATAL: color =
"\x1b[41m\x1b[37m"; ss <<
"] [FATAL] ";
break;
174 default: ss <<
"] [UNKNOWN] ";
break;
176 static char* s =
new char[1024 * 1024 * 5];
177 vsnprintf(s, 1024 * 1024 * 5, msg.c_str(), ap);
178 ss << s << std::endl;
179 std::string str = ss.str();
180 std::cerr << color << str <<
"\x1b[49m\x1b[39m";
184 g_filesize += str.size();
186 return (
int) str.size();
Lock Guard for a Mutex instance.
Abstract base class for different kinds of events.