reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofLog.h
Go to the documentation of this file.
1#pragma once
2
3#include "ofFileUtils.h"
4#include "ofConstants.h"
5#include "ofUtils.h" // ofVAArgsToString
6#include <sstream>
7
99
100
103#ifdef __GNUC__
104#define OF_PRINTF_ATTR(x, y) __attribute__ ((format (printf, x, y)))
105#else
106#define OF_PRINTF_ATTR(x, y)
107#endif
109
110
111//--------------------------------------------------
114
116enum ofLogLevel: short{
122 OF_LOG_SILENT // OF_LOG_SILENT can be used to disable _all_ log messages.
123 // All logging can be disabled by calling
126
127//--------------------------------------------
128//console colors for our logger - shame this doesn't work with the xcode console
129#ifdef TARGET_WIN32
130
131 #define OF_CONSOLE_COLOR_RESTORE (0 | (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) )
132 #define OF_CONSOLE_COLOR_BLACK (0)
133 #define OF_CONSOLE_COLOR_RED (FOREGROUND_RED)
134 #define OF_CONSOLE_COLOR_GREEN (FOREGROUND_GREEN)
135 #define OF_CONSOLE_COLOR_YELLOW (FOREGROUND_RED|FOREGROUND_GREEN)
136 #define OF_CONSOLE_COLOR_BLUE (FOREGROUND_BLUE)
137 #define OF_CONSOLE_COLOR_PURPLE (FOREGROUND_RED | FOREGROUND_BLUE )
138 #define OF_CONSOLE_COLOR_CYAN (FOREGROUND_GREEN | FOREGROUND_BLUE)
139 #define OF_CONSOLE_COLOR_WHITE (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
140
141#else
142
143 #define OF_CONSOLE_COLOR_RESTORE (0)
144 #define OF_CONSOLE_COLOR_BLACK (30)
145 #define OF_CONSOLE_COLOR_RED (31)
146 #define OF_CONSOLE_COLOR_GREEN (32)
147 #define OF_CONSOLE_COLOR_YELLOW (33)
148 #define OF_CONSOLE_COLOR_BLUE (34)
149 #define OF_CONSOLE_COLOR_PURPLE (35)
150 #define OF_CONSOLE_COLOR_CYAN (36)
151 #define OF_CONSOLE_COLOR_WHITE (37)
152
153#endif
154
155
178void ofSetLogLevel(ofLogLevel level);
179
180
211void ofSetLogLevel(std::string module, ofLogLevel level);
212
216
220ofLogLevel ofGetLogLevel(std::string module);
221
226std::string ofGetLogLevelName(ofLogLevel level, bool pad=false);
227
229
230//--------------------------------------------------
233
234class ofBaseLoggerChannel;
235
239void ofLogToFile(const of::filesystem::path & path, bool append=false);
240
245void ofLogToConsole();
246
247#ifdef TARGET_WIN32
252void ofLogToDebugView();
253#endif
254
262void ofSetLoggerChannel(std::shared_ptr<ofBaseLoggerChannel> loggerChannel);
263
265std::shared_ptr<ofBaseLoggerChannel> ofGetLoggerChannel();
266
268
295
296// Class idea from http://www.gamedev.net/community/forums/topic.asp?topic_id=525405&whichpage=1&#3406418
297// How to catch std::endl (which is actually a func pointer) http://yvan.seth.id.au/Entries/Technology/Code/std__endl.html
298
299class ofLog{
300 public:
301
304
324 ofLog();
325
355 ofLog(ofLogLevel level);
356
357
376 ofLog(ofLogLevel level, const std::string & message);
377
435 template <typename ... Args>
436 ofLog(ofLogLevel level, const char* format, Args&& ... args)
437 : ofLog(level, ofVAArgsToString(format, args...)){}
439
440 //--------------------------------------------------
443
449 static void setAutoSpace(bool autoSpace);
450
457 static void setChannel(std::shared_ptr<ofBaseLoggerChannel> channel);
458
460 static std::shared_ptr<ofBaseLoggerChannel> getChannel();
461
463
464
466
470 virtual ~ofLog();
471
480 template <class T>
481 ofLog& operator<<(const T& value){
482 message << value << getPadding();
483 return *this;
484 }
485
493 ofLog& operator<<(std::ostream& (*func)(std::ostream&)){
494 func(message);
495 return *this;
496 }
497
499
500
501
502
503 protected:
505
506 ofLogLevel level;
507 bool bPrinted;
508 std::string module;
509
514 void _log(ofLogLevel level, const std::string & module, const std::string & message);
515
520 bool checkLog(ofLogLevel level, const std::string & module);
521
522 static std::shared_ptr<ofBaseLoggerChannel> & channel();
523
525
526 private:
527 std::stringstream message;
528
529 static bool bAutoSpace;
530
531 ofLog(ofLog const&) {} // not defined, not copyable
532 ofLog& operator=(ofLog& from) {return *this;} // not defined, not assignable
533
534 static std::string & getPadding();
535};
536
537
541class ofLogVerbose : public ofLog{
542 public:
545 ofLogVerbose(const std::string &module="");
546
550 ofLogVerbose(const std::string & module, const std::string & message);
551
555 template <typename ... Args>
556 ofLogVerbose(const std::string & module, const char* format, Args&& ... args)
557 : ofLogVerbose(module, ofVAArgsToString(format, args...)){}
558};
559
563class ofLogNotice : public ofLog{
564 public:
567 ofLogNotice(const std::string & module="");
568
572 ofLogNotice(const std::string & module, const std::string & message);
573
577 template <typename ... Args>
578 ofLogNotice(const std::string & module, const char* format, Args&& ... args)
579 : ofLogNotice(module, ofVAArgsToString(format, args...)){}
580};
581
585class ofLogWarning : public ofLog{
586 public:
589 ofLogWarning(const std::string & module="");
593 ofLogWarning(const std::string & module, const std::string & message);
594
598 template <typename ... Args>
599 ofLogWarning(const std::string & module, const char* format, Args&& ... args)
600 : ofLogWarning(module, ofVAArgsToString(format, args...)){}
601};
602
606class ofLogError : public ofLog{
607 public:
610 ofLogError(const std::string & module="");
611
615 ofLogError(const std::string & module, const std::string & message);
616
620 template <typename ... Args>
621 ofLogError(const std::string & module, const char* format, Args&& ... args)
622 : ofLogError(module, ofVAArgsToString(format, args...)){}
623};
624
628class ofLogFatalError : public ofLog{
629 public:
632 ofLogFatalError(const std::string & module="");
633
637 ofLogFatalError(const std::string & module, const std::string & message);
638
642 template <typename ... Args>
643 ofLogFatalError(const std::string & module, const char* format, Args&& ... args)
644 : ofLogFatalError(module, ofVAArgsToString(format, args...)){}
645};
646
647
649
650//--------------------------------------------------------------
651// Logger Channels
652
657class ofBaseLoggerChannel{
658public:
660 virtual ~ofBaseLoggerChannel(){};
661
666 virtual void log(ofLogLevel level, const std::string & module, const std::string & message)=0;
667
668
673 template <typename ... Args>
674 void log(ofLogLevel level, const std::string & module, const char* format, Args&& ... args){
675 log(level, module, ofVAArgsToString(format, args...));
676 }
677};
678
680class ofConsoleLoggerChannel: public ofBaseLoggerChannel{
681public:
683 virtual ~ofConsoleLoggerChannel(){};
684 void log(ofLogLevel level, const std::string & module, const std::string & message);
685};
686
687#ifdef TARGET_WIN32
689class ofDebugViewLoggerChannel : public ofBaseLoggerChannel {
690public:
692 virtual ~ofDebugViewLoggerChannel() {};
693 void log(ofLogLevel level, const std::string & module, const std::string & message);
694};
695#endif
696
698class ofFileLoggerChannel: public ofBaseLoggerChannel{
699public:
701 ofFileLoggerChannel();
702
706 ofFileLoggerChannel(const of::filesystem::path & path, bool append);
707
709 virtual ~ofFileLoggerChannel();
710
714 void setFile(const of::filesystem::path & path,bool append=false);
715
716 void log(ofLogLevel level, const std::string & module, const std::string & message);
717
719 void close();
720
721private:
722 ofFile file;
723
724};
725
Definition ofFileUtils.h:472
Derived log class for easy error logging.
Definition ofLog.h:606
ofLogError(const std::string &module, const char *format, Args &&... args)
Create a error log message.
Definition ofLog.h:621
ofLogError(const std::string &module, const std::string &message)
Create a error log message.
ofLogError(const std::string &module="")
Create a error log message.
Derived log class for easy fatal error logging.
Definition ofLog.h:628
ofLogFatalError(const std::string &module, const std::string &message)
Create a fatal error log message.
ofLogFatalError(const std::string &module, const char *format, Args &&... args)
Create a fatal error log message.
Definition ofLog.h:643
ofLogFatalError(const std::string &module="")
Create a fatal error log message.
A C++ stream-style logging interface.
Definition ofLog.h:299
ofLog()
Start logging on notice level.
Definition ofLog.cpp:80
ofLog(ofLogLevel level, const char *format, Args &&... args)
Logs a message at a specific log level using the printf interface.
Definition ofLog.h:436
static std::shared_ptr< ofBaseLoggerChannel > getChannel()
Get the current logging channel.
Definition ofLog.cpp:205
ofLog(ofLogLevel level, const std::string &message)
Log a string at a specific log level.
static void setAutoSpace(bool autoSpace)
Let the logger automaticly add spaces between messages.
Definition ofLog.cpp:101
static void setChannel(std::shared_ptr< ofBaseLoggerChannel > channel)
Set the logging channel destinations for messages.
Definition ofLog.cpp:197
Derived log class for easy notice logging.
Definition ofLog.h:563
ofLogNotice(const std::string &module, const char *format, Args &&... args)
Create a notice log message.
Definition ofLog.h:578
ofLogNotice(const std::string &module, const std::string &message)
Create a notice log message.
ofLogNotice(const std::string &module="")
Create a notice log message.
Derived log class for easy verbose logging.
Definition ofLog.h:541
ofLogVerbose(const std::string &module="")
Create a verbose log message.
ofLogVerbose(const std::string &module, const std::string &message)
Create a verbose log message.
ofLogVerbose(const std::string &module, const char *format, Args &&... args)
Create a verbose log message.
Definition ofLog.h:556
Derived log class for easy warning logging.
Definition ofLog.h:585
ofLogWarning(const std::string &module, const std::string &message)
Create a verbose log message.
ofLogWarning(const std::string &module="")
Create a verbose log message.
ofLogWarning(const std::string &module, const char *format, Args &&... args)
Create a verbose log message.
Definition ofLog.h:599
std::shared_ptr< ofBaseLoggerChannel > ofGetLoggerChannel()
Get the current logger channel.
Definition ofLog.cpp:209
std::string ofGetLogLevelName(ofLogLevel level, bool pad=false)
Get log level name as a string.
Definition ofLog.cpp:213
void ofLogToFile(const of::filesystem::path &path, bool append=false)
Set the logging to output to a file instead of the console.
Definition ofLog.cpp:64
void ofSetLogLevel(ofLogLevel level)
Sets the logging level to selectively show log messages.
Definition ofLog.cpp:40
void ofSetLoggerChannel(std::shared_ptr< ofBaseLoggerChannel > loggerChannel)
Set the logger to use a custom logger channel.
ofLogLevel
The supported logging levels. Default is OF_LOG_NOTICE.
Definition ofLog.h:116
@ OF_LOG_WARNING
Definition ofLog.h:119
@ OF_LOG_VERBOSE
Definition ofLog.h:117
@ OF_LOG_FATAL_ERROR
Definition ofLog.h:121
@ OF_LOG_ERROR
Definition ofLog.h:120
@ OF_LOG_SILENT
ofSetLogLevel(OF_LOG_SILENT).
Definition ofLog.h:122
@ OF_LOG_NOTICE
Definition ofLog.h:118
ofLogLevel ofGetLogLevel()
Get the currently set global logging level.
Definition ofLog.cpp:50
void ofLogToConsole()
Set the logging to ouptut to the console.
Definition ofLog.cpp:69
std::ostream & operator<<(std::ostream &os, const ofMatrix3x3 &M)
Definition ofMatrix3x3.cpp:304
std::string ofVAArgsToString(const char *format, Args &&... args)
Convert a variable length argument to a string.
Definition ofUtils.h:619