-
Notifications
You must be signed in to change notification settings - Fork 3
/
TarsLogger.cpp
153 lines (115 loc) · 3.86 KB
/
TarsLogger.cpp
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
#include "TarsLogger.h"
#include <iostream>
using namespace std;
namespace tars
{
int RollWriteT::_dyeingThread = 0;
/////////////////////////////////////////////////////////////////////////////////////
RollWriteT::RollWriteT():_dyeingRollLogger(NULL), _maxSize(10000), _maxNum(1)
{
}
RollWriteT::~RollWriteT()
{
if(_dyeingRollLogger)
{
delete _dyeingRollLogger;
}
}
void RollWriteT::operator()(ostream &of, const deque<pair<int, string> > &ds)
{
vector<string> vRemoteDyeing;
deque<pair<int, string> >::const_iterator it = ds.begin();
while(it != ds.end())
{
of << it->second;
//染色线程id不存在
if(it->first != 0)
{
if(!_dyeingRollLogger)
{
string sDyeingDir = _logPath;
sDyeingDir += "/";
sDyeingDir += DYEING_DIR;
sDyeingDir += "/";
string sDyeingFile = sDyeingDir;
sDyeingFile += DYEING_FILE;
TC_File::makeDirRecursive(sDyeingDir);
//初始化染色循环日志
_dyeingRollLogger = new TC_RollLogger();
_dyeingRollLogger->init(sDyeingFile, _maxSize, _maxNum);
_dyeingRollLogger->modFlag(TC_DayLogger::HAS_TIME, false);
_dyeingRollLogger->modFlag(TC_DayLogger::HAS_TIME|TC_DayLogger::HAS_LEVEL|TC_DayLogger::HAS_PID, true);
_dyeingRollLogger->setLogLevel("DEBUG");
}
_dyeingRollLogger->roll(make_pair(it->first, _app + "." + _server + "|" + it->second ));
vRemoteDyeing.push_back(_app + "." + _server + "|" + it->second);
}
++it;
}
of.flush();
}
void RollWriteT::setDyeingLogInfo(const string &sApp, const string &sServer, const string & sLogPath, int iMaxSize, int iMaxNum, const string &sLogObj)
{
_app = sApp;
_server = sServer;
_logPath = sLogPath;
_maxSize = iMaxSize;
_maxNum = iMaxNum;
}
/////////////////////////////////////////////////////////////////////////////////////
void TarsRollLogger::setLogInfo(const string &sApp, const string &sServer, const string &sLogpath, int iMaxSize, int iMaxNum, const string &sLogObj)
{
_app = sApp;
_server = sServer;
_logpath = sLogpath;
//生成目录
TC_File::makeDirRecursive(_logpath + "/" + _app + "/" + _server);
cout<<"start threadpool"<<endl;
_local.start(1);
//初始化本地循环日志
cout<<"_logger.init"<<endl;
_logger.init(_logpath + "/" + _app + "/" + _server + "/" + _app + "." + _server, iMaxSize, iMaxNum);
_logger.modFlag(TC_DayLogger::HAS_TIME, false);
_logger.modFlag(TC_DayLogger::HAS_TIME|TC_DayLogger::HAS_LEVEL|TC_DayLogger::HAS_PID, true);
//设置为异步
sync(false);
}
void TarsRollLogger::sync(bool bSync)
{
if(bSync)
{
_logger.unSetupThread();
}
else
{
_logger.setupThread(&_local);
}
}
void TarsRollLogger::enableDyeing(bool bEnable, const string& sDyeingKey/* = ""*/)
{
_logger.getRoll()->enableDyeing(bEnable, sDyeingKey);
}
/////////////////////////////////////////////////////////////////////////////////////
TarsLoggerThread::TarsLoggerThread()
{
_local.start(1);
_remote.start(1);
}
TarsLoggerThread::~TarsLoggerThread()
{
//先刷新本地日志
_local.flush();
//再刷新远程日志, 保证不会丢日志
_remote.flush();
}
TC_LoggerThreadGroup* TarsLoggerThread::local()
{
return &_local;
}
TC_LoggerThreadGroup* TarsLoggerThread::remote()
{
return &_remote;
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
}