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 8441943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:42:31+00:00 2026-06-10T08:42:31+00:00

I work on a logging mechanism using macros: #define LOGFATAL 1 // very serious

  • 0

I work on a logging mechanism using macros:

#define LOGFATAL 1 // very serious errors
#define TOSTRING(s) dynamic_cast< std::ostringstream & >((std::ostringstream() << s ) ).str()

#define LOG_TRANSFER(t, s) "[" << t << "] " << s

void inline print_log(const char *level, const std::string& value)
{
    std::cout << "[" << timestamp() << "]["<< level << "]" << value << std::endl;
}

#ifdef DEBUGGING
    #define DEBUG_OUT(l, s) print_log(l, TOSTRING(s));
#else
    #define DEBUG_OUT(s, l)
#endif


#if DEBUGGING >= LOGFATAL
    #define LOG_FATAL(s)        DEBUG_OUT("FATAL", s)
#else
    #define LOG_FATAL(s)
#endif

I use it his way:
LOG_TRACE(LOG_TRANSFER(ptr, “MemoryManager > allocate “))
LOG_TRACE(“MemoryManager > size : ” << active_list.size() )

What gcc does is:

print_log("TRACE", dynamic_cast< std::ostringstream & >((std::ostringstream() << "[" << ptr << "] " << "MemoryManager > allocate " ) ).str());
print_log("TRACE", dynamic_cast< std::ostringstream & >((std::ostringstream() << "MemoryManager > size : " << active_list.size() ) ).str());

This looks ok to me but I get the following output:

[0 s][TRACE]0x80940d40x9988ea8] MemoryManager > allocate 
[0 s][TRACE]0x80941e01

Where is the mistake?

  • 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-06-10T08:42:33+00:00Added an answer on June 10, 2026 at 8:42 am

    The problem is with this:

    dynamic_cast< std::ostringstream & >((std::ostringstream() << s )).str()
    

    or more precisely with this:

    std::ostringstream() << s
    

    The reason is that the expression std::ostringstream() is a temporary object on which you cannot call non-member overload <<, as all non-member << overloads take first argument as std::ostream&, which is non-const reference. But a temporary cannot be bound to non-const reference. Hence, a member function is called when you want to print char*, which gets converted into void* and address is printed.

    You need to do a little hack. Convert the temporary object (rvalue) into a reference type (lvalue) as:

    std::ostringstream().flush() << s
    

    Why would I call flush() like that??

    Well you can call a member function on a temporary object. I chose flush() because it returns std::ostream& which is non-const reference, and which is what we need.

    The difference it makes is this:

    std::ostringstream() << "nawaz" ; //it prints the address of "nawaz".
    std::ostringstream().flush() << "nawaz" ; //it prints "nawaz"
    

    The first one calls the member operator<< which prints the address, the second one call the non-member operator<< which prints the c-string. That is the difference behind the scene.

    So try this:

    dynamic_cast< std::ostringstream & >((std::ostringstream().flush() << s )).str()
    

    That should work.

    Also, I would suggest static_cast here, as you know the type already:

    static_cast< std::ostringstream & >((std::ostringstream().flush() << s )).str()
    

    It doesn’t depend on RTTI.

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

Sidebar

Related Questions

How does logging in a Hadoop job work? Using SLF4J and Logback, what sort
The standard logging code does not seem to work instantiating a logger with the
i'm trying to work out the best method to perform logging in the application
work on SQL Server 2000. want to Automated Email Notifications using SQL Server Job
I have a windows service doing some repetitive work (wcf calls) after logging programmaticaly
I spent a day trying to make Ent Lib Logging work and log anything
I'm just starting to work on a logging library that everyone can use to
For some reason I cannot get the error logging to work, I'm getting the
Now my colleagues work on logging subsystem and they want to bind separate operations,
I found a some post about difficulties found on logging sql queries when using

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.