@@ -457,6 +457,24 @@ static FLB_INLINE const char *flb_skip_leading_zeros_simd(const char *data, cons
457457 return data ;
458458}
459459
460+ /* Return a UTF-8 safe cut position <= max */
461+ static size_t utf8_safe_truncate_pos (const char * s , size_t len , size_t max )
462+ {
463+ size_t cut = 0 ;
464+
465+ cut = (len <= max ) ? len : max ;
466+ if (cut == len ) {
467+ return cut ;
468+ }
469+
470+ /* backtrack over continuation bytes 10xxxxxx */
471+ while (cut > 0 && ((unsigned char )s [cut ] & 0xC0 ) == 0x80 ) {
472+ cut -- ;
473+ }
474+
475+ return cut ;
476+ }
477+
460478static int process_content (struct flb_tail_file * file , size_t * bytes )
461479{
462480 size_t len ;
@@ -481,6 +499,12 @@ static int process_content(struct flb_tail_file *file, size_t *bytes)
481499#ifdef FLB_HAVE_UNICODE_ENCODER
482500 size_t decoded_len ;
483501#endif
502+ size_t cut = 0 ;
503+ size_t dec_len = 0 ;
504+ size_t window = 0 ;
505+ int truncation_happened = FLB_FALSE ;
506+ size_t bytes_override = 0 ;
507+ void * nl = NULL ;
484508#ifdef FLB_HAVE_METRICS
485509 uint64_t ts ;
486510 char * name ;
@@ -542,6 +566,43 @@ static int process_content(struct flb_tail_file *file, size_t *bytes)
542566 data = (char * )flb_skip_leading_zeros_simd (data , end , & processed_bytes );
543567 }
544568
569+ if (ctx -> truncate_long_lines == FLB_TRUE ) {
570+ dec_len = (size_t )(end - data );
571+ window = ctx -> buf_max_size + 1 ;
572+ if (window > dec_len ) {
573+ window = dec_len ;
574+ }
575+
576+ nl = memchr (data , '\n' , window );
577+ if (nl == NULL && dec_len > ctx -> buf_max_size ) {
578+ cut = utf8_safe_truncate_pos (data , dec_len , ctx -> buf_max_size );
579+
580+ if (cut > 0 ) {
581+ if (ctx -> multiline == FLB_TRUE ) {
582+ flb_tail_mult_flush (file , ctx );
583+ }
584+
585+ flb_tail_file_pack_line (NULL , data , cut , file , processed_bytes );
586+
587+ #ifdef FLB_HAVE_METRICS
588+ cmt_counter_inc (ctx -> cmt_long_line_truncated ,
589+ cfl_time_now (), 1 ,
590+ (char * []){ (char * ) flb_input_name (ctx -> ins ) });
591+ #endif
592+ file -> skip_next = FLB_TRUE ;
593+
594+ bytes_override = (original_len > 0 ) ? original_len : file -> buf_len ;
595+ truncation_happened = FLB_TRUE ;
596+
597+ lines ++ ;
598+ goto truncation_end ;
599+ }
600+ else {
601+ file -> skip_next = FLB_TRUE ;
602+ }
603+ }
604+ }
605+
545606 while (data < end && (p = memchr (data , '\n' , end - data ))) {
546607 len = (p - data );
547608 crlf = 0 ;
@@ -700,6 +761,7 @@ static int process_content(struct flb_tail_file *file, size_t *bytes)
700761 file -> last_processed_bytes = processed_bytes ;
701762 }
702763
764+ truncation_end :
703765 if (decoded ) {
704766 flb_free (decoded );
705767 decoded = NULL ;
@@ -709,9 +771,13 @@ static int process_content(struct flb_tail_file *file, size_t *bytes)
709771
710772 if (lines > 0 ) {
711773 /* Append buffer content to a chunk */
712- if (original_len > 0 ) {
774+ if (truncation_happened ) {
775+ * bytes = bytes_override ;
776+ }
777+ else if (original_len > 0 ) {
713778 * bytes = original_len ;
714- } else {
779+ }
780+ else {
715781 * bytes = processed_bytes ;
716782 }
717783
0 commit comments