this is really two questions, I'll start with the easiest. I get an access violation exception at the end of this simple program. This is in MSVC6 (yes, I know...).
int main()
{
std::fstream logFile("clog.txt");
std::clog.rdbuf( logFile.rdbuf() );
// ... use clog ...
logFile.close(); // I've tried removing this, same problem.
return 0;
}
The second question is how am trying to apply std::clog. I want to implement a fairly simple tracing functionality, that is only active while debugging. Any tracing during "release mode" would be too slow.
My current idea is basically:
#define TRACE_LOG_TOGGLE 1
#if TRACE_LOG_TOGGLE
#define TRACE_LOG(a) // something that ultimately uses std::clog
#else
#define TRACE_LOG(a) // empty.
#endif
First, does anyone know in MSVC6 if there is a preprocessor constant like _DEBUG_
or something that corresponds to whether the configuration is Debug or Release mode? That would eliminate the need of the programmer to toggle this manually (but it's a very minor issue).
The more detailed question was if there was some kind of template magic way of doing this sans macros. I feel a little bit neanderthal starting each and every function with a macro.
Three important points:
(1) This program is stored in a DLL. It would be perfectly fine to have two otherwise identical functions, one with tracing and one without. In fact that would be ideal. I don't care if the binary is twice the size, as long as it improves the code maintainability.
(2) The "release mode" function has to have a NOOP for the trace logging.
(3) I don't want to make every function a template function with the trace logging parameter however.
As always, thanks much in advance guys.
For your first problem, you probably want to restore the original buffer. Something like:
std::fstream logFile("clog.txt");
streambuf * old = std::clog.rdbuf( logFile.rdbuf() );
// ... use clog ...
std::clog.rdbuf( old );
logFile.close(); // I've tried removing this, same problem.
Or better,create a RAII class to handle it.
For the tracing, macros are the way to go. I would stick with using your own #define, as that way you have the option of using tracing in a non-debug build.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.