Browse Source

log_write_() improved

personal/stbuehler/wip
Thomas Porzelt 14 years ago
parent
commit
b2697e7cb8
  1. 4
      src/base.h
  2. 61
      src/log.c
  3. 14
      src/log.h
  4. 11
      src/server.c

4
src/base.h

@ -28,7 +28,7 @@ struct server {
GMutex *mutex;
gint error_log_fd;
GArray *logs;
};
struct connection {
@ -43,7 +43,7 @@ struct connection {
GMutex *mutex;
gint error_log_fd;
guint log_ndx;
};
server* server_new();

61
src/log.c

@ -34,28 +34,43 @@ gboolean log_write_(server *srv, connection *con, const char *fmt, ...) {
va_list ap;
GString *log_line;
static GStaticMutex log_mutex = G_STATIC_MUTEX_INIT;
gint log_fd;
log_t *log;
guint log_ndx;
gssize bytes_written;
gssize write_res;
if (con != NULL) {
/* get fd from connection */
/* get log index from connection */
g_mutex_lock(con->mutex);
log_fd = con->error_log_fd;
log_ndx = con->log_ndx;
g_mutex_unlock(con->mutex);
}
else {
/* get fd from server */
g_mutex_lock(srv->mutex);
log_fd = srv->error_log_fd;
g_mutex_unlock(srv->mutex);
}
else
log_ndx = 0;
/* get fd from server */
g_mutex_lock(srv->mutex);
log = &g_array_index(srv->logs, log_t, log_ndx);
g_mutex_unlock(srv->mutex);
log_line = g_string_sized_new(0);
va_start(ap, fmt);
g_string_vprintf(log_line, fmt, ap);
va_end(ap);
/* check if last message for this log was the same */
if (g_string_equal(log->lastmsg, log_line)) {
log->lastmsg_count++;
return TRUE;
}
else {
if (log->lastmsg_count > 0) {
log_write_(srv, con, "last message repeated %d times", log->lastmsg_count);
}
log->lastmsg_count = 0;
}
g_string_append_len(log_line, CONST_STR_LEN("\r\n"));
bytes_written = 0;
@ -63,7 +78,7 @@ gboolean log_write_(server *srv, connection *con, const char *fmt, ...) {
/* lock to ensure that multiple threads don't mess up the logs */
g_static_mutex_lock(&log_mutex);
while (bytes_written < (gssize)log_line->len) {
write_res = write(log_fd, log_line->str + bytes_written, log_line->len - bytes_written);
write_res = write(log->fd, log_line->str + bytes_written, log_line->len - bytes_written);
assert(write_res <= (gssize) log_line->len);
@ -91,3 +106,29 @@ gboolean log_write_(server *srv, connection *con, const char *fmt, ...) {
return TRUE;
}
log_t *log_new(const gchar* filename) {
gint fd;
log_t *log;
fd = open(filename, O_RDWR | O_CREAT | O_APPEND, 0660);
if (fd == -1)
return NULL;
log = g_slice_new0(log_t);
log->fd = fd;
log->mutex = g_mutex_new();
log->lastmsg = g_string_new("hubba bubba");
return log;
}
void log_free(log_t *log) {
close(log->fd);
g_mutex_free(log->mutex);
g_string_free(log->lastmsg, TRUE);
}

14
src/log.h

@ -3,6 +3,7 @@
/* #include "valgrind/valgrind.h" */
#include "base.h"
#include "ev.h"
#define REMOVE_PATH_FROM_FILE 1
#if REMOVE_PATH_FROM_FILE
@ -45,4 +46,17 @@ LI_API const char *remove_path(const char *path);
LI_API int log_write(server *srv, connection *con, const char *fmt, ...) __ATTRIBUTE_PRINTF_FORMAT(3, 4);
struct log_t;
typedef struct log_t log_t;
struct log_t {
gint fd;
GMutex *mutex;
GString *lastmsg;
guint lastmsg_count;
};
log_t *log_new(const gchar* filename);
void log_free(log_t *log);
#endif

11
src/server.c

@ -1,5 +1,6 @@
#include "base.h"
#include "log.h"
server* server_new() {
@ -7,7 +8,8 @@ server* server_new() {
srv->plugins = g_hash_table_new(g_str_hash, g_str_equal);
srv->options = g_hash_table_new(g_str_hash, g_str_equal);
srv->mutex = g_mutex_new();
srv->error_log_fd = STDERR_FILENO;
srv->logs = g_array_new(FALSE, FALSE, sizeof(log_t));
return srv;
}
@ -20,5 +22,12 @@ void server_free(server* srv) {
g_hash_table_destroy(srv->options);
g_mutex_free(srv->mutex);
/* free logs */
for (guint i; i < srv->logs->len; i++) {
log_t *log = &g_array_index(srv->logs, log_t, i);
log_free(log);
}
g_array_free(srv->logs, TRUE);
g_slice_free(server, srv);
}

Loading…
Cancel
Save