framework2
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
8using std::map;
9using std::string;
10using std::shared_ptr;
11
12static ofLogLevel currentLogLevel = OF_LOG_NOTICE;
13
14bool ofLog::bAutoSpace = false;
15string & ofLog::getPadding() {
16static string * padding = new string;
17return *padding;
18}
19
20static map<string,ofLogLevel> & getModules(){
21static map<string,ofLogLevel> * modules = new map<string,ofLogLevel>;
22return *modules;
23}
24
25static void noopDeleter(ofBaseLoggerChannel*){}
26
27shared_ptr<ofBaseLoggerChannel> & ofLog::channel(){
28#ifdef TARGET_ANDROID
29static shared_ptr<ofBaseLoggerChannel> channel = shared_ptr<ofxAndroidLogChannel>(new ofxAndroidLogChannel, std::function<void(ofBaseLoggerChannel *)>(noopDeleter));
30#elif defined(TARGET_WIN32)
31static 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
33static shared_ptr<ofBaseLoggerChannel> channel = shared_ptr<ofConsoleLoggerChannel>(new ofConsoleLoggerChannel, std::function<void(ofBaseLoggerChannel *)>(noopDeleter));
34#endif
35
36return channel;
37}
38
39//--------------------------------------------------
40void ofSetLogLevel(ofLogLevel level){
41currentLogLevel = level;
42}
43
44//--------------------------------------------------
45void ofSetLogLevel(string module, ofLogLevel level){
46getModules()[module] = level;
47}
48
49//--------------------------------------------------
50ofLogLevel ofGetLogLevel(){
51return currentLogLevel;
52}
53
54//--------------------------------------------------
55ofLogLevel ofGetLogLevel(string module){
56if (getModules().find(module) == getModules().end()) {
57return currentLogLevel;
58} else {
59return getModules()[module];
60}
61}
62
63//--------------------------------------------------
64void ofLogToFile(const of::filesystem::path & path, bool append){
65ofLog::setChannel(std::make_shared<ofFileLoggerChannel>(path,append));
66}
67
68//--------------------------------------------------
69void ofLogToConsole(){
70ofLog::setChannel(shared_ptr<ofConsoleLoggerChannel>(new ofConsoleLoggerChannel, std::function<void(ofBaseLoggerChannel *)>(noopDeleter)));
71}
72
73#ifdef TARGET_WIN32
74void ofLogToDebugView() {
75ofLog::setChannel(shared_ptr<ofDebugViewLoggerChannel>(new ofDebugViewLoggerChannel, std::function<void(ofBaseLoggerChannel *)>(noopDeleter)));
76}
77#endif
78
79//--------------------------------------------------
80ofLog::ofLog(){
81level = OF_LOG_NOTICE;
82module = "";
83bPrinted = false;
84}
85
86//--------------------------------------------------
87ofLog::ofLog(ofLogLevel _level){
88level = _level;
89module = "";
90bPrinted = false;
91}
92
93//--------------------------------------------------
94ofLog::ofLog(ofLogLevel level, const string & message){
95_log(level,"",message);
96bPrinted = true;
97}
98
99
100//--------------------------------------------------
101void ofLog::setAutoSpace(bool autoSpace){
102bAutoSpace = autoSpace;
103if(bAutoSpace){
104ofLog::getPadding() = " ";
105}
106else{
107ofLog::getPadding() = "";
108}
109}
110
111//-------------------------------------------------------
112ofLog::~ofLog(){
113// don't log if we printed in the constructor already
114if(!bPrinted){
115_log(level,module,message.str());
116}
117}
118
119bool ofLog::checkLog(ofLogLevel level, const string & module){
120if(getModules().find(module)==getModules().end()){
121if(level >= currentLogLevel) return true;
122}else{
123if(level >= getModules()[module]) return true;
124}
125return false;
126}
127
128//-------------------------------------------------------
129void ofLog::_log(ofLogLevel level, const string & module, const string & message){
130if(checkLog(level,module)){
131channel()->log(level,module, message);
132}
133}
134
135//--------------------------------------------------
136ofLogVerbose::ofLogVerbose(const string & _module){
137level = OF_LOG_VERBOSE;
138module = _module;
139bPrinted=false;
140}
141
142ofLogVerbose::ofLogVerbose(const string & _module, const string & _message){
143_log(OF_LOG_VERBOSE,_module,_message);
144bPrinted = true;
145}
146
147//--------------------------------------------------
148ofLogNotice::ofLogNotice(const string & _module){
149level = OF_LOG_NOTICE;
150module = _module;
151bPrinted=false;
152}
153
154ofLogNotice::ofLogNotice(const string & _module, const string & _message){
155_log(OF_LOG_NOTICE,_module,_message);
156bPrinted = true;
157}
158
159//--------------------------------------------------
160ofLogWarning::ofLogWarning(const string & _module){
161level = OF_LOG_WARNING;
162module = _module;
163bPrinted=false;
164}
165
166ofLogWarning::ofLogWarning(const string & _module, const string & _message){
167_log(OF_LOG_WARNING,_module,_message);
168bPrinted = true;
169}
170
171//--------------------------------------------------
172ofLogError::ofLogError(const string & _module){
173level = OF_LOG_ERROR;
174module = _module;
175bPrinted=false;
176}
177
178ofLogError::ofLogError(const string & _module, const string & _message){
179_log(OF_LOG_ERROR,_module,_message);
180bPrinted = true;
181}
182
183//--------------------------------------------------
184ofLogFatalError::ofLogFatalError(const string & _module){
185level = OF_LOG_FATAL_ERROR;
186module = _module;
187bPrinted=false;
188}
189
190ofLogFatalError::ofLogFatalError(const string & _module, const string & _message){
191_log(OF_LOG_FATAL_ERROR,_module,_message);
192bPrinted = true;
193}
194
195
196//--------------------------------------------------
197void ofLog::setChannel(shared_ptr<ofBaseLoggerChannel> _channel){
198channel() = _channel;
199}
200
201void ofSetLoggerChannel(shared_ptr<ofBaseLoggerChannel> loggerChannel){
202ofLog::setChannel(loggerChannel);
203}
204
205shared_ptr<ofBaseLoggerChannel> ofLog::getChannel(){
206return channel();
207}
208
209shared_ptr<ofBaseLoggerChannel> ofGetLoggerChannel(){
210return ofLog::getChannel();
211}
212
213string ofGetLogLevelName(ofLogLevel level, bool pad){
214switch(level){
215case OF_LOG_VERBOSE:
216return "verbose";
217case OF_LOG_NOTICE:
218return pad ? "notice " : "notice";
219case OF_LOG_WARNING:
220return "warning";
221case OF_LOG_ERROR:
222return pad ? " error " : "error";
223case OF_LOG_FATAL_ERROR:
224return pad ? " fatal " : "fatal";
225case OF_LOG_SILENT:
226return pad ? "silent " : "silent";
227default:
228return "";
229}
230}
231
232//--------------------------------------------------
233void 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
235std::ostream& out = level < OF_LOG_ERROR ? std::cout : std::cerr;
236out << "[" << ofGetLogLevelName(level, true) << "] ";
237// only print the module name if it's not ""
238if(module != ""){
239out << module << ": ";
240}
241out << message << std::endl;
242}
243
244
245#ifdef TARGET_WIN32
246#include <array>
247void 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
249std::stringstream out;
250out << "[" << ofGetLogLevelName(level, true) << "] ";
251// only print the module name if it's not ""
252if (module != "") {
253out << module << ": ";
254}
255out << message << std::endl;
256OutputDebugStringA(out.str().c_str());
257}
258#endif
259
260//--------------------------------------------------
261ofFileLoggerChannel::ofFileLoggerChannel(){
262}
263
264ofFileLoggerChannel::ofFileLoggerChannel(const of::filesystem::path & path, bool append){
265setFile(path,append);
266}
267
268ofFileLoggerChannel::~ofFileLoggerChannel(){
269close();
270}
271
272void ofFileLoggerChannel::close(){
273file.close();
274}
275
276void ofFileLoggerChannel::setFile(const of::filesystem::path & path,bool append){
277file.open(path,append?ofFile::Append:ofFile::WriteOnly);
278file << std::endl;
279file << std::endl;
280file << "--------------------------------------- " << ofGetTimestampString() << std::endl;
281}
282
283void ofFileLoggerChannel::log(ofLogLevel level, const string & module, const string & message){
284file << "[" << ofGetLogLevelName(level, true) << "] ";
285if(module != ""){
286file << module << ": ";
287}
288file << message << std::endl;
289}
290
291