在C编程中存储日志/错误消息

时间:2022-10-17 21:49:26

When an error occurs, I would like my C code to store the error before exiting the program. Is it advised to store the stderr to a file (e.g., /home/logs.txt) or would it be advised to use a different method to keep the logs/error report (considering the programming environment is Linux). E.g., for the code below, how I could apply the method to store the logs/error message on /home/log.txt or /home/log

当发生错误时,我希望我的C代码在退出程序之前存储错误。是否建议将stderr存储到一个文件(例如,/home/log .txt),或者建议使用不同的方法来保存日志/错误报告(考虑到编程环境是Linux)。例如,对于下面的代码,我如何应用这个方法在/home/ logon上存储日志/错误消息。txt或/home/log

FILE *fp1;
fp1 = fopen("/sys/class/gpio/export","w"); 
if(fp1 == NULL){
    fprintf(stderr, "errno:%s - opening GPIO136 failed - line 739\n ", strerror(errno));
    close(fp1);
    exit(1);
}

Thank you.

谢谢你!

2 个解决方案

#1


3  

If stderr is always used to print out all your error message, so, you can redirect output to a specific file.

如果stderr总是用于打印所有错误消息,那么可以将输出重定向到特定的文件。

$ program 2>~/logs.txt

For a better logging tool, you can use:

要获得更好的日志记录工具,您可以使用:

  • syslog standard function.

    syslog标准函数。

  • log4c library.

    log4c图书馆。

#2


0  

If you want to store the error, stderr is probably not a good choice because you'll need to pipe stderr to a file every time you run the program.

如果希望存储错误,stderr可能不是一个好的选择,因为每次运行程序时都需要将stderr管道到文件中。

If you want to write to /home/log.txt, open a FILE pointer to it and write with fprintf the same way you tried to open /sys/class/gpio/export and write to that instead of stderr. Also be sure to open the log file with append mode.

如果你想写/home/ logxa。txt,打开一个文件指针,用fprintf写,就像你尝试打开/sys/class/gpio/导出并写入到那个而不是stderr。还要确保使用append模式打开日志文件。

FILE *fp1;
fp1 = fopen("/sys/class/gpio/export","w"); 
if(fp1 == NULL){
   FILE *fpErr = fopen("/home/log.txt", "a");
   if(fpErr != NULL)
      fprintf(fpErr, "errno:%s - opening GPIO136 failed - line 739\n ", strerror(errno));
   close(fpErr);
   close(fp1);
   exit(1);
}

#1


3  

If stderr is always used to print out all your error message, so, you can redirect output to a specific file.

如果stderr总是用于打印所有错误消息,那么可以将输出重定向到特定的文件。

$ program 2>~/logs.txt

For a better logging tool, you can use:

要获得更好的日志记录工具,您可以使用:

  • syslog standard function.

    syslog标准函数。

  • log4c library.

    log4c图书馆。

#2


0  

If you want to store the error, stderr is probably not a good choice because you'll need to pipe stderr to a file every time you run the program.

如果希望存储错误,stderr可能不是一个好的选择,因为每次运行程序时都需要将stderr管道到文件中。

If you want to write to /home/log.txt, open a FILE pointer to it and write with fprintf the same way you tried to open /sys/class/gpio/export and write to that instead of stderr. Also be sure to open the log file with append mode.

如果你想写/home/ logxa。txt,打开一个文件指针,用fprintf写,就像你尝试打开/sys/class/gpio/导出并写入到那个而不是stderr。还要确保使用append模式打开日志文件。

FILE *fp1;
fp1 = fopen("/sys/class/gpio/export","w"); 
if(fp1 == NULL){
   FILE *fpErr = fopen("/home/log.txt", "a");
   if(fpErr != NULL)
      fprintf(fpErr, "errno:%s - opening GPIO136 failed - line 739\n ", strerror(errno));
   close(fpErr);
   close(fp1);
   exit(1);
}