用c/c++编写日志文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7400418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing a Log file in c/c++
提问by noddy
I want to write a log file in c++. I am processing certain things and thus i need to maintain a log of the properties of the things that i process so that i could revert back to this log file to see the properties of anything that interests me in particular.. Could someone help me in achieving this?
我想用 C++ 写一个日志文件。我正在处理某些事情,因此我需要维护我处理的事情的属性日志,以便我可以恢复到此日志文件以查看我特别感兴趣的任何内容的属性..有人可以帮助我吗实现这个?
回答by VolatileStorm
The standard method of logging (in my experience) is to use either the stdout or stderr streams. In C++ to use these you would need to include iostream, and use as below:
记录日志的标准方法(根据我的经验)是使用 stdout 或 stderr 流。在 C++ 中使用这些你需要包含 iostream,并使用如下:
#include <iostream>
int main(int argc, char* argv[])
{
using std::cout;
using std::cerr;
using std::endl;
cout << "Output message" << endl;
cerr << "Error message" << endl;
}
This, however, only achieves printing to those outputs, which usually end up at a terminal. If you want to use these standard stream methods (which are quite readable) to output to a file, then you have to redirect your output somehow. One way of doing this is by using the freopen
function, provided by cstdio. What this does is open a file, and moves a given stream to that file. See herefor documentation. An example would be:
然而,这只能实现打印到那些通常在终端结束的输出。如果您想使用这些标准流方法(它们非常易读)输出到文件,那么您必须以某种方式重定向输出。一种方法是使用freopen
cstdio 提供的函数。它的作用是打开一个文件,并将给定的流移动到该文件。有关文档,请参见此处。一个例子是:
#include <iostream>
#include <cstdio>
int main(int argc, char* argv[])
{
using namespace std;
freopen( "output.txt", "w", stdout );
freopen( "error.txt", "w", stderr );
cout << "Output message" << endl;
cerr << "Error message" << endl;
}
(I've changed to using namespace std;
there just for conciseness.)
(using namespace std;
为了简洁起见,我已更改为那里。)
You're moving the standard output stream stdout
(which is used by cout
) to output.txt (in write mode), and you're moving stderr
(which is used by cerr
) to error.txt also in write mode.
您正在将标准输出流stdout
(由 使用cout
)移动到 output.txt(在写入模式下),并且您正在将stderr
(由 使用cerr
)移动到 error.txt,也在写入模式下。
Hopefully this does the trick.
希望这能解决问题。
回答by Streamsoup
This is quite handy, just plug into e.g. some common header file to be called from anywhere in the program (better approach would be to form a class with these functions)
这非常方便,只需插入一些通用头文件即可从程序中的任何位置调用(更好的方法是使用这些函数形成一个类)
inline string getCurrentDateTime( string s ){
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
if(s=="now")
strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);
else if(s=="date")
strftime(buf, sizeof(buf), "%Y-%m-%d", &tstruct);
return string(buf);
};
inline void Logger( string logMsg ){
string filePath = "/somedir/log_"+getCurrentDateTime("date")+".txt";
string now = getCurrentDateTime("now");
ofstream ofs(filePath.c_str(), std::ios_base::out | std::ios_base::app );
ofs << now << '\t' << logMsg << '\n';
ofs.close();
}
Usage: Logger("This is log message"); Writes a file (or appends existing file)
用法:Logger("这是日志消息"); 写入文件(或追加现有文件)
/somedir/log_2017-10-20.txt
with content:
内容:
2017-10-20 09:50:59 This is log message
回答by noddy
The sort of thing you're trying to do is too in-depth to provide a complete solution on stack overflow. What you can do is check out the documentation for the logging library of your choice. In my case, that's Boost.Log
, a logging library for the Boost C++ librariesthe documentation for which can be found here.
您尝试做的事情太深入,无法提供有关堆栈溢出的完整解决方案。您可以做的是查看您选择的日志库的文档。就我而言,这是Boost C++ 库的Boost.Log
日志记录库,可在此处找到其文档。
It's pointed out at the bottom of the page I've just linked to that
它在我刚刚链接到的页面底部指出
This library is not an official part of Boost libraries collection although it has passed the review and is provisionally accepted. The review result is available here.
尽管该库已通过审核并被临时接受,但它不是 Boost 库集合的官方部分。结果可在此处获得。
so make of that what you will.
所以你会怎么做。
回答by razlebe
Why not use one of the many logging frameworks available, like Apache log4cxx? I would suggest this rather than attempting to roll your own - why re-invent the wheel?
为什么不使用许多可用的日志框架之一,比如Apache log4cxx?我建议这样做而不是尝试自己动手 - 为什么要重新发明轮子?
回答by johnwbyrd
You also might want to consider http://www.logog.org. It's a performance-oriented C++ logging system. However, if that's a little too intense for your project, good old cerr and cout work fine for this.
您可能还想考虑http://www.logog.org。它是一个面向性能的 C++ 日志系统。但是,如果这对您的项目来说有点过于紧张,那么好的旧 cerr 和 cout 可以很好地解决这个问题。