-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.h
164 lines (125 loc) · 4.05 KB
/
log.h
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*****************************************************************************
Copyright 2005 - 2011 The Board of Trustees of the University of Illinois.
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
*****************************************************************************/
/*****************************************************************************
written by
Yunhong Gu, last updated 08/19/2010
*****************************************************************************/
#ifndef __SECTOR_LOG_H__
#define __SECTOR_LOG_H__
#include <fstream>
#include <map>
#include <string>
#include <iosfwd>
#include "osportable.h"
#include "udt.h"
struct LogLevel
{
static const int LEVEL_0 = 0;
static const int LEVEL_1 = 1;
static const int LEVEL_2 = 2;
static const int LEVEL_3 = 3;
static const int LEVEL_4 = 4;
static const int LEVEL_5 = 5;
static const int LEVEL_6 = 6;
static const int LEVEL_7 = 7;
static const int LEVEL_8 = 8;
static const int LEVEL_9 = 9;
static const int SCREEN = 10;
};
struct LogTag
{
static const int START = 0;
static const int END = 1;
};
struct LogString
{
int m_iLevel;
std::string m_strLog;
};
struct LogStringTag
{
LogStringTag(const int tag = LogTag::START, const int level = LogLevel::SCREEN);
int m_iTag;
int m_iLevel;
std::string m_strSrcFile;
int m_iLine;
std::string m_strFunc;
};
struct LogStart: LogStringTag
{
LogStart(const int level = LogLevel::SCREEN) { m_iTag = LogTag::START; m_iLevel = level; }
};
struct LogEnd: LogStringTag
{
LogEnd() { m_iTag = LogTag::END; };
};
//TODO: make LogStart and LogEnd optional. Support endl and setLevel inline.
class SectorLog
{
public:
SectorLog();
~SectorLog();
public:
int init(const char* path);
void close();
void setLevel(const int level);
void copyScreen(bool screen);
void insert(const char* text, const int level = 1);
SectorLog& operator<<(const LogStringTag& tag);
SectorLog& operator<<(const std::string& message);
SectorLog& operator<<(const int64_t& val);
static SectorLog& endl(SectorLog& log);
private:
void insert_(const char* text, const int level = 1);
int m_iLevel;
CMutex m_LogLock;
typedef std::map<pthread_t, LogString> ThreadIdStringMap;
ThreadIdStringMap m_mStoredString;
};
namespace logger {
// These are highest to lowest 'severity'. Note: these values are used as indices into a tuple; do not change!
enum LogLevel {
Screen = 0,
Error = 1,
Warning = 2,
Info = 3,
Trace = 4,
Debug = 5
};
struct LogAggregate {
public:
typedef std::basic_ostream<char> stream_type;
public:
inline LogAggregate( const std::string& loggerName, stream_type& screen, stream_type& error, stream_type& warning,
stream_type& info, stream_type& trace, stream_type& debug ) : loggerName( loggerName ), screen( screen ),
error( error ), warning( warning ), info( info ), trace( trace ), debug( debug ) {}
void setLogLevel( LogLevel lvl );
private: // noncopyable
LogAggregate( const LogAggregate& );
LogAggregate& operator=( const LogAggregate& );
private:
std::string loggerName;
public:
stream_type& screen;
stream_type& error;
stream_type& warning;
stream_type& info;
stream_type& trace;
stream_type& debug;
};
void config( const std::string& outputDir, const std::string& fileNamePrefix );
LogAggregate& getLogger( const char* name = 0 );
LogAggregate& getLogger( const std::string& name );
void setThreadName( const std::string& name );
}
#endif