lichao
2021-05-10 77a6c3512a44dfe6540dde71946e6484fe4f173f
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef LOG_SPKA2JY7
#define LOG_SPKA2JY7
 
#include "macro.h"
#include <boost/log/trivial.hpp>
#include <string>
 
namespace ns_log
{
 
void GetTrace();
typedef boost::log::trivial::severity_level LogLevel;
inline std::string &MsgPrefix()
{
    thread_local std::string prefix;
    return prefix;
}
inline int MsgIndent(int offset)
{
    thread_local int indent = 0;
    indent += offset;
    if (indent > 0) {
        MsgPrefix() = std::string(indent, ' ');
    } else {
        MsgPrefix().clear();
    }
    return indent;
}
std::string TimeStamp();
std::string LogPrefix(LogLevel level);
void AddLogRaw(const std::string &file_ptn, bool flush = true, bool with_console = false);
void AddLog(const std::string &file_ptn, bool flush = true, bool with_console = false);
LogLevel ResetLogLevel(LogLevel level, bool log_this = true);
LogLevel GetLogLevel();
 
} // namespace ns_log
 
#define LOG_WITH_PREFIX(severity) BOOST_LOG_TRIVIAL(severity) << ns_log::LogPrefix(boost::log::trivial::severity)
 
#define LOG__TRACE() LOG_WITH_PREFIX(trace)
#define LOG__DEBUG() LOG_WITH_PREFIX(debug)
#define LOG__INFO() LOG_WITH_PREFIX(info)
#define LOG__WARNING() LOG_WITH_PREFIX(warning)
#define LOG__ERROR() LOG_WITH_PREFIX(error)
#define LOG__FATAL() LOG_WITH_PREFIX(fatal)
 
#ifndef LOG_TRACE
#define LOG_TRACE() LOG__TRACE()
#endif
 
#ifndef LOG_DEBUG
#define LOG_DEBUG() LOG__DEBUG()
#endif
 
#ifndef LOG_INFO
#define LOG_INFO() LOG__INFO()
#endif
 
#ifndef LOG_WARNING
#define LOG_WARNING() LOG__WARNING()
#endif
 
#ifndef LOG_ERROR
#define LOG_ERROR() LOG__ERROR()
#endif
 
#ifndef LOG_FATAL
#define LOG_FATAL() LOG__FATAL()
#endif
 
#define LOG_POS() LOG__DEBUG() << __FILE__ << " +" << __LINE__ << ": "
 
// usage:
// LOG__WARNING() << "warning msg ...";
// LOG__ERROR() << "error msg ...";
 
namespace ns_log
{
namespace impl
{
using namespace boost::log::trivial;
class FuncLog
{
    const char *name_;
 
public:
    FuncLog(const char *name) :
        name_(name)
    {
        LOG__DEBUG() << ">>> " << name_;
        if (GetLogLevel() <= debug) {
            MsgIndent(2);
        }
    }
    ~FuncLog()
    {
        if (GetLogLevel() <= debug) {
            MsgIndent(-2);
        }
        LOG__DEBUG() << "<// " << name_;
    }
};
} // namespace impl
} // namespace ns_log
 
#if defined(__GNUG__)
#define LOG_FUNCTION ns_log::impl::FuncLog JOIN(log_func_, JOIN(__LINE__, _))(__PRETTY_FUNCTION__)
#else
#define LOG_FUNCTION ns_log::impl::FuncLog JOIN(log_func_, JOIN(__LINE__, _))(__FUNCTION__)
#endif
 
#endif // end of include guard: LOG_SPKA2JY7