@@ -243,6 +243,13 @@ void r_free_cfg(r_cfg_t *cfg)
243243
244244 list_free_elems (& cfg -> output_handler , (list_elem_free_fn )data_output_free );
245245
246+ for (void * * iter = cfg -> output_files .elems ; iter && * iter ; ++ iter ) {
247+ FILE * file = * iter ;
248+ if (file && file != stdout )
249+ fclose (file );
250+ }
251+ list_free_elems (& cfg -> output_files , NULL );
252+
246253 list_free_elems (& cfg -> data_tags , (list_elem_free_fn )data_tag_free );
247254
248255 list_free_elems (& cfg -> in_files , NULL );
@@ -1007,7 +1014,7 @@ static int lvlarg_param(char **param, int default_verb)
10071014}
10081015
10091016/// Opens the path @p param (or STDOUT if empty or `-`) for append writing, removes leading `,` and `:` from path name.
1010- static FILE * fopen_output (char const * param )
1017+ static FILE * fopen_output (r_cfg_t * cfg , char const * param )
10111018{
10121019 if (!param || !* param ) {
10131020 return stdout ; // No path given
@@ -1026,19 +1033,20 @@ static FILE *fopen_output(char const *param)
10261033 fprintf (stderr , "rtl_433: failed to open output file\n" );
10271034 exit (1 );
10281035 }
1036+ list_push (& cfg -> output_files , file );
10291037 return file ;
10301038}
10311039
10321040void add_json_output (r_cfg_t * cfg , char * param )
10331041{
10341042 int log_level = lvlarg_param (& param , 0 );
1035- list_push (& cfg -> output_handler , data_output_json_create (log_level , fopen_output (param )));
1043+ list_push (& cfg -> output_handler , data_output_json_create (log_level , fopen_output (cfg , param )));
10361044}
10371045
10381046void add_csv_output (r_cfg_t * cfg , char * param )
10391047{
10401048 int log_level = lvlarg_param (& param , 0 );
1041- list_push (& cfg -> output_handler , data_output_csv_create (log_level , fopen_output (param )));
1049+ list_push (& cfg -> output_handler , data_output_csv_create (log_level , fopen_output (cfg , param )));
10421050}
10431051
10441052void start_outputs (r_cfg_t * cfg , char const * const * well_known )
@@ -1057,13 +1065,13 @@ void start_outputs(r_cfg_t *cfg, char const *const *well_known)
10571065void add_log_output (r_cfg_t * cfg , char * param )
10581066{
10591067 int log_level = lvlarg_param (& param , LOG_TRACE );
1060- list_push (& cfg -> output_handler , data_output_log_create (log_level , fopen_output (param )));
1068+ list_push (& cfg -> output_handler , data_output_log_create (log_level , fopen_output (cfg , param )));
10611069}
10621070
10631071void add_kv_output (r_cfg_t * cfg , char * param )
10641072{
10651073 int log_level = lvlarg_param (& param , LOG_TRACE );
1066- list_push (& cfg -> output_handler , data_output_kv_create (log_level , fopen_output (param )));
1074+ list_push (& cfg -> output_handler , data_output_kv_create (log_level , fopen_output (cfg , param )));
10671075}
10681076
10691077void add_mqtt_output (r_cfg_t * cfg , char * param )
@@ -1107,7 +1115,7 @@ void add_http_output(r_cfg_t *cfg, char *param)
11071115void add_trigger_output (r_cfg_t * cfg , char * param )
11081116{
11091117 // Note: no log_level, we never trigger on logs.
1110- list_push (& cfg -> output_handler , data_output_trigger_create (fopen_output (param )));
1118+ list_push (& cfg -> output_handler , data_output_trigger_create (fopen_output (cfg , param )));
11111119}
11121120
11131121void add_null_output (r_cfg_t * cfg , char * param )
0 commit comments