summaryrefslogtreecommitdiffhomepage
path: root/src/log.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/log.c')
-rw-r--r--src/log.c80
1 files changed, 43 insertions, 37 deletions
diff --git a/src/log.c b/src/log.c
index f85d29d..14fc3fe 100644
--- a/src/log.c
+++ b/src/log.c
@@ -27,8 +27,9 @@
#include "heap.h"
#include "log.h"
#include "utils.h"
-#include "vector.h"
+#include "sblist.h"
#include "conf.h"
+#include <pthread.h>
static const char *syslog_level[] = {
NULL,
@@ -45,6 +46,8 @@ static const char *syslog_level[] = {
#define TIME_LENGTH 16
#define STRING_LENGTH 800
+static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
+
/*
* Global file descriptor for the log file
*/
@@ -61,7 +64,7 @@ static int log_level = LOG_INFO;
* The key is the actual messages (already filled in full), and the value
* is the log level.
*/
-static vector_t log_message_storage;
+static sblist *log_message_storage;
static unsigned int logging_initialized = FALSE; /* boolean */
@@ -71,10 +74,7 @@ static unsigned int logging_initialized = FALSE; /* boolean */
int open_log_file (const char *log_file_name)
{
if (log_file_name == NULL) {
- if(config.godaemon == FALSE)
- log_file_fd = fileno(stdout);
- else
- log_file_fd = -1;
+ log_file_fd = fileno(stdout);
} else {
log_file_fd = create_file_safely (log_file_name, FALSE);
}
@@ -108,7 +108,8 @@ void set_log_level (int level)
void log_message (int level, const char *fmt, ...)
{
va_list args;
- time_t nowtime;
+ struct timespec nowtime;
+ struct tm tm_buf;
char time_string[TIME_LENGTH];
char str[STRING_LENGTH];
@@ -129,7 +130,7 @@ void log_message (int level, const char *fmt, ...)
return;
#endif
- if (config.syslog && level == LOG_CONN)
+ if (config && config->syslog && level == LOG_CONN)
level = LOG_INFO;
va_start (args, fmt);
@@ -142,7 +143,7 @@ void log_message (int level, const char *fmt, ...)
char *entry_buffer;
if (!log_message_storage) {
- log_message_storage = vector_create ();
+ log_message_storage = sblist_new (sizeof(char*), 64);
if (!log_message_storage)
goto out;
}
@@ -154,33 +155,34 @@ void log_message (int level, const char *fmt, ...)
goto out;
sprintf (entry_buffer, "%d %s", level, str);
- vector_append (log_message_storage, entry_buffer,
- strlen (entry_buffer) + 1);
-
- safefree (entry_buffer);
+ if(!sblist_add (log_message_storage, &entry_buffer))
+ safefree (entry_buffer);
goto out;
}
- if(!config.syslog && log_file_fd == -1)
+ if(!config->syslog && log_file_fd == -1)
goto out;
- if (config.syslog) {
+ if (config->syslog) {
+ pthread_mutex_lock(&log_mutex);
#ifdef HAVE_VSYSLOG_H
vsyslog (level, fmt, args);
#else
vsnprintf (str, STRING_LENGTH, fmt, args);
syslog (level, "%s", str);
#endif
+ pthread_mutex_unlock(&log_mutex);
} else {
char *p;
- nowtime = time (NULL);
+ clock_gettime(CLOCK_REALTIME, &nowtime);
/* Format is month day hour:minute:second (24 time) */
strftime (time_string, TIME_LENGTH, "%b %d %H:%M:%S",
- localtime (&nowtime));
+ localtime_r (&nowtime.tv_sec, &tm_buf));
- snprintf (str, STRING_LENGTH, "%-9s %s [%ld]: ",
+ snprintf (str, STRING_LENGTH, "%-9s %s.%03lu [%ld]: ",
syslog_level[level], time_string,
+ (unsigned long) nowtime.tv_nsec/1000000ul,
(long int) getpid ());
/*
@@ -196,18 +198,24 @@ void log_message (int level, const char *fmt, ...)
assert (log_file_fd >= 0);
+ pthread_mutex_lock(&log_mutex);
ret = write (log_file_fd, str, strlen (str));
+ pthread_mutex_unlock(&log_mutex);
+
if (ret == -1) {
- config.syslog = TRUE;
+ config->syslog = TRUE;
log_message(LOG_CRIT, "ERROR: Could not write to log "
"file %s: %s.",
- config.logf_name, strerror(errno));
+ config->logf_name, strerror(errno));
log_message(LOG_CRIT,
"Falling back to syslog logging");
}
+ pthread_mutex_lock(&log_mutex);
fsync (log_file_fd);
+ pthread_mutex_unlock(&log_mutex);
+
}
out:
@@ -219,7 +227,7 @@ out:
*/
static void send_stored_logs (void)
{
- char *string;
+ char **string;
char *ptr;
int level;
size_t i;
@@ -229,12 +237,12 @@ static void send_stored_logs (void)
log_message(LOG_DEBUG, "sending stored logs");
- for (i = 0; (ssize_t) i != vector_length (log_message_storage); ++i) {
- string =
- (char *) vector_getentry (log_message_storage, i, NULL);
+ for (i = 0; i < sblist_getsize (log_message_storage); ++i) {
+ string = sblist_get (log_message_storage, i);
+ if (!string || !*string) continue;
- ptr = strchr (string, ' ') + 1;
- level = atoi (string);
+ ptr = strchr (*string, ' ') + 1;
+ level = atoi (*string);
#ifdef NDEBUG
if (log_level == LOG_CONN && level == LOG_INFO)
@@ -247,9 +255,10 @@ static void send_stored_logs (void)
#endif
log_message (level, "%s", ptr);
+ safefree(*string);
}
- vector_delete (log_message_storage);
+ sblist_free (log_message_storage);
log_message_storage = NULL;
log_message(LOG_DEBUG, "done sending stored logs");
@@ -264,27 +273,24 @@ static void send_stored_logs (void)
*/
int setup_logging (void)
{
- if (!config.syslog) {
- if (open_log_file (config.logf_name) < 0) {
+ if (!config->syslog) {
+ if (open_log_file (config->logf_name) < 0) {
/*
* If opening the log file fails, we try
* to fall back to syslog logging...
*/
- config.syslog = TRUE;
+ config->syslog = TRUE;
log_message (LOG_CRIT, "ERROR: Could not create log "
"file %s: %s.",
- config.logf_name, strerror (errno));
+ config->logf_name, strerror (errno));
log_message (LOG_CRIT,
"Falling back to syslog logging.");
}
}
- if (config.syslog) {
- if (config.godaemon == TRUE)
- openlog ("tinyproxy", LOG_PID, LOG_DAEMON);
- else
- openlog ("tinyproxy", LOG_PID, LOG_USER);
+ if (config->syslog) {
+ openlog ("tinyproxy", LOG_PID, LOG_USER);
}
logging_initialized = TRUE;
@@ -302,7 +308,7 @@ void shutdown_logging (void)
return;
}
- if (config.syslog) {
+ if (config->syslog) {
closelog ();
} else {
close_log_file ();