feat(log): use syslog logging
This commit is contained in:
@@ -31,4 +31,9 @@
|
|||||||
#define CWS_LOG_WARNING(msg, ...) fprintf(stdout, _WARNING " " msg "\n", ##__VA_ARGS__)
|
#define CWS_LOG_WARNING(msg, ...) fprintf(stdout, _WARNING " " msg "\n", ##__VA_ARGS__)
|
||||||
#define CWS_LOG_INFO(msg, ...) fprintf(stdout, _INFO " " msg "\n", ##__VA_ARGS__)
|
#define CWS_LOG_INFO(msg, ...) fprintf(stdout, _INFO " " msg "\n", ##__VA_ARGS__)
|
||||||
|
|
||||||
|
void cws_log_init(void);
|
||||||
|
void cws_log_info(const char *fmt, ...);
|
||||||
|
void cws_log_error(const char *fmt, ...);
|
||||||
|
void cws_log_shutdown(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
10
src/main.c
10
src/main.c
@@ -14,13 +14,18 @@ void cws_signal_handler(int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
cws_log_info("Running cws...");
|
||||||
|
|
||||||
if (signal(SIGINT, cws_signal_handler) == SIG_ERR) {
|
if (signal(SIGINT, cws_signal_handler) == SIG_ERR) {
|
||||||
|
cws_log_error("signal()");
|
||||||
CWS_LOG_ERROR("signal()");
|
CWS_LOG_ERROR("signal()");
|
||||||
|
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
cws_config_s *config = cws_config_init();
|
cws_config_s *config = cws_config_init();
|
||||||
if (!config) {
|
if (!config) {
|
||||||
|
cws_log_error("Unable to read config file");
|
||||||
CWS_LOG_ERROR("Unable to read config file");
|
CWS_LOG_ERROR("Unable to read config file");
|
||||||
|
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
@@ -42,12 +47,17 @@ int main(void) {
|
|||||||
|
|
||||||
ret = cws_server_setup(&server, config);
|
ret = cws_server_setup(&server, config);
|
||||||
if (ret != CWS_OK) {
|
if (ret != CWS_OK) {
|
||||||
|
cws_log_error("Unable to setup web server: %s", cws_error_str(ret));
|
||||||
CWS_LOG_ERROR("Unable to setup web server: %s", cws_error_str(ret));
|
CWS_LOG_ERROR("Unable to setup web server: %s", cws_error_str(ret));
|
||||||
|
cws_config_free(config);
|
||||||
|
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
CWS_LOG_INFO("Running cws on http://%s:%s...", config->hostname, config->port);
|
CWS_LOG_INFO("Running cws on http://%s:%s...", config->hostname, config->port);
|
||||||
ret = cws_server_start(&server);
|
ret = cws_server_start(&server);
|
||||||
if (ret != CWS_OK) {
|
if (ret != CWS_OK) {
|
||||||
|
cws_log_error("Unable to start web server: %s", cws_error_str(ret));
|
||||||
CWS_LOG_ERROR("Unable to start web server: %s", cws_error_str(ret));
|
CWS_LOG_ERROR("Unable to start web server: %s", cws_error_str(ret));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ server += files(
|
|||||||
)
|
)
|
||||||
server += files('config/config.c')
|
server += files('config/config.c')
|
||||||
server += files(
|
server += files(
|
||||||
|
'utils/debug.c',
|
||||||
'utils/error.c',
|
'utils/error.c',
|
||||||
'utils/hash.c',
|
'utils/hash.c',
|
||||||
'utils/net.c',
|
'utils/net.c',
|
||||||
|
|||||||
29
src/utils/debug.c
Normal file
29
src/utils/debug.c
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#include "utils/debug.h"
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <syslog.h>
|
||||||
|
|
||||||
|
void cws_log_init(void) {
|
||||||
|
openlog("cws", LOG_PID | LOG_CONS, LOG_DAEMON);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cws_log_info(const char *fmt, ...) {
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
|
||||||
|
syslog(LOG_INFO, fmt, args);
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cws_log_error(const char *fmt, ...) {
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
|
||||||
|
syslog(LOG_ERR, fmt, args);
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cws_log_shutdown(void) {
|
||||||
|
closelog();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user