Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I have a question about the C++ code in the Dr.Dobbs Logging article located here: drdobbs.com/cpp/logging-in-c/201804215 To show how to use the Log class, this example is given: Code: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2011
    Posts
    10

    Question about Dr.Dobbs Logging C++ code


    Hi, I have a question about the C++ code in the Dr.Dobbs Logging article located here:
    drdobbs.com/cpp/logging-in-c/201804215

    To show how to use the Log class, this example is given:

    Code:
    Log().Get(logINFO) << "Hello " << username;
    Now, why doesn't:

    Code:
    Log().Get(logINFO) << "Hello";
    Log().Get(logINFO) << "Hello again";
    create two different Log objects each with a different stringstream containing "Hello" and "Hello again" respectively?
    How does the compiler know to append the string to the same Log object?

    Here is the code listing of Dr.Dobb's Log class given on page 1 of the article (the author hasn't explicitly defined all the methods yet):

    Code:
     
    
    // Log, version 0.1: a simple logging class
    enum TLogLevel {logERROR, logWARNING, logINFO, logDEBUG, logDEBUG1,
    logDEBUG2, logDEBUG3, logDEBUG4};
    class Log
    {
    public:
       Log();
       virtual ~Log();
       std::ostringstream& Get(TLogLevel level = logINFO);
    public:
       static TLogLevel& ReportingLevel();
    protected:
       std::ostringstream os;
    private:
       Log(const Log&);
       Log& operator =(const Log&);
    private:
       TLogLevel messageLevel;
    };
    std::ostringstream& Log::Get(TLogLevel level)
    {
       os << "- " << NowTime();
       os << " " << ToString(level) << ": ";
       os << std::string(level > logDEBUG ? 0 : level - logDEBUG, '\t');
       messageLevel = level;
       return os;
    }
    Log::~Log()
    {
       if (messageLevel >= Log::ReportingLevel())
       {
          os << std::endl;
          fprintf(stderr, "%s", os.str().c_str());
          fflush(stderr);
       }
    }
    Thanks,
    Disinterred

  2. #2
    Just Joined!
    Join Date
    May 2012
    Posts
    43
    I don't know anything about C++, but from what I can gather, there's no defined identifier, unless (logINFO) is the identifier. Basically, without a way to determine which log to write to, it will write to the same one.

    Could be completely wrong, but it sounds reasonable.

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    I'm confused. Why do you think that:
    Code:
    Log().Get(logINFO) << "Hello";
    Log().Get(logINFO) << "Hello again";
    reuses the same stream? As far as I can tell, it should create different streams.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    May 2011
    Posts
    10
    Thanks Cancerous and Cabhan for the replies!

    Cabhan - So two destructors will be called in total when the program using the log closes? One destructor for a log object with a "Hello" ostringstream and the other for a log object with a "Hello Again" ostringstream?

    This makes sense to me. Originally, I was thinking along the lines of Cancerous, but something didn't feel right with that interpretation.

    Thanks,
    Disinterred

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •