Belle II Software development
LogFile Struct Reference

Public Types

enum  Priority {
  UNKNOWN = 0 ,
  DEBUG ,
  INFO ,
  NOTICE ,
  WARNING ,
  ERROR ,
  FATAL
}
 

Static Public Member Functions

static Priority getPriority (const std::string &str)
 
static void open (const std::string &filename, Priority priority=UNKNOWN)
 
static void open ()
 
static void close ()
 
static void debug (const std::string &msg,...)
 
static void info (const std::string &msg,...)
 
static void notice (const std::string &msg,...)
 
static void warning (const std::string &msg,...)
 
static void error (const std::string &msg,...)
 
static void fatal (const std::string &msg,...)
 
static void put (Priority priority, const std::string &msg,...)
 
static void setStdErr (bool stdErr)
 

Static Private Member Functions

static int put_impl (const std::string &msg, Priority priority, va_list ap)
 

Static Private Attributes

static bool g_stderr = true
 
static bool g_opened = false
 
static std::string g_filepath
 
static std::string g_linkpath
 
static std::ofstream g_stream
 
static unsigned int g_filesize = 0
 
static Mutex g_mutex
 
static Priority g_threshold
 
static std::string g_filename
 
static Date g_date
 

Detailed Description

Definition at line 25 of file LogFile.h.

Member Enumeration Documentation

◆ Priority

enum Priority

Definition at line 28 of file LogFile.h.

28 {
29 UNKNOWN = 0, DEBUG, INFO, NOTICE, WARNING, ERROR, FATAL
30 };

Constructor & Destructor Documentation

◆ LogFile()

LogFile ( )
inlineprivate

Definition at line 36 of file LogFile.h.

36{}

◆ ~LogFile()

~LogFile ( )
inlineprivate

Definition at line 37 of file LogFile.h.

37{}

Member Function Documentation

◆ close()

void close ( )
static

Definition at line 89 of file LogFile.cc.

90{
91 if (!g_opened) return;
92 g_stream.close();
93 g_opened = false;
94}

◆ debug()

void debug ( const std::string & msg,
... )
static

Definition at line 96 of file LogFile.cc.

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}

◆ error()

void error ( const std::string & msg,
... )
static

Definition at line 132 of file LogFile.cc.

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}

◆ fatal()

void fatal ( const std::string & msg,
... )
static

Definition at line 141 of file LogFile.cc.

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}

◆ getPriority()

LogFile::Priority getPriority ( const std::string & str)
static

Definition at line 34 of file LogFile.cc.

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}

◆ info()

void info ( const std::string & msg,
... )
static

Definition at line 105 of file LogFile.cc.

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}

◆ notice()

void notice ( const std::string & msg,
... )
static

Definition at line 114 of file LogFile.cc.

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}

◆ open() [1/2]

void open ( )
static

Definition at line 71 of file LogFile.cc.

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}

◆ open() [2/2]

void open ( const std::string & filename,
Priority priority = UNKNOWN )
static

Definition at line 53 of file LogFile.cc.

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}

◆ put()

void put ( Priority priority,
const std::string & msg,
... )
static

Definition at line 151 of file LogFile.cc.

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}

◆ put_impl()

int put_impl ( const std::string & msg,
Priority priority,
va_list ap )
staticprivate

Definition at line 160 of file LogFile.cc.

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}

◆ setStdErr()

static void setStdErr ( bool stdErr)
inlinestatic

Definition at line 64 of file LogFile.h.

65 {
66 g_stderr = stdErr;
67 }

◆ warning()

void warning ( const std::string & msg,
... )
static

Definition at line 123 of file LogFile.cc.

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}

Member Data Documentation

◆ g_date

Date g_date
staticprivate

Definition at line 49 of file LogFile.h.

◆ g_filename

std::string g_filename
staticprivate

Definition at line 48 of file LogFile.h.

◆ g_filepath

std::string g_filepath
staticprivate

Definition at line 42 of file LogFile.h.

◆ g_filesize

unsigned int g_filesize = 0
staticprivate

Definition at line 45 of file LogFile.h.

◆ g_linkpath

std::string g_linkpath
staticprivate

Definition at line 43 of file LogFile.h.

◆ g_mutex

Mutex g_mutex
staticprivate

Definition at line 46 of file LogFile.h.

◆ g_opened

bool g_opened = false
staticprivate

Definition at line 41 of file LogFile.h.

◆ g_stderr

bool g_stderr = true
staticprivate

Definition at line 40 of file LogFile.h.

◆ g_stream

std::ofstream g_stream
staticprivate

Definition at line 44 of file LogFile.h.

◆ g_threshold

LogFile::Priority g_threshold
staticprivate

Definition at line 47 of file LogFile.h.


The documentation for this struct was generated from the following files: