From abb74a5a11e238ef6df86a622b58b2d06425711c Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 4 Sep 2021 14:53:08 -0700 Subject: [PATCH] ... --- .gitignore | 1 + Makefile.am | 4 ++++ configure.ac | 47 +++++++++++++++++++++++++++++++++++++++++++++++ inc/default.h | 1 + inc/log.h | 4 +++- inc/main.h | 2 ++ inc/setup.h | 1 + src/default.c | 2 ++ src/log.c | 21 +++++++++++++++------ src/main.c | 3 --- src/setup.c | 20 +++++++++++++++++++- 11 files changed, 95 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index eedf05a..1a7cd80 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ stamp-h1 *.tests # binaries +seederd diff --git a/Makefile.am b/Makefile.am index aff6ed3..563cb17 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,3 +1,7 @@ +AM_CPPFLAGS = \ + -Wall \ + -Werror + bin_PROGRAMS = seederd seederd_SOURCES = \ diff --git a/configure.ac b/configure.ac index d1145bc..a792a5f 100644 --- a/configure.ac +++ b/configure.ac @@ -9,6 +9,53 @@ AM_INIT_AUTOMAKE([foreign subdir-objects -Wall -Werror]) AC_CONFIG_SRCDIR([src/main.c]) AC_CONFIG_HEADERS([inc/config.h]) +AC_ARG_ENABLE([debug], + [AS_HELP_STRING([--enable-debug], + [enable debugging])], + [enable_debug=$enableval], + [enable_debug=no]) + +AC_ARG_ENABLE([tests], + [AS_HELP_STRING([--disable-tests], + [disable tests (enabled by default)])], + [enable_tests=$enableval], + [enable_tests=yes]) + +AC_ARG_ENABLE([memcheck], + [AS_HELP_STRING([--disable-memcheck], + [disable memcheck with valgrind (enabled by default)])], + [enable_memcheck=$enableval], + [enable_memcheck=yes]) + +AC_PATH_PROG([VALGRIND], [valgrind]) +AM_CONDITIONAL([HAVE_VALGRIND], [test -n "$VALGRIND"]) + +AC_MSG_CHECKING([if debugging]) +if test x$enable_debug != xno; then + AC_MSG_RESULT(yes) + CFLAGS="-ggdb -O0" +else + AC_MSG_RESULT(no) +fi + +AM_CONDITIONAL([ENABLE_DEBUG],[test x$enable_debug != xno]) + +dnl disable memcheck if valgrind not found +if test "x$enable_memcheck" != "xno"; then + if test -z "$VALGRIND"; then + enable_memcheck=no + fi +fi + +AC_MSG_CHECKING([if memcheck should be enabled]) +if test x$enable_memcheck != xno; then + AC_MSG_RESULT(yes) +else + AC_MSG_RESULT(no) +fi + +AM_CONDITIONAL([ENABLE_MEMCHECK],[test x$enable_memcheck = xyes]) + # Checks for programs. AC_PROG_CC diff --git a/inc/default.h b/inc/default.h index c043bf8..5d26697 100644 --- a/inc/default.h +++ b/inc/default.h @@ -1,6 +1,7 @@ #ifndef __DEFAULT_H_ #define __DEFAULT_H_ +#include #include int defaults(); diff --git a/inc/log.h b/inc/log.h index 8d3c19b..3b075d7 100644 --- a/inc/log.h +++ b/inc/log.h @@ -22,6 +22,8 @@ enum log_level { #define log_msg(...) log_message(LOG_LEVEL_DEFAULT,stdout,__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 @@ -45,7 +47,7 @@ void log_enqueue(struct log_entry*); int log_entries_init(); void log_entries_clean(); void log_flush(); -void log_print(struct log_entry*); +void log_print(); void log_message(enum log_level,FILE*,const char*,...); void *log_poll(void*); diff --git a/inc/main.h b/inc/main.h index 99e116d..ea1db5b 100644 --- a/inc/main.h +++ b/inc/main.h @@ -3,9 +3,11 @@ #include #include +#include #include #include +#include #include int main(int,char**); diff --git a/inc/setup.h b/inc/setup.h index c3e6a24..bcf246a 100644 --- a/inc/setup.h +++ b/inc/setup.h @@ -4,5 +4,6 @@ #include int setup(); +int setup_logging(); #endif diff --git a/src/default.c b/src/default.c index b891dc4..5a82487 100644 --- a/src/default.c +++ b/src/default.c @@ -1,6 +1,8 @@ #include int defaults() { + logging_thread = pthread_self(); + opt_set_log_level(LOG_LEVEL_DEFAULT); return 1; diff --git a/src/log.c b/src/log.c index 9a28062..5c03915 100644 --- a/src/log.c +++ b/src/log.c @@ -54,13 +54,14 @@ void log_entries_clean() { } void log_flush() { - log_err(LOG_FLUSH_MESSAGE); - pthread_mutex_lock(&logging_mutex); + if(helper.start!=NULL) { + fputs(LOG_FLUSH_MESSAGE,stderr); + } + while(helper.start!=NULL) { log_print(helper.start); - helper.start = helper.start->next; } helper.start = NULL; @@ -70,8 +71,12 @@ void log_flush() { pthread_mutex_unlock(&logging_mutex); } -void log_print(struct log_entry *p) { - fputs(p->out_stream,p->buf); +void log_print() { + struct log_entry *p = helper.start; + if(NULL!=p) { + fputs(p->buf,p->out_stream); + helper.start = p->next; + } } void log_message(enum log_level level, FILE *out_stream, const char *format,...) { @@ -100,7 +105,7 @@ void log_message(enum log_level level, FILE *out_stream, const char *format,...) } } - p = &(helper.p[offset*sizeof(struct log_entry)]); + p = helper.p + offset*sizeof(struct log_entry); p->level = level; p->out_stream = out_stream; @@ -116,7 +121,11 @@ void log_message(enum log_level level, FILE *out_stream, const char *format,...) } void *log_poll(void *p) { + log_info(LOG_THREAD_START_MESSAGE); while(1) { + pthread_mutex_lock(&logging_mutex); + log_print(); + pthread_mutex_unlock(&logging_mutex); } return NULL; diff --git a/src/main.c b/src/main.c index 97a854b..8cabfb6 100644 --- a/src/main.c +++ b/src/main.c @@ -38,10 +38,7 @@ int main(int argc, char **argv) { } } - log_msg("here?\n"); - if(setup()<0) { return EXIT_FAILURE; } - log_msg("here!\n"); return EXIT_FAILURE; } diff --git a/src/setup.c b/src/setup.c index d1d8f4c..44851a8 100644 --- a/src/setup.c +++ b/src/setup.c @@ -1,12 +1,30 @@ #include + +int setup() { + if(setup_logging()<0) { return -1; } + return 1; +} + pthread_t logging_thread; pthread_mutex_t logging_mutex = PTHREAD_MUTEX_INITIALIZER; -int setup() { +int setup_logging() { + if(0!=atexit(&log_entries_clean)) { + perror("atexit"); + return -1; + } + + if(0!=atexit(&log_flush)) { + perror("atexit"); + return -1; + } + if(log_entries_init()<0) { return -1; } if(pthread_create(&logging_thread,NULL,&log_poll,NULL)!=0) { perror("pthread_create"); return -1; } + + return 1; } -- 2.30.2