summaryrefslogtreecommitdiff
path: root/src/log.c
diff options
context:
space:
mode:
authorGlenn Strauss <gstrauss@gluelogic.com>2019-03-21 04:03:07 -0400
committerGlenn Strauss <gstrauss@gluelogic.com>2019-04-20 02:10:28 -0400
commitcbad7517c864c1192de3e0fd08e187858bedab2c (patch)
treeab0c19d827423e3b9614b57caa57d2c6f9780af1 /src/log.c
parent9eead7db7c0638c901cc7af522757181549d5d7f (diff)
downloadlighttpd-git-cbad7517c864c1192de3e0fd08e187858bedab2c.tar.gz
[core] struct log_error_st for error logging
Diffstat (limited to 'src/log.c')
-rw-r--r--src/log.c67
1 files changed, 47 insertions, 20 deletions
diff --git a/src/log.c b/src/log.c
index 866ade39..bc1a82c5 100644
--- a/src/log.c
+++ b/src/log.c
@@ -8,6 +8,7 @@
#include <time.h>
#include <string.h>
#include <stdarg.h>
+#include <stdlib.h> /* malloc() free() */
#include <unistd.h>
#ifdef HAVE_SYSLOG_H
@@ -115,21 +116,20 @@ static void log_buffer_append_printf(buffer *out, const char *fmt, va_list ap) {
}
}
-static int log_buffer_prepare(buffer *b, server *srv, const char *filename, unsigned int line) {
- switch(srv->errorlog_mode) {
+static int log_buffer_prepare(const log_error_st *errh, const char *filename, unsigned int line, buffer *b) {
+ switch(errh->errorlog_mode) {
case ERRORLOG_PIPE:
case ERRORLOG_FILE:
case ERRORLOG_FD:
- if (-1 == srv->errorlog_fd) return -1;
+ if (-1 == errh->errorlog_fd) return -1;
/* cache the generated timestamp */
- if (srv->cur_ts != srv->last_generated_debug_ts) {
- buffer_clear(srv->ts_debug_str);
- buffer_append_strftime(srv->ts_debug_str, "%Y-%m-%d %H:%M:%S", localtime(&(srv->cur_ts)));
-
- srv->last_generated_debug_ts = srv->cur_ts;
+ if (*errh->last_ts != *errh->cur_ts) {
+ *errh->last_ts = *errh->cur_ts;
+ buffer_clear(errh->tb);
+ buffer_append_strftime(errh->tb, "%Y-%m-%d %H:%M:%S", localtime(errh->cur_ts));
}
- buffer_copy_buffer(b, srv->ts_debug_str);
+ buffer_copy_buffer(b, errh->tb);
buffer_append_string_len(b, CONST_STR_LEN(": ("));
break;
case ERRORLOG_SYSLOG:
@@ -146,13 +146,13 @@ static int log_buffer_prepare(buffer *b, server *srv, const char *filename, unsi
return 0;
}
-static void log_write(server *srv, buffer *b) {
- switch(srv->errorlog_mode) {
+static void log_write(const log_error_st *errh, buffer *b) {
+ switch(errh->errorlog_mode) {
case ERRORLOG_PIPE:
case ERRORLOG_FILE:
case ERRORLOG_FD:
buffer_append_string_len(b, CONST_STR_LEN("\n"));
- write_all(srv->errorlog_fd, CONST_BUF_LEN(b));
+ write_all(errh->errorlog_fd, CONST_BUF_LEN(b));
break;
case ERRORLOG_SYSLOG:
syslog(LOG_ERR, "%s", b->ptr);
@@ -161,28 +161,30 @@ static void log_write(server *srv, buffer *b) {
}
int log_error_write(server *srv, const char *filename, unsigned int line, const char *fmt, ...) {
- va_list ap;
-
- if (-1 == log_buffer_prepare(srv->errorlog_buf, srv, filename, line)) return 0;
+ const log_error_st *errh = srv->errh;
+ buffer *b = errh->b;
+ if (-1 == log_buffer_prepare(errh, filename, line, b)) return 0;
+ va_list ap;
va_start(ap, fmt);
- log_buffer_append_printf(srv->errorlog_buf, fmt, ap);
+ log_buffer_append_printf(b, fmt, ap);
va_end(ap);
- log_write(srv, srv->errorlog_buf);
+ log_write(errh, b);
return 0;
}
int log_error_write_multiline_buffer(server *srv, const char *filename, unsigned int line, buffer *multiline, const char *fmt, ...) {
+ const log_error_st *errh = srv->errh;
+ buffer *b = errh->b;
va_list ap;
size_t prefix_len;
- buffer *b = srv->errorlog_buf;
char *pos, *end, *current_line;
if (buffer_string_is_empty(multiline)) return 0;
- if (-1 == log_buffer_prepare(b, srv, filename, line)) return 0;
+ if (-1 == log_buffer_prepare(errh, filename, line, b)) return 0;
va_start(ap, fmt);
log_buffer_append_printf(b, fmt, ap);
@@ -203,7 +205,7 @@ int log_error_write_multiline_buffer(server *srv, const char *filename, unsigned
buffer_string_set_length(b, prefix_len);
buffer_append_string_len(b, current_line, pos - current_line);
- log_write(srv, b);
+ log_write(errh, b);
}
current_line = pos + 1;
break;
@@ -214,3 +216,28 @@ int log_error_write_multiline_buffer(server *srv, const char *filename, unsigned
return 0;
}
+
+
+log_error_st *
+log_error_st_init (time_t *cur_ts_ptr, time_t *last_ts_ptr)
+{
+ log_error_st *errh = calloc(1, sizeof(log_error_st));
+ force_assert(errh);
+ errh->errorlog_fd = STDERR_FILENO;
+ errh->errorlog_mode = ERRORLOG_FD;
+ errh->b = buffer_init();
+ errh->tb = buffer_init();
+ errh->cur_ts = cur_ts_ptr;
+ errh->last_ts = last_ts_ptr;
+ return errh;
+}
+
+
+void
+log_error_st_free (log_error_st *errh)
+{
+ if (NULL == errh) return;
+ buffer_free(errh->tb);
+ buffer_free(errh->b);
+ free(errh);
+}