Skip to content

Commit

Permalink
common: support syslog
Browse files Browse the repository at this point in the history
  • Loading branch information
liudongmiao committed Apr 11, 2024
1 parent e26dfde commit 47d4681
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 22 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror")

include(CheckSymbolExists)
check_symbol_exists(syslog "syslog.h" HAVE_SYSLOG)
if (HAVE_SYSLOG)
add_definitions(-DHAVE_SYSLOG)
endif()

include(CheckFunctionExists)

find_package(OpenSSL 1.1.1 REQUIRED)
Expand Down
32 changes: 28 additions & 4 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ void log_callback(int severity, const char *msg) {
}

#ifndef NDEBUG
static enum log_level log_level = DEBUG;
static volatile int log_level = DEBUG;
#else
static enum log_level log_level = INFO;
static volatile int log_level = INFO;
#endif

static void set_log_level(enum log_level level) {
static volatile int log_to_syslog = -1;

static void set_log_level(int level) {
log_level = level;
}

Expand All @@ -141,10 +143,32 @@ void init_log_level(const char *loglevel) {
}
}

enum log_level get_log_level() {
int get_log_level() {
return log_level;
}

int use_syslog(void) {
if (log_to_syslog == -1) {
#ifdef HAVE_SYSLOG
log_to_syslog = find_option(getenv("SS_PLUGIN_OPTIONS"), "syslog", "1") != NULL;
if (log_to_syslog) {
openlog(LOGGER_NAME, LOG_PID | LOG_CONS, LOG_DAEMON);
}
#else
log_to_syslog = 0;
#endif
}
return log_to_syslog;
}

void close_syslog(void) {
#ifdef HAVE_SYSLOG
if (log_to_syslog) {
closelog();
}
#endif
}

const char *find_option(const char *options, const char *key, const char *no_value) {
size_t len;
const char *pos, *value;
Expand Down
54 changes: 36 additions & 18 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
#ifdef WSS_PROXY_CLIENT
#include <openssl/lhash.h>
#endif
#ifdef HAVE_SYSLOG
#include <syslog.h>
#else
#define syslog(x, y, ...) do {} while (0)
#endif
#include "ws-header.h"

#ifndef WSS_PAYLOAD_SIZE
Expand Down Expand Up @@ -44,12 +49,17 @@

#define TIME_FORMAT "%Y-%m-%d %H:%M:%S"

enum log_level {
DEBUG,
INFO,
WARN,
ERROR,
};
#ifdef HAVE_SYSLOG
#define DEBUG LOG_DEBUG
#define INFO LOG_INFO
#define WARN LOG_WARNING
#define ERROR LOG_ERR
#else
#define DEBUG 7
#define INFO 6
#define WARN 4
#define ERROR 3
#endif

#define MAX_UDP_FRAME_SIZE 65535
#define UDP_FRAME_LENGTH_SIZE 2
Expand Down Expand Up @@ -109,18 +119,26 @@ void log_callback(int severity, const char *msg);

void init_log_level(const char *loglevel);

enum log_level get_log_level(void);

#define LOG(format, stream, level, ...) \
do { \
if (get_log_level() <= level) { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(stream, " %s " #level " " LOGGER_NAME " " format "\n", timestr, \
## __VA_ARGS__); \
fflush(stream); \
} \
int get_log_level(void);

int use_syslog(void);

void close_syslog(void);

#define LOG(format, stream, level, ...) \
do { \
if (get_log_level() >= level) { \
if (use_syslog()) { \
syslog(level, format, ## __VA_ARGS__); \
} else { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(stream, " %s " #level " " LOGGER_NAME " " format "\n", \
timestr, ## __VA_ARGS__); \
fflush(stream); \
} \
} \
} while (0)

#define LOGD(format, ...) LOG(format, stdout, DEBUG, ## __VA_ARGS__)
Expand Down
1 change: 1 addition & 0 deletions wss-proxy-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -655,5 +655,6 @@ int main() {
if (wss_context.server.path) {
free((char *) wss_context.server.path);
}
close_syslog();
return code;
}
1 change: 1 addition & 0 deletions wss-proxy-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,6 @@ int main() {
if (cfg) {
event_config_free(cfg);
}
close_syslog();
return code;
}

0 comments on commit 47d4681

Please sign in to comment.