framework2

Форк
0
289 строк · 7.8 Кб
1
#include "ofLog.h"
2
#include <ofUtils.h>
3
#include <map>
4
#ifdef TARGET_ANDROID
5
	#include "ofxAndroidLogChannel.h"
6
#endif
7

8
using std::map;
9
using std::string;
10
using std::shared_ptr;
11

12
static ofLogLevel currentLogLevel =  OF_LOG_NOTICE;
13

14
bool ofLog::bAutoSpace = false;
15
string & ofLog::getPadding() {
16
	static string * padding = new string;
17
	return *padding;
18
}
19

20
static map<string,ofLogLevel> & getModules(){
21
	static map<string,ofLogLevel> * modules = new map<string,ofLogLevel>;
22
	return *modules;
23
}
24

25
static void noopDeleter(ofBaseLoggerChannel*){}
26

27
shared_ptr<ofBaseLoggerChannel> & ofLog::channel(){
28
#ifdef TARGET_ANDROID
29
	static shared_ptr<ofBaseLoggerChannel> channel = shared_ptr<ofxAndroidLogChannel>(new ofxAndroidLogChannel, std::function<void(ofBaseLoggerChannel *)>(noopDeleter));
30
#elif defined(TARGET_WIN32)
31
	static shared_ptr<ofBaseLoggerChannel> channel = IsDebuggerPresent() ? shared_ptr<ofBaseLoggerChannel>(new ofDebugViewLoggerChannel, std::function<void(ofBaseLoggerChannel *)>(noopDeleter)) : shared_ptr<ofBaseLoggerChannel>(new ofConsoleLoggerChannel, std::function<void(ofBaseLoggerChannel *)>(noopDeleter));
32
#else
33
	static shared_ptr<ofBaseLoggerChannel> channel = shared_ptr<ofConsoleLoggerChannel>(new ofConsoleLoggerChannel, std::function<void(ofBaseLoggerChannel *)>(noopDeleter));
34
#endif
35

36
	return channel;
37
}
38

39
//--------------------------------------------------
40
void ofSetLogLevel(ofLogLevel level){
41
	currentLogLevel = level;
42
}
43

44
//--------------------------------------------------
45
void ofSetLogLevel(string module, ofLogLevel level){
46
	getModules()[module] = level;
47
}
48

49
//--------------------------------------------------
50
ofLogLevel ofGetLogLevel(){
51
	return currentLogLevel;
52
}
53

54
//--------------------------------------------------
55
ofLogLevel ofGetLogLevel(string module){
56
	if (getModules().find(module) == getModules().end()) {
57
		return currentLogLevel;
58
	} else {
59
		return getModules()[module];
60
	}
61
}
62

63
//--------------------------------------------------
64
void ofLogToFile(const of::filesystem::path & path, bool append){
65
	ofLog::setChannel(std::make_shared<ofFileLoggerChannel>(path,append));
66
}
67

68
//--------------------------------------------------
69
void ofLogToConsole(){
70
	ofLog::setChannel(shared_ptr<ofConsoleLoggerChannel>(new ofConsoleLoggerChannel, std::function<void(ofBaseLoggerChannel *)>(noopDeleter)));
71
}
72

73
#ifdef TARGET_WIN32
74
void ofLogToDebugView() {
75
	ofLog::setChannel(shared_ptr<ofDebugViewLoggerChannel>(new ofDebugViewLoggerChannel, std::function<void(ofBaseLoggerChannel *)>(noopDeleter)));
76
}
77
#endif
78

79
//--------------------------------------------------
80
ofLog::ofLog(){
81
	level = OF_LOG_NOTICE;
82
	module = "";
83
	bPrinted = false;
84
}
85
		
86
//--------------------------------------------------
87
ofLog::ofLog(ofLogLevel _level){
88
	level = _level;
89
	module = "";
90
	bPrinted = false;
91
}
92

93
//--------------------------------------------------
94
ofLog::ofLog(ofLogLevel level, const string & message){
95
	_log(level,"",message);
96
	bPrinted = true;
97
}
98

99

100
//--------------------------------------------------
101
void ofLog::setAutoSpace(bool autoSpace){
102
	bAutoSpace = autoSpace;
103
	if(bAutoSpace){
104
		ofLog::getPadding() = " ";
105
	}
106
	else{
107
		ofLog::getPadding() = "";
108
	}
109
}
110

111
//-------------------------------------------------------
112
ofLog::~ofLog(){
113
	// don't log if we printed in the constructor already
114
	if(!bPrinted){
115
		_log(level,module,message.str());
116
	}
117
}
118

119
bool ofLog::checkLog(ofLogLevel level, const string & module){
120
	if(getModules().find(module)==getModules().end()){
121
		if(level >= currentLogLevel) return true;
122
	}else{
123
		if(level >= getModules()[module]) return true;
124
	}
125
	return false;
126
}
127

128
//-------------------------------------------------------
129
void ofLog::_log(ofLogLevel level, const string & module, const string & message){
130
	if(checkLog(level,module)){
131
		channel()->log(level,module, message);
132
	}
133
}
134

135
//--------------------------------------------------
136
ofLogVerbose::ofLogVerbose(const string & _module){
137
	level = OF_LOG_VERBOSE;
138
	module = _module;
139
	bPrinted=false;
140
}
141

142
ofLogVerbose::ofLogVerbose(const string & _module, const string & _message){
143
	_log(OF_LOG_VERBOSE,_module,_message);
144
	bPrinted = true;
145
}
146

147
//--------------------------------------------------
148
ofLogNotice::ofLogNotice(const string & _module){
149
	level = OF_LOG_NOTICE;
150
	module = _module;
151
	bPrinted=false;
152
}
153

154
ofLogNotice::ofLogNotice(const string & _module, const string & _message){
155
	_log(OF_LOG_NOTICE,_module,_message);
156
	bPrinted = true;
157
}
158

159
//--------------------------------------------------
160
ofLogWarning::ofLogWarning(const string & _module){
161
	level = OF_LOG_WARNING;
162
	module = _module;
163
	bPrinted=false;
164
}
165

166
ofLogWarning::ofLogWarning(const string & _module, const string & _message){
167
	_log(OF_LOG_WARNING,_module,_message);
168
	bPrinted = true;
169
}
170

171
//--------------------------------------------------
172
ofLogError::ofLogError(const string & _module){
173
	level = OF_LOG_ERROR;
174
	module = _module;
175
	bPrinted=false;
176
}
177

178
ofLogError::ofLogError(const string & _module, const string & _message){
179
	_log(OF_LOG_ERROR,_module,_message);
180
	bPrinted = true;
181
}
182

183
//--------------------------------------------------
184
ofLogFatalError::ofLogFatalError(const string &  _module){
185
	level = OF_LOG_FATAL_ERROR;
186
	module = _module;
187
	bPrinted=false;
188
}
189

190
ofLogFatalError::ofLogFatalError(const string & _module, const string & _message){
191
	_log(OF_LOG_FATAL_ERROR,_module,_message);
192
	bPrinted = true;
193
}
194

195

196
//--------------------------------------------------
197
void ofLog::setChannel(shared_ptr<ofBaseLoggerChannel> _channel){
198
	channel() = _channel;
199
}
200

201
void ofSetLoggerChannel(shared_ptr<ofBaseLoggerChannel> loggerChannel){
202
	ofLog::setChannel(loggerChannel);
203
}
204

205
shared_ptr<ofBaseLoggerChannel> ofLog::getChannel(){
206
	return channel();
207
}
208

209
shared_ptr<ofBaseLoggerChannel> ofGetLoggerChannel(){
210
	return ofLog::getChannel();
211
}
212

213
string ofGetLogLevelName(ofLogLevel level, bool pad){
214
	switch(level){
215
		case OF_LOG_VERBOSE:
216
			return "verbose";
217
		case OF_LOG_NOTICE:
218
			return pad ? "notice " : "notice";
219
		case OF_LOG_WARNING:
220
			return "warning";
221
		case OF_LOG_ERROR:
222
			return pad ? " error " : "error";
223
		case OF_LOG_FATAL_ERROR:
224
			return pad ? " fatal " : "fatal";
225
		case OF_LOG_SILENT:
226
			return pad ? "silent " : "silent";
227
		default:
228
			return "";
229
	}
230
}
231

232
//--------------------------------------------------
233
void ofConsoleLoggerChannel::log(ofLogLevel level, const string & module, const string & message){
234
	// print to cerr for OF_LOG_ERROR and OF_LOG_FATAL_ERROR, everything else to cout 
235
	std::ostream& out = level < OF_LOG_ERROR ? std::cout : std::cerr;
236
	out << "[" << ofGetLogLevelName(level, true)  << "] ";
237
	// only print the module name if it's not ""
238
	if(module != ""){
239
		out << module << ": ";
240
	}
241
	out << message << std::endl;
242
}
243

244

245
#ifdef TARGET_WIN32
246
#include <array>
247
void ofDebugViewLoggerChannel::log(ofLogLevel level, const string & module, const string & message) {
248
	// print to cerr for OF_LOG_ERROR and OF_LOG_FATAL_ERROR, everything else to cout 
249
	std::stringstream out;
250
	out << "[" << ofGetLogLevelName(level, true) << "] ";
251
	// only print the module name if it's not ""
252
	if (module != "") {
253
		out << module << ": ";
254
	}
255
	out << message << std::endl;
256
	OutputDebugStringA(out.str().c_str());
257
}
258
#endif
259

260
//--------------------------------------------------
261
ofFileLoggerChannel::ofFileLoggerChannel(){
262
}
263

264
ofFileLoggerChannel::ofFileLoggerChannel(const of::filesystem::path & path, bool append){
265
	setFile(path,append);
266
}
267

268
ofFileLoggerChannel::~ofFileLoggerChannel(){
269
	close();
270
}
271

272
void ofFileLoggerChannel::close(){
273
	file.close();
274
}
275

276
void ofFileLoggerChannel::setFile(const of::filesystem::path & path,bool append){
277
	file.open(path,append?ofFile::Append:ofFile::WriteOnly);
278
	file << std::endl;
279
	file << std::endl;
280
	file << "--------------------------------------- " << ofGetTimestampString() << std::endl;
281
}
282

283
void ofFileLoggerChannel::log(ofLogLevel level, const string & module, const string & message){
284
	file << "[" << ofGetLogLevelName(level, true) << "] ";
285
	if(module != ""){
286
		file << module << ": ";
287
	}
288
	file << message << std::endl;
289
}
290

291

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.