2004-01-28 20:02:12 -05:00
|
|
|
/*
|
|
|
|
** Logging framework.
|
|
|
|
**
|
|
|
|
** This program is distributed under the GNU General Public License, version 2.
|
|
|
|
** A copy of this license is included with this source.
|
|
|
|
*/
|
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2003-07-08 22:45:22 -04:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <pthread.h>
|
|
|
|
#else
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
#define LOG_MAXLOGS 25
|
|
|
|
#define LOG_MAXLINELEN 1024
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define mutex_t CRITICAL_SECTION
|
2001-10-20 01:13:12 -04:00
|
|
|
#define snprintf _snprintf
|
|
|
|
#define vsnprintf _vsnprintf
|
2001-09-09 22:24:39 -04:00
|
|
|
#else
|
|
|
|
#define mutex_t pthread_mutex_t
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static mutex_t _logger_mutex;
|
|
|
|
static int _initialized = 0;
|
|
|
|
|
|
|
|
typedef struct log_tag
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
int in_use;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
unsigned level;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
char *filename;
|
|
|
|
FILE *logfile;
|
2003-07-08 22:45:22 -04:00
|
|
|
unsigned size;
|
|
|
|
unsigned trigger_level;
|
2003-03-14 21:10:19 -05:00
|
|
|
|
2002-04-05 04:28:26 -05:00
|
|
|
char *buffer;
|
2001-09-09 22:24:39 -04:00
|
|
|
} log_t;
|
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
static log_t loglist[LOG_MAXLOGS];
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
static int _get_log_id();
|
|
|
|
static void _release_log_id(int log_id);
|
2001-09-09 22:24:39 -04:00
|
|
|
static void _lock_logger();
|
|
|
|
static void _unlock_logger();
|
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
|
|
|
|
static int _log_open (int id)
|
|
|
|
{
|
|
|
|
if (loglist [id] . in_use == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* check for cases where an open of the logfile is wanted */
|
|
|
|
if (loglist [id] . logfile == NULL ||
|
|
|
|
(loglist [id] . trigger_level && loglist [id] . size > loglist [id] . trigger_level))
|
|
|
|
{
|
|
|
|
if (loglist [id] . filename) /* only re-open files where we have a name */
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (loglist [id] . logfile)
|
|
|
|
{
|
|
|
|
char new_name [255];
|
|
|
|
fclose (loglist [id] . logfile);
|
|
|
|
loglist [id] . logfile = NULL;
|
|
|
|
/* simple rename, but could use time providing locking were used */
|
|
|
|
snprintf (new_name, sizeof(new_name), "%s.old", loglist [id] . filename);
|
|
|
|
rename (loglist [id] . filename, new_name);
|
|
|
|
}
|
|
|
|
loglist [id] . logfile = fopen (loglist [id] . filename, "a");
|
|
|
|
if (loglist [id] . logfile == NULL)
|
|
|
|
return 0;
|
|
|
|
setvbuf (loglist [id] . logfile, NULL, IO_BUFFER_TYPE, 0);
|
|
|
|
if (stat (loglist [id] . filename, &st) < 0)
|
|
|
|
loglist [id] . size = 0;
|
|
|
|
else
|
|
|
|
loglist [id] . size = st.st_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
loglist [id] . size = 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
void log_initialize()
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
int i;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (_initialized) return;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
for (i = 0; i < LOG_MAXLOGS; i++) {
|
|
|
|
loglist[i].in_use = 0;
|
|
|
|
loglist[i].level = 2;
|
2003-07-08 22:45:22 -04:00
|
|
|
loglist[i].size = 0;
|
|
|
|
loglist[i].trigger_level = 0;
|
2003-03-14 21:10:19 -05:00
|
|
|
loglist[i].filename = NULL;
|
|
|
|
loglist[i].logfile = NULL;
|
|
|
|
loglist[i].buffer = NULL;
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* initialize mutexes */
|
2001-09-09 22:24:39 -04:00
|
|
|
#ifndef _WIN32
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_mutex_init(&_logger_mutex, NULL);
|
2001-09-09 22:24:39 -04:00
|
|
|
#else
|
2003-03-14 21:10:19 -05:00
|
|
|
InitializeCriticalSection(&_logger_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_initialized = 1;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2002-01-29 04:20:28 -05:00
|
|
|
int log_open_file(FILE *file)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2002-01-29 04:20:28 -05:00
|
|
|
int log_id;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2002-01-29 04:20:28 -05:00
|
|
|
if(file == NULL) return LOG_EINSANE;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
log_id = _get_log_id();
|
|
|
|
if (log_id < 0) return LOG_ENOMORELOGS;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
loglist[log_id].logfile = file;
|
2003-07-08 22:45:22 -04:00
|
|
|
loglist[log_id].filename = NULL;
|
|
|
|
loglist[log_id].size = 0;
|
2001-10-20 01:13:12 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return log_id;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2002-01-29 04:20:28 -05:00
|
|
|
|
|
|
|
int log_open(const char *filename)
|
|
|
|
{
|
2003-07-08 22:45:22 -04:00
|
|
|
int id;
|
2002-01-29 04:20:28 -05:00
|
|
|
FILE *file;
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (filename == NULL) return LOG_EINSANE;
|
|
|
|
if (strcmp(filename, "") == 0) return LOG_EINSANE;
|
2002-01-29 04:20:28 -05:00
|
|
|
|
|
|
|
file = fopen(filename, "a");
|
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
id = log_open_file(file);
|
|
|
|
|
|
|
|
if (id >= 0)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
setvbuf (loglist [id] . logfile, NULL, IO_BUFFER_TYPE, 0);
|
|
|
|
loglist [id] . filename = strdup (filename);
|
|
|
|
if (stat (loglist [id] . filename, &st) == 0)
|
|
|
|
loglist [id] . size = st.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* set the trigger level to trigger, represented in kilobytes */
|
|
|
|
void log_set_trigger(int id, unsigned trigger)
|
|
|
|
{
|
|
|
|
if (id >= 0 && id < LOG_MAXLOGS && loglist [id] . in_use)
|
|
|
|
{
|
|
|
|
loglist [id] . trigger_level = trigger*1024;
|
|
|
|
}
|
|
|
|
}
|
2002-01-29 04:20:28 -05:00
|
|
|
|
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
int log_set_filename(int id, const char *filename)
|
|
|
|
{
|
2003-07-11 19:36:49 -04:00
|
|
|
if (id < 0 || id >= LOG_MAXLOGS)
|
2003-07-08 22:45:22 -04:00
|
|
|
return LOG_EINSANE;
|
2003-07-11 19:36:49 -04:00
|
|
|
if (!strcmp(filename, "") || loglist [id] . in_use == 0)
|
2003-07-08 22:45:22 -04:00
|
|
|
return LOG_EINSANE;
|
|
|
|
_lock_logger();
|
|
|
|
if (loglist [id] . filename)
|
|
|
|
free (loglist [id] . filename);
|
2003-07-11 19:36:49 -04:00
|
|
|
if (filename)
|
|
|
|
loglist [id] . filename = strdup (filename);
|
|
|
|
else
|
|
|
|
loglist [id] . filename = NULL;
|
2003-07-08 22:45:22 -04:00
|
|
|
_unlock_logger();
|
|
|
|
return id;
|
2002-01-29 04:20:28 -05:00
|
|
|
}
|
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
int log_open_with_buffer(const char *filename, int size)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
/* not implemented */
|
|
|
|
return LOG_ENOTIMPL;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
|
|
|
|
void log_set_level(int log_id, unsigned level)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
if (log_id < 0 || log_id >= LOG_MAXLOGS) return;
|
|
|
|
if (loglist[log_id].in_use == 0) return;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
loglist[log_id].level = level;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void log_flush(int log_id)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
if (log_id < 0 || log_id >= LOG_MAXLOGS) return;
|
|
|
|
if (loglist[log_id].in_use == 0) return;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
_lock_logger();
|
|
|
|
if (loglist[log_id].logfile)
|
|
|
|
fflush(loglist[log_id].logfile);
|
|
|
|
_unlock_logger();
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void log_reopen(int log_id)
|
|
|
|
{
|
2003-07-11 19:36:49 -04:00
|
|
|
if (log_id < 0 && log_id >= LOG_MAXLOGS)
|
2003-07-08 22:45:22 -04:00
|
|
|
return;
|
2003-07-11 19:36:49 -04:00
|
|
|
if (loglist [log_id] . filename && loglist [log_id] . logfile)
|
2003-07-08 22:45:22 -04:00
|
|
|
{
|
|
|
|
_lock_logger();
|
|
|
|
|
|
|
|
fclose (loglist [log_id] . logfile);
|
|
|
|
loglist [log_id] . logfile = NULL;
|
2003-07-11 19:36:49 -04:00
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
_unlock_logger();
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void log_close(int log_id)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
if (log_id < 0 || log_id >= LOG_MAXLOGS) return;
|
2003-07-11 19:36:49 -04:00
|
|
|
|
|
|
|
_lock_logger();
|
|
|
|
|
2003-11-17 18:53:37 -05:00
|
|
|
if (loglist[log_id].in_use == 0)
|
|
|
|
{
|
|
|
|
_unlock_logger();
|
|
|
|
return;
|
|
|
|
}
|
2003-03-14 21:10:19 -05:00
|
|
|
|
|
|
|
loglist[log_id].in_use = 0;
|
|
|
|
loglist[log_id].level = 2;
|
|
|
|
if (loglist[log_id].filename) free(loglist[log_id].filename);
|
|
|
|
if (loglist[log_id].buffer) free(loglist[log_id].buffer);
|
2003-07-11 19:36:49 -04:00
|
|
|
|
|
|
|
if (loglist [log_id] . logfile)
|
|
|
|
{
|
|
|
|
fclose (loglist [log_id] . logfile);
|
|
|
|
loglist [log_id] . logfile = NULL;
|
|
|
|
}
|
|
|
|
_unlock_logger();
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void log_shutdown()
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
/* destroy mutexes */
|
2001-09-09 22:24:39 -04:00
|
|
|
#ifndef _WIN32
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_mutex_destroy(&_logger_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
#else
|
2003-03-14 21:10:19 -05:00
|
|
|
DeleteCriticalSection(&_logger_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
_initialized = 0;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
void log_write(int log_id, unsigned priority, const char *cat, const char *func,
|
2002-08-03 04:14:56 -04:00
|
|
|
const char *fmt, ...)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-07-08 22:45:22 -04:00
|
|
|
static char *prior[] = { "EROR", "WARN", "INFO", "DBUG" };
|
2003-03-14 21:10:19 -05:00
|
|
|
char tyme[128];
|
|
|
|
char pre[256];
|
|
|
|
char line[LOG_MAXLINELEN];
|
|
|
|
time_t now;
|
|
|
|
va_list ap;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (log_id < 0) return;
|
2002-04-05 04:28:26 -05:00
|
|
|
if (log_id > LOG_MAXLOGS) return; /* Bad log number */
|
2003-03-14 21:10:19 -05:00
|
|
|
if (loglist[log_id].level < priority) return;
|
2003-07-08 22:45:22 -04:00
|
|
|
if (priority > sizeof(prior)/sizeof(prior[0])) return; /* Bad priority */
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(line, LOG_MAXLINELEN, fmt, ap);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
now = time(NULL);
|
2002-08-09 23:22:44 -04:00
|
|
|
|
|
|
|
_lock_logger();
|
2003-07-08 22:45:22 -04:00
|
|
|
strftime(tyme, sizeof (tyme), "[%Y-%m-%d %H:%M:%S]", localtime(&now));
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
snprintf(pre, sizeof (pre), "%s %s%s", prior[priority-1], cat, func);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
if (_log_open (log_id))
|
|
|
|
{
|
|
|
|
int len = fprintf (loglist[log_id].logfile, "%s %s %s\n", tyme, pre, line);
|
|
|
|
if (len > 0)
|
|
|
|
loglist[log_id].size += len;
|
|
|
|
}
|
|
|
|
_unlock_logger();
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
va_end(ap);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void log_write_direct(int log_id, const char *fmt, ...)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
char line[LOG_MAXLINELEN];
|
|
|
|
va_list ap;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (log_id < 0) return;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2003-07-08 22:45:22 -04:00
|
|
|
|
|
|
|
_lock_logger();
|
2003-03-14 21:10:19 -05:00
|
|
|
vsnprintf(line, LOG_MAXLINELEN, fmt, ap);
|
2003-07-08 22:45:22 -04:00
|
|
|
if (_log_open (log_id))
|
|
|
|
{
|
|
|
|
int len = fprintf(loglist[log_id].logfile, "%s\n", line);
|
|
|
|
if (len > 0)
|
|
|
|
loglist[log_id].size += len;
|
|
|
|
}
|
|
|
|
_unlock_logger();
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
va_end(ap);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
fflush(loglist[log_id].logfile);
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
static int _get_log_id()
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
int i;
|
|
|
|
int id = -1;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* lock mutex */
|
|
|
|
_lock_logger();
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
for (i = 0; i < LOG_MAXLOGS; i++)
|
|
|
|
if (loglist[i].in_use == 0) {
|
|
|
|
loglist[i].in_use = 1;
|
|
|
|
id = i;
|
|
|
|
break;
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* unlock mutex */
|
|
|
|
_unlock_logger();
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return id;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
static void _release_log_id(int log_id)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
/* lock mutex */
|
|
|
|
_lock_logger();
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
loglist[log_id].in_use = 0;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
/* unlock mutex */
|
|
|
|
_unlock_logger();
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _lock_logger()
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_mutex_lock(&_logger_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
#else
|
2003-03-14 21:10:19 -05:00
|
|
|
EnterCriticalSection(&_logger_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _unlock_logger()
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
2003-03-14 21:10:19 -05:00
|
|
|
pthread_mutex_unlock(&_logger_mutex);
|
2001-09-09 22:24:39 -04:00
|
|
|
#else
|
2003-03-14 21:10:19 -05:00
|
|
|
LeaveCriticalSection(&_logger_mutex);
|
|
|
|
#endif
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|