1 #include "daq/slc/system/LogFile.h"
3 #include <daq/slc/base/ConfigFile.h>
4 #include <daq/slc/base/StringUtil.h>
12 #include <daq/slc/system/LockGuard.h>
16 bool LogFile::g_stderr =
true;
17 bool LogFile::g_opened =
false;
18 std::string LogFile::g_filepath;
19 std::string LogFile::g_linkpath;
20 std::ofstream LogFile::g_stream;
21 unsigned int LogFile::g_filesize = 0;
22 Mutex LogFile::g_mutex;
23 LogFile::Priority LogFile::g_threshold;
24 std::string LogFile::g_filename;
27 LogFile::Priority LogFile::getPriority(
const std::string& str)
29 const std::string s = StringUtil::toupper(str);
32 }
else if (s ==
"INFO") {
34 }
else if (s ==
"NOTICE") {
36 }
else if (s ==
"WARNING") {
38 }
else if (s ==
"ERROR") {
40 }
else if (s ==
"FATAL") {
46 void LogFile::open(
const std::string& filename, Priority threshold)
50 std::string path = config.get(
"log.dir");
52 system((
"mkdir -p " + path +
"/" + filename).c_str());
55 g_filepath = path + StringUtil::form(
"/%s/%s.log",
filename.c_str(), g_date.toString(
"%Y.%m.%d"));
56 g_linkpath = path + StringUtil::form(
"/%s/latest.log",
filename.c_str());
58 g_threshold = threshold;
66 if (!g_opened)
return;
68 if (stat(g_filepath.c_str(), &st) == 0) {
69 g_filesize = st.st_size;
70 g_stream.open(g_filepath.c_str(), std::ios::out | std::ios::app);
73 g_stream.open(g_filepath.c_str(), std::ios::out);
75 debug(
"/* ---------- log file opened ---------- */");
76 debug(
"log file : %s (%d) ", g_filepath.c_str(), g_filesize);
77 std::string cmd =
"ln -sf " + g_filepath +
" " + g_linkpath;
79 debug(
"sym link : %s", g_linkpath.c_str());
84 if (!g_opened)
return;
89 void LogFile::debug(
const std::string& msg, ...)
93 put_impl(msg, DEBUG, ap);
97 void LogFile::info(
const std::string& msg, ...)
101 put_impl(msg, INFO, ap);
105 void LogFile::notice(
const std::string& msg, ...)
109 put_impl(msg, NOTICE, ap);
113 void LogFile::warning(
const std::string& msg, ...)
117 put_impl(msg, WARNING, ap);
121 void LogFile::error(
const std::string& msg, ...)
125 put_impl(msg, ERROR, ap);
129 void LogFile::fatal(
const std::string& msg, ...)
133 put_impl(msg, FATAL, ap);
138 void LogFile::put(Priority priority,
const std::string& msg, ...)
142 put_impl(msg, priority, ap);
146 int LogFile::put_impl(
const std::string& msg, Priority priority, va_list ap)
149 if (g_threshold > priority) {
153 if (g_date.getDay() != date.getDay()) {
157 std::stringstream ss;
158 ss <<
"[" << date.toString();
159 std::string color =
"\x1b[49m\x1b[39m";
161 case DEBUG: color =
"\x1b[49m\x1b[39m"; ss <<
"] [DEBUG] ";
break;
162 case INFO: color =
"\x1b[49m\x1b[32m"; ss <<
"] [INFO] ";
break;
163 case NOTICE: color =
"\x1b[49m\x1b[34m"; ss <<
"] [NOTICE] ";
break;
164 case WARNING: color =
"\x1b[49m\x1b[35m"; ss <<
"] [WARNING] ";
break;
165 case ERROR: color =
"\x1b[49m\x1b[31m"; ss <<
"] [ERROR] ";
break;
166 case FATAL: color =
"\x1b[41m\x1b[37m"; ss <<
"] [FATAL] ";
break;
167 default: ss <<
"] [UNKNOWN] ";
break;
169 static char* s =
new char[1024 * 1024 * 5];
170 vsnprintf(s, 1024 * 1024 * 5, msg.c_str(), ap);
171 ss << s << std::endl;
172 std::string str = ss.str();
173 std::cerr << color << str <<
"\x1b[49m\x1b[39m";
177 g_filesize += str.size();
179 return (
int) str.size();