-1

I need to design a logger class which will be used by all other classed to log the messages. Currently I am creating the poiner of logger in each of my classes and calling the method of logger thorugh the logger pointer to log the messages. I want to design it in singleton pattern way and it has to be thread safe. Can anyone suggest a good approach.

  • 1
    http://stackoverflow.com/questions/1008019/c-singleton-design-pattern?rq=1 Have you checked this? – olevegard May 03 '13 at 06:18
  • It is unlikely you need a singleton, so concentrate on a thread safe logger that is not a singleton. This simplifies the problem. – juanchopanza May 03 '13 at 06:19

1 Answers1

1

Forget about singleton, simply make everything in the class static. You will likely want to provide macros to easily access the log method, for example:

#define logdbg(fmt, ...) Log::log(__FUNCTION__, Log::LEVEL_DEBUG, fmt, ##__VA_ARGS__)

Which, when implemented as a singleton would need to be:

#define logdbg(fmt, ...) Log::instance().log(__FUNCTION__, Log::LEVEL_DEBUG, fmt, ##__VA_ARGS__)

Which makes very little difference.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242