2004-01-28 20:02:12 -05:00
|
|
|
/*
|
|
|
|
** Logging framework.
|
|
|
|
**
|
2014-12-05 04:31:08 -05:00
|
|
|
** Copyright (C) 2014 Michael Smith <msmith@icecast.org>,
|
|
|
|
** Ralph Giles <giles@xiph.org>,
|
|
|
|
** Ed "oddsock" Zaleski <oddsock@xiph.org>,
|
|
|
|
** Karl Heyes <karl@xiph.org>,
|
|
|
|
** Jack Moffitt <jack@icecast.org>,
|
|
|
|
** Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
|
|
|
|
** Thomas Ruecker <thomas@ruecker.fi>
|
|
|
|
**
|
|
|
|
** This library is free software; you can redistribute it and/or
|
|
|
|
** modify it under the terms of the GNU Library General Public
|
|
|
|
** License as published by the Free Software Foundation; either
|
|
|
|
** version 2 of the License, or (at your option) any later version.
|
|
|
|
**
|
|
|
|
** This library is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
** Library General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU Library General Public
|
|
|
|
** License along with this library; if not, write to the
|
|
|
|
** Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
** Boston, MA 02110-1301, USA.
|
|
|
|
**
|
2004-01-28 20:02:12 -05:00
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
2006-03-26 21:26:06 -05:00
|
|
|
typedef struct _log_entry_t
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
unsigned int len;
|
|
|
|
struct _log_entry_t *next;
|
|
|
|
} log_entry_t;
|
|
|
|
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
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;
|
2005-08-06 09:10:30 -04:00
|
|
|
off_t size;
|
|
|
|
off_t trigger_level;
|
2005-10-24 10:52:35 -04:00
|
|
|
int archive_timestamp;
|
2006-03-26 21:26:06 -05:00
|
|
|
|
|
|
|
unsigned long total;
|
|
|
|
unsigned int entries;
|
|
|
|
unsigned int keep_entries;
|
|
|
|
log_entry_t *log_head;
|
|
|
|
log_entry_t **log_tail;
|
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
|
|
|
|
2005-12-17 07:23:54 -05:00
|
|
|
static int _get_log_id(void);
|
2003-07-08 22:45:22 -04:00
|
|
|
static void _release_log_id(int log_id);
|
2005-12-17 07:23:54 -05:00
|
|
|
static void _lock_logger(void);
|
|
|
|
static void _unlock_logger(void);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
|
2008-01-23 22:28:11 -05:00
|
|
|
static int _log_open (int id)
|
2003-07-08 22:45:22 -04:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2005-08-06 09:10:30 -04:00
|
|
|
char new_name [4096];
|
2003-07-08 22:45:22 -04:00
|
|
|
fclose (loglist [id] . logfile);
|
|
|
|
loglist [id] . logfile = NULL;
|
|
|
|
/* simple rename, but could use time providing locking were used */
|
2008-01-23 22:28:11 -05:00
|
|
|
if (loglist[id].archive_timestamp)
|
|
|
|
{
|
|
|
|
char timestamp [128];
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
strftime (timestamp, sizeof (timestamp), "%Y%m%d_%H%M%S", localtime (&now));
|
|
|
|
snprintf (new_name, sizeof(new_name), "%s.%s", loglist[id].filename, timestamp);
|
2005-10-24 10:52:35 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
snprintf (new_name, sizeof(new_name), "%s.old", loglist [id] . filename);
|
|
|
|
}
|
2005-08-06 09:10:30 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (stat (new_name, &st) == 0)
|
|
|
|
remove (new_name);
|
|
|
|
#endif
|
2003-07-08 22:45:22 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-12-17 07:23:54 -05:00
|
|
|
void log_initialize(void)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
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;
|
2005-08-06 09:10:30 -04:00
|
|
|
loglist[i].trigger_level = 1000000000;
|
2003-03-14 21:10:19 -05:00
|
|
|
loglist[i].filename = NULL;
|
|
|
|
loglist[i].logfile = NULL;
|
|
|
|
loglist[i].buffer = NULL;
|
2006-03-26 21:26:06 -05:00
|
|
|
loglist[i].total = 0;
|
|
|
|
loglist[i].entries = 0;
|
|
|
|
loglist[i].keep_entries = 0;
|
|
|
|
loglist[i].log_head = NULL;
|
|
|
|
loglist[i].log_tail = &loglist[i].log_head;
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
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;
|
2006-03-26 21:26:06 -05:00
|
|
|
loglist [id] . entries = 0;
|
|
|
|
loglist [id] . log_head = NULL;
|
|
|
|
loglist [id] . log_tail = &loglist [id] . log_head;
|
2003-07-08 22:45:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2006-03-07 14:07:20 -05:00
|
|
|
/* NULL filename is ok, empty filename is not. */
|
|
|
|
if ((filename && !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
|
|
|
}
|
|
|
|
|
2005-10-24 10:52:35 -04:00
|
|
|
int log_set_archive_timestamp(int id, int value)
|
|
|
|
{
|
|
|
|
if (id < 0 || id >= LOG_MAXLOGS)
|
|
|
|
return LOG_EINSANE;
|
|
|
|
_lock_logger();
|
|
|
|
loglist[id].archive_timestamp = value;
|
|
|
|
_unlock_logger();
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2006-03-26 21:26:06 -05:00
|
|
|
void log_set_lines_kept (int log_id, unsigned int count)
|
|
|
|
{
|
|
|
|
if (log_id < 0 || log_id >= LOG_MAXLOGS) return;
|
|
|
|
if (loglist[log_id].in_use == 0) return;
|
|
|
|
|
|
|
|
_lock_logger ();
|
|
|
|
loglist[log_id].keep_entries = count;
|
|
|
|
while (loglist[log_id].entries > count)
|
|
|
|
{
|
|
|
|
log_entry_t *to_go = loglist [log_id].log_head;
|
|
|
|
loglist [log_id].log_head = to_go->next;
|
|
|
|
loglist [log_id].total -= to_go->len;
|
|
|
|
free (to_go->line);
|
|
|
|
free (to_go);
|
|
|
|
loglist [log_id].entries--;
|
|
|
|
}
|
|
|
|
_unlock_logger ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2013-07-04 08:31:17 -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;
|
|
|
|
}
|
2006-03-26 21:26:06 -05:00
|
|
|
while (loglist[log_id].entries)
|
|
|
|
{
|
|
|
|
log_entry_t *to_go = loglist [log_id].log_head;
|
|
|
|
loglist [log_id].log_head = to_go->next;
|
|
|
|
loglist [log_id].total -= to_go->len;
|
|
|
|
free (to_go->line);
|
|
|
|
free (to_go);
|
|
|
|
loglist [log_id].entries--;
|
|
|
|
}
|
2003-07-11 19:36:49 -04:00
|
|
|
_unlock_logger();
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
2005-12-17 07:23:54 -05:00
|
|
|
void log_shutdown(void)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2006-03-26 21:26:06 -05:00
|
|
|
|
|
|
|
static int create_log_entry (int log_id, const char *pre, const char *line)
|
|
|
|
{
|
|
|
|
log_entry_t *entry;
|
|
|
|
|
|
|
|
if (loglist[log_id].keep_entries == 0)
|
|
|
|
return fprintf (loglist[log_id].logfile, "%s%s\n", pre, line);
|
|
|
|
|
|
|
|
entry = calloc (1, sizeof (log_entry_t));
|
2009-01-07 21:18:11 -05:00
|
|
|
entry->len = strlen (pre) + strlen (line) + 2;
|
|
|
|
entry->line = malloc (entry->len);
|
|
|
|
snprintf (entry->line, entry->len, "%s%s\n", pre, line);
|
|
|
|
loglist [log_id].total += entry->len;
|
2006-03-26 21:26:06 -05:00
|
|
|
fprintf (loglist[log_id].logfile, "%s", entry->line);
|
|
|
|
|
|
|
|
*loglist [log_id].log_tail = entry;
|
|
|
|
loglist [log_id].log_tail = &entry->next;
|
|
|
|
|
|
|
|
if (loglist [log_id].entries >= loglist [log_id].keep_entries)
|
|
|
|
{
|
|
|
|
log_entry_t *to_go = loglist [log_id].log_head;
|
|
|
|
loglist [log_id].log_head = to_go->next;
|
|
|
|
loglist [log_id].total -= to_go->len;
|
|
|
|
free (to_go->line);
|
|
|
|
free (to_go);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
loglist [log_id].entries++;
|
2009-01-07 21:18:11 -05:00
|
|
|
return entry->len;
|
2006-03-26 21:26:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void log_contents (int log_id, char **_contents, unsigned int *_len)
|
|
|
|
{
|
|
|
|
int remain;
|
|
|
|
log_entry_t *entry;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
if (log_id < 0) return;
|
2006-03-30 21:14:12 -05:00
|
|
|
if (log_id >= LOG_MAXLOGS) return; /* Bad log number */
|
2006-03-26 21:26:06 -05:00
|
|
|
|
|
|
|
_lock_logger ();
|
|
|
|
remain = loglist [log_id].total + 1;
|
|
|
|
*_contents = malloc (remain);
|
2008-01-23 22:28:11 -05:00
|
|
|
**_contents= '\0';
|
2006-03-26 21:26:06 -05:00
|
|
|
*_len = loglist [log_id].total;
|
|
|
|
|
|
|
|
entry = loglist [log_id].log_head;
|
|
|
|
ptr = *_contents;
|
|
|
|
while (entry)
|
|
|
|
{
|
|
|
|
int len = snprintf (ptr, remain, "%s", entry->line);
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
ptr += len;
|
|
|
|
remain -= len;
|
|
|
|
}
|
|
|
|
entry = entry->next;
|
|
|
|
}
|
|
|
|
_unlock_logger ();
|
|
|
|
}
|
|
|
|
|
2012-06-07 11:57:59 -04:00
|
|
|
static void __vsnprintf(char *str, size_t size, const char *format, va_list ap) {
|
|
|
|
int in_block = 0;
|
|
|
|
int block_size = 0;
|
|
|
|
int block_len;
|
2013-04-02 08:17:43 -04:00
|
|
|
int block_space = 0;
|
2012-06-07 11:57:59 -04:00
|
|
|
const char * arg;
|
|
|
|
char buf[80];
|
|
|
|
|
|
|
|
for (; *format && size; format++)
|
|
|
|
{
|
|
|
|
if ( !in_block )
|
|
|
|
{
|
|
|
|
if ( *format == '%' ) {
|
|
|
|
in_block = 1;
|
|
|
|
block_size = 0;
|
|
|
|
block_len = 0;
|
2013-04-02 08:17:43 -04:00
|
|
|
block_space = 0;
|
2012-06-07 11:57:59 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*(str++) = *format;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO: %l*[sdupi] as well as %.4080s and "%.*s
|
|
|
|
arg = NULL;
|
|
|
|
switch (*format)
|
|
|
|
{
|
|
|
|
case 'l':
|
|
|
|
block_size++;
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
// just ignore '.'. If somebody cares: fix it.
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
block_len = va_arg(ap, int);
|
|
|
|
break;
|
2013-04-02 08:17:43 -04:00
|
|
|
case ' ':
|
|
|
|
block_space = 1;
|
|
|
|
break;
|
2012-06-07 11:57:59 -04:00
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
block_len = atoi(format);
|
|
|
|
for (; *format >= '0' && *format <= '9'; format++);
|
2012-06-07 14:27:58 -04:00
|
|
|
format--;
|
2012-06-07 11:57:59 -04:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
snprintf(buf, sizeof(buf), "%p", (void*)va_arg(ap, void *));
|
|
|
|
arg = buf;
|
|
|
|
case 'd':
|
|
|
|
case 'i':
|
|
|
|
case 'u':
|
|
|
|
if (!arg)
|
|
|
|
{
|
|
|
|
switch (block_size)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
if (*format == 'u')
|
|
|
|
snprintf(buf, sizeof(buf), "%u", (unsigned int)va_arg(ap, unsigned int));
|
|
|
|
else
|
|
|
|
snprintf(buf, sizeof(buf), "%i", (int)va_arg(ap, int));
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (*format == 'u')
|
|
|
|
snprintf(buf, sizeof(buf), "%lu", (unsigned long int)va_arg(ap, unsigned long int));
|
|
|
|
else
|
|
|
|
snprintf(buf, sizeof(buf), "%li", (long int)va_arg(ap, long int));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (*format == 'u')
|
|
|
|
snprintf(buf, sizeof(buf), "%llu", (unsigned long long int)va_arg(ap, unsigned long long int));
|
|
|
|
else
|
|
|
|
snprintf(buf, sizeof(buf), "%lli", (long long int)va_arg(ap, long long int));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
snprintf(buf, sizeof(buf), "<<<invalid>>>");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
arg = buf;
|
|
|
|
}
|
|
|
|
case 's':
|
|
|
|
case 'H':
|
|
|
|
// TODO.
|
|
|
|
if (!arg)
|
|
|
|
arg = va_arg(ap, const char *);
|
|
|
|
if (!arg)
|
|
|
|
arg = "(null)";
|
|
|
|
if (!block_len)
|
|
|
|
block_len = strlen(arg);
|
|
|
|
|
|
|
|
// the if() is the outer structure so the inner for()
|
|
|
|
// is branch optimized.
|
|
|
|
if (*format == 'H' )
|
|
|
|
{
|
2012-06-07 14:13:28 -04:00
|
|
|
for (; *arg && block_len && size; arg++, size--, block_len--)
|
2012-06-07 11:57:59 -04:00
|
|
|
{
|
2013-04-02 08:17:43 -04:00
|
|
|
if ((*arg <= '"' || *arg == '`' || *arg == '\\') && !(block_space && *arg == ' '))
|
2012-06-07 11:57:59 -04:00
|
|
|
*(str++) = '.';
|
|
|
|
else
|
|
|
|
*(str++) = *arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-07 14:13:28 -04:00
|
|
|
for (; *arg && block_len && size; arg++, size--, block_len--)
|
2012-06-07 11:57:59 -04:00
|
|
|
*(str++) = *arg;
|
|
|
|
}
|
|
|
|
in_block = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !size )
|
|
|
|
str--;
|
|
|
|
|
|
|
|
*str = 0;
|
|
|
|
}
|
2006-03-26 21:26:06 -05: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
|
|
|
{
|
2012-06-07 11:57:59 -04:00
|
|
|
static const char *prior[] = { "EROR", "WARN", "INFO", "DBUG" };
|
2006-03-26 21:26:06 -05:00
|
|
|
int datelen;
|
2008-01-23 22:28:11 -05:00
|
|
|
time_t now;
|
2003-03-14 21:10:19 -05:00
|
|
|
char pre[256];
|
|
|
|
char line[LOG_MAXLINELEN];
|
|
|
|
va_list ap;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2006-03-07 14:17:06 -05:00
|
|
|
if (log_id < 0 || log_id >= LOG_MAXLOGS) return; /* Bad log number */
|
2003-03-14 21:10:19 -05:00
|
|
|
if (loglist[log_id].level < priority) return;
|
2012-07-31 19:09:42 -04:00
|
|
|
if (!priority || priority > sizeof(prior)/sizeof(prior[0])) return; /* Bad priority */
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2012-06-07 11:57:59 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
va_start(ap, fmt);
|
2012-06-07 11:57:59 -04:00
|
|
|
__vsnprintf(line, sizeof(line), fmt, ap);
|
|
|
|
va_end(ap);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
now = time(NULL);
|
2006-03-26 21:26:06 -05:00
|
|
|
datelen = strftime (pre, sizeof (pre), "[%Y-%m-%d %H:%M:%S]", localtime(&now));
|
|
|
|
snprintf (pre+datelen, sizeof (pre)-datelen, " %s %s%s ", prior [priority-1], cat, func);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2012-06-07 11:57:59 -04:00
|
|
|
_lock_logger();
|
2008-01-23 22:28:11 -05:00
|
|
|
if (_log_open (log_id))
|
2003-07-08 22:45:22 -04:00
|
|
|
{
|
2006-03-26 21:26:06 -05:00
|
|
|
int len = create_log_entry (log_id, pre, line);
|
2003-07-08 22:45:22 -04:00
|
|
|
if (len > 0)
|
|
|
|
loglist[log_id].size += len;
|
|
|
|
}
|
|
|
|
_unlock_logger();
|
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
|
|
|
va_list ap;
|
2005-10-24 10:52:35 -04:00
|
|
|
time_t now;
|
2009-01-07 21:18:11 -05:00
|
|
|
char line[LOG_MAXLINELEN];
|
2001-09-09 22:24:39 -04:00
|
|
|
|
2006-03-07 14:17:06 -05:00
|
|
|
if (log_id < 0 || log_id >= LOG_MAXLOGS) return;
|
2003-03-14 21:10:19 -05:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2003-07-08 22:45:22 -04:00
|
|
|
|
2005-10-24 10:52:35 -04:00
|
|
|
now = time(NULL);
|
|
|
|
|
2003-07-08 22:45:22 -04:00
|
|
|
_lock_logger();
|
2013-01-02 09:44:43 -05:00
|
|
|
__vsnprintf(line, LOG_MAXLINELEN, fmt, ap);
|
2008-01-23 22:28:11 -05:00
|
|
|
if (_log_open (log_id))
|
2003-07-08 22:45:22 -04:00
|
|
|
{
|
2006-03-26 21:26:06 -05:00
|
|
|
int len = create_log_entry (log_id, "", line);
|
2003-07-08 22:45:22 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2005-12-17 07:23:54 -05:00
|
|
|
static int _get_log_id(void)
|
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
|
|
|
}
|
|
|
|
|
2005-12-17 07:23:54 -05:00
|
|
|
static void _lock_logger(void)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
2005-12-17 07:23:54 -05:00
|
|
|
static void _unlock_logger(void)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
|
|
|
#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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|