Prefix all config options with FLOG_CONFIG_
[flog.git] / flog_output_file.c
blob5086045ea99d0e1eb1347d6b38087a8bc89a0cd5
1 //! file output for Flog
3 //! @file flog_output_file.c
4 //! @author Nabeel Sowan (nabeel.sowan@vibes.se)
5 //!
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"
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
20 //! Output function for simple log output to a file
21 //! filename is stored in log.output_func_data as a string
23 //! @retval 0 success
24 int flog_output_file(FLOG_T *log,const FLOG_MSG_T *msg)
26 if(log->output_func_data==NULL) {
27 log->output_error=1;
28 flog_print(log->error_log,"flog_output_file",FLOG_ERROR,0,"please set log output filename");
29 return(-1);
31 char *str;
32 if(flog_get_str_message(&str,msg))
33 return(-1);
35 FILE *f;
36 if((f = fopen(log->output_func_data,"a+t"))==NULL) {
37 log->output_error=1;
38 free(str);
39 flog_printf(log->error_log,"fopen",FLOG_ERROR,0,"cannot open file: %s",log->output_func_data);
40 return(-2);
42 if(fprintf(f,str)<0) {
43 log->output_error=1;
44 free(str);
45 flog_printf(log->error_log,"fprintf",FLOG_ERROR,0,"cannot write to file: %s",log->output_func_data);
46 return(-2);
48 free(str);
49 if(fclose(f)==EOF) {
50 log->output_error=1;
51 flog_printf(log->error_log,"fflush",FLOG_ERROR,0,"cannot write to file: %s",log->output_func_data);
52 return(-2);
54 return(0);
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)
63 FLOG_T *p;
64 if((p=create_flog_t(name,accepted_msg_type))==NULL)
65 return(NULL);
66 p->output_func=flog_output_file;
67 if(filename && filename[0]) {
68 if((p->output_func_data=strdup(filename))==NULL) {
69 destroy_flog_t(p);
70 return(NULL);
73 return(p);
77 //! free an output_file FLOG_T
78 void destroy_flog_output_file(FLOG_T *p)
80 if(p!=NULL) {
81 free(p->output_func_data);
82 p->output_func_data=NULL;
83 destroy_flog_t(p);
88 #endif //FLOG_CONFIG_OUTPUT_FILE