1 //! file output for Flog
3 //! @file flog_output_file.c
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
10 #include "flog_output_file.h"
12 #ifdef FLOG_CONFIG_OUTPUT_FILE
14 #include "flog_string.h"
20 //! Output function for simple log output to a file
21 //! filename is stored in log.output_func_data as a string
24 int flog_output_file(FLOG_T
*log
,const FLOG_MSG_T
*msg
)
26 if(log
->output_func_data
==NULL
) {
28 flog_print(log
->error_log
,"flog_output_file",FLOG_ERROR
,0,"please set log output filename");
32 if(flog_get_str_message(&str
,msg
))
36 if((f
= fopen(log
->output_func_data
,"a+t"))==NULL
) {
39 flog_printf(log
->error_log
,"fopen",FLOG_ERROR
,0,"cannot open file: %s",log
->output_func_data
);
42 if(fprintf(f
,str
)<0) {
45 flog_printf(log
->error_log
,"fprintf",FLOG_ERROR
,0,"cannot write to file: %s",log
->output_func_data
);
51 flog_printf(log
->error_log
,"fflush",FLOG_ERROR
,0,"cannot write to file: %s",log
->output_func_data
);
58 //! create and return a log that writes to file
60 //! @retval NULL error
61 FLOG_T
* create_flog_output_file(const char *name
, FLOG_MSG_TYPE_T accepted_msg_type
, const char *filename
)
64 if((p
=create_flog_t(name
,accepted_msg_type
))==NULL
)
66 p
->output_func
=flog_output_file
;
67 if(filename
&& filename
[0]) {
68 if((p
->output_func_data
=strdup(filename
))==NULL
) {
77 //! free an output_file FLOG_T
78 void destroy_flog_output_file(FLOG_T
*p
)
81 free(p
->output_func_data
);
82 p
->output_func_data
=NULL
;
88 #endif //FLOG_CONFIG_OUTPUT_FILE