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.
            Asked
            
        
        
            Active
            
        
            Viewed 127 times
        
    -1
            
            
        - 
                    1http://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 Answers
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
- 
                    1I think this is ignoring the thread safety issue, which could be the main point (although it is difficult to know for sure what OPs requirements are.) – juanchopanza May 03 '13 at 06:36
- 
                    1@juanchopanza How does the singleton pattern help with thread safety? – trojanfoe May 03 '13 at 06:38
- 
                    I never said it did. Obviously it doesn't. But there is nothing in your solution that addresses thread safety, and that seems to be one of OP's concerns. – juanchopanza May 03 '13 at 06:58
- 
                    
