]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Sat, 4 Sep 2021 21:53:08 +0000 (14:53 -0700)
committeralex <[email protected]>
Sat, 4 Sep 2021 21:53:08 +0000 (14:53 -0700)
.gitignore
Makefile.am
configure.ac
inc/default.h
inc/log.h
inc/main.h
inc/setup.h
src/default.c
src/log.c
src/main.c
src/setup.c

index eedf05a8d0c95d8dbae44c327c4415d1ac58b97f..1a7cd80df5def7f1dd372bf1527fa7f0ad1a0aa5 100644 (file)
@@ -25,3 +25,4 @@ stamp-h1
 *.tests
 
 # binaries
+seederd
index aff6ed3b795ff1ee9a15f7317dfee688d6393a38..563cb17cf4883370f6a5a3e124b80aec982cec7f 100644 (file)
@@ -1,3 +1,7 @@
+AM_CPPFLAGS = \
+       -Wall \
+       -Werror
+
 bin_PROGRAMS = seederd
 
 seederd_SOURCES = \
index d1145bc3ede34ae9548482c775460cb153050ef4..a792a5ffe5369ea61761ec2f61152fcbafbc1dcc 100644 (file)
@@ -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
 
index c043bf8b1d720fcfb3828eb505e11819e0ab1855..5d2669745082ec14a649fa0e7ef3f0a0767da670 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef __DEFAULT_H_
 #define __DEFAULT_H_
 
+#include<log.h>
 #include<opt.h>
 
 int defaults();
index 8d3c19b37c7ea1ff29367a4392e5f46285f6377d..3b075d7d0b7f00066445f59e5e3e825fdc55d80a 100644 (file)
--- 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*);
 
index 99e116d0d13a0877a460dce4f1a391b5d3cc402c..ea1db5b4d16f4c4598a2bc8394d94d9c12ccf3c3 100644 (file)
@@ -3,9 +3,11 @@
 
 #include<getopt.h>
 #include<stdlib.h>
+#include<unistd.h>
 
 #include<default.h>
 #include<log.h>
+#include<setup.h>
 #include<usage.h>
 
 int main(int,char**);
index c3e6a24879b2380f4f4e50912f6e7d949ee83b08..bcf246ae0cba7e49214892ed6e2f592f9cbf04ad 100644 (file)
@@ -4,5 +4,6 @@
 #include<log.h>
 
 int setup();
+int setup_logging();
 
 #endif
index b891dc47b3e8ec166b5172a7be34f223cf6dac02..5a8248724c07879e9d94e350f840b70136897143 100644 (file)
@@ -1,6 +1,8 @@
 #include<default.h>
 
 int defaults() {
+       logging_thread = pthread_self();
+
        opt_set_log_level(LOG_LEVEL_DEFAULT);
        
        return 1;
index 9a280626c8ea9be537118403cc22ae9b05fe702f..5c03915a896b4794cd6d2d3b14dacabb5d66f941 100644 (file)
--- 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;
index 97a854bf14aabed37c9e699134b0457fb2880ce0..8cabfb64f3efa0ca80a2fcce462a74633305d308 100644 (file)
@@ -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;
 }
index d1d8f4ce69dec6a3729df1e34013fdf1e9b126d3..44851a8daf1aaa5df92cfc3f2e70e291a0721885 100644 (file)
@@ -1,12 +1,30 @@
 #include<setup.h>
 
+
+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;
 }