Belle II Software  release-05-02-19
LogFile.cc
1 #include "daq/slc/system/LogFile.h"
2 
3 #include <daq/slc/base/ConfigFile.h>
4 #include <daq/slc/base/StringUtil.h>
5 
6 #include <iostream>
7 #include <sstream>
8 #include <cstdio>
9 
10 #include <sys/stat.h>
11 #include <cstdlib>
12 #include <daq/slc/system/LockGuard.h>
13 
14 using namespace Belle2;
15 
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;
25 Date LogFile::g_date;
26 
27 LogFile::Priority LogFile::getPriority(const std::string& str)
28 {
29  const std::string s = StringUtil::toupper(str);
30  if (s == "DEBUG") {
31  return DEBUG;
32  } else if (s == "INFO") {
33  return INFO;
34  } else if (s == "NOTICE") {
35  return NOTICE;
36  } else if (s == "WARNING") {
37  return WARNING;
38  } else if (s == "ERROR") {
39  return ERROR;
40  } else if (s == "FATAL") {
41  return FATAL;
42  }
43  return UNKNOWN;
44 }
45 
46 void LogFile::open(const std::string& filename, Priority threshold)
47 {
48  if (!g_opened) {
49  ConfigFile config("slowcontrol");
50  std::string path = config.get("log.dir");
51  //if (path.size() == 0) path = config.get("logfile.dir");
52  system(("mkdir -p " + path + "/" + filename).c_str());
53  g_filename = filename;
54  g_date = Date();
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());
57  //"/latest.log";
58  g_threshold = threshold;
59  g_opened = true;
60  open();
61  }
62 }
63 
64 void LogFile::open()
65 {
66  if (!g_opened) return;
67  struct stat st;
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);
71  } else {
72  g_filesize = 0;
73  g_stream.open(g_filepath.c_str(), std::ios::out);
74  }
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;
78  system(cmd.c_str());
79  debug("sym link : %s", g_linkpath.c_str());
80 }
81 
82 void LogFile::close()
83 {
84  if (!g_opened) return;
85  g_stream.close();
86  g_opened = false;
87 }
88 
89 void LogFile::debug(const std::string& msg, ...)
90 {
91  va_list ap;
92  va_start(ap, msg);
93  put_impl(msg, DEBUG, ap);
94  va_end(ap);
95 }
96 
97 void LogFile::info(const std::string& msg, ...)
98 {
99  va_list ap;
100  va_start(ap, msg);
101  put_impl(msg, INFO, ap);
102  va_end(ap);
103 }
104 
105 void LogFile::notice(const std::string& msg, ...)
106 {
107  va_list ap;
108  va_start(ap, msg);
109  put_impl(msg, NOTICE, ap);
110  va_end(ap);
111 }
112 
113 void LogFile::warning(const std::string& msg, ...)
114 {
115  va_list ap;
116  va_start(ap, msg);
117  put_impl(msg, WARNING, ap);
118  va_end(ap);
119 }
120 
121 void LogFile::error(const std::string& msg, ...)
122 {
123  va_list ap;
124  va_start(ap, msg);
125  put_impl(msg, ERROR, ap);
126  va_end(ap);
127 }
128 
129 void LogFile::fatal(const std::string& msg, ...)
130 {
131  va_list ap;
132  va_start(ap, msg);
133  put_impl(msg, FATAL, ap);
134  va_end(ap);
135 }
136 
137 
138 void LogFile::put(Priority priority, const std::string& msg, ...)
139 {
140  va_list ap;
141  va_start(ap, msg);
142  put_impl(msg, priority, ap);
143  va_end(ap);
144 }
145 
146 int LogFile::put_impl(const std::string& msg, Priority priority, va_list ap)
147 {
148  LockGuard lockGuard(g_mutex);
149  if (g_threshold > priority) {
150  return 0;
151  }
152  Date date;
153  if (g_date.getDay() != date.getDay()) {
154  g_stream.close();
155  open();
156  }
157  std::stringstream ss;
158  ss << "[" << date.toString();
159  std::string color = "\x1b[49m\x1b[39m";
160  switch (priority) {
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;
168  }
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";
174  if (g_opened) {
175  g_stream << str;
176  g_stream.flush();
177  g_filesize += str.size();
178  }
179  return (int) str.size();
180 }
Belle2::Mutex
Definition: Mutex.h:12
Belle2::GenericLockGuard
Lock Guard for a Mutex instance.
Definition: LockGuard.h:40
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
alignment.constraints_generator.filename
filename
File name.
Definition: constraints_generator.py:224
Belle2::Date
Definition: Date.h:12
Belle2::ConfigFile
Definition: ConfigFile.h:15