Skip to content

Commit 47d4681

Browse files
committed
common: support syslog
1 parent e26dfde commit 47d4681

File tree

5 files changed

+72
-22
lines changed

5 files changed

+72
-22
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ endif()
2323

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

26+
include(CheckSymbolExists)
27+
check_symbol_exists(syslog "syslog.h" HAVE_SYSLOG)
28+
if (HAVE_SYSLOG)
29+
add_definitions(-DHAVE_SYSLOG)
30+
endif()
31+
2632
include(CheckFunctionExists)
2733

2834
find_package(OpenSSL 1.1.1 REQUIRED)

common.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,14 @@ void log_callback(int severity, const char *msg) {
120120
}
121121

122122
#ifndef NDEBUG
123-
static enum log_level log_level = DEBUG;
123+
static volatile int log_level = DEBUG;
124124
#else
125-
static enum log_level log_level = INFO;
125+
static volatile int log_level = INFO;
126126
#endif
127127

128-
static void set_log_level(enum log_level level) {
128+
static volatile int log_to_syslog = -1;
129+
130+
static void set_log_level(int level) {
129131
log_level = level;
130132
}
131133

@@ -141,10 +143,32 @@ void init_log_level(const char *loglevel) {
141143
}
142144
}
143145

144-
enum log_level get_log_level() {
146+
int get_log_level() {
145147
return log_level;
146148
}
147149

150+
int use_syslog(void) {
151+
if (log_to_syslog == -1) {
152+
#ifdef HAVE_SYSLOG
153+
log_to_syslog = find_option(getenv("SS_PLUGIN_OPTIONS"), "syslog", "1") != NULL;
154+
if (log_to_syslog) {
155+
openlog(LOGGER_NAME, LOG_PID | LOG_CONS, LOG_DAEMON);
156+
}
157+
#else
158+
log_to_syslog = 0;
159+
#endif
160+
}
161+
return log_to_syslog;
162+
}
163+
164+
void close_syslog(void) {
165+
#ifdef HAVE_SYSLOG
166+
if (log_to_syslog) {
167+
closelog();
168+
}
169+
#endif
170+
}
171+
148172
const char *find_option(const char *options, const char *key, const char *no_value) {
149173
size_t len;
150174
const char *pos, *value;

common.h

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
#ifdef WSS_PROXY_CLIENT
1313
#include <openssl/lhash.h>
1414
#endif
15+
#ifdef HAVE_SYSLOG
16+
#include <syslog.h>
17+
#else
18+
#define syslog(x, y, ...) do {} while (0)
19+
#endif
1520
#include "ws-header.h"
1621

1722
#ifndef WSS_PAYLOAD_SIZE
@@ -44,12 +49,17 @@
4449

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

47-
enum log_level {
48-
DEBUG,
49-
INFO,
50-
WARN,
51-
ERROR,
52-
};
52+
#ifdef HAVE_SYSLOG
53+
#define DEBUG LOG_DEBUG
54+
#define INFO LOG_INFO
55+
#define WARN LOG_WARNING
56+
#define ERROR LOG_ERR
57+
#else
58+
#define DEBUG 7
59+
#define INFO 6
60+
#define WARN 4
61+
#define ERROR 3
62+
#endif
5363

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

110120
void init_log_level(const char *loglevel);
111121

112-
enum log_level get_log_level(void);
113-
114-
#define LOG(format, stream, level, ...) \
115-
do { \
116-
if (get_log_level() <= level) { \
117-
time_t now = time(NULL); \
118-
char timestr[20]; \
119-
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
120-
fprintf(stream, " %s " #level " " LOGGER_NAME " " format "\n", timestr, \
121-
## __VA_ARGS__); \
122-
fflush(stream); \
123-
} \
122+
int get_log_level(void);
123+
124+
int use_syslog(void);
125+
126+
void close_syslog(void);
127+
128+
#define LOG(format, stream, level, ...) \
129+
do { \
130+
if (get_log_level() >= level) { \
131+
if (use_syslog()) { \
132+
syslog(level, format, ## __VA_ARGS__); \
133+
} else { \
134+
time_t now = time(NULL); \
135+
char timestr[20]; \
136+
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
137+
fprintf(stream, " %s " #level " " LOGGER_NAME " " format "\n", \
138+
timestr, ## __VA_ARGS__); \
139+
fflush(stream); \
140+
} \
141+
} \
124142
} while (0)
125143

126144
#define LOGD(format, ...) LOG(format, stdout, DEBUG, ## __VA_ARGS__)

wss-proxy-client.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,5 +655,6 @@ int main() {
655655
if (wss_context.server.path) {
656656
free((char *) wss_context.server.path);
657657
}
658+
close_syslog();
658659
return code;
659660
}

wss-proxy-server.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,5 +328,6 @@ int main() {
328328
if (cfg) {
329329
event_config_free(cfg);
330330
}
331+
close_syslog();
331332
return code;
332333
}

0 commit comments

Comments
 (0)