class - Simple C++ Logging -


i have been set task of:

to evolve logger class can integrated projects/developments, declaring single global instance; used capture , save log information.

there 2 levels acceptable:

  1. logger - debug code in source, no class.
  2. logger - packaged in class, default behavior (not configurable). controlled _debug , writes std::clog (can re-directed).

i not know start this, , have spent hours trying find somewhere.

your task may use more complex logger class makes logging throughout entire project easier due functionalities such

  • being able break string on multiple lines of code when debugging it
  • separate methods between error logging , debug logging
  • being able turn logging on/off logger instance, debug messages throughout project become obsolete (instead of needing comment of them instance)
  • adding other methods specific case (you see below)

this logger class used windows project. supports features mentioned. feel free use it.

#include "logger.h"  logger::logger(     wstring wstrcomponentname,     wstring wstrfunctionname) :     _wstrapplicationname(application_name),     _wstrcomponentname(stripfilename(wstrcomponentname)),     _wstrfunctionname(wstrfunctionname) {}  // function doesn't support string, wstring it's arguments void logger::debug(     lpcwstr format,     ...     ) {     wstring wstrbase =          _wstrapplicationname + wstring(l": ") +         _wstrcomponentname + wstring(l": ") +         _wstrfunctionname + wstring(l": ");      va_list args;     va_start(args, format);      wchar_t* msg = new wchar_t[_1kb];     wvsprintf(msg, format, args);      va_end(args);      wstring wstroutput = wstrbase + msg;      delete[] msg;      outputdebugstring(wstroutput.c_str()); }  // function doesn't support string, wstring it's arguments void logger::error(     lpcwstr format,     ...     ) {     wstring wstrbase = wstring(l"[error] ") +         _wstrapplicationname + wstring(l": ") +         _wstrcomponentname + wstring(l": ") +         _wstrfunctionname + wstring(l": ");      va_list args;     va_start(args, format);      wchar_t* msg = new wchar_t[_1kb];     wvsprintf(msg, format, args);      va_end(args);      wstring wstroutput = wstrbase + msg;      delete[] msg;      outputdebugstring(wstroutput.c_str()); }  wstring logger::stripfilename(     __in wstring wstrfilename     ) {     dword dwleftlimit = 0;     dword dwrightlimit = wstrfilename.length();     wstring wstrresult(l"filename unavailable");      if (wstrfilename.rfind(l"\\") != wstring::npos)     {         dwleftlimit = wstrfilename.rfind(l"\\") + 1;     }      if (wstrfilename.rfind(l".") != wstring::npos)     {         dwrightlimit = wstrfilename.rfind(l".");     }      if (dwrightlimit > dwleftlimit)     {         wstrresult = wstrfilename.substr(             dwleftlimit,             dwrightlimit - dwleftlimit             );     }      return wstrresult; } 

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -