logging changes

personal/stbuehler/wip
Thomas Porzelt 15 years ago
parent f5fdc41644
commit a37ec08db5

@ -30,12 +30,14 @@ struct server {
gboolean exiting;
GMutex *mutex;
/* logs */
gboolean rotate_logs;
GHashTable *logs;
struct log_t *log_stderr;
struct log_t *log_syslog;
GAsyncQueue *log_queue;
GThread *log_thread;
GMutex *log_mutex;
};
struct connection {

@ -73,6 +73,7 @@ int main(int argc, char *argv[]) {
log_write_(srv, NULL, LOG_LEVEL_WARNING, "test %s", "foo1");
log_write_(srv, NULL, LOG_LEVEL_WARNING, "test %s", "foo2");
log_debug(srv, NULL, "test %s", "message");
srv->exiting = TRUE;
sleep(3);
log_error(srv, NULL, "error %d", 23);
log_write_(srv, NULL, LOG_LEVEL_WARNING, "test %s", "foo3");

@ -39,10 +39,8 @@ gboolean log_write_(server *srv, connection *con, log_level_t log_level, const g
if (con != NULL) {
/* get log index from connection */
g_mutex_lock(con->mutex);
log = con->log;
log_level_want = con->log_level;
g_mutex_unlock(con->mutex);
}
else {
log = srv->log_stderr;
@ -53,6 +51,8 @@ gboolean log_write_(server *srv, connection *con, log_level_t log_level, const g
if (log_level < log_level_want)
return TRUE;
log_ref(log);
log_line = g_string_sized_new(0);
va_start(ap, fmt);
g_string_vprintf(log_line, fmt, ap);
@ -61,6 +61,7 @@ gboolean log_write_(server *srv, connection *con, log_level_t log_level, const g
/* check if last message for this log was the same */
if (g_string_equal(log->lastmsg, log_line)) {
log->lastmsg_count++;
log_unref(srv, log);
return TRUE;
}
else {
@ -71,9 +72,7 @@ gboolean log_write_(server *srv, connection *con, log_level_t log_level, const g
}
}
g_string_assign(log->lastmsg, log_line->str);
log->lastmsg_fd = log->fd;
g_string_append_len(log_line, CONST_STR_LEN("\r\n"));
@ -82,7 +81,6 @@ gboolean log_write_(server *srv, connection *con, log_level_t log_level, const g
log_entry->log = log;
log_entry->msg = log_line;
log_ref(log);
g_async_queue_push(srv->log_queue, log_entry);
return TRUE;
@ -100,17 +98,19 @@ gpointer log_thread(server *srv) {
queue = srv->log_queue;
while (TRUE) {
log_entry = g_async_queue_pop(srv->log_queue);
/* lighty is exiting, end logging thread */
if (srv->exiting)
if (srv->exiting && g_async_queue_length(srv->log_queue) == 0)
break;
if (srv->rotate_logs) {
g_hash_table_foreach(srv->logs, (GHFunc) log_rotate, NULL);
srv->rotate_logs = FALSE;
g_mutex_lock(srv->log_mutex);
g_hash_table_foreach(srv->logs, (GHFunc) log_rotate, srv);
g_mutex_unlock(srv->log_mutex);
}
log_entry = g_async_queue_pop(srv->log_queue);
if (log_entry == NULL)
continue;
@ -122,8 +122,6 @@ gpointer log_thread(server *srv) {
while (bytes_written < (gssize)msg->len) {
write_res = write(log->fd, msg->str + bytes_written, msg->len - bytes_written);
assert(write_res <= (gssize) msg->len);
/* write() failed, check why */
if (write_res == -1) {
switch (errno) {
@ -133,6 +131,7 @@ gpointer log_thread(server *srv) {
}
g_printerr("could not write to log: %s\n", msg->str);
break;
}
else {
bytes_written += write_res;
@ -148,7 +147,8 @@ gpointer log_thread(server *srv) {
return NULL;
}
void log_rotate(gchar * path, log_t *log, gpointer UNUSED_PARAM(user_data)) {
void log_rotate(gchar * path, log_t *log, server * UNUSED_PARAM(srv)) {
switch (log->type) {
case LOG_TYPE_FILE:
close(log->fd);
@ -165,6 +165,9 @@ void log_rotate(gchar * path, log_t *log, gpointer UNUSED_PARAM(user_data)) {
/* TODO */
assert(NULL);
}
g_string_truncate(log->lastmsg, 0);
log->lastmsg_count = 0;
}
@ -181,7 +184,7 @@ log_t *log_new(server *srv, log_type_t type, GString *path) {
log_t *log;
gint fd;
g_mutex_lock(srv->mutex);
g_mutex_lock(srv->log_mutex);
log = g_hash_table_lookup(srv->logs, path->str);
/* log already open, inc refcount */
@ -216,13 +219,13 @@ log_t *log_new(server *srv, log_type_t type, GString *path) {
log->path = path;
log->refcount = 1;
g_mutex_unlock(srv->mutex);
g_mutex_unlock(srv->log_mutex);
return log;
}
void log_free(server *srv, log_t *log) {
g_mutex_lock(srv->mutex);
g_mutex_lock(srv->log_mutex);
if (log->type == LOG_TYPE_FILE || log->type == LOG_TYPE_PIPE)
close(log->fd);
@ -232,7 +235,7 @@ void log_free(server *srv, log_t *log) {
g_string_free(log->lastmsg, TRUE);
g_slice_free(log_t, log);
g_mutex_unlock(srv->mutex);
g_mutex_unlock(srv->log_mutex);
}
void log_init(server *srv) {
@ -241,6 +244,7 @@ void log_init(server *srv) {
srv->logs = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) log_free);
srv->log_queue = g_async_queue_new();
srv->log_mutex = g_mutex_new();
/* first entry in srv->logs is the plain good old stderr */
str = g_string_new_len(CONST_STR_LEN("stderr"));

@ -93,8 +93,8 @@ struct log_t {
guint refcount;
gint fd;
GString *lastmsg;
gint lastmsg_fd;
guint lastmsg_count;
GMutex *mutex;
};
struct log_entry_t {
@ -110,7 +110,7 @@ void log_free(server *srv, log_t *log);
void log_ref(log_t *log);
void log_unref(server *srv, log_t *log);
void log_rotate(gchar *path, log_t *log, gpointer user_data);
void log_rotate(gchar *path, log_t *log, server *srv);
gpointer log_thread(server *srv);
void log_init(server *srv);

Loading…
Cancel
Save