diff options
| author | Loic Guegan <manzerbredes@mailbox.org> | 2023-07-17 20:29:37 +0200 |
|---|---|---|
| committer | Loic Guegan <manzerbredes@mailbox.org> | 2023-07-17 20:29:37 +0200 |
| commit | 29ff963194bc5c93be12f69d253edc9052671efa (patch) | |
| tree | 92deb592add5ce61a7b9fadf0a1adce52fdaf537 | |
| parent | a32c39d8a5c991d73aeb852aebf4e290f07e9168 (diff) | |
Minor changes
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | src/logger.c | 32 |
2 files changed, 25 insertions, 9 deletions
@@ -17,7 +17,7 @@ subscriber: src/subscriber.c src/utils.c config.mk $(CC) -lzmq $(filter-out config.mk,$^) -o $@ $(MACROS) logger: src/logger.c src/utils.c config.mk - $(CC) -lzmq $(filter-out config.mk,$^) -o $@ $(MACROS) + $(CC) -lzmq -lpthread $(filter-out config.mk,$^) -o $@ $(MACROS) publish: publisher logger for client in $$(basename -a /sys/kernel/ina260/*); \ diff --git a/src/logger.c b/src/logger.c index 9f9d78d..2aa565b 100644 --- a/src/logger.c +++ b/src/logger.c @@ -5,6 +5,7 @@ #include <unistd.h> #include <sys/stat.h> #include <signal.h> +#include <pthread.h> #include "utils.h" #ifndef LOGGERS_DELAY @@ -28,7 +29,7 @@ void sighandler(int signo){ } } -void publish(int queue_id, void* publisher); +void *publish(void *publisher); typedef struct queue { int size; @@ -87,6 +88,7 @@ int main (int argc, char *argv []) //----- Start logging + pthread_t zmq_thread; printf("Logger started [client=%s,interval=%ds]\n",__client,__loginterval); FILE *regptr,*logptr; @@ -102,6 +104,7 @@ int main (int argc, char *argv []) for(int i=0;i<MAX_QUEUES;i++){ queues[queue_id].issending=0; } + pthread_create(&zmq_thread, NULL, publish, publisher); while(!__stop){ interval=INTERVAL(__loginterval); @@ -137,20 +140,33 @@ int main (int argc, char *argv []) //printf("Tick\n"); fflush(stdout); } queues[queue_id].issending=1; - publish(queue_id,publisher); } fclose(regptr); - + pthread_join(zmq_thread, NULL); zmq_close (publisher); zmq_ctx_destroy (context); return 0; } -void publish(int queue_id, void* publisher){ - printf("Publishing..."); - zmq_send(publisher,queues[queue_id].msg,queues[queue_id].size,0); - queues[queue_id].issending=0; - printf("done\n"); +void *publish(void *publisher){ + int queue_id=0; + while(!__stop){ + if(queues[queue_id].issending){ + printf("Publishing..."); + zmq_send(publisher,queues[queue_id].msg,queues[queue_id].size,0); + queues[queue_id].issending=0; + printf("done\n"); + } else { +#if LOGGERS_DELAY > 0 + usleep(LOGGERS_DELAY*1000); +#endif + continue; + } + queue_id++; + if(queue_id>=MAX_QUEUES) + queue_id=0; + } + pthread_exit(EXIT_SUCCESS); }
\ No newline at end of file |
