@@ -112,8 +112,17 @@ void flb_tail_file_close_handle(struct flb_tail_file *file)
112112/* Close file handle during tail if it's open and keep_file_handle is false */
113113void flb_tail_file_close_handle_during_tail (struct flb_tail_file * file )
114114{
115+ flb_plg_debug (file -> config -> ins , "close_handle_during_tail: DEBUG: entering, file=%s, keep_file_handle=%d, fd=%d" ,
116+ file -> name ? file -> name : "unknown" , file -> config -> keep_file_handle , file -> fd );
115117 if (file -> config -> keep_file_handle == FLB_FALSE && file -> fd != -1 ) {
118+ flb_plg_debug (file -> config -> ins , "close_handle_during_tail: DEBUG: closing handle for %s, fd=%d" ,
119+ file -> name , file -> fd );
116120 flb_tail_file_close_handle (file );
121+ flb_plg_debug (file -> config -> ins , "close_handle_during_tail: DEBUG: handle closed, file->fd=%d" , file -> fd );
122+ }
123+ else {
124+ flb_plg_debug (file -> config -> ins , "close_handle_during_tail: DEBUG: NOT closing (keep_file_handle=%d, fd=%d)" ,
125+ file -> config -> keep_file_handle , file -> fd );
117126 }
118127}
119128
@@ -1087,63 +1096,85 @@ static int set_file_position(struct flb_tail_config *ctx,
10871096{
10881097 int64_t ret ;
10891098
1099+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: entering, file=%s, fd=%d, offset=%ld, read_from_head=%d" ,
1100+ file -> name ? file -> name : "unknown" , file -> fd , file -> offset , ctx -> read_from_head );
1101+
10901102#ifdef FLB_HAVE_SQLDB
10911103 /*
10921104 * If the database option is enabled, try to gather the file position. The
10931105 * database function updates the file->offset entry.
10941106 */
10951107 if (ctx -> db ) {
1108+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: database enabled, calling flb_tail_db_file_set" );
10961109 ret = flb_tail_db_file_set (file , ctx );
1110+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: flb_tail_db_file_set returned %ld, file->offset=%ld" ,
1111+ ret , file -> offset );
10971112 if (ret == 0 ) {
10981113 if (file -> offset > 0 ) {
1114+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: seeking to offset %ld, fd=%d" , file -> offset , file -> fd );
10991115 ret = lseek (file -> fd , file -> offset , SEEK_SET );
11001116 if (ret == -1 ) {
11011117 flb_errno ();
1118+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: lseek to offset %ld FAILED, fd=%d" , file -> offset , file -> fd );
11021119 return -1 ;
11031120 }
1121+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: lseek to offset %ld succeeded, ret=%ld" , file -> offset , ret );
11041122 }
11051123 else if (ctx -> read_from_head == FLB_FALSE ) {
1124+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: offset=0, read_from_head=false, seeking to end, fd=%d" , file -> fd );
11061125 ret = lseek (file -> fd , 0 , SEEK_END );
11071126 if (ret == -1 ) {
11081127 flb_errno ();
1128+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: lseek to end FAILED, fd=%d" , file -> fd );
11091129 return -1 ;
11101130 }
11111131 file -> offset = ret ;
1132+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: lseek to end succeeded, offset set to %ld" , file -> offset );
11121133 flb_tail_db_file_offset (file , ctx );
11131134 }
1135+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: returning 0 (database path)" );
11141136 return 0 ;
11151137 }
1138+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: database file_set returned non-zero, continuing with fallback logic" );
11161139 }
11171140#endif
11181141
11191142 if (ctx -> read_from_head == FLB_TRUE ) {
11201143 /* no need to seek, offset position is already zero */
1144+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: read_from_head=true, returning 0 without seek" );
11211145 return 0 ;
11221146 }
11231147
11241148 if (file -> offset > 0 ) {
1149+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: seeking to offset %ld, fd=%d" , file -> offset , file -> fd );
11251150 ret = lseek (file -> fd , file -> offset , SEEK_SET );
11261151
11271152 if (ret == -1 ) {
11281153 flb_errno ();
1154+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: lseek to offset %ld FAILED, fd=%d" , file -> offset , file -> fd );
11291155 return -1 ;
11301156 }
1157+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: lseek to offset %ld succeeded, ret=%ld" , file -> offset , ret );
11311158 }
11321159 else {
1160+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: offset=0, seeking to end, fd=%d" , file -> fd );
11331161 ret = lseek (file -> fd , 0 , SEEK_END );
11341162
11351163 if (ret == -1 ) {
11361164 flb_errno ();
1165+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: lseek to end FAILED, fd=%d" , file -> fd );
11371166 return -1 ;
11381167 }
11391168
11401169 file -> offset = ret ;
1170+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: lseek to end succeeded, offset set to %ld" , file -> offset );
11411171 }
11421172
11431173 if (file -> decompression_context == NULL ) {
11441174 file -> stream_offset = ret ;
11451175 }
11461176
1177+ flb_plg_debug (ctx -> ins , "set_file_position: DEBUG: returning 0 (success)" );
11471178 return 0 ;
11481179}
11491180
@@ -1204,11 +1235,16 @@ int flb_tail_file_append(char *path, struct stat *st, int mode,
12041235 struct stat lst ;
12051236 flb_sds_t inode_str ;
12061237
1238+ flb_plg_debug (ctx -> ins , "tail_append: DEBUG: entering flb_tail_file_append for %s, keep_file_handle=%d" ,
1239+ path , ctx -> keep_file_handle );
1240+
12071241 if (!S_ISREG (st -> st_mode )) {
1242+ flb_plg_debug (ctx -> ins , "tail_append: DEBUG: not a regular file, returning -1" );
12081243 return -1 ;
12091244 }
12101245
12111246 if (flb_tail_file_exists (st , ctx ) == FLB_TRUE ) {
1247+ flb_plg_debug (ctx -> ins , "tail_append: DEBUG: file already exists, returning -1" );
12121248 return -1 ;
12131249 }
12141250
@@ -1225,6 +1261,9 @@ int flb_tail_file_append(char *path, struct stat *st, int mode,
12251261 return -1 ;
12261262 }
12271263
1264+ flb_plg_debug (ctx -> ins , "tail_append: DEBUG: file opened successfully, fd=%d, keep_file_handle=%d (FLB_FALSE=%d, FLB_TRUE=%d)" ,
1265+ fd , ctx -> keep_file_handle , FLB_FALSE , FLB_TRUE );
1266+
12281267 file = flb_calloc (1 , sizeof (struct flb_tail_file ));
12291268 if (!file ) {
12301269 flb_errno ();
@@ -1440,19 +1479,34 @@ int flb_tail_file_append(char *path, struct stat *st, int mode,
14401479 }
14411480
14421481 /* Set the file position (database offset, head or tail) */
1482+ flb_plg_debug (ctx -> ins , "tail_append: DEBUG: about to call set_file_position for %s, current fd=%d, offset=%ld" ,
1483+ file -> name ? file -> name : path , file -> fd , file -> offset );
14431484 ret = set_file_position (ctx , file );
1485+ flb_plg_debug (ctx -> ins , "tail_append: DEBUG: set_file_position returned %d for %s, file->offset=%ld, file->fd=%d" ,
1486+ ret , file -> name ? file -> name : path , file -> offset , file -> fd );
14441487 if (ret == -1 ) {
1488+ flb_plg_debug (ctx -> ins , "tail_append: DEBUG: set_file_position failed, removing file and going to error" );
14451489 flb_tail_file_remove (file );
14461490 goto error ;
14471491 }
14481492
14491493 /* Remaining bytes to read */
14501494 file -> pending_bytes = file -> size - file -> offset ;
1495+ flb_plg_debug (ctx -> ins , "tail_append: DEBUG: pending_bytes=%ld, size=%ld, offset=%ld" ,
1496+ file -> pending_bytes , file -> size , file -> offset );
14511497
14521498 /* Close file handle if keep_file_handle is false */
1499+ flb_plg_debug (ctx -> ins , "tail_append: DEBUG: checking keep_file_handle: ctx->keep_file_handle=%d (FLB_FALSE=%d), file->fd=%d" ,
1500+ ctx -> keep_file_handle , FLB_FALSE , file -> fd );
14531501 if (ctx -> keep_file_handle == FLB_FALSE ) {
14541502 flb_plg_debug (ctx -> ins , "tail_append: file will be read without keeping file handle opened %s" , file -> name );
1503+ flb_plg_debug (ctx -> ins , "tail_append: DEBUG: calling flb_tail_file_close_handle_during_tail for %s, fd=%d" ,
1504+ file -> name , file -> fd );
14551505 flb_tail_file_close_handle_during_tail (file );
1506+ flb_plg_debug (ctx -> ins , "tail_append: DEBUG: after close_handle_during_tail, file->fd=%d" , file -> fd );
1507+ }
1508+ else {
1509+ flb_plg_debug (ctx -> ins , "tail_append: DEBUG: keep_file_handle is TRUE, NOT closing handle for %s" , file -> name );
14561510 }
14571511
14581512#ifdef FLB_HAVE_METRICS
0 commit comments