Trace Macro
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Trace Macro

  1. #1
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,359

    Trace Macro

    I have a Trace.h file which is include in pretty much all my files, which contains the following:

    Code:
    #ifndef TRACE_H_INCLUDED
    #define TRACE_H_INCLUDED
    
    #include <iostream>
    #include <fstream>
    
    static std::ofstream log_stream("output.log");
    
    #define TRACE(ARG) if(true){log_stream << __FILE << " at line " << __LINE << ARG << std::endl;} else void(0)
    
    #endif // TRACE_H_INCLUDED
    Basically, the idea is I can log stuff to a file as my program runs.

    The problem is that after running my program, I realize that a good amount of my traces are missing, most are out of order, and finally, a lot of them are missing the beginning or end of the message, like this:

    Code:
    /main.cpp at line 28 there
    ne 33 here
    /EnColor.h at line 33 hello
    What am I doing wrong? I thought std::endl was supposed to flush my stream, doesn't that mean this shouldn't happen?

    Do I have a broken declaration of log_stream, and actually have several handles on the same file?

    Is this an ofstream problem, or what? What would you guys do?

    PS: Codeblocks with mingw on winxp32
    PS2, replacing log_stream with the standard std::cout works fine, but that is not what I want
    Last edited by monarch_dodra; January 28th, 2010 at 12:30 PM.

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Trace Macro

    Quote Originally Posted by monarch_dodra View Post
    Do I have a broken declaration of log_stream, and actually have several handles on the same file?
    Yes you will have a more than one instance of log_stream. One for each module that you have included that header.
    That's how static variables work. They are only visible from inside the module they are declared. That's why the linker doesn't complain.

    Declare it extern in the header and create only one in your main module.

    Kurt
    Last edited by ZuK; January 28th, 2010 at 01:01 PM.

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,359

    Re: Trace Macro

    Quote Originally Posted by ZuK View Post
    Yes you will have a more than one instance of log_stream. One for each module that you have included that header.
    That's how static variables work. They are only visible from inside the module they are declared. That's why the linker doesn't complain.

    Declare it extern in the header and create only one in your main module.

    Kurt
    Wow, after 1.5 years of coding C++ proffesionally, and I didn't even know that. Now I do though.

    Thanks a lot.

    That's what I get for never having used a global

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center