As a precaution, set some pointers to NULL after free
[flog.git] / flog_file.c
blob0f927998f4d6ea36bb0ec64bc98d7ac62a1ebef0
1 /*!
2 @file flog_file.c
3 @brief file output for Flog
4 @author Nabeel Sowan (nabeel.sowan@vibes.se)
6 When you want flog to write to a file
7 Choose logfile name by setting data to string
8 */
10 #include "flog_file.h"
11 #include <stdlib.h>
12 #include <stdio.h>
14 int flog_output_file(FLOG_T *log,const FLOG_MSG_T *msg)
16 char *str;
17 int e=0;
18 if((str=flog_msg_t_to_str(msg))==NULL)
19 return(1);
20 if(log->output_func_data==NULL) {
21 log->output_error=1;
22 flog_print(log->error_log,FLOG_ERROR,"flog_file_output","please set log output filename");
23 e=1;
24 } else {
25 FILE *f;
26 if((f = fopen(log->output_func_data,"a+t"))==NULL) {
27 log->output_error=1;
28 flog_printf(log->error_log,FLOG_ERROR,NULL,"cannot open file: %s",log->output_func_data);
29 e=1;
30 } else {
31 if(fprintf(f,str)<0) {
32 log->output_error=1;
33 flog_printf(log->error_log,FLOG_ERROR,"fprintf","cannot write to file: %s",log->output_func_data);
34 e=1;
36 if(fclose(f)==EOF) {
37 log->output_error=1;
38 flog_printf(log->error_log,FLOG_ERROR,"fflush","cannot write to file: %s",log->output_func_data);
39 e=1;
43 free(str);
44 return(e);