用C/C++编写日志文件
Writing a Log file in C/C++
我想用C++编写一个日志文件。我正在处理某些事情,因此我需要维护我处理的事情的属性日志,这样我就可以恢复到此日志文件,查看我特别感兴趣的任何事情的属性。。。有人能帮我实现这一点吗?
日志记录的标准方法(根据我的经验(是使用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;
}
然而,这只实现了对这些输出的打印,这些输出通常会在终端结束。如果您想使用这些标准流方法(可读性很强(输出到文件,那么您必须以某种方式重定向输出。一种方法是使用cstdio提供的freopen
函数。这样做的目的是打开一个文件,并将给定的流移动到该文件。请参阅此处获取文档。例如:
#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;
}
(为了简洁起见,我在那里改成了using namespace std;
。(
您正在将标准输出流stdout
(由cout
使用(移动到output.txt(在写入模式下(,并且您正在将stderr
(由cerr
使用(也移动到error.txt(在写模式下(。
希望这能奏效。
这非常方便;只需插入,例如,从程序中的任何地方调用的一些通用头文件(更好的方法是用这些函数形成一个类(
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();
}
用法:Logger("This is log message");
写入文件(或附加现有文件(
/somedir/log_2017-10-20.txt
内容:
2017-10-20 09:50:59 This is log message
您尝试做的这类事情过于深入,无法提供关于堆栈溢出的完整解决方案。您可以查看所选日志库的文档。在我的例子中,它是Boost.Log
,一个Boost C++库的日志库,其文档可以在这里找到。
它在我刚刚链接到的页面底部指出
此库不是Boost库集合的官方组成部分尽管它已经通过审查并被暂时接受。这个评审结果可在此处查看。
所以你想怎样就怎样。
为什么不使用众多可用的日志框架之一,如Apache log4cxx?我建议这样做,而不是试图自己滚动——为什么要重新发明轮子?
您可能还需要考虑https://github.com/johnwbyrd/logog。这是一个面向性能的C++日志记录系统。然而,如果这对你的项目来说有点过于紧张,那么好的旧cerr和cout可以很好地解决这个问题。
#include<string>
#include <fstream>
inline std::string getCurrentDateTime( std::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 std::string(buf);
};
inline void Logger(std::string logMsg){
std::string filePath = "./log_"+getCurrentDateTime("date")+".txt";
std::string now = getCurrentDateTime("now");
std::ofstream ofs(filePath.c_str(), std::ios_base::out | std::ios_base::app );
ofs << now << 't' << logMsg << 'n';
ofs.close();
}
例如:
Logger("JAMLEE: ---------| start VT_SETMODE");
- 加快在C++中读取/处理日志文件的速度
- 如何将消息时间戳写入日志文件?
- 分析包含 NMEA 句子的日志文件C++
- 如何从日志文件中抓取状态代码?(在 C++ 中)
- 未创建日志文件
- 尝试使用 EvtSetChannelConfigProperty() 函数更新最大事件日志文件大小时插入的错误值
- 提升日志:文件轮换
- POSIX C/C++日志文件(VEX V5 Brain)
- boost日志文件无法创建sample.log文件
- 哪个更适合从C++写入敏感的日志文件,在文件描述符上写()或文件上的fprintf()?
- 如何编写日志文件,以便可以使用记事本实时读取它以进行C++
- 如何在QT中使用DEBUG对齐日志文件中的数据?
- Qt - 如何在 30 天后删除日志文件,文件名不固定
- Mac OS X:应用程序可以创建日志文件
- 将断言消息写入日志文件
- 检查日志文件以找出 vcredist.exe 安装失败的原因
- C++同时记录到控制台和日志文件
- 无法创建将包含日志文件的文件夹
- (罗)如何使用相对路径保存日志文件
- boost::log关闭日志文件并打开一个新文件