From: alex Date: Mon, 6 Sep 2021 19:36:12 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=a47218647d1ce34b0e3f937621282b00a7732932;p=seeder ... --- diff --git a/inc/log.h b/inc/log.h index d705a8c..7bed5e4 100644 --- a/inc/log.h +++ b/inc/log.h @@ -17,23 +17,23 @@ enum log_level { LOG_LEVEL_VERBOSE = 3 /* logging and debugging info */ }; +#include + #ifndef NDEBUG -#define LOG_WITH_THREAD_PREFIX(level,fd,...) log_message(level,fd,LOG_THREAD_PREFIX_MESSAGE,pthread_self());log_message(level,fd,__VA_ARGS__) -#define log_err(...) LOG_WITH_THREAD_PREFIX(LOG_LEVEL_ERRORS,stderr,__VA_ARGS__) -#define log_info(...) LOG_WITH_THREAD_PREFIX(LOG_LEVEL_VERBOSE,stdout,__VA_ARGS__) -#define log_msg(...) LOG_WITH_THREAD_PREFIX(LOG_LEVEL_DEFAULT,stdout,__VA_ARGS__) +#define LOG_MESSAGE_PREFIX "[thread=%ld] %s " #else -#define log_err(...) log_message(LOG_LEVEL_ERRORS,stderr,__VA_ARGS__) -#define log_info(...) log_message(LOG_LEVEL_VERBOSE,stdout,__VA_ARGS__) -#define log_msg(...) log_message(LOG_LEVEL_DEFAULT,stdout,__VA_ARGS__) +#define LOG_MESSAGE_PREFIX "%s " #endif +#define log_err(...) log_message_with_prefix(LOG_LEVEL_ERRORS,stderr,LOG_MESSAGE_PREFIX __VA_ARGS__) +#define log_info(...) log_message_with_prefix(LOG_LEVEL_VERBOSE,stdout,LOG_MESSAGE_PREFIX __VA_ARGS__) +#define log_msg(...) log_message_with_prefix(LOG_LEVEL_DEFAULT,stdout,LOG_MESSAGE_PREFIX __VA_ARGS__) + #define LOG_FLUSH_MESSAGE "flushing log queue...\n" #define LOG_THREAD_START_MESSAGE "logging thread start\n" -#define LOG_THREAD_PREFIX_MESSAGE "[thread=%ld] " - #define LOG_ENTRY_MAX_LENGTH 100 #define LOG_QUEUE_SIZE 100 +#define TIMESTAMP_BUF_LENGTH 20 struct log_entry { enum log_level level; @@ -55,6 +55,7 @@ int log_entries_init(); void log_entries_clean(); void log_flush(); void log_print(); +void log_message_with_prefix(enum log_level,FILE*,const char*,...); void log_message(enum log_level,FILE*,const char*,...); void *log_poll(void*); diff --git a/inc/opt.h b/inc/opt.h index b1f5407..4a43e3b 100644 --- a/inc/opt.h +++ b/inc/opt.h @@ -10,6 +10,7 @@ struct options { unsigned int worker_threads; unsigned long long piece_length; + int verbose_flag; }; extern struct options global_options; diff --git a/src/log.c b/src/log.c index 5c03915..4406e23 100644 --- a/src/log.c +++ b/src/log.c @@ -1,8 +1,6 @@ #include -int verbose_flag = LOG_LEVEL_DEFAULT; - -struct log_helper helper; +static struct log_helper helper; struct log_entry *log_dequeue() { struct log_entry *p; @@ -79,11 +77,31 @@ void log_print() { } } -void log_message(enum log_level level, FILE *out_stream, const char *format,...) { - if(level>verbose_flag) { return; } +void log_message_with_prefix(enum log_level level, FILE *out_stream, const char *format,...) { + time_t t; + + if(level>global_options.verbose_flag) { return; } + + t = time(NULL); + struct tm now = *localtime(&t); + char buf[TIMESTAMP_BUF_LENGTH]; + strftime(buf,TIMESTAMP_BUF_LENGTH,"%Y-%m-%d-%H:%M:%S",&now); va_list args; va_start(args,format); + +#ifndef NDEBUG + log_message(level,out_stream,format,pthread_self(),buf,args); +#else + log_message(level,out_stream,format,buf,args); +#endif + + va_end(args); +} + +void log_message(enum log_level level, FILE *out_stream, const char *format, va_list args,...) { + //va_list args; + //va_start(args,format); if(0==pthread_equal(pthread_self(),logging_thread)) { // not on logging_thread diff --git a/src/main.c b/src/main.c index 54c6c5d..359bb3f 100644 --- a/src/main.c +++ b/src/main.c @@ -4,8 +4,8 @@ static struct option long_options[] = { {"config-file", required_argument, 0, 'c'}, {"daemon", no_argument, 0, 'd'}, {"help", no_argument, 0, 'h'}, - {"quiet", no_argument, &verbose_flag, LOG_LEVEL_SILENT}, - {"verbose", no_argument, &verbose_flag, LOG_LEVEL_VERBOSE}, + {"quiet", no_argument, 0, 'q'}, + {"verbose", no_argument, 0, 'v'}, {"watch", required_argument, 0, 'w'}, {"worker-threads", required_argument, 0, 1}, {0,0,0,0} @@ -53,7 +53,7 @@ int main(int argc, char **argv) { if(setup()<0) { return EXIT_FAILURE; } - log_err("this is a test %d %s\n",10,"what?"); + log_err(LOG_MESSAGE_PREFIX "this is a test %d %s\n",10,"what?"); while(1) { } return EXIT_FAILURE; diff --git a/src/opt/loglevel.c b/src/opt/loglevel.c index 7c142f8..7296598 100644 --- a/src/opt/loglevel.c +++ b/src/opt/loglevel.c @@ -1,5 +1,5 @@ #include void opt_set_log_level(enum log_level level) { - verbose_flag = level; + global_options.verbose_flag = level; }