Skip to content

Commit 0753fc4

Browse files
committed
Refactor fatal logging infrastructure
- Centralize fatal logging by introducing fatal_log_printf() and updating lprintf() macro - Ensure all fatal log paths call exit_failure() correctly - Improve thread-safety and readability of logging calls - Standardize thread ID formatting using (unsigned long) cast and %lx specifier - Clean up log_printf() by removing redundant fatal handling - Fix various pre-commit failures (clang-tidy, clang-format)
1 parent 4a70e51 commit 0753fc4

7 files changed

Lines changed: 197 additions & 154 deletions

File tree

src/cache.c

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ static char *CacheSystem_calc_dir(const char *url)
119119

120120
void CacheSystem_init(const char *path, int url_supplied)
121121
{
122-
lprintf(cache_lock_debug, "thread %x: initialise cf_lock;\n",
123-
pthread_self());
122+
lprintf(cache_lock_debug, "thread %lx: initialise cf_lock;\n",
123+
(unsigned long)pthread_self());
124124
PTHREAD_MUTEX_INIT(&cf_lock, NULL);
125125

126126
if (url_supplied) {
@@ -386,8 +386,8 @@ static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset)
386386
return -EINVAL;
387387
}
388388

389-
lprintf(cache_lock_debug, "thread %x: locking seek_lock;\n",
390-
pthread_self());
389+
lprintf(cache_lock_debug, "thread %lx: locking seek_lock;\n",
390+
(unsigned long)pthread_self());
391391
PTHREAD_MUTEX_LOCK(&cf->seek_lock);
392392

393393
long byte_read = 0;
@@ -434,8 +434,8 @@ static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset)
434434

435435
end:
436436

437-
lprintf(cache_lock_debug, "thread %x: unlocking seek_lock;\n",
438-
pthread_self());
437+
lprintf(cache_lock_debug, "thread %lx: unlocking seek_lock;\n",
438+
(unsigned long)pthread_self());
439439
PTHREAD_MUTEX_UNLOCK(&cf->seek_lock);
440440
return byte_read;
441441
}
@@ -459,8 +459,8 @@ static long Data_write(Cache *cf, const uint8_t *buf, off_t len, off_t offset)
459459
return 0;
460460
}
461461

462-
lprintf(cache_lock_debug, "thread %x: locking seek_lock;\n",
463-
pthread_self());
462+
lprintf(cache_lock_debug, "thread %lx: locking seek_lock;\n",
463+
(unsigned long)pthread_self());
464464
PTHREAD_MUTEX_LOCK(&cf->seek_lock);
465465

466466
long byte_written = 0;
@@ -489,8 +489,8 @@ static long Data_write(Cache *cf, const uint8_t *buf, off_t len, off_t offset)
489489
}
490490

491491
end:
492-
lprintf(cache_lock_debug, "thread %x: unlocking seek_lock;\n",
493-
pthread_self());
492+
lprintf(cache_lock_debug, "thread %lx: unlocking seek_lock;\n",
493+
(unsigned long)pthread_self());
494494
PTHREAD_MUTEX_UNLOCK(&cf->seek_lock);
495495
return byte_written;
496496
}
@@ -760,13 +760,14 @@ Cache *Cache_open(const char *fn)
760760
return NULL;
761761
}
762762

763-
lprintf(cache_lock_debug, "thread %x: locking cf_lock;\n", pthread_self());
763+
lprintf(cache_lock_debug, "thread %lx: locking cf_lock;\n",
764+
(unsigned long)pthread_self());
764765
PTHREAD_MUTEX_LOCK(&cf_lock);
765766

766767
if (link->cache_ptr) {
767768
link->cache_ptr->cache_opened++;
768-
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
769-
pthread_self());
769+
lprintf(cache_lock_debug, "thread %lx: unlocking cf_lock;\n",
770+
(unsigned long)pthread_self());
770771
PTHREAD_MUTEX_UNLOCK(&cf_lock);
771772
return link->cache_ptr;
772773
}
@@ -777,16 +778,16 @@ Cache *Cache_open(const char *fn)
777778
if (CONFIG.mode == NORMAL || CONFIG.mode == SINGLE) {
778779
if (Cache_exist(fn)) {
779780

780-
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
781-
pthread_self());
781+
lprintf(cache_lock_debug, "thread %lx: unlocking cf_lock;\n",
782+
(unsigned long)pthread_self());
782783
PTHREAD_MUTEX_UNLOCK(&cf_lock);
783784
return NULL;
784785
}
785786
} else if (CONFIG.mode == SONIC) {
786787
if (Cache_exist(link->sonic.id)) {
787788

788-
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
789-
pthread_self());
789+
lprintf(cache_lock_debug, "thread %lx: unlocking cf_lock;\n",
790+
(unsigned long)pthread_self());
790791
PTHREAD_MUTEX_UNLOCK(&cf_lock);
791792
return NULL;
792793
}
@@ -823,8 +824,8 @@ Cache *Cache_open(const char *fn)
823824
lprintf(error, "cannot open metadata file %s.\n", fn);
824825
Cache_free(cf);
825826

826-
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
827-
pthread_self());
827+
lprintf(cache_lock_debug, "thread %lx: unlocking cf_lock;\n",
828+
(unsigned long)pthread_self());
828829
PTHREAD_MUTEX_UNLOCK(&cf_lock);
829830
return NULL;
830831
}
@@ -836,8 +837,8 @@ Cache *Cache_open(const char *fn)
836837
lprintf(error, "metadata error: %s.\n", fn);
837838
Cache_free(cf);
838839

839-
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
840-
pthread_self());
840+
lprintf(cache_lock_debug, "thread %lx: unlocking cf_lock;\n",
841+
(unsigned long)pthread_self());
841842
PTHREAD_MUTEX_UNLOCK(&cf_lock);
842843
return NULL;
843844
}
@@ -853,8 +854,8 @@ cf->content_length: %ld, Data_size(fn): %ld.\n",
853854
fn, cf->content_length, Data_size(fn));
854855
Cache_free(cf);
855856

856-
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
857-
pthread_self());
857+
lprintf(cache_lock_debug, "thread %lx: unlocking cf_lock;\n",
858+
(unsigned long)pthread_self());
858859
PTHREAD_MUTEX_UNLOCK(&cf_lock);
859860
return NULL;
860861
}
@@ -866,8 +867,8 @@ cf->content_length: %ld, Data_size(fn): %ld.\n",
866867
lprintf(warning, "outdated cache file: %s.\n", fn);
867868
Cache_free(cf);
868869

869-
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
870-
pthread_self());
870+
lprintf(cache_lock_debug, "thread %lx: unlocking cf_lock;\n",
871+
(unsigned long)pthread_self());
871872
PTHREAD_MUTEX_UNLOCK(&cf_lock);
872873
return NULL;
873874
}
@@ -876,8 +877,8 @@ cf->content_length: %ld, Data_size(fn): %ld.\n",
876877
lprintf(error, "cannot open data file %s.\n", fn);
877878
Cache_free(cf);
878879

879-
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
880-
pthread_self());
880+
lprintf(cache_lock_debug, "thread %lx: unlocking cf_lock;\n",
881+
(unsigned long)pthread_self());
881882
PTHREAD_MUTEX_UNLOCK(&cf_lock);
882883
return NULL;
883884
}
@@ -887,24 +888,24 @@ cf->content_length: %ld, Data_size(fn): %ld.\n",
887888
*/
888889
cf->link->cache_ptr = cf;
889890

890-
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
891-
pthread_self());
891+
lprintf(cache_lock_debug, "thread %lx: unlocking cf_lock;\n",
892+
(unsigned long)pthread_self());
892893
PTHREAD_MUTEX_UNLOCK(&cf_lock);
893894
return cf;
894895
}
895896

896897
void Cache_close(Cache *cf)
897898
{
898-
lprintf(cache_lock_debug, "thread %x: locking cf_lock: %s\n",
899-
pthread_self(), cf->path);
899+
lprintf(cache_lock_debug, "thread %lx: locking cf_lock: %s\n",
900+
(unsigned long)pthread_self(), cf->path);
900901
PTHREAD_MUTEX_LOCK(&cf_lock);
901902

902903
cf->cache_opened--;
903904

904905
if (cf->cache_opened > 0) {
905906
lprintf(cache_lock_debug,
906-
"thread %x: unlocking cf_lock: %s, cache_opened: %d\n",
907-
pthread_self(), cf->path, cf->cache_opened);
907+
"thread %lx: unlocking cf_lock: %s, cache_opened: %d\n",
908+
(unsigned long)pthread_self(), cf->path, cf->cache_opened);
908909
PTHREAD_MUTEX_UNLOCK(&cf_lock);
909910
return;
910911
}
@@ -915,8 +916,8 @@ void Cache_close(Cache *cf)
915916
* running. This will cause a use-after-free error.
916917
*/
917918
lprintf(cache_lock_debug,
918-
"thread %x: waiting for background download to finish for %s\n",
919-
pthread_self(), cf->path);
919+
"thread %lx: waiting for background download to finish for %s\n",
920+
(unsigned long)pthread_self(), cf->path);
920921
SEM_WAIT(&cf->bgt_sem);
921922

922923
if (Meta_write(cf)) {
@@ -934,8 +935,8 @@ void Cache_close(Cache *cf)
934935
cf->link->cache_ptr = NULL;
935936

936937
lprintf(cache_lock_debug,
937-
"thread %x: unlocking cf_lock, cache closed: %s\n", pthread_self(),
938-
cf->path);
938+
"thread %lx: unlocking cf_lock, cache closed: %s\n",
939+
(unsigned long)pthread_self(), cf->path);
939940
Cache_free(cf);
940941
PTHREAD_MUTEX_UNLOCK(&cf_lock);
941942
}
@@ -973,17 +974,19 @@ static void *Cache_bgdl(void *arg)
973974
{
974975
Cache *cf = (Cache *)arg;
975976

976-
lprintf(cache_lock_debug, "thread %x: locking w_lock;\n", pthread_self());
977+
lprintf(cache_lock_debug, "thread %lx: locking w_lock;\n",
978+
(unsigned long)pthread_self());
977979
PTHREAD_MUTEX_LOCK(&cf->w_lock);
978980

979981
uint8_t *recv_buf = CALLOC(cf->blksz, sizeof(uint8_t));
980-
lprintf(debug, "thread %x spawned.\n ", pthread_self());
982+
lprintf(debug, "thread %lx spawned.\n ", (unsigned long)pthread_self());
981983
long recv = Link_download(cf->link, (char *)recv_buf, cf->blksz,
982984
cf->next_dl_offset);
983985
if (recv < 0) {
984-
lprintf(error, "thread %x received %ld bytes, \
985-
which doesn't make sense\n",
986-
pthread_self(), recv);
986+
lprintf(error,
987+
"thread %lx received %ld bytes, "
988+
"which doesn't make sense\n",
989+
(unsigned long)pthread_self(), recv);
987990
}
988991

989992
if ((recv == cf->blksz)
@@ -993,14 +996,16 @@ which doesn't make sense\n",
993996
Seg_set(cf, cf->next_dl_offset, 1);
994997
}
995998
} else {
996-
lprintf(error, "received %ld rather than %ld, possible network \
997-
error.\n",
999+
lprintf(error,
1000+
"received %ld rather than %d, possible network "
1001+
"error.\n",
9981002
recv, cf->blksz);
9991003
}
10001004

10011005
FREE(recv_buf);
10021006

1003-
lprintf(cache_lock_debug, "thread %x: unlocking w_lock;\n", pthread_self());
1007+
lprintf(cache_lock_debug, "thread %lx: unlocking w_lock;\n",
1008+
(unsigned long)pthread_self());
10041009
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
10051010
SEM_POST(&cf->bgt_sem);
10061011

@@ -1052,8 +1057,8 @@ static long Cache_read_segment(Cache *cf, char *const output_buf,
10521057
* Wait for any other download thread to finish
10531058
*/
10541059

1055-
lprintf(cache_lock_debug, "thread %ld: locking w_lock;\n",
1056-
pthread_self());
1060+
lprintf(cache_lock_debug, "thread %lx: locking w_lock;\n",
1061+
(unsigned long)pthread_self());
10571062
PTHREAD_MUTEX_LOCK(&cf->w_lock);
10581063

10591064
if (Seg_exist(cf, dl_offset)) {
@@ -1063,8 +1068,8 @@ static long Cache_read_segment(Cache *cf, char *const output_buf,
10631068
*/
10641069
send = Data_read(cf, (uint8_t *)output_buf, len, offset_start);
10651070

1066-
lprintf(cache_lock_debug, "thread %x: unlocking w_lock;\n",
1067-
pthread_self());
1071+
lprintf(cache_lock_debug, "thread %lx: unlocking w_lock;\n",
1072+
(unsigned long)pthread_self());
10681073
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
10691074

10701075
goto bgdl;
@@ -1076,12 +1081,13 @@ static long Cache_read_segment(Cache *cf, char *const output_buf,
10761081
*/
10771082

10781083
uint8_t *recv_buf = CALLOC(cf->blksz, sizeof(uint8_t));
1079-
lprintf(debug, "thread %x: spawned.\n ", pthread_self());
1084+
lprintf(debug, "thread %lx: spawned.\n ", (unsigned long)pthread_self());
10801085
long recv = Link_download(cf->link, (char *)recv_buf, cf->blksz, dl_offset);
10811086
if (recv < 0) {
1082-
lprintf(error, "thread %x received %ld bytes, \
1083-
which doesn't make sense\n",
1084-
pthread_self(), recv);
1087+
lprintf(error,
1088+
"thread %lx received %ld bytes, "
1089+
"which doesn't make sense\n",
1090+
(unsigned long)pthread_self(), recv);
10851091
}
10861092
/*
10871093
* check if we have received enough data, write it to the disk
@@ -1095,8 +1101,9 @@ which doesn't make sense\n",
10951101
Seg_set(cf, dl_offset, 1);
10961102
}
10971103
} else {
1098-
lprintf(error, "received %ld rather than %ld, possible network \
1099-
error.\n",
1104+
lprintf(error,
1105+
"received %ld rather than %d, possible network "
1106+
"error.\n",
11001107
recv, cf->blksz);
11011108
}
11021109
send = len;
@@ -1110,7 +1117,8 @@ error.\n",
11101117
}
11111118
FREE(recv_buf);
11121119

1113-
lprintf(cache_lock_debug, "thread %x: unlocking w_lock;\n", pthread_self());
1120+
lprintf(cache_lock_debug, "thread %lx: unlocking w_lock;\n",
1121+
(unsigned long)pthread_self());
11141122
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
11151123

11161124
/*
@@ -1126,8 +1134,8 @@ bgdl: {
11261134
int ret = sem_trywait(&cf->bgt_sem);
11271135
if (!ret) {
11281136
lprintf(cache_lock_debug,
1129-
"parent thread %x: sem_trywait successful\n",
1130-
pthread_self());
1137+
"parent thread %lx: sem_trywait successful\n",
1138+
(unsigned long)pthread_self());
11311139
cf->next_dl_offset = next_dl_offset;
11321140
Cache_bgdl_launcher(cf);
11331141
} else if (errno != EAGAIN) {

0 commit comments

Comments
 (0)