From 3d7da070dd7d1eac7ed7029888d3b56dd030491d Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Tue, 18 Sep 2018 23:42:24 +0300 Subject: [PATCH] safe logging --- src/device.c | 1 + src/http.c | 2 +- src/logging.h | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 8 +++-- src/stream.c | 1 + src/tools.h | 45 +++---------------------- 6 files changed, 105 insertions(+), 44 deletions(-) create mode 100644 src/logging.h diff --git a/src/device.c b/src/device.c index 4d3d646..b34b42d 100644 --- a/src/device.c +++ b/src/device.c @@ -10,6 +10,7 @@ #include #include "tools.h" +#include "logging.h" #include "device.h" diff --git a/src/http.c b/src/http.c index b6b3649..d21b297 100644 --- a/src/http.c +++ b/src/http.c @@ -14,6 +14,7 @@ #endif #include "tools.h" +#include "logging.h" #include "stream.h" #include "http.h" @@ -82,7 +83,6 @@ void http_server_loop(struct http_server_t *server) { } void http_server_loop_break(struct http_server_t *server) { - LOG_INFO("Stopping HTTP eventloop ..."); event_base_loopbreak(server->run->base); } diff --git a/src/logging.h b/src/logging.h new file mode 100644 index 0000000..122320a --- /dev/null +++ b/src/logging.h @@ -0,0 +1,92 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include "tools.h" + + +unsigned log_level; +pthread_mutex_t log_mutex; + + +#define LOG_LEVEL_INFO 0 +#define LOG_LEVEL_VERBOSE 1 +#define LOG_LEVEL_PERF 2 +#define LOG_LEVEL_DEBUG 3 + + +#define LOGGING_INIT assert(!pthread_mutex_init(&log_mutex, NULL)) +#define LOGGING_DESTROY assert(!pthread_mutex_destroy(&log_mutex)) + +#define LOGGING_LOCK assert(!pthread_mutex_lock(&log_mutex)) +#define LOGGING_UNLOCK assert(!pthread_mutex_unlock(&log_mutex)) + + +#define SEP_INFO(_x_ch) { \ + LOGGING_LOCK; \ + for (int _i = 0; _i < 80; ++_i) { \ + putchar(_x_ch); \ + } \ + putchar('\n'); \ + LOGGING_UNLOCK; \ + } + +#define SEP_DEBUG(_x_ch) { \ + if (log_level >= LOG_LEVEL_DEBUG) { \ + SEP_INFO(_x_ch); \ + } \ + } + +#define LOG_ERROR(_x_msg, ...) { \ + LOGGING_LOCK; \ + printf("-- ERROR [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \ + LOGGING_UNLOCK; \ + } + +#define LOG_PERROR(_x_msg, ...) { \ + char _buf[1024]; \ + strerror_r(errno, _buf, 1024); \ + LOGGING_LOCK; \ + printf("-- ERROR [%.03Lf tid=%ld] -- " _x_msg ": %s\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__, _buf); \ + LOGGING_UNLOCK; \ + } + +#define LOG_INFO(_x_msg, ...) { \ + LOGGING_LOCK; \ + printf("-- INFO [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \ + LOGGING_UNLOCK; \ + } + +#define LOG_INFO_NOLOCK(_x_msg, ...) { \ + printf("-- INFO [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \ + } + +#define LOG_VERBOSE(_x_msg, ...) { \ + if (log_level >= LOG_LEVEL_VERBOSE) { \ + LOGGING_LOCK; \ + printf("-- VERB [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \ + LOGGING_UNLOCK; \ + } \ + } + +#define LOG_PERF(_x_msg, ...) { \ + if (log_level >= LOG_LEVEL_PERF) { \ + LOGGING_LOCK; \ + printf("-- PERF [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \ + LOGGING_UNLOCK; \ + } \ + } + +#define LOG_DEBUG(_x_msg, ...) { \ + if (log_level >= LOG_LEVEL_DEBUG) { \ + LOGGING_LOCK; \ + printf("-- DEBUG [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); \ + LOGGING_UNLOCK; \ + } \ + } diff --git a/src/main.c b/src/main.c index c1034ac..52ebcaa 100644 --- a/src/main.c +++ b/src/main.c @@ -5,13 +5,13 @@ #include #include -#include #include #include #include #include #include "tools.h" +#include "logging.h" #include "device.h" #include "stream.h" #include "http.h" @@ -131,7 +131,7 @@ static void *_server_loop_thread(UNUSED void *_) { } static void _signal_handler(int signum) { - LOG_INFO("===== Stopping by %s =====", strsignal(signum)); + LOG_INFO_NOLOCK("===== Stopping by %s =====", (signum == SIGTERM ? "SIGTERM" : "SIGINT")); stream_loop_break(_ctx->stream); http_server_loop_break(_ctx->server); } @@ -161,6 +161,8 @@ int main(int argc, char *argv[]) { struct http_server_t *server; int exit_code = 0; + LOGGING_INIT; + dev = device_init(); stream = stream_init(dev); server = http_server_init(stream); @@ -187,5 +189,7 @@ int main(int argc, char *argv[]) { http_server_destroy(server); stream_destroy(stream); device_destroy(dev); + + LOGGING_DESTROY; return abs(exit_code); } diff --git a/src/stream.c b/src/stream.c index b07fa08..6948fff 100644 --- a/src/stream.c +++ b/src/stream.c @@ -15,6 +15,7 @@ #endif #include "tools.h" +#include "logging.h" #include "device.h" #include "jpeg.h" #include "stream.h" diff --git a/src/tools.h b/src/tools.h index d0b0ded..fc1b264 100644 --- a/src/tools.h +++ b/src/tools.h @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -13,42 +12,7 @@ #include #include - -unsigned log_level; - - -#define LOG_LEVEL_INFO 0 -#define LOG_LEVEL_VERBOSE 1 -#define LOG_LEVEL_PERF 2 -#define LOG_LEVEL_DEBUG 3 - -#define SEP_INFO(_x_ch) \ - { for (int _i = 0; _i < 80; ++_i) putchar(_x_ch); putchar('\n'); } - -#define SEP_DEBUG(_x_ch) \ - { if (log_level >= LOG_LEVEL_DEBUG) { SEP_INFO(_x_ch); } } - -#define LOG_ERROR(_x_msg, ...) \ - printf("-- ERROR [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__) - -#define LOG_PERROR(_x_msg, ...) \ - { char _buf[1024]; strerror_r(errno, _buf, 1024); \ - printf("-- ERROR [%.03Lf tid=%ld] -- " _x_msg ": %s\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__, _buf); } - -#define LOG_INFO(_x_msg, ...) \ - printf("-- INFO [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__) - -#define LOG_VERBOSE(_x_msg, ...) \ - { if (log_level >= LOG_LEVEL_VERBOSE) \ - printf("-- VERB [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); } - -#define LOG_PERF(_x_msg, ...) \ - { if (log_level >= LOG_LEVEL_PERF) \ - printf("-- PERF [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); } - -#define LOG_DEBUG(_x_msg, ...) \ - { if (log_level >= LOG_LEVEL_DEBUG) \ - printf("-- DEBUG [%.03Lf tid=%ld] -- " _x_msg "\n", now_ms_ld(), syscall(SYS_gettid), ##__VA_ARGS__); } +#include "logging.h" #define A_PTHREAD_CREATE(_tid, _func, _arg) assert(!pthread_create(_tid, NULL, _func, _arg)) @@ -56,14 +20,13 @@ unsigned log_level; #define A_PTHREAD_M_INIT(_mutex) assert(!pthread_mutex_init(_mutex, NULL)) #define A_PTHREAD_M_DESTROY(_mutex) assert(!pthread_mutex_destroy(_mutex)) -#define A_PTHREAD_M_LOCK(...) assert(!pthread_mutex_lock(__VA_ARGS__)) -#define A_PTHREAD_M_UNLOCK(...) assert(!pthread_mutex_unlock(__VA_ARGS__)) +#define A_PTHREAD_M_LOCK(_mutex) assert(!pthread_mutex_lock(_mutex)) +#define A_PTHREAD_M_UNLOCK(_mutex) assert(!pthread_mutex_unlock(_mutex)) #define A_PTHREAD_C_INIT(_cond) assert(!pthread_cond_init(_cond, NULL)) #define A_PTHREAD_C_DESTROY(_cond) assert(!pthread_cond_destroy(_cond)) #define A_PTHREAD_C_SIGNAL(...) assert(!pthread_cond_signal(__VA_ARGS__)) -#define A_PTHREAD_C_WAIT_TRUE(_var, _cond, _mutex) \ - { while(!_var) assert(!pthread_cond_wait(_cond, _mutex)); } +#define A_PTHREAD_C_WAIT_TRUE(_var, _cond, _mutex) { while(!_var) assert(!pthread_cond_wait(_cond, _mutex)); } #define A_CALLOC(_dest, _nmemb) assert((_dest = calloc(_nmemb, sizeof(*(_dest)))))