Skip to content

Instantly share code, notes, and snippets.

@fat-lobyte
Created September 4, 2012 14:35
Show Gist options
  • Select an option

  • Save fat-lobyte/3621720 to your computer and use it in GitHub Desktop.

Select an option

Save fat-lobyte/3621720 to your computer and use it in GitHub Desktop.
Do the log!
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