refactor(log): use syslog
This commit is contained in:
@@ -57,7 +57,7 @@ cws_config_s *cws_config_init(void) {
|
||||
|
||||
cyaml_err_t err = cyaml_load_file(path, &cyaml_config, &top_schema, (cyaml_data_t **)&config, NULL);
|
||||
if (err != CYAML_OK) {
|
||||
CWS_LOG_ERROR("%s", cyaml_strerror(err));
|
||||
cws_log_error("%s", cyaml_strerror(err));
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -53,32 +53,32 @@ cws_return cws_server_setup(cws_server_s *server, cws_config_s *config) {
|
||||
|
||||
int status = getaddrinfo(config->hostname, config->port, &hints, &res);
|
||||
if (status != 0) {
|
||||
CWS_LOG_ERROR("getaddrinfo() error: %s", gai_strerror(status));
|
||||
cws_log_error("getaddrinfo() error: %s", gai_strerror(status));
|
||||
return CWS_GETADDRINFO_ERROR;
|
||||
}
|
||||
|
||||
server->sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||
if (server->sockfd < 0) {
|
||||
CWS_LOG_ERROR("socket(): %s", strerror(errno));
|
||||
cws_log_error("socket(): %s", strerror(errno));
|
||||
return CWS_SOCKET_ERROR;
|
||||
}
|
||||
|
||||
const int opt = 1;
|
||||
status = setsockopt(server->sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt);
|
||||
if (status != 0) {
|
||||
CWS_LOG_ERROR("setsockopt(): %s", strerror(errno));
|
||||
cws_log_error("setsockopt(): %s", strerror(errno));
|
||||
return CWS_SETSOCKOPT_ERROR;
|
||||
}
|
||||
|
||||
status = bind(server->sockfd, res->ai_addr, res->ai_addrlen);
|
||||
if (status != 0) {
|
||||
CWS_LOG_ERROR("bind(): %s", strerror(errno));
|
||||
cws_log_error("bind(): %s", strerror(errno));
|
||||
return CWS_BIND_ERROR;
|
||||
}
|
||||
|
||||
status = listen(server->sockfd, CWS_SERVER_BACKLOG);
|
||||
if (status != 0) {
|
||||
CWS_LOG_ERROR("listen(): %s", strerror(errno));
|
||||
cws_log_error("listen(): %s", strerror(errno));
|
||||
return CWS_LISTEN_ERROR;
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ int cws_server_handle_new_client(int server_fd) {
|
||||
}
|
||||
|
||||
cws_utils_get_client_ip(&their_sa, ip);
|
||||
CWS_LOG_INFO("Client (%s) (fd: %d) connected", ip, client_fd);
|
||||
cws_log_info("Client (%s) (fd: %d) connected", ip, client_fd);
|
||||
|
||||
return client_fd;
|
||||
}
|
||||
@@ -151,7 +151,7 @@ int cws_server_accept_client(int server_fd, struct sockaddr_storage *their_sa) {
|
||||
|
||||
if (client_fd == -1) {
|
||||
if (errno != EWOULDBLOCK) {
|
||||
CWS_LOG_ERROR("accept(): %s", strerror(errno));
|
||||
cws_log_error("accept(): %s", strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ cws_response_s *cws_handler_static_file(cws_request_s *request, cws_handler_conf
|
||||
string_s *filepath = resolve_file_path(string_cstr(request->path), config);
|
||||
const char *path = string_cstr(filepath);
|
||||
|
||||
CWS_LOG_DEBUG("Resolved path: %s", path);
|
||||
cws_log_debug("Resolved path: %s", path);
|
||||
|
||||
if (!file_exists(path)) {
|
||||
string_free(filepath);
|
||||
@@ -50,7 +50,7 @@ cws_response_s *cws_handler_static_file(cws_request_s *request, cws_handler_conf
|
||||
}
|
||||
|
||||
cws_response_set_body_file(response, path);
|
||||
CWS_LOG_DEBUG("Serving file: %s (%zu bytes)", path, response->content_length);
|
||||
cws_log_debug("Serving file: %s (%zu bytes)", path, response->content_length);
|
||||
string_free(filepath);
|
||||
|
||||
return response;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "utils/debug.h"
|
||||
#include "utils/hash.h"
|
||||
|
||||
static cws_request_s *http_new() {
|
||||
static cws_request_s *http_new(void) {
|
||||
cws_request_s *request = malloc(sizeof(*request));
|
||||
if (!request) {
|
||||
return NULL;
|
||||
@@ -52,7 +52,7 @@ static bool parse_method(cws_request_s *req, char **cursor) {
|
||||
}
|
||||
|
||||
s[len] = '\0';
|
||||
CWS_LOG_DEBUG("Method: %s", s);
|
||||
cws_log_debug("Method: %s", s);
|
||||
req->method = http_parse_method(s);
|
||||
*cursor = s + len + 1;
|
||||
|
||||
@@ -67,7 +67,7 @@ static bool parse_location(cws_request_s *req, char **cursor) {
|
||||
}
|
||||
|
||||
s[len] = '\0';
|
||||
CWS_LOG_DEBUG("Location: %s", s);
|
||||
cws_log_debug("Location: %s", s);
|
||||
string_append(req->path, s);
|
||||
*cursor = s + len + 1;
|
||||
|
||||
@@ -82,7 +82,7 @@ static bool parse_version(cws_request_s *req, char **cursor) {
|
||||
}
|
||||
|
||||
s[len] = '\0';
|
||||
CWS_LOG_DEBUG("Version: %s", s);
|
||||
cws_log_debug("Version: %s", s);
|
||||
string_append(req->http_version, s);
|
||||
*cursor = s + len + 1;
|
||||
|
||||
|
||||
@@ -90,7 +90,7 @@ void cws_response_set_body_file(cws_response_s *response, const char *filepath)
|
||||
|
||||
FILE *fp = fopen(filepath, "rb");
|
||||
if (!fp) {
|
||||
CWS_LOG_ERROR("Cannot open file: %s", filepath);
|
||||
cws_log_error("Cannot open file: %s", filepath);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
20
src/main.c
20
src/main.c
@@ -1,5 +1,4 @@
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -8,21 +7,24 @@
|
||||
#include "utils/debug.h"
|
||||
#include "utils/error.h"
|
||||
|
||||
void cws_signal_handler(int) {
|
||||
void cws_signal_handler(int signo) {
|
||||
(void)signo;
|
||||
cws_server_run = 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
cws_log_init();
|
||||
cws_log_info("Running cws...");
|
||||
|
||||
if (signal(SIGINT, cws_signal_handler) == SIG_ERR) {
|
||||
CWS_LOG_ERROR("signal()");
|
||||
cws_log_error("signal()");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
cws_config_s *config = cws_config_init();
|
||||
if (!config) {
|
||||
CWS_LOG_ERROR("Unable to read config file");
|
||||
cws_log_error("Unable to read config file");
|
||||
cws_log_shutdown();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
@@ -31,20 +33,22 @@ int main(void) {
|
||||
|
||||
ret = cws_server_setup(&server, config);
|
||||
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_config_free(config);
|
||||
cws_log_shutdown();
|
||||
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);
|
||||
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_INFO("Shutting down cws");
|
||||
cws_log_info("Shutting down cws");
|
||||
cws_server_shutdown(&server);
|
||||
cws_config_free(config);
|
||||
cws_log_shutdown();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "utils/debug.h"
|
||||
#include <stdarg.h>
|
||||
#include <syslog.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/syslog.h>
|
||||
|
||||
void cws_log_init(void) {
|
||||
openlog("cws", LOG_PID | LOG_CONS, LOG_DAEMON);
|
||||
@@ -8,22 +9,57 @@ void cws_log_init(void) {
|
||||
|
||||
void cws_log_info(const char *fmt, ...) {
|
||||
va_list args;
|
||||
fprintf(stdout, _INFO " ");
|
||||
va_start(args, fmt);
|
||||
|
||||
vfprintf(stdout, fmt, args);
|
||||
syslog(LOG_INFO, fmt, args);
|
||||
|
||||
va_end(args);
|
||||
fprintf(stdout, "\n");
|
||||
}
|
||||
|
||||
void cws_log_warning(const char *fmt, ...) {
|
||||
va_list args;
|
||||
fprintf(stdout, _WARNING " ");
|
||||
va_start(args, fmt);
|
||||
|
||||
vfprintf(stdout, fmt, args);
|
||||
|
||||
va_end(args);
|
||||
fprintf(stdout, "\n");
|
||||
}
|
||||
|
||||
void cws_log_error(const char *fmt, ...) {
|
||||
va_list args;
|
||||
fprintf(stdout, _ERR " ");
|
||||
va_start(args, fmt);
|
||||
|
||||
vfprintf(stdout, fmt, args);
|
||||
syslog(LOG_ERR, fmt, args);
|
||||
|
||||
va_end(args);
|
||||
fprintf(stdout, "\n");
|
||||
}
|
||||
|
||||
void cws_log_shutdown(void) {
|
||||
closelog();
|
||||
}
|
||||
|
||||
#ifdef EVELOPER
|
||||
void cws_log_debug(const char *fmt, ...) {
|
||||
fprintf(stdout, _DEBUG " [%s:%d] ", __FILE__, __LINE__);
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
|
||||
vfprintf(stdout, fmt, args);
|
||||
syslog(LOG_DEBUG, fmt, args);
|
||||
|
||||
va_end(args);
|
||||
fprintf(stdout, "\n");
|
||||
}
|
||||
#else
|
||||
void cws_log_debug(const char *fmt, ...) {
|
||||
/* Nothing */
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user