1999-12-05 07:28:29 +00:00
|
|
|
/*
|
2002-02-11 04:25:04 +00:00
|
|
|
* Asterisk Logger
|
1999-12-05 07:28:29 +00:00
|
|
|
*
|
|
|
|
* Mark Spencer <markster@marko.net>
|
|
|
|
*
|
2002-02-11 04:25:04 +00:00
|
|
|
* Copyright(C)1999, Linux Support Services, Inc.
|
1999-12-05 07:28:29 +00:00
|
|
|
*
|
|
|
|
* Distributed under the terms of the GNU General Public License (GPL) Version 2
|
|
|
|
*
|
|
|
|
* Logging routines
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-11-26 22:00:07 +00:00
|
|
|
#include <signal.h>
|
1999-12-05 07:28:29 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
2005-04-22 13:11:34 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#define SYSLOG_NAMES /* so we can map syslog facilities names to their numeric values,
|
|
|
|
from <syslog.h> which is included by logger.h */
|
|
|
|
#include <syslog.h>
|
|
|
|
|
2005-06-06 20:27:51 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
2005-06-06 22:12:19 +00:00
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
2005-06-06 20:27:51 +00:00
|
|
|
|
2004-01-12 05:05:35 +00:00
|
|
|
static int syslog_level_map[] = {
|
|
|
|
LOG_DEBUG,
|
|
|
|
LOG_INFO, /* arbitrary equivalent of LOG_EVENT */
|
|
|
|
LOG_NOTICE,
|
|
|
|
LOG_WARNING,
|
2004-01-27 00:23:39 +00:00
|
|
|
LOG_ERR,
|
2005-08-22 21:19:59 +00:00
|
|
|
LOG_DEBUG,
|
2004-06-29 04:42:19 +00:00
|
|
|
LOG_DEBUG
|
2004-01-12 05:05:35 +00:00
|
|
|
};
|
2004-06-29 04:42:19 +00:00
|
|
|
|
2004-01-27 00:23:39 +00:00
|
|
|
#define SYSLOG_NLEVELS 6
|
2004-01-12 05:05:35 +00:00
|
|
|
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/logger.h"
|
2005-08-31 02:43:44 +00:00
|
|
|
#include "asterisk/lock.h"
|
|
|
|
#include "asterisk/options.h"
|
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/config.h"
|
|
|
|
#include "asterisk/term.h"
|
|
|
|
#include "asterisk/cli.h"
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
#include "asterisk/manager.h"
|
2004-01-12 05:05:35 +00:00
|
|
|
|
1999-12-05 07:28:29 +00:00
|
|
|
#define MAX_MSG_QUEUE 200
|
|
|
|
|
2004-11-15 00:35:49 +00:00
|
|
|
#if defined(__linux__) && defined(__NR_gettid)
|
2004-11-01 16:26:32 +00:00
|
|
|
#include <asm/unistd.h>
|
|
|
|
#define GETTID() syscall(__NR_gettid)
|
|
|
|
#else
|
|
|
|
#define GETTID() getpid()
|
|
|
|
#endif
|
|
|
|
|
2004-05-18 05:47:27 +00:00
|
|
|
static char dateformat[256] = "%b %e %T"; /* Original Asterisk Format */
|
2005-03-31 19:08:51 +00:00
|
|
|
|
2004-06-09 01:45:08 +00:00
|
|
|
AST_MUTEX_DEFINE_STATIC(msglist_lock);
|
|
|
|
AST_MUTEX_DEFINE_STATIC(loglock);
|
2005-07-05 20:31:34 +00:00
|
|
|
static int filesize_reload_needed = 0;
|
2004-11-01 01:48:35 +00:00
|
|
|
static int global_logmask = -1;
|
1999-12-05 07:28:29 +00:00
|
|
|
|
2005-03-11 08:49:01 +00:00
|
|
|
static struct {
|
|
|
|
unsigned int queue_log:1;
|
|
|
|
unsigned int event_log:1;
|
|
|
|
} logfiles = { 1, 1 };
|
|
|
|
|
1999-12-05 07:28:29 +00:00
|
|
|
static struct msglist {
|
|
|
|
char *msg;
|
|
|
|
struct msglist *next;
|
|
|
|
} *list = NULL, *last = NULL;
|
|
|
|
|
2005-05-08 16:44:25 +00:00
|
|
|
static char hostname[MAXHOSTNAMELEN];
|
2004-09-05 03:48:05 +00:00
|
|
|
|
2005-03-31 19:08:51 +00:00
|
|
|
enum logtypes {
|
|
|
|
LOGTYPE_SYSLOG,
|
|
|
|
LOGTYPE_FILE,
|
|
|
|
LOGTYPE_CONSOLE,
|
|
|
|
};
|
|
|
|
|
2004-01-12 05:05:35 +00:00
|
|
|
struct logchannel {
|
2005-03-31 19:08:51 +00:00
|
|
|
int logmask; /* What to log to this channel */
|
|
|
|
int disabled; /* If this channel is disabled or not */
|
|
|
|
int facility; /* syslog facility */
|
|
|
|
enum logtypes type; /* Type of log channel */
|
|
|
|
FILE *fileptr; /* logfile logging file pointer */
|
|
|
|
char filename[256]; /* Filename */
|
|
|
|
struct logchannel *next; /* Next channel in chain */
|
2002-02-11 04:25:04 +00:00
|
|
|
};
|
|
|
|
|
2004-01-12 05:05:35 +00:00
|
|
|
static struct logchannel *logchannels = NULL;
|
2002-02-11 04:25:04 +00:00
|
|
|
|
1999-12-05 07:28:29 +00:00
|
|
|
static int msgcnt = 0;
|
|
|
|
|
|
|
|
static FILE *eventlog = NULL;
|
|
|
|
|
|
|
|
static char *levels[] = {
|
2004-06-29 04:42:19 +00:00
|
|
|
"DEBUG",
|
|
|
|
"EVENT",
|
|
|
|
"NOTICE",
|
|
|
|
"WARNING",
|
|
|
|
"ERROR",
|
2005-08-22 21:19:59 +00:00
|
|
|
"VERBOSE",
|
|
|
|
"DTMF"
|
1999-12-05 07:28:29 +00:00
|
|
|
};
|
|
|
|
|
2002-06-21 01:40:13 +00:00
|
|
|
static int colors[] = {
|
2004-06-29 04:42:19 +00:00
|
|
|
COLOR_BRGREEN,
|
|
|
|
COLOR_BRBLUE,
|
|
|
|
COLOR_YELLOW,
|
|
|
|
COLOR_BRRED,
|
|
|
|
COLOR_RED,
|
2005-08-22 21:19:59 +00:00
|
|
|
COLOR_GREEN,
|
|
|
|
COLOR_BRGREEN
|
2002-06-21 01:40:13 +00:00
|
|
|
};
|
|
|
|
|
2002-02-11 04:25:04 +00:00
|
|
|
static int make_components(char *s, int lineno)
|
|
|
|
{
|
|
|
|
char *w;
|
|
|
|
int res = 0;
|
2003-01-30 15:03:20 +00:00
|
|
|
char *stringp=NULL;
|
|
|
|
stringp=s;
|
|
|
|
w = strsep(&stringp, ",");
|
2002-02-11 04:25:04 +00:00
|
|
|
while(w) {
|
2005-03-31 19:08:51 +00:00
|
|
|
while(*w && (*w < 33))
|
|
|
|
w++;
|
|
|
|
if (!strcasecmp(w, "error"))
|
|
|
|
res |= (1 << __LOG_ERROR);
|
|
|
|
else if (!strcasecmp(w, "warning"))
|
|
|
|
res |= (1 << __LOG_WARNING);
|
|
|
|
else if (!strcasecmp(w, "notice"))
|
|
|
|
res |= (1 << __LOG_NOTICE);
|
|
|
|
else if (!strcasecmp(w, "event"))
|
|
|
|
res |= (1 << __LOG_EVENT);
|
|
|
|
else if (!strcasecmp(w, "debug"))
|
|
|
|
res |= (1 << __LOG_DEBUG);
|
|
|
|
else if (!strcasecmp(w, "verbose"))
|
|
|
|
res |= (1 << __LOG_VERBOSE);
|
2005-08-22 21:19:59 +00:00
|
|
|
else if (!strcasecmp(w, "dtmf"))
|
|
|
|
res |= (1 << __LOG_DTMF);
|
2005-03-31 19:08:51 +00:00
|
|
|
else {
|
|
|
|
fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
|
|
|
|
}
|
|
|
|
w = strsep(&stringp, ",");
|
2002-02-11 04:25:04 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-01-12 05:05:35 +00:00
|
|
|
static struct logchannel *make_logchannel(char *channel, char *components, int lineno)
|
2002-02-11 04:25:04 +00:00
|
|
|
{
|
2004-01-12 05:05:35 +00:00
|
|
|
struct logchannel *chan;
|
|
|
|
char *facility;
|
2004-12-14 23:36:30 +00:00
|
|
|
#ifndef SOLARIS
|
2004-01-12 05:05:35 +00:00
|
|
|
CODE *cptr;
|
2004-12-14 23:36:30 +00:00
|
|
|
#endif
|
2004-01-12 05:05:35 +00:00
|
|
|
|
2004-05-06 20:23:48 +00:00
|
|
|
if (ast_strlen_zero(channel))
|
2002-02-11 04:25:04 +00:00
|
|
|
return NULL;
|
2004-01-12 05:05:35 +00:00
|
|
|
chan = malloc(sizeof(struct logchannel));
|
|
|
|
|
2005-04-01 16:09:21 +00:00
|
|
|
if (!chan) /* Can't allocate memory */
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(chan, 0, sizeof(struct logchannel));
|
|
|
|
if (!strcasecmp(channel, "console")) {
|
|
|
|
chan->type = LOGTYPE_CONSOLE;
|
|
|
|
} else if (!strncasecmp(channel, "syslog", 6)) {
|
|
|
|
/*
|
|
|
|
* syntax is:
|
|
|
|
* syslog.facility => level,level,level
|
|
|
|
*/
|
|
|
|
facility = strchr(channel, '.');
|
|
|
|
if(!facility++ || !facility) {
|
|
|
|
facility = "local0";
|
|
|
|
}
|
2004-12-14 23:36:30 +00:00
|
|
|
|
|
|
|
#ifndef SOLARIS
|
2005-04-01 16:09:21 +00:00
|
|
|
/*
|
|
|
|
* Walk through the list of facilitynames (defined in sys/syslog.h)
|
|
|
|
* to see if we can find the one we have been given
|
|
|
|
*/
|
|
|
|
chan->facility = -1;
|
|
|
|
cptr = facilitynames;
|
|
|
|
while (cptr->c_name) {
|
|
|
|
if (!strcasecmp(facility, cptr->c_name)) {
|
|
|
|
chan->facility = cptr->c_val;
|
|
|
|
break;
|
2004-01-12 05:05:35 +00:00
|
|
|
}
|
2005-04-01 16:09:21 +00:00
|
|
|
cptr++;
|
|
|
|
}
|
2004-12-14 23:36:30 +00:00
|
|
|
#else
|
2005-04-01 16:09:21 +00:00
|
|
|
chan->facility = -1;
|
|
|
|
if (!strcasecmp(facility, "kern"))
|
|
|
|
chan->facility = LOG_KERN;
|
|
|
|
else if (!strcasecmp(facility, "USER"))
|
|
|
|
chan->facility = LOG_USER;
|
|
|
|
else if (!strcasecmp(facility, "MAIL"))
|
|
|
|
chan->facility = LOG_MAIL;
|
|
|
|
else if (!strcasecmp(facility, "DAEMON"))
|
|
|
|
chan->facility = LOG_DAEMON;
|
|
|
|
else if (!strcasecmp(facility, "AUTH"))
|
|
|
|
chan->facility = LOG_AUTH;
|
|
|
|
else if (!strcasecmp(facility, "SYSLOG"))
|
|
|
|
chan->facility = LOG_SYSLOG;
|
|
|
|
else if (!strcasecmp(facility, "LPR"))
|
|
|
|
chan->facility = LOG_LPR;
|
|
|
|
else if (!strcasecmp(facility, "NEWS"))
|
|
|
|
chan->facility = LOG_NEWS;
|
|
|
|
else if (!strcasecmp(facility, "UUCP"))
|
|
|
|
chan->facility = LOG_UUCP;
|
|
|
|
else if (!strcasecmp(facility, "CRON"))
|
|
|
|
chan->facility = LOG_CRON;
|
|
|
|
else if (!strcasecmp(facility, "LOCAL0"))
|
|
|
|
chan->facility = LOG_LOCAL0;
|
|
|
|
else if (!strcasecmp(facility, "LOCAL1"))
|
|
|
|
chan->facility = LOG_LOCAL1;
|
|
|
|
else if (!strcasecmp(facility, "LOCAL2"))
|
|
|
|
chan->facility = LOG_LOCAL2;
|
|
|
|
else if (!strcasecmp(facility, "LOCAL3"))
|
|
|
|
chan->facility = LOG_LOCAL3;
|
|
|
|
else if (!strcasecmp(facility, "LOCAL4"))
|
|
|
|
chan->facility = LOG_LOCAL4;
|
|
|
|
else if (!strcasecmp(facility, "LOCAL5"))
|
|
|
|
chan->facility = LOG_LOCAL5;
|
|
|
|
else if (!strcasecmp(facility, "LOCAL6"))
|
|
|
|
chan->facility = LOG_LOCAL6;
|
|
|
|
else if (!strcasecmp(facility, "LOCAL7"))
|
|
|
|
chan->facility = LOG_LOCAL7;
|
2005-03-31 19:08:51 +00:00
|
|
|
#endif /* Solaris */
|
|
|
|
|
2005-04-01 16:09:21 +00:00
|
|
|
if (0 > chan->facility) {
|
|
|
|
fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
|
|
|
|
free(chan);
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-01-12 05:05:35 +00:00
|
|
|
|
2005-04-01 16:09:21 +00:00
|
|
|
chan->type = LOGTYPE_SYSLOG;
|
|
|
|
snprintf(chan->filename, sizeof(chan->filename), "%s", channel);
|
|
|
|
openlog("asterisk", LOG_PID, chan->facility);
|
|
|
|
} else {
|
|
|
|
if (channel[0] == '/') {
|
|
|
|
if(!ast_strlen_zero(hostname)) {
|
|
|
|
snprintf(chan->filename, sizeof(chan->filename) - 1,"%s.%s", channel, hostname);
|
2004-09-05 03:48:05 +00:00
|
|
|
} else {
|
2005-07-10 22:56:21 +00:00
|
|
|
ast_copy_string(chan->filename, channel, sizeof(chan->filename));
|
2004-09-05 03:48:05 +00:00
|
|
|
}
|
2005-04-01 16:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!ast_strlen_zero(hostname)) {
|
|
|
|
snprintf(chan->filename, sizeof(chan->filename), "%s/%s.%s",(char *)ast_config_AST_LOG_DIR, channel, hostname);
|
|
|
|
} else {
|
|
|
|
snprintf(chan->filename, sizeof(chan->filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, channel);
|
2002-02-11 04:25:04 +00:00
|
|
|
}
|
2005-04-01 16:09:21 +00:00
|
|
|
chan->fileptr = fopen(chan->filename, "a");
|
|
|
|
if (!chan->fileptr) {
|
|
|
|
/* Can't log here, since we're called with a lock */
|
|
|
|
fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
|
|
|
|
}
|
|
|
|
chan->type = LOGTYPE_FILE;
|
2002-02-11 04:25:04 +00:00
|
|
|
}
|
2005-04-01 16:09:21 +00:00
|
|
|
chan->logmask = make_components(components, lineno);
|
2004-01-12 05:05:35 +00:00
|
|
|
return chan;
|
2002-02-11 04:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void init_logger_chain(void)
|
|
|
|
{
|
2004-01-12 05:05:35 +00:00
|
|
|
struct logchannel *chan, *cur;
|
2002-02-11 04:25:04 +00:00
|
|
|
struct ast_config *cfg;
|
|
|
|
struct ast_variable *var;
|
2004-05-18 05:47:27 +00:00
|
|
|
char *s;
|
2002-02-11 04:25:04 +00:00
|
|
|
|
2004-01-12 05:05:35 +00:00
|
|
|
/* delete our list of log channels */
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_lock(&loglock);
|
2004-01-12 05:05:35 +00:00
|
|
|
chan = logchannels;
|
|
|
|
while (chan) {
|
2005-03-31 19:08:51 +00:00
|
|
|
cur = chan->next;
|
|
|
|
free(chan);
|
|
|
|
chan = cur;
|
2002-02-11 04:25:04 +00:00
|
|
|
}
|
2004-01-12 05:05:35 +00:00
|
|
|
logchannels = NULL;
|
|
|
|
ast_mutex_unlock(&loglock);
|
2004-09-05 03:48:05 +00:00
|
|
|
|
2004-10-24 06:12:21 +00:00
|
|
|
global_logmask = 0;
|
2004-01-12 05:05:35 +00:00
|
|
|
/* close syslog */
|
|
|
|
closelog();
|
2004-09-05 03:48:05 +00:00
|
|
|
|
2005-01-25 06:10:20 +00:00
|
|
|
cfg = ast_config_load("logger.conf");
|
2002-02-11 04:25:04 +00:00
|
|
|
|
|
|
|
/* If no config file, we're fine */
|
2004-01-12 05:05:35 +00:00
|
|
|
if (!cfg)
|
2005-03-31 19:08:51 +00:00
|
|
|
return;
|
2004-09-05 03:48:05 +00:00
|
|
|
|
2004-01-12 05:05:35 +00:00
|
|
|
ast_mutex_lock(&loglock);
|
2004-09-05 03:48:05 +00:00
|
|
|
if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
|
|
|
|
if(ast_true(s)) {
|
2005-05-08 16:44:25 +00:00
|
|
|
if(gethostname(hostname, sizeof(hostname)-1)) {
|
2005-07-10 22:56:21 +00:00
|
|
|
ast_copy_string(hostname, "unknown", sizeof(hostname));
|
2004-09-05 03:48:05 +00:00
|
|
|
ast_log(LOG_WARNING, "What box has no hostname???\n");
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
hostname[0] = '\0';
|
|
|
|
} else
|
|
|
|
hostname[0] = '\0';
|
2004-05-18 05:47:27 +00:00
|
|
|
if ((s = ast_variable_retrieve(cfg, "general", "dateformat"))) {
|
2005-07-10 22:56:21 +00:00
|
|
|
ast_copy_string(dateformat, s, sizeof(dateformat));
|
2004-09-05 03:48:05 +00:00
|
|
|
} else
|
2005-07-10 22:56:21 +00:00
|
|
|
ast_copy_string(dateformat, "%b %e %T", sizeof(dateformat));
|
2005-03-11 08:49:01 +00:00
|
|
|
if ((s = ast_variable_retrieve(cfg, "general", "queue_log"))) {
|
|
|
|
logfiles.queue_log = ast_true(s);
|
|
|
|
}
|
|
|
|
if ((s = ast_variable_retrieve(cfg, "general", "event_log"))) {
|
|
|
|
logfiles.event_log = ast_true(s);
|
|
|
|
}
|
|
|
|
|
2002-02-11 04:25:04 +00:00
|
|
|
var = ast_variable_browse(cfg, "logfiles");
|
|
|
|
while(var) {
|
2004-01-12 05:05:35 +00:00
|
|
|
chan = make_logchannel(var->name, var->value, var->lineno);
|
|
|
|
if (chan) {
|
|
|
|
chan->next = logchannels;
|
|
|
|
logchannels = chan;
|
2004-10-24 06:12:21 +00:00
|
|
|
global_logmask |= chan->logmask;
|
2002-02-11 04:25:04 +00:00
|
|
|
}
|
|
|
|
var = var->next;
|
|
|
|
}
|
2004-01-12 05:05:35 +00:00
|
|
|
|
2005-01-25 06:10:20 +00:00
|
|
|
ast_config_destroy(cfg);
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&loglock);
|
2002-02-11 04:25:04 +00:00
|
|
|
}
|
|
|
|
|
2004-02-12 22:11:02 +00:00
|
|
|
static FILE *qlog = NULL;
|
2004-06-09 01:45:08 +00:00
|
|
|
AST_MUTEX_DEFINE_STATIC(qloglock);
|
2004-02-12 22:11:02 +00:00
|
|
|
|
|
|
|
void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
ast_mutex_lock(&qloglock);
|
|
|
|
if (qlog) {
|
|
|
|
va_start(ap, fmt);
|
|
|
|
fprintf(qlog, "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
|
|
|
|
vfprintf(qlog, fmt, ap);
|
|
|
|
fprintf(qlog, "\n");
|
|
|
|
va_end(ap);
|
|
|
|
fflush(qlog);
|
|
|
|
}
|
|
|
|
ast_mutex_unlock(&qloglock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void queue_log_init(void)
|
|
|
|
{
|
|
|
|
char filename[256];
|
|
|
|
int reloaded = 0;
|
2005-03-31 19:08:51 +00:00
|
|
|
|
2004-02-12 22:11:02 +00:00
|
|
|
ast_mutex_lock(&qloglock);
|
|
|
|
if (qlog) {
|
|
|
|
reloaded = 1;
|
|
|
|
fclose(qlog);
|
|
|
|
qlog = NULL;
|
|
|
|
}
|
|
|
|
snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, "queue_log");
|
2005-03-11 08:49:01 +00:00
|
|
|
if (logfiles.queue_log) {
|
|
|
|
qlog = fopen(filename, "a");
|
|
|
|
}
|
2004-02-12 22:11:02 +00:00
|
|
|
ast_mutex_unlock(&qloglock);
|
|
|
|
if (reloaded)
|
2004-02-27 04:32:54 +00:00
|
|
|
ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
|
2004-02-12 22:11:02 +00:00
|
|
|
else
|
2004-02-27 04:32:54 +00:00
|
|
|
ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
|
2004-02-12 22:11:02 +00:00
|
|
|
}
|
|
|
|
|
2003-11-26 22:00:07 +00:00
|
|
|
int reload_logger(int rotate)
|
1999-12-05 07:28:29 +00:00
|
|
|
{
|
2004-07-14 07:44:19 +00:00
|
|
|
char old[AST_CONFIG_MAX_PATH] = "";
|
2003-11-26 22:00:07 +00:00
|
|
|
char new[AST_CONFIG_MAX_PATH];
|
2004-01-12 05:05:35 +00:00
|
|
|
struct logchannel *f;
|
2003-12-05 00:50:42 +00:00
|
|
|
FILE *myf;
|
2003-11-26 22:00:07 +00:00
|
|
|
int x;
|
2005-03-31 19:08:51 +00:00
|
|
|
|
2003-10-02 06:40:10 +00:00
|
|
|
ast_mutex_lock(&loglock);
|
2003-11-26 22:00:07 +00:00
|
|
|
if (eventlog)
|
2004-01-12 05:05:35 +00:00
|
|
|
fclose(eventlog);
|
2003-11-26 22:00:07 +00:00
|
|
|
else
|
2004-01-12 05:05:35 +00:00
|
|
|
rotate = 0;
|
2003-12-05 00:50:42 +00:00
|
|
|
eventlog = NULL;
|
2003-11-26 22:00:07 +00:00
|
|
|
|
2003-01-30 15:03:20 +00:00
|
|
|
mkdir((char *)ast_config_AST_LOG_DIR, 0755);
|
2003-11-26 22:00:07 +00:00
|
|
|
snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
|
|
|
|
|
2005-03-11 08:49:01 +00:00
|
|
|
if (logfiles.event_log) {
|
|
|
|
if (rotate) {
|
|
|
|
for (x=0;;x++) {
|
|
|
|
snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
|
|
|
|
myf = fopen((char *)new, "r");
|
2005-03-31 19:08:51 +00:00
|
|
|
if (myf) /* File exists */
|
2005-03-11 08:49:01 +00:00
|
|
|
fclose(myf);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2003-12-05 00:50:42 +00:00
|
|
|
|
2005-03-11 08:49:01 +00:00
|
|
|
/* do it */
|
|
|
|
if (rename(old,new))
|
|
|
|
fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
|
|
|
|
}
|
2003-11-26 22:00:07 +00:00
|
|
|
|
2005-03-11 08:49:01 +00:00
|
|
|
eventlog = fopen(old, "a");
|
|
|
|
}
|
2003-11-26 22:00:07 +00:00
|
|
|
|
2004-01-12 05:05:35 +00:00
|
|
|
f = logchannels;
|
2003-11-26 22:00:07 +00:00
|
|
|
while(f) {
|
2005-03-31 19:08:51 +00:00
|
|
|
if (f->disabled) {
|
|
|
|
f->disabled = 0; /* Re-enable logging at reload */
|
|
|
|
manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
|
|
|
|
}
|
2004-01-12 05:05:35 +00:00
|
|
|
if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
|
2005-03-31 19:08:51 +00:00
|
|
|
fclose(f->fileptr); /* Close file */
|
2004-01-12 05:05:35 +00:00
|
|
|
f->fileptr = NULL;
|
|
|
|
if(rotate) {
|
2005-07-10 22:56:21 +00:00
|
|
|
ast_copy_string(old, f->filename, sizeof(old));
|
2003-12-05 00:50:42 +00:00
|
|
|
|
2004-01-12 05:05:35 +00:00
|
|
|
for(x=0;;x++) {
|
|
|
|
snprintf(new, sizeof(new), "%s.%d", f->filename, x);
|
|
|
|
myf = fopen((char *)new, "r");
|
|
|
|
if (myf) {
|
|
|
|
fclose(myf);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-11-26 22:00:07 +00:00
|
|
|
|
2004-01-12 05:05:35 +00:00
|
|
|
/* do it */
|
|
|
|
if (rename(old,new))
|
|
|
|
fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f = f->next;
|
2003-11-26 22:00:07 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 06:40:10 +00:00
|
|
|
ast_mutex_unlock(&loglock);
|
|
|
|
|
2005-07-05 20:31:34 +00:00
|
|
|
filesize_reload_needed = 0;
|
|
|
|
|
2004-02-23 20:18:27 +00:00
|
|
|
queue_log_init();
|
2002-02-11 04:25:04 +00:00
|
|
|
init_logger_chain();
|
2005-03-11 08:49:01 +00:00
|
|
|
|
|
|
|
if (logfiles.event_log) {
|
|
|
|
if (eventlog) {
|
|
|
|
ast_log(LOG_EVENT, "Restarted Asterisk Event Logger\n");
|
|
|
|
if (option_verbose)
|
|
|
|
ast_verbose("Asterisk Event Logger restarted\n");
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
|
2005-08-15 19:35:25 +00:00
|
|
|
} else
|
|
|
|
return 0;
|
2002-02-11 04:25:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-10-02 06:40:10 +00:00
|
|
|
static int handle_logger_reload(int fd, int argc, char *argv[])
|
|
|
|
{
|
2005-03-31 19:08:51 +00:00
|
|
|
if(reload_logger(0)) {
|
2004-12-18 22:04:07 +00:00
|
|
|
ast_cli(fd, "Failed to reload the logger\n");
|
2003-11-26 22:00:07 +00:00
|
|
|
return RESULT_FAILURE;
|
2005-03-31 19:08:51 +00:00
|
|
|
} else
|
2003-11-26 22:00:07 +00:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_logger_rotate(int fd, int argc, char *argv[])
|
|
|
|
{
|
2005-03-31 19:08:51 +00:00
|
|
|
if(reload_logger(1)) {
|
2004-12-18 22:04:07 +00:00
|
|
|
ast_cli(fd, "Failed to reload the logger and rotate log files\n");
|
2003-10-02 06:40:10 +00:00
|
|
|
return RESULT_FAILURE;
|
2005-03-31 19:08:51 +00:00
|
|
|
} else
|
2003-10-02 06:40:10 +00:00
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-03-31 19:08:51 +00:00
|
|
|
/*--- handle_logger_show_channels: CLI command to show logging system
|
|
|
|
configuration */
|
|
|
|
static int handle_logger_show_channels(int fd, int argc, char *argv[])
|
|
|
|
{
|
|
|
|
#define FORMATL "%-35.35s %-8.8s %-9.9s "
|
|
|
|
struct logchannel *chan;
|
|
|
|
|
|
|
|
ast_mutex_lock(&loglock);
|
|
|
|
|
|
|
|
chan = logchannels;
|
|
|
|
ast_cli(fd,FORMATL, "Channel", "Type", "Status");
|
|
|
|
ast_cli(fd, "Configuration\n");
|
|
|
|
ast_cli(fd,FORMATL, "-------", "----", "------");
|
|
|
|
ast_cli(fd, "-------------\n");
|
|
|
|
while (chan) {
|
|
|
|
ast_cli(fd, FORMATL, chan->filename, chan->type==LOGTYPE_CONSOLE ? "Console" : (chan->type==LOGTYPE_SYSLOG ? "Syslog" : "File"),
|
|
|
|
chan->disabled ? "Disabled" : "Enabled");
|
|
|
|
ast_cli(fd, " - ");
|
|
|
|
if (chan->logmask & (1 << __LOG_DEBUG))
|
|
|
|
ast_cli(fd, "Debug ");
|
2005-08-22 21:19:59 +00:00
|
|
|
if (chan->logmask & (1 << __LOG_DTMF))
|
|
|
|
ast_cli(fd, "DTMF ");
|
2005-03-31 19:08:51 +00:00
|
|
|
if (chan->logmask & (1 << __LOG_VERBOSE))
|
|
|
|
ast_cli(fd, "Verbose ");
|
|
|
|
if (chan->logmask & (1 << __LOG_WARNING))
|
|
|
|
ast_cli(fd, "Warning ");
|
|
|
|
if (chan->logmask & (1 << __LOG_NOTICE))
|
|
|
|
ast_cli(fd, "Notice ");
|
|
|
|
if (chan->logmask & (1 << __LOG_ERROR))
|
|
|
|
ast_cli(fd, "Error ");
|
|
|
|
if (chan->logmask & (1 << __LOG_EVENT))
|
|
|
|
ast_cli(fd, "Event ");
|
|
|
|
ast_cli(fd, "\n");
|
|
|
|
chan = chan->next;
|
|
|
|
}
|
|
|
|
ast_cli(fd, "\n");
|
|
|
|
|
|
|
|
ast_mutex_unlock(&loglock);
|
|
|
|
|
|
|
|
return RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2003-10-02 06:40:10 +00:00
|
|
|
static struct verb {
|
|
|
|
void (*verboser)(const char *string, int opos, int replacelast, int complete);
|
|
|
|
struct verb *next;
|
|
|
|
} *verboser = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
static char logger_reload_help[] =
|
|
|
|
"Usage: logger reload\n"
|
2005-03-31 19:08:51 +00:00
|
|
|
" Reloads the logger subsystem state. Use after restarting syslogd(8) if you are using syslog logging.\n";
|
2003-11-26 22:00:07 +00:00
|
|
|
|
|
|
|
static char logger_rotate_help[] =
|
2004-04-22 02:57:10 +00:00
|
|
|
"Usage: logger rotate\n"
|
2003-11-26 22:00:07 +00:00
|
|
|
" Rotates and Reopens the log files.\n";
|
|
|
|
|
2005-03-31 19:08:51 +00:00
|
|
|
static char logger_show_channels_help[] =
|
|
|
|
"Usage: logger show channels\n"
|
|
|
|
" Show configured logger channels.\n";
|
|
|
|
|
|
|
|
static struct ast_cli_entry logger_show_channels_cli =
|
|
|
|
{ { "logger", "show", "channels", NULL },
|
|
|
|
handle_logger_show_channels, "List configured log channels",
|
|
|
|
logger_show_channels_help };
|
|
|
|
|
2003-10-02 06:40:10 +00:00
|
|
|
static struct ast_cli_entry reload_logger_cli =
|
|
|
|
{ { "logger", "reload", NULL },
|
|
|
|
handle_logger_reload, "Reopens the log files",
|
|
|
|
logger_reload_help };
|
|
|
|
|
2003-11-26 22:00:07 +00:00
|
|
|
static struct ast_cli_entry rotate_logger_cli =
|
|
|
|
{ { "logger", "rotate", NULL },
|
2004-01-26 05:32:30 +00:00
|
|
|
handle_logger_rotate, "Rotates and reopens the log files",
|
2003-11-26 22:00:07 +00:00
|
|
|
logger_rotate_help };
|
|
|
|
|
2004-06-29 04:42:19 +00:00
|
|
|
static int handle_SIGXFSZ(int sig)
|
|
|
|
{
|
2004-03-30 16:16:05 +00:00
|
|
|
/* Indicate need to reload */
|
2005-07-05 20:31:34 +00:00
|
|
|
filesize_reload_needed = 1;
|
2004-06-29 04:42:19 +00:00
|
|
|
return 0;
|
2003-11-26 22:00:07 +00:00
|
|
|
}
|
|
|
|
|
2003-10-02 06:40:10 +00:00
|
|
|
int init_logger(void)
|
2002-02-11 04:25:04 +00:00
|
|
|
{
|
2004-01-12 05:05:35 +00:00
|
|
|
char tmp[256];
|
2003-11-26 22:00:07 +00:00
|
|
|
|
|
|
|
/* auto rotate if sig SIGXFSZ comes a-knockin */
|
|
|
|
(void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
|
|
|
|
|
2003-10-02 06:40:10 +00:00
|
|
|
/* register the relaod logger cli command */
|
|
|
|
ast_cli_register(&reload_logger_cli);
|
2003-11-26 22:00:07 +00:00
|
|
|
ast_cli_register(&rotate_logger_cli);
|
2005-03-31 19:08:51 +00:00
|
|
|
ast_cli_register(&logger_show_channels_cli);
|
2004-01-12 05:05:35 +00:00
|
|
|
|
2004-02-23 20:18:27 +00:00
|
|
|
/* initialize queue logger */
|
|
|
|
queue_log_init();
|
|
|
|
|
2004-01-12 05:05:35 +00:00
|
|
|
/* create log channels */
|
2002-02-11 04:25:04 +00:00
|
|
|
init_logger_chain();
|
2005-03-11 08:49:01 +00:00
|
|
|
|
|
|
|
/* create the eventlog */
|
|
|
|
if (logfiles.event_log) {
|
|
|
|
mkdir((char *)ast_config_AST_LOG_DIR, 0755);
|
|
|
|
snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
|
|
|
|
eventlog = fopen((char *)tmp, "a");
|
|
|
|
if (eventlog) {
|
|
|
|
ast_log(LOG_EVENT, "Started Asterisk Event Logger\n");
|
|
|
|
if (option_verbose)
|
|
|
|
ast_verbose("Asterisk Event Logger Started %s\n",(char *)tmp);
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
|
2005-08-15 19:35:25 +00:00
|
|
|
} else
|
|
|
|
return 0;
|
2005-03-11 08:49:01 +00:00
|
|
|
|
1999-12-05 07:28:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-06-17 01:13:10 +00:00
|
|
|
void close_logger(void)
|
|
|
|
{
|
|
|
|
struct msglist *m, *tmp;
|
|
|
|
|
|
|
|
ast_mutex_lock(&msglist_lock);
|
2004-09-28 21:13:50 +00:00
|
|
|
m = list;
|
2004-06-17 01:13:10 +00:00
|
|
|
while(m) {
|
|
|
|
if (m->msg) {
|
|
|
|
free(m->msg);
|
|
|
|
}
|
|
|
|
tmp = m->next;
|
|
|
|
free(m);
|
|
|
|
m = tmp;
|
|
|
|
}
|
|
|
|
list = last = NULL;
|
2004-07-20 15:10:18 +00:00
|
|
|
msgcnt = 0;
|
2004-06-17 01:13:10 +00:00
|
|
|
ast_mutex_unlock(&msglist_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-04-01 23:49:57 +00:00
|
|
|
static void strip_coloring(char *str)
|
|
|
|
{
|
2005-04-02 23:20:11 +00:00
|
|
|
char *src, *dest, *end;
|
2005-04-01 23:49:57 +00:00
|
|
|
|
2005-04-03 21:25:45 +00:00
|
|
|
if (!str)
|
2005-04-01 23:49:57 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* find the first potential escape sequence in the string */
|
|
|
|
|
2005-04-02 23:20:11 +00:00
|
|
|
src = strchr(str, '\033');
|
|
|
|
if (!src)
|
2005-04-01 23:49:57 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
dest = src;
|
|
|
|
while (*src) {
|
|
|
|
/* at the top of this loop, *src will always be an ESC character */
|
|
|
|
if ((src[1] == '[') && ((end = strchr(src + 2, 'm'))))
|
|
|
|
src = end + 1;
|
|
|
|
else
|
|
|
|
*dest++ = *src++;
|
|
|
|
|
|
|
|
/* copy characters, checking for ESC as we go */
|
|
|
|
while (*src && (*src != '\033'))
|
|
|
|
*dest++ = *src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest = '\0';
|
|
|
|
}
|
|
|
|
|
2004-06-29 04:42:19 +00:00
|
|
|
static void ast_log_vsyslog(int level, const char *file, int line, const char *function, const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
2005-04-01 23:49:57 +00:00
|
|
|
char *s;
|
2004-01-12 05:05:35 +00:00
|
|
|
|
2004-06-29 04:42:19 +00:00
|
|
|
if (level >= SYSLOG_NLEVELS) {
|
|
|
|
/* we are locked here, so cannot ast_log() */
|
|
|
|
fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", level);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (level == __LOG_VERBOSE) {
|
2004-11-01 16:26:32 +00:00
|
|
|
snprintf(buf, sizeof(buf), "VERBOSE[%ld]: ", (long)GETTID());
|
2004-06-29 04:42:19 +00:00
|
|
|
level = __LOG_DEBUG;
|
2005-08-22 21:19:59 +00:00
|
|
|
} else if (level == __LOG_DTMF) {
|
|
|
|
snprintf(buf, sizeof(buf), "DTMF[%ld]: ", (long)GETTID());
|
|
|
|
level = __LOG_DEBUG;
|
2004-06-29 04:42:19 +00:00
|
|
|
} else {
|
|
|
|
snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: ",
|
2005-04-01 23:49:57 +00:00
|
|
|
levels[level], (long)GETTID(), file, line, function);
|
2004-06-29 04:42:19 +00:00
|
|
|
}
|
2005-04-01 23:49:57 +00:00
|
|
|
s = buf + strlen(buf);
|
|
|
|
vsnprintf(s, sizeof(buf) - strlen(buf), fmt, args);
|
|
|
|
strip_coloring(s);
|
2004-06-29 04:42:19 +00:00
|
|
|
syslog(syslog_level_map[level], "%s", buf);
|
2004-01-12 05:05:35 +00:00
|
|
|
}
|
2003-10-02 06:40:10 +00:00
|
|
|
|
2004-01-12 05:05:35 +00:00
|
|
|
/*
|
|
|
|
* send log messages to syslog and/or the console
|
|
|
|
*/
|
|
|
|
void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
|
1999-12-05 07:28:29 +00:00
|
|
|
{
|
2004-06-29 04:42:19 +00:00
|
|
|
struct logchannel *chan;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
time_t t;
|
|
|
|
struct tm tm;
|
|
|
|
char date[256];
|
2004-01-12 05:05:35 +00:00
|
|
|
|
2004-06-29 04:42:19 +00:00
|
|
|
va_list ap;
|
2004-01-12 05:05:35 +00:00
|
|
|
|
2005-05-20 13:58:39 +00:00
|
|
|
/* don't display LOG_DEBUG messages unless option_verbose _or_ option_debug
|
|
|
|
are non-zero; LOG_DEBUG messages can still be displayed if option_debug
|
|
|
|
is zero, if option_verbose is non-zero (this allows for 'level zero'
|
|
|
|
LOG_DEBUG messages to be displayed, if the logmask on any channel
|
|
|
|
allows it)
|
|
|
|
*/
|
2005-05-19 20:33:55 +00:00
|
|
|
if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
|
2004-06-29 04:42:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-05-20 03:52:27 +00:00
|
|
|
|
2004-10-24 06:12:21 +00:00
|
|
|
/* Ignore anything that never gets logged anywhere */
|
|
|
|
if (!(global_logmask & (1 << level)))
|
|
|
|
return;
|
2005-03-05 04:04:55 +00:00
|
|
|
|
|
|
|
/* Ignore anything other than the currently debugged file if there is one */
|
|
|
|
if ((level == __LOG_DEBUG) && !ast_strlen_zero(debug_filename) && strcasecmp(debug_filename, file))
|
|
|
|
return;
|
2004-01-12 07:44:22 +00:00
|
|
|
|
2004-06-29 04:42:19 +00:00
|
|
|
/* begin critical section */
|
|
|
|
ast_mutex_lock(&loglock);
|
2004-01-12 07:44:22 +00:00
|
|
|
|
2004-06-29 04:42:19 +00:00
|
|
|
time(&t);
|
|
|
|
localtime_r(&t, &tm);
|
|
|
|
strftime(date, sizeof(date), dateformat, &tm);
|
2004-01-12 06:32:40 +00:00
|
|
|
|
2005-03-11 08:49:01 +00:00
|
|
|
if (logfiles.event_log && level == __LOG_EVENT) {
|
2004-06-29 04:42:19 +00:00
|
|
|
va_start(ap, fmt);
|
2004-01-12 06:32:40 +00:00
|
|
|
|
2004-06-29 04:42:19 +00:00
|
|
|
fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
|
|
|
|
vfprintf(eventlog, fmt, ap);
|
|
|
|
fflush(eventlog);
|
2004-01-12 05:05:35 +00:00
|
|
|
|
|
|
|
va_end(ap);
|
2004-06-29 04:42:19 +00:00
|
|
|
ast_mutex_unlock(&loglock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (logchannels) {
|
|
|
|
chan = logchannels;
|
2005-03-31 19:08:51 +00:00
|
|
|
while(chan && !chan->disabled) {
|
|
|
|
/* Check syslog channels */
|
2005-05-14 23:28:47 +00:00
|
|
|
if (chan->type == LOGTYPE_SYSLOG && (chan->logmask & (1 << level))) {
|
2004-06-29 04:42:19 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
ast_log_vsyslog(level, file, line, function, fmt, ap);
|
|
|
|
va_end(ap);
|
2005-03-31 19:08:51 +00:00
|
|
|
/* Console channels */
|
|
|
|
} else if ((chan->logmask & (1 << level)) && (chan->type == LOGTYPE_CONSOLE)) {
|
2004-06-29 04:42:19 +00:00
|
|
|
char linestr[128];
|
|
|
|
char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
|
|
|
|
|
|
|
|
if (level != __LOG_VERBOSE) {
|
|
|
|
sprintf(linestr, "%d", line);
|
2005-03-11 07:24:10 +00:00
|
|
|
snprintf(buf, sizeof(buf), option_timestamp ? "[%s] %s[%ld]: %s:%s %s: " : "%s %s[%ld]: %s:%s %s: ",
|
2004-06-29 04:42:19 +00:00
|
|
|
date,
|
|
|
|
term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
|
2004-11-01 16:26:32 +00:00
|
|
|
(long)GETTID(),
|
2004-06-29 04:42:19 +00:00
|
|
|
term_color(tmp2, file, COLOR_BRWHITE, 0, sizeof(tmp2)),
|
|
|
|
term_color(tmp3, linestr, COLOR_BRWHITE, 0, sizeof(tmp3)),
|
|
|
|
term_color(tmp4, function, COLOR_BRWHITE, 0, sizeof(tmp4)));
|
2005-03-11 07:24:10 +00:00
|
|
|
|
2004-06-29 04:42:19 +00:00
|
|
|
ast_console_puts(buf);
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
ast_console_puts(buf);
|
|
|
|
}
|
2005-03-31 19:08:51 +00:00
|
|
|
/* File channels */
|
2004-06-29 04:42:19 +00:00
|
|
|
} else if ((chan->logmask & (1 << level)) && (chan->fileptr)) {
|
2005-03-31 19:08:51 +00:00
|
|
|
int res;
|
|
|
|
snprintf(buf, sizeof(buf), option_timestamp ? "[%s] %s[%ld]: " : "%s %s[%ld] %s: ", date,
|
|
|
|
levels[level], (long)GETTID(), file);
|
|
|
|
res = fprintf(chan->fileptr, buf);
|
|
|
|
if (res <= 0 && buf[0] != '\0') { /* Error, no characters printed */
|
|
|
|
fprintf(stderr,"**** Asterisk Logging Error: ***********\n");
|
|
|
|
if (errno == ENOMEM || errno == ENOSPC) {
|
|
|
|
fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
|
|
|
|
} else
|
|
|
|
fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
|
|
|
|
manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
|
|
|
|
chan->disabled = 1;
|
|
|
|
} else {
|
|
|
|
/* No error message, continue printing */
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
va_end(ap);
|
2005-04-01 23:49:57 +00:00
|
|
|
strip_coloring(buf);
|
2005-03-31 19:08:51 +00:00
|
|
|
fputs(buf, chan->fileptr);
|
|
|
|
fflush(chan->fileptr);
|
|
|
|
}
|
2004-06-29 04:42:19 +00:00
|
|
|
}
|
|
|
|
chan = chan->next;
|
2004-01-27 00:23:39 +00:00
|
|
|
}
|
2004-06-29 04:42:19 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* we don't have the logger chain configured yet,
|
|
|
|
* so just log to stdout
|
|
|
|
*/
|
2004-01-27 18:20:17 +00:00
|
|
|
if (level != __LOG_VERBOSE) {
|
2004-06-29 04:42:19 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fputs(buf, stdout);
|
2004-01-27 18:20:17 +00:00
|
|
|
}
|
2004-06-29 04:42:19 +00:00
|
|
|
}
|
2004-01-12 05:05:35 +00:00
|
|
|
|
2004-06-29 04:42:19 +00:00
|
|
|
ast_mutex_unlock(&loglock);
|
|
|
|
/* end critical section */
|
2005-07-05 20:31:34 +00:00
|
|
|
if (filesize_reload_needed) {
|
2004-06-29 04:42:19 +00:00
|
|
|
reload_logger(1);
|
2005-03-31 19:08:51 +00:00
|
|
|
ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
|
2004-06-29 04:42:19 +00:00
|
|
|
if (option_verbose)
|
2005-03-31 19:08:51 +00:00
|
|
|
ast_verbose("Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
|
2004-03-30 16:16:05 +00:00
|
|
|
}
|
1999-12-05 07:28:29 +00:00
|
|
|
}
|
|
|
|
|
2003-03-16 22:37:31 +00:00
|
|
|
extern void ast_verbose(const char *fmt, ...)
|
1999-12-05 07:28:29 +00:00
|
|
|
{
|
1999-12-16 22:47:49 +00:00
|
|
|
static char stuff[4096];
|
1999-12-05 07:28:29 +00:00
|
|
|
static int pos = 0, opos;
|
|
|
|
static int replacelast = 0, complete;
|
|
|
|
struct msglist *m;
|
|
|
|
struct verb *v;
|
2005-03-11 07:24:10 +00:00
|
|
|
time_t t;
|
|
|
|
struct tm tm;
|
|
|
|
char date[40];
|
|
|
|
char *datefmt;
|
|
|
|
|
1999-12-05 07:28:29 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_lock(&msglist_lock);
|
2005-03-11 07:24:10 +00:00
|
|
|
time(&t);
|
|
|
|
localtime_r(&t, &tm);
|
|
|
|
strftime(date, sizeof(date), dateformat, &tm);
|
|
|
|
|
|
|
|
if (option_timestamp) {
|
|
|
|
datefmt = alloca(strlen(date) + 3 + strlen(fmt) + 1);
|
|
|
|
if (datefmt) {
|
|
|
|
sprintf(datefmt, "[%s] %s", date, fmt);
|
|
|
|
fmt = datefmt;
|
|
|
|
}
|
|
|
|
}
|
1999-12-05 07:28:29 +00:00
|
|
|
vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
|
|
|
|
opos = pos;
|
|
|
|
pos = strlen(stuff);
|
2004-11-17 19:27:47 +00:00
|
|
|
|
2005-03-11 07:24:10 +00:00
|
|
|
|
2004-11-16 13:52:35 +00:00
|
|
|
if (stuff[strlen(stuff)-1] == '\n')
|
1999-12-05 07:28:29 +00:00
|
|
|
complete = 1;
|
|
|
|
else
|
|
|
|
complete=0;
|
|
|
|
if (complete) {
|
|
|
|
if (msgcnt < MAX_MSG_QUEUE) {
|
|
|
|
/* Allocate new structure */
|
|
|
|
m = malloc(sizeof(struct msglist));
|
|
|
|
msgcnt++;
|
|
|
|
} else {
|
|
|
|
/* Recycle the oldest entry */
|
|
|
|
m = list;
|
|
|
|
list = list->next;
|
|
|
|
free(m->msg);
|
|
|
|
}
|
|
|
|
if (m) {
|
|
|
|
m->msg = strdup(stuff);
|
|
|
|
if (m->msg) {
|
|
|
|
if (last)
|
|
|
|
last->next = m;
|
|
|
|
else
|
|
|
|
list = m;
|
|
|
|
m->next = NULL;
|
|
|
|
last = m;
|
|
|
|
} else {
|
|
|
|
msgcnt--;
|
2003-11-26 22:00:07 +00:00
|
|
|
ast_log(LOG_ERROR, "Out of memory\n");
|
1999-12-05 07:28:29 +00:00
|
|
|
free(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (verboser) {
|
|
|
|
v = verboser;
|
|
|
|
while(v) {
|
|
|
|
v->verboser(stuff, opos, replacelast, complete);
|
|
|
|
v = v->next;
|
|
|
|
}
|
2001-04-30 17:39:02 +00:00
|
|
|
} /* else
|
|
|
|
fprintf(stdout, stuff + opos); */
|
2004-12-27 22:41:58 +00:00
|
|
|
ast_log(LOG_VERBOSE, "%s", stuff);
|
2004-11-17 19:27:47 +00:00
|
|
|
if (strlen(stuff)) {
|
|
|
|
if (stuff[strlen(stuff)-1] != '\n')
|
|
|
|
replacelast = 1;
|
|
|
|
else
|
|
|
|
replacelast = pos = 0;
|
|
|
|
}
|
1999-12-05 07:28:29 +00:00
|
|
|
va_end(ap);
|
2004-01-12 05:05:35 +00:00
|
|
|
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&msglist_lock);
|
1999-12-05 07:28:29 +00:00
|
|
|
}
|
|
|
|
|
2003-03-16 22:37:31 +00:00
|
|
|
int ast_verbose_dmesg(void (*v)(const char *string, int opos, int replacelast, int complete))
|
2001-04-30 17:39:02 +00:00
|
|
|
{
|
|
|
|
struct msglist *m;
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_lock(&msglist_lock);
|
2004-09-28 21:13:50 +00:00
|
|
|
m = list;
|
2001-04-30 17:39:02 +00:00
|
|
|
while(m) {
|
|
|
|
/* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
|
|
|
|
v(m->msg, 0, 0, 1);
|
|
|
|
m = m->next;
|
|
|
|
}
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&msglist_lock);
|
2001-04-30 17:39:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1999-12-05 07:28:29 +00:00
|
|
|
|
2003-03-16 22:37:31 +00:00
|
|
|
int ast_register_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
|
1999-12-05 07:28:29 +00:00
|
|
|
{
|
|
|
|
struct msglist *m;
|
|
|
|
struct verb *tmp;
|
|
|
|
/* XXX Should be more flexible here, taking > 1 verboser XXX */
|
|
|
|
if ((tmp = malloc(sizeof (struct verb)))) {
|
|
|
|
tmp->verboser = v;
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_lock(&msglist_lock);
|
1999-12-05 07:28:29 +00:00
|
|
|
tmp->next = verboser;
|
|
|
|
verboser = tmp;
|
|
|
|
m = list;
|
|
|
|
while(m) {
|
|
|
|
/* Send all the existing entries that we have queued (i.e. they're likely to have missed) */
|
|
|
|
v(m->msg, 0, 0, 1);
|
|
|
|
m = m->next;
|
|
|
|
}
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&msglist_lock);
|
1999-12-05 07:28:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-03-16 22:37:31 +00:00
|
|
|
int ast_unregister_verbose(void (*v)(const char *string, int opos, int replacelast, int complete))
|
1999-12-05 07:28:29 +00:00
|
|
|
{
|
|
|
|
int res = -1;
|
|
|
|
struct verb *tmp, *tmpl=NULL;
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_lock(&msglist_lock);
|
1999-12-05 07:28:29 +00:00
|
|
|
tmp = verboser;
|
|
|
|
while(tmp) {
|
|
|
|
if (tmp->verboser == v) {
|
|
|
|
if (tmpl)
|
|
|
|
tmpl->next = tmp->next;
|
|
|
|
else
|
|
|
|
verboser = tmp->next;
|
2001-04-30 17:39:02 +00:00
|
|
|
free(tmp);
|
1999-12-05 07:28:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmpl = tmp;
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
if (tmp)
|
|
|
|
res = 0;
|
2003-08-13 15:25:16 +00:00
|
|
|
ast_mutex_unlock(&msglist_lock);
|
1999-12-05 07:28:29 +00:00
|
|
|
return res;
|
|
|
|
}
|