LOG_LEVEL_VERBOSE = 3 /* logging and debugging info */
};
+#include<opt.h>
+
#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;
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*);
struct options {
unsigned int worker_threads;
unsigned long long piece_length;
+ int verbose_flag;
};
extern struct options global_options;
#include<log.h>
-int verbose_flag = LOG_LEVEL_DEFAULT;
-
-struct log_helper helper;
+static struct log_helper helper;
struct log_entry *log_dequeue() {
struct log_entry *p;
}
}
-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
{"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}
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;
#include<opt.h>
void opt_set_log_level(enum log_level level) {
- verbose_flag = level;
+ global_options.verbose_flag = level;
}