Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6901203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:38:38+00:00 2026-05-27T07:38:38+00:00

My current implementation, simplified: #include <string> #include <memory> class Log { public: ~Log() {

  • 0

My current implementation, simplified:

#include <string>
#include <memory>

class Log
{
  public:
    ~Log() {
      // closing file-descriptors, etc...
    }
    static void LogMsg( const std::string& msg )
    {
      static std::unique_ptr<Log> g_singleton;
      if ( !g_singleton.get() )
        g_singleton.reset( new Log );
      g_singleton->logMsg( msg );
    }
  private:
    Log() { }
    void logMsg( const std::string& msg ) {
      // do work
    }
};

In general, I am satisfied with this implementation because:

  • lazy instantiation means I don’t pay unless I use it
  • use of unique_ptr means automatic cleanup so valgrind is happy
  • relatively simple, easy-to-understand implementation

However, the negatives are:

  • singletons aren’t conducive to unit-testing
  • dissonance in the back of my mind for introducing a pseudo-global (a bit of a code smell)

So here are my questions directed towards those developers who are successful in exorcising all singletons from their C++ code:

  • What kind of non-Singleton implementation do you use for application-wide logging?
  • Is the interface as simple and accessible as a Log::LogMsg() call above?

I want to avoid passing a Log instance all over my code, if at all possible – note: I am asking because, I, too, want to exorcise all Singletons from my code if there is a good, reasonable alternative.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:38:39+00:00Added an answer on May 27, 2026 at 7:38 am

    First: the use of std::unique_ptr is unnecessary:

    void Log::LogMsg(std::string const& s) {
      static Log L;
      L.log(s);
    }
    

    Produces exactly the same lazy initialization and cleanup semantics without introducing all the syntax noise (and redundant test).

    Now that is out of the way…

    Your class is extremely simple. You might want to build a slightly more complicated version, typical requirements for log messages are:

    • timestamp
    • level
    • file
    • line
    • function
    • process name / thread id (if relevant)

    on top of the message itself.

    As such, it is perfectly conceivable to have several objects with different parameters:

    // LogSink is a backend consuming preformatted messages
    // there can be several different instances depending on where
    // to send the data
    class Logger {
    public:
      Logger(Level l, LogSink& ls);
    
      void operator()(std::string const& message,
                      char const* function,
                      char const* file,
                      int line);
    
    private:
      Level _level;
      LogSink& _sink;
    };
    

    And you usually wrap the access inside a macro for convenience:

    #define LOG(Logger_, Message_)                  \
      Logger_(                                      \
        static_cast<std::ostringstream&>(           \
          std::ostringstream().flush() << Message_  \
        ).str(),                                    \
        __FUNCTION__,                               \
        __FILE__,                                   \
        __LINE__                                    \
      );
    

    Now, we can create a simple verbose logger:

    Logger& Debug() {
      static Logger logger(Level::Debug, Console);
      return logger;
    }
    
    #ifdef NDEBUG
    #  define LOG_DEBUG(_) do {} while(0)
    #else
    #  define LOG_DEBUG(Message_) LOG(Debug(), Message_)
    #endif
    

    And use it conveniently:

    int foo(int a, int b) {
      int result = a + b;
    
      LOG_DEBUG("a = " << a << ", b = " << b << " --> result = " << result)
      return result;
    }
    

    The purpose of this rant ? Not all that is a global need be unique. The uniqueness of Singletons is generally useless.

    Note: if the bit of magic involving std::ostringstream scares you, this is normal, see this question

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a simplified version of my class: public abstract class Task { private static
The current implementation of Remy Sharp's jQuery tag suggestion plugin only checks for matches
My current implementation of an Hash Table is using Linear Probing and now I
I've tried with Postsharp, but their current implementation targets 2.0, and it's getting painful
Paramiko's SFTPClient apparently does not have an exists method. This is my current implementation:
Implementing Equals() for reference types is harder than it seems. My current canonical implementation
A reliable coder friend told me that Python's current multi-threading implementation is seriously buggy
I read that with .NET Framework 4 the current garbage collection implementation is replaced:
The current guidlelines for explicit member implementation recommend: Using explicit members to approximate private
What's the current status of Mono 's Platform Invoke implementation on Linux and on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.