-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.c
39 lines (34 loc) · 1.17 KB
/
logger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "logger.h"
// extern variables used here
pthread_t* fetcher_threads;
struct arguments cli_arguments;
// VERBOSE
bool _verbose = false;
void _verbose_print(const char* function_name, const char* format, ...) {
va_list args;
va_start(args, format);
if(_verbose) {
unsigned int curr_thread_number = _get_current_thread_number();
if(curr_thread_number == MAIN_THREAD_NUMBER) {
flockfile(stdout);
fprintf(stderr, "%s[i]%s main thread %s: ", BLUE, RESET, function_name);
vprintf(format, args);
funlockfile(stdout);
} else {
flockfile(stdout);
fprintf(stderr, "%s[i]%s thread #%u %s: ", BLUE, RESET, curr_thread_number, function_name);
vprintf(format, args);
funlockfile(stdout);
}
}
va_end(args);
}
unsigned int _get_current_thread_number() {
pthread_t curr = pthread_self();
for(size_t i = 0; i < cli_arguments.threads; i++) {
if(pthread_equal(curr, fetcher_threads[i]) != 0) {
return 1 + i; // avoid having the first thread being confused with the main thread
}
}
return MAIN_THREAD_NUMBER;
}