Created
September 4, 2012 14:35
-
-
Save fat-lobyte/3621720 to your computer and use it in GitHub Desktop.
Do the log!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Do this: http://www.techrepublic.com/article/use-stl-streams-for-easy-c-thread-safe-logging/5072104 | |
| Read this: http://www.drdobbs.com/cpp/logging-in-c/201804215?pgno=2 | |
| ====== logging.hpp: ====== | |
| class LoggerBackend { | |
| void log(enum level l, const char* who, <whatever> message) | |
| { | |
| print time, who, level, message; | |
| } | |
| }; | |
| extern LoggerBackend basic_static_logger; | |
| class Logger { | |
| LoggerBackend * backend; | |
| std::string who; | |
| std::string hash; | |
| operator << (...) | |
| { backend->log(...) } | |
| }; | |
| #define GET_LOGGER(name) LoggerBackend(&basic_static_logger, name, this) | |
| ====== logging.cpp: ====== | |
| static LoggerBackend basic_static_logger; | |
| ====== impl.cpp ====== | |
| struct Component { | |
| Component() | |
| { | |
| Logger logger; | |
| ComponentLogger() : logger(GET_LOGGER("Component"))) {} | |
| void func() | |
| { | |
| logger<<"Everything is good."<<std::flush; | |
| logger.info<<"Everything still good."<<std::flush; | |
| logger.warning<<"Trouble ahead"<<std::flush; | |
| logger.error<<"Stuff just exploded."<<std::flush; | |
| } | |
| }; | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment