mirror of
https://github.com/pikvm/ustreamer.git
synced 2026-02-18 02:55:46 +00:00
safe logging
This commit is contained in:
parent
2d1a8e43f9
commit
3d7da070dd
@ -10,6 +10,7 @@
|
||||
#include <linux/videodev2.h>
|
||||
|
||||
#include "tools.h"
|
||||
#include "logging.h"
|
||||
#include "device.h"
|
||||
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
92
src/logging.h
Normal file
92
src/logging.h
Normal file
@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
|
||||
#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; \
|
||||
} \
|
||||
}
|
||||
@ -5,13 +5,13 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <pthread.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
#endif
|
||||
|
||||
#include "tools.h"
|
||||
#include "logging.h"
|
||||
#include "device.h"
|
||||
#include "jpeg.h"
|
||||
#include "stream.h"
|
||||
|
||||
45
src/tools.h
45
src/tools.h
@ -2,7 +2,6 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
@ -13,42 +12,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/syscall.h>
|
||||
|
||||
|
||||
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)))))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user