简体   繁体   English

用 C/C++ 编写日志文件

[英]Writing a Log file in C/C++

I want to write a log file in C++.我想用 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?我正在处理某些事情,因此我需要维护我处理的事情的属性日志,以便我可以恢复到此日志文件以查看我特别感兴趣的任何内容的属性......有人可以帮助我吗在实现这一目标?

The standard method of logging (in my experience) is to use either the stdout or stderr streams.记录日志的标准方法(根据我的经验)是使用 stdout 或 stderr 流。 In C++ to use these you would need to include iostream, and use as below:在 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.一种方法是使用 cstdio 提供的freopen函数。 What this does is open a file, and moves a given stream to that file.它的作用是打开一个文件,并将给定的流移动到该文件。 See here for documentation.有关文档,请参见此处 An example would be:一个例子是:

#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.希望这能解决问题。

This is quite handy, just plug into eg 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");用法:Logger("这是日志消息"); Writes a file (or appends existing file)写入文件(或追加现有文件)

/somedir/log_2017-10-20.txt 

with content:内容:

2017-10-20 09:50:59 This is log message

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++ libraries the documentation for which can be found here .就我而言,这是Boost.Log ,它是Boost C++ 库的日志库,其文档可在 此处找到。

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.尽管该库已通过审核并被临时接受,但它不是 Boost 库集合的官方部分。 The review result is available here .审查结果可在此处获得

so make of that what you will.所以你会怎么做。

Why not use one of the many logging frameworks available, like Apache log4cxx ?为什么不使用许多可用的日志框架之一,比如Apache log4cxx I would suggest this rather than attempting to roll your own - why re-invent the wheel?我建议这样做而不是尝试自己动手 - 为什么要重新发明轮子?

You also might want to consider https://github.com/johnwbyrd/logog .您可能还想考虑https://github.com/johnwbyrd/logog It's a performance-oriented C++ logging system.它是一个面向性能的 C++ 日志系统。 However, if that's a little too intense for your project, good old cerr and cout work fine for this.但是,如果这对您的项目来说有点过于紧张,那么好的旧 cerr 和 cout 可以很好地解决这个问题。

非常感谢所有的回复...我认为我正在寻找的答案是,甚至UTF8中的日志文件的格式就像一个txt文件所以c在编写带有简单文件的那种文件时没有问题写它提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM