C++跟踪日志记录问题,fstream 访问冲突问题
这确实是两个问题,我将从最简单的开始。在这个简单的程序结束时,我遇到了访问冲突异常。这是在 MSVC6 中(是的,我知道......)。
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;
}
第二个问题是我如何尝试应用 std::clog。我想实现一个相当简单的跟踪功能,该功能仅在调试时有效。 “发布模式”期间的任何跟踪都会太慢。
我目前的想法基本上是:
#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
首先,有谁知道在MSVC6中是否有像_DEBUG_
这样的预处理器常量或者与配置是调试还是发布模式相对应的东西?这将消除程序员手动切换的需要(但这是一个非常小的问题)。
更详细的问题是是否有某种模板魔术方法可以在没有宏的情况下完成此操作。我觉得有点像尼安德特人,每个函数都用宏来启动。
三个要点:
(1) 该程序存储在DLL 中。如果有两个完全相同的功能,一个带跟踪功能,一个不带跟踪功能,那就太好了。事实上,这将是理想的。我不在乎二进制文件的大小是否是原来的两倍,只要它能提高代码的可维护性即可。
(2) “发布模式”函数必须具有用于跟踪日志记录的 NOOP。
(3) 但是,我不想让每个函数都成为带有跟踪日志记录参数的模板函数。
一如既往,提前非常感谢大家。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第一个问题,您可能想要恢复原始缓冲区。比如:
或者更好的是,创建一个 RAII 类来处理它。
对于跟踪,宏是最佳选择。我会坚持使用您自己的 #define,因为这样您就可以选择在非调试构建中使用跟踪。
For your first problem, you probably want to restore the original buffer. Something like:
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.