@@ -83,6 +83,7 @@ static unsigned long file_rotate_size;
8383static str file_suffix ;
8484static pv_elem_p file_suffix_format ;
8585static str escape_delimiter = {0 , 0 };
86+ static str file_header ;
8687
8788static void raise_rotation_event (struct flat_file * file , const char * reason );
8889static void update_counters_and_rotate (struct flat_file * file ,
@@ -116,6 +117,7 @@ static const param_export_t mod_params[] = {
116117 {"rotate_size" , INT_PARAM |STR_PARAM |USE_FUNC_PARAM , (void * )rotate_size_param },
117118 {"suffix" , STR_PARAM , & file_suffix .s },
118119 {"escape_delimiter" , STR_PARAM , & escape_delimiter .s },
120+ {"header" , STR_PARAM , & file_header .s },
119121 {0 ,0 ,0 }
120122};
121123
@@ -283,6 +285,9 @@ static int mod_init(void) {
283285 LM_DBG ("The delimiter for separating columns in files was set at %.*s\n" , delimiter .len , delimiter .s );
284286 }
285287
288+ if (file_header .s )
289+ file_header .len = strlen (file_header .s );
290+
286291 if (escape_delimiter .s ) {
287292 escape_delimiter .len = strlen (escape_delimiter .s );
288293 if (escape_delimiter .len != delimiter .len ) {
@@ -467,6 +472,7 @@ mi_response_t *mi_rotate(const mi_params_t *params,
467472 found_fd -> rotate_version ++ ;
468473 found_fd -> record_count = 0 ;
469474 found_fd -> bytes_written = 0 ;
475+ found_fd -> header_written = 0 ;
470476 ensure_file_path (found_fd , 1 );
471477
472478 lock_release (global_lock );
@@ -503,6 +509,7 @@ static int insert_in_list(struct flat_file *entry) {
503509 * list_files = entry ;
504510 entry -> prev = NULL ;
505511 entry -> next = NULL ;
512+ entry -> header_written = 0 ;
506513 return 0 ;
507514 }
508515
@@ -512,6 +519,7 @@ static int insert_in_list(struct flat_file *entry) {
512519 entry -> file_index_process = head -> file_index_process + 1 ;
513520 entry -> prev = NULL ;
514521 entry -> next = head ;
522+ entry -> header_written = 0 ;
515523 head -> prev = entry ;
516524 * list_files = entry ;
517525 return 0 ;
@@ -524,6 +532,7 @@ static int insert_in_list(struct flat_file *entry) {
524532 entry -> file_index_process = expected ;
525533 entry -> prev = aux -> prev ;
526534 entry -> next = aux ;
535+ entry -> header_written = 0 ;
527536 aux -> prev = entry ;
528537 entry -> prev -> next = entry ;
529538 return 0 ;
@@ -537,6 +546,7 @@ static int insert_in_list(struct flat_file *entry) {
537546 entry -> file_index_process = expected ;
538547 entry -> prev = parent ;
539548 entry -> next = NULL ;
549+ entry -> header_written = 0 ;
540550 parent -> next = entry ;
541551 return 0 ;
542552 }
@@ -777,6 +787,17 @@ static void rotating(struct flat_file *file){
777787 file -> counter_open ++ ;
778788 LM_DBG ("File %s is opened %d time\n" , file -> pathname , file -> counter_open );
779789
790+ /* write header */
791+ if (file_header .s && !file -> header_written ) {
792+ if (write (opened_fds [index ], file_header .s ,
793+ file_header .len ) == file_header .len &&
794+ write (opened_fds [index ], "\n" , 1 ) == 1 ) {
795+ file -> header_written = 1 ;
796+ } else {
797+ LM_ERR ("failed to write header to %s\n" , file -> pathname );
798+ }
799+ }
800+
780801 lock_release (global_lock );
781802 return ;
782803
@@ -1032,6 +1053,7 @@ static void event_flatstore_timer(unsigned int ticks, void *param)
10321053 file -> rotate_version ++ ;
10331054 file -> record_count = 0 ;
10341055 file -> bytes_written = 0 ;
1056+ file -> header_written = 0 ;
10351057 ensure_file_path (file , 1 );
10361058 raise_rotation_event (file , ROTATE_REASON_PERIOD );
10371059 LM_DBG ("File %s is being rotated at %u - new file is %s\n" ,
@@ -1179,18 +1201,17 @@ static void update_counters_and_rotate(struct flat_file *file, ssize_t bytes_inc
11791201 file -> record_count ++ ;
11801202 file -> bytes_written += (unsigned long )bytes_inc ;
11811203
1182- if (file_rotate_count &&
1183- file -> record_count >= file_rotate_count )
1204+ if (file_rotate_count && file -> record_count >= file_rotate_count )
11841205 hit_cnt = 1 ;
11851206
1186- if (!hit_cnt && file_rotate_size &&
1187- file -> bytes_written >= file_rotate_size )
1207+ if (!hit_cnt && file_rotate_size && file -> bytes_written >= file_rotate_size )
11881208 hit_sz = 1 ;
11891209
11901210 if (hit_cnt || hit_sz ) {
11911211 file -> rotate_version ++ ;
11921212 file -> record_count = 0 ;
11931213 file -> bytes_written = 0 ;
1214+ file -> header_written = 0 ;
11941215 ensure_file_path (file , 1 );
11951216 }
11961217 lock_release (global_lock );
0 commit comments