-
January 28th, 2010, 11:25 AM
#1
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 11:30 AM.
-
January 28th, 2010, 11:53 AM
#2
Re: Trace Macro
 Originally Posted by monarch_dodra
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 12:01 PM.
-
January 28th, 2010, 02:20 PM
#3
Re: Trace Macro
 Originally Posted by ZuK
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
On-Demand Webinars (sponsored)
|