-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogger.hpp
73 lines (61 loc) · 2.23 KB
/
Logger.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
NAME
Logger.hpp - Header file of the Logger class.
DESCRIPTION
Simplify write log to file.
*/
#ifndef __LOGGER_HPP__
#define __LOGGER_HPP__
#include <fstream>
#include <sstream>
#include <string>
#include <pthread.h>
class Logger
{
public:
enum logger_level {
min_level,
log_level_trace = min_level,
log_level_debug,
log_level_info,
log_level_warn,
log_level_error,
log_level_fatal,
max_level = log_level_fatal
};
public:
static void start(const std::string& filename);
static void stop();
static void write_message(const logger_level level, const std::string& file,
const unsigned int line, const std::string& func,
const std::string& msg);
protected:
static pthread_mutex_t* mutex;
static std::ofstream* out_stream;
private:
static const char* level_str[];
static const unsigned long _max_length;
static std::string _filename;
};
#ifndef CURRENT_LOGGER_LEVEL
#define CURRENT_LOGGER_LEVEL 0
#endif
#define LOG_TRACE_MSG(msg) if(Logger::log_level_trace >= CURRENT_LOGGER_LEVEL) \
Logger::write_message(Logger::log_level_trace, __FILE__, __LINE__, \
__FUNCTION__, msg)
#define LOG_DEBUG_MSG(msg) if(Logger::log_level_debug >= CURRENT_LOGGER_LEVEL) \
Logger::write_message(Logger::log_level_debug, __FILE__, __LINE__, \
__FUNCTION__, msg)
#define LOG_INFO_MSG(msg) if(Logger::log_level_info >= CURRENT_LOGGER_LEVEL) \
Logger::write_message(Logger::log_level_info, __FILE__, __LINE__, \
__FUNCTION__, msg)
#define LOG_WARN_MSG(msg) if(Logger::log_level_warn >= CURRENT_LOGGER_LEVEL) \
Logger::write_message(Logger::log_level_warn, __FILE__, __LINE__, \
__FUNCTION__, msg)
#define LOG_ERROR_MSG(msg) if(Logger::log_level_error >= CURRENT_LOGGER_LEVEL) \
Logger::write_message(Logger::log_level_error, __FILE__, __LINE__, \
__FUNCTION__, msg)
#define LOG_FATAL_MSG(msg) if(Logger::log_level_fatal >= CURRENT_LOGGER_LEVEL) \
Logger::write_message(Logger::log_level_fatal, __FILE__, __LINE__, \
__FUNCTION__, msg)
#endif