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 va_start(ap, msg);
100 put_impl(msg, DEBUG, ap);
101 va_end(ap);
102}

◆ error()

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

Definition at line 128 of file LogFile.cc.

129{
130 va_list ap;
131 va_start(ap, msg);
132 put_impl(msg, ERROR, ap);
133 va_end(ap);
134}

◆ fatal()

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

Definition at line 136 of file LogFile.cc.

137{
138 va_list ap;
139 va_start(ap, msg);
140 put_impl(msg, FATAL, ap);
141 va_end(ap);
142}

◆ 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 104 of file LogFile.cc.

105{
106 va_list ap;
107 va_start(ap, msg);
108 put_impl(msg, INFO, ap);
109 va_end(ap);
110}

◆ notice()

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

Definition at line 112 of file LogFile.cc.

113{
114 va_list ap;
115 va_start(ap, msg);
116 put_impl(msg, NOTICE, ap);
117 va_end(ap);
118}

◆ 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 145 of file LogFile.cc.

146{
147 va_list ap;
148 va_start(ap, msg);
149 put_impl(msg, priority, ap);
150 va_end(ap);
151}

◆ put_impl()

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

Definition at line 153 of file LogFile.cc.

154{
155 LockGuard lockGuard(g_mutex);
156 if (g_threshold > priority) {
157 return 0;
158 }
159 Date date;
160 if (g_date.getDay() != date.getDay()) {
161 g_stream.close();
162 open();
163 }
164 std::stringstream ss;
165 ss << "[" << date.toString();
166 std::string color = "\x1b[49m\x1b[39m";
167 switch (priority) {
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;
175 }
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";
181 if (g_opened) {
182 g_stream << str;
183 g_stream.flush();
184 g_filesize += str.size();
185 }
186 return (int) str.size();
187}
Lock Guard for a Mutex instance.
Definition: LockGuard.h:47

◆ 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 120 of file LogFile.cc.

121{
122 va_list ap;
123 va_start(ap, msg);
124 put_impl(msg, WARNING, ap);
125 va_end(ap);
126}

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: