mirror of
https://github.com/asterisk/asterisk.git
synced 2025-10-27 06:31:54 +00:00
Add ww's improved syslog support (bug #587)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@1973 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
33
asterisk.c
33
asterisk.c
@@ -148,13 +148,34 @@ static int fdprint(int fd, const char *s)
|
|||||||
return write(fd, s, strlen(s) + 1);
|
return write(fd, s, strlen(s) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void network_verboser(const char *s, int pos, int replace, int complete)
|
/*
|
||||||
|
* write the string to all attached console clients
|
||||||
|
*/
|
||||||
|
static void ast_network_puts(const char *string)
|
||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
for (x=0;x<AST_MAX_CONNECTS; x++) {
|
for (x=0;x<AST_MAX_CONNECTS; x++) {
|
||||||
if (consoles[x].fd > -1)
|
if (consoles[x].fd > -1)
|
||||||
fdprint(consoles[x].p[1], s);
|
fdprint(consoles[x].p[1], string);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* write the string to the console, and all attached
|
||||||
|
* console clients
|
||||||
|
*/
|
||||||
|
void ast_console_puts(const char *string)
|
||||||
|
{
|
||||||
|
fputs(string, stdout);
|
||||||
|
fflush(stdout);
|
||||||
|
ast_network_puts(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void network_verboser(const char *s, int pos, int replace, int complete)
|
||||||
|
/* ARGUSED */
|
||||||
|
{
|
||||||
|
ast_network_puts(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
static pthread_t lthread;
|
static pthread_t lthread;
|
||||||
|
|||||||
@@ -17,9 +17,6 @@ console => notice,warning,error
|
|||||||
messages => notice,warning,error
|
messages => notice,warning,error
|
||||||
|
|
||||||
;syslog keyword : This special keyword logs to syslog facility
|
;syslog keyword : This special keyword logs to syslog facility
|
||||||
;local0 edit /etc/syslog.conf and add
|
|
||||||
;local0.* /var/log/myastlog
|
|
||||||
;
|
;
|
||||||
;syslog => notice,warning,error
|
;syslog.local0 => notice,warning,error
|
||||||
|
;
|
||||||
|
|
||||||
|
|||||||
@@ -54,13 +54,39 @@ extern void ast_verbose(const char *fmt, ...)
|
|||||||
extern int ast_register_verbose(void (*verboser)(const char *string, int opos, int replacelast, int complete));
|
extern int ast_register_verbose(void (*verboser)(const char *string, int opos, int replacelast, int complete));
|
||||||
extern int ast_unregister_verbose(void (*verboser)(const char *string, int opos, int replacelast, int complete));
|
extern int ast_unregister_verbose(void (*verboser)(const char *string, int opos, int replacelast, int complete));
|
||||||
extern int ast_verbose_dmesg(void (*verboser)(const char *string, int opos, int replacelast, int complete));
|
extern int ast_verbose_dmesg(void (*verboser)(const char *string, int opos, int replacelast, int complete));
|
||||||
|
extern void ast_console_puts(const char *string);
|
||||||
|
|
||||||
#define _A_ __FILE__, __LINE__, __PRETTY_FUNCTION__
|
#define _A_ __FILE__, __LINE__, __PRETTY_FUNCTION__
|
||||||
|
|
||||||
#define LOG_DEBUG 0, _A_
|
#ifdef LOG_DEBUG
|
||||||
#define LOG_EVENT 1, _A_
|
#undef LOG_DEBUG
|
||||||
#define LOG_NOTICE 2, _A_
|
#endif
|
||||||
#define LOG_WARNING 3, _A_
|
#define __LOG_DEBUG 0
|
||||||
#define LOG_ERROR 4, _A_
|
#define LOG_DEBUG __LOG_DEBUG, _A_
|
||||||
|
|
||||||
|
#ifdef LOG_EVENT
|
||||||
|
#undef LOG_EVENT
|
||||||
|
#endif
|
||||||
|
#define __LOG_EVENT 1
|
||||||
|
#define LOG_EVENT __LOG_EVENT, _A_
|
||||||
|
|
||||||
|
#ifdef LOG_NOTICE
|
||||||
|
#undef LOG_NOTICE
|
||||||
|
#endif
|
||||||
|
#define __LOG_NOTICE 2
|
||||||
|
#define LOG_NOTICE __LOG_NOTICE, _A_
|
||||||
|
|
||||||
|
#ifdef LOG_WARNING
|
||||||
|
#undef LOG_WARNING
|
||||||
|
#endif
|
||||||
|
#define __LOG_WARNING 3
|
||||||
|
#define LOG_WARNING __LOG_WARNING, _A_
|
||||||
|
|
||||||
|
#ifdef LOG_ERROR
|
||||||
|
#undef LOG_ERROR
|
||||||
|
#endif
|
||||||
|
#define __LOG_ERROR 4
|
||||||
|
#define LOG_ERROR __LOG_ERROR, _A_
|
||||||
|
|
||||||
#if defined(__cplusplus) || defined(c_plusplus)
|
#if defined(__cplusplus) || defined(c_plusplus)
|
||||||
}
|
}
|
||||||
|
|||||||
489
logger.c
489
logger.c
@@ -17,7 +17,6 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <asterisk/lock.h>
|
#include <asterisk/lock.h>
|
||||||
#include <asterisk/logger.h>
|
|
||||||
#include <asterisk/options.h>
|
#include <asterisk/options.h>
|
||||||
#include <asterisk/channel.h>
|
#include <asterisk/channel.h>
|
||||||
#include <asterisk/config.h>
|
#include <asterisk/config.h>
|
||||||
@@ -28,10 +27,23 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <syslog.h>
|
|
||||||
#include "asterisk.h"
|
#include "asterisk.h"
|
||||||
#include "astconf.h"
|
#include "astconf.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>
|
||||||
|
static int syslog_level_map[] = {
|
||||||
|
LOG_DEBUG,
|
||||||
|
LOG_INFO, /* arbitrary equivalent of LOG_EVENT */
|
||||||
|
LOG_NOTICE,
|
||||||
|
LOG_WARNING,
|
||||||
|
LOG_ERR
|
||||||
|
};
|
||||||
|
#define SYSLOG_NLEVELS 5
|
||||||
|
|
||||||
|
#include <asterisk/logger.h>
|
||||||
|
|
||||||
#define MAX_MSG_QUEUE 200
|
#define MAX_MSG_QUEUE 200
|
||||||
|
|
||||||
static ast_mutex_t msglist_lock = AST_MUTEX_INITIALIZER;
|
static ast_mutex_t msglist_lock = AST_MUTEX_INITIALIZER;
|
||||||
@@ -42,38 +54,38 @@ static struct msglist {
|
|||||||
struct msglist *next;
|
struct msglist *next;
|
||||||
} *list = NULL, *last = NULL;
|
} *list = NULL, *last = NULL;
|
||||||
|
|
||||||
struct logfile {
|
struct logchannel {
|
||||||
char fn[256];
|
int logmask;
|
||||||
int logflags;
|
|
||||||
FILE *f;
|
|
||||||
int facility; /* syslog */
|
int facility; /* syslog */
|
||||||
struct logfile *next;
|
int syslog; /* syslog flag */
|
||||||
|
int console; /* console logging */
|
||||||
|
FILE *fileptr; /* logfile logging */
|
||||||
|
char filename[256];
|
||||||
|
struct logchannel *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct logfile *logfiles = NULL;
|
static struct logchannel *logchannels = NULL;
|
||||||
|
|
||||||
static int msgcnt = 0;
|
static int msgcnt = 0;
|
||||||
|
|
||||||
static FILE *eventlog = NULL;
|
static FILE *eventlog = NULL;
|
||||||
|
|
||||||
static char *levels[] = {
|
static char *levels[] = {
|
||||||
"DEBUG",
|
"DEBUG",
|
||||||
"EVENT",
|
"EVENT",
|
||||||
"NOTICE",
|
"NOTICE",
|
||||||
"WARNING",
|
"WARNING",
|
||||||
"ERROR"
|
"ERROR"
|
||||||
};
|
};
|
||||||
|
|
||||||
static int colors[] = {
|
static int colors[] = {
|
||||||
COLOR_BRGREEN,
|
COLOR_BRGREEN,
|
||||||
COLOR_BRBLUE,
|
COLOR_BRBLUE,
|
||||||
COLOR_YELLOW,
|
COLOR_YELLOW,
|
||||||
COLOR_BRRED,
|
COLOR_BRRED,
|
||||||
COLOR_RED
|
COLOR_RED
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int make_components(char *s, int lineno)
|
static int make_components(char *s, int lineno)
|
||||||
{
|
{
|
||||||
char *w;
|
char *w;
|
||||||
@@ -82,120 +94,141 @@ static int make_components(char *s, int lineno)
|
|||||||
stringp=s;
|
stringp=s;
|
||||||
w = strsep(&stringp, ",");
|
w = strsep(&stringp, ",");
|
||||||
while(w) {
|
while(w) {
|
||||||
while(*w && (*w < 33))
|
while(*w && (*w < 33))
|
||||||
w++;
|
w++;
|
||||||
if (!strcasecmp(w, "debug"))
|
if (!strcasecmp(w, "error"))
|
||||||
res |= (1 << 0);
|
res |= (1 << __LOG_ERROR);
|
||||||
else if (!strcasecmp(w, "notice"))
|
else if (!strcasecmp(w, "warning"))
|
||||||
res |= (1 << 2);
|
res |= (1 << __LOG_WARNING);
|
||||||
else if (!strcasecmp(w, "warning"))
|
else if (!strcasecmp(w, "notice"))
|
||||||
res |= (1 << 3);
|
res |= (1 << __LOG_NOTICE);
|
||||||
else if (!strcasecmp(w, "error"))
|
else if (!strcasecmp(w, "event"))
|
||||||
res |= (1 << 4);
|
res |= (1 << __LOG_EVENT);
|
||||||
else {
|
else if (!strcasecmp(w, "debug"))
|
||||||
fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
|
res |= (1 << __LOG_DEBUG);
|
||||||
}
|
else {
|
||||||
w = strsep(&stringp, ",");
|
fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n", w, lineno);
|
||||||
|
}
|
||||||
|
w = strsep(&stringp, ",");
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct logfile *make_logfile(char *fn, char *components, int lineno)
|
static struct logchannel *make_logchannel(char *channel, char *components, int lineno)
|
||||||
{
|
{
|
||||||
struct logfile *f;
|
struct logchannel *chan;
|
||||||
char tmp[256];
|
char *facility;
|
||||||
if (!strlen(fn))
|
CODE *cptr;
|
||||||
|
|
||||||
|
if (!strlen(channel))
|
||||||
return NULL;
|
return NULL;
|
||||||
f = malloc(sizeof(struct logfile));
|
chan = malloc(sizeof(struct logchannel));
|
||||||
if (f) {
|
|
||||||
memset(f, 0, sizeof(struct logfile));
|
if (chan) {
|
||||||
strncpy(f->fn, fn, sizeof(f->fn) - 1);
|
memset(chan, 0, sizeof(chan));
|
||||||
if (!strcasecmp(fn, "ignore")) {
|
if (!strcasecmp(channel, "console")) {
|
||||||
f->f = NULL;
|
chan->console = 1;
|
||||||
} else if (!strcasecmp(fn, "console")) {
|
} else if (!strncasecmp(channel, "syslog", 6)) {
|
||||||
f->f = stdout;
|
/*
|
||||||
} else if (!strcasecmp(fn, "syslog")) {
|
* syntax is:
|
||||||
f->f = NULL;
|
* syslog.facility => level,level,level
|
||||||
f->facility = LOG_LOCAL0;
|
*/
|
||||||
|
facility = strchr(channel, '.');
|
||||||
|
if(!facility++ || !facility) {
|
||||||
|
facility = "local0";
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* 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 (!strncasecmp(facility, cptr->c_name, sizeof(cptr->c_name))) {
|
||||||
|
chan->facility = cptr->c_val;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cptr++;
|
||||||
|
}
|
||||||
|
if (0 > chan->facility) {
|
||||||
|
fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
|
||||||
|
free(chan);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
chan->syslog = 1;
|
||||||
|
openlog("asterisk", LOG_PID, chan->facility);
|
||||||
} else {
|
} else {
|
||||||
if (fn[0] == '/')
|
if (channel[0] == '/')
|
||||||
strncpy(tmp, fn, sizeof(tmp) - 1);
|
strncpy(chan->filename, channel, sizeof(chan->filename) - 1);
|
||||||
else
|
else
|
||||||
snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, fn);
|
snprintf(chan->filename, sizeof(chan->filename), "%s/%s", (char *)ast_config_AST_LOG_DIR, channel);
|
||||||
f->f = fopen(tmp, "a");
|
chan->fileptr = fopen(chan->filename, "a");
|
||||||
if (!f->f) {
|
if (!chan->fileptr) {
|
||||||
/* Can't log here, since we're called with a lock */
|
/* Can't log here, since we're called with a lock */
|
||||||
fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", tmp, strerror(errno));
|
fprintf(stderr, "Logger Warning: Unable to open log file '%s': %s\n", chan->filename, strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f->logflags = make_components(components, lineno);
|
chan->logmask = make_components(components, lineno);
|
||||||
|
|
||||||
}
|
}
|
||||||
return f;
|
return chan;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init_logger_chain(void)
|
static void init_logger_chain(void)
|
||||||
{
|
{
|
||||||
struct logfile *f, *cur;
|
struct logchannel *chan, *cur;
|
||||||
struct ast_config *cfg;
|
struct ast_config *cfg;
|
||||||
struct ast_variable *var;
|
struct ast_variable *var;
|
||||||
|
|
||||||
|
/* delete our list of log channels */
|
||||||
ast_mutex_lock(&loglock);
|
ast_mutex_lock(&loglock);
|
||||||
|
chan = logchannels;
|
||||||
/* Free anything that is here */
|
while (chan) {
|
||||||
f = logfiles;
|
cur = chan->next;
|
||||||
while(f) {
|
free(chan);
|
||||||
cur = f->next;
|
chan = cur;
|
||||||
if (f->f && (f->f != stdout) && (f->f != stderr))
|
|
||||||
fclose(f->f);
|
|
||||||
free(f);
|
|
||||||
f = cur;
|
|
||||||
}
|
}
|
||||||
|
logchannels = NULL;
|
||||||
logfiles = NULL;
|
|
||||||
|
|
||||||
ast_mutex_unlock(&loglock);
|
ast_mutex_unlock(&loglock);
|
||||||
|
|
||||||
|
/* close syslog */
|
||||||
|
closelog();
|
||||||
|
|
||||||
cfg = ast_load("logger.conf");
|
cfg = ast_load("logger.conf");
|
||||||
ast_mutex_lock(&loglock);
|
|
||||||
|
|
||||||
/* If no config file, we're fine */
|
/* If no config file, we're fine */
|
||||||
if (!cfg) {
|
if (!cfg)
|
||||||
ast_mutex_unlock(&loglock);
|
return;
|
||||||
return;
|
|
||||||
}
|
ast_mutex_lock(&loglock);
|
||||||
var = ast_variable_browse(cfg, "logfiles");
|
var = ast_variable_browse(cfg, "logfiles");
|
||||||
while(var) {
|
while(var) {
|
||||||
f = make_logfile(var->name, var->value, var->lineno);
|
chan = make_logchannel(var->name, var->value, var->lineno);
|
||||||
if (f) {
|
if (chan) {
|
||||||
f->next = logfiles;
|
chan->next = logchannels;
|
||||||
logfiles = f;
|
logchannels = chan;
|
||||||
}
|
}
|
||||||
var = var->next;
|
var = var->next;
|
||||||
}
|
}
|
||||||
if (!logfiles) {
|
|
||||||
/* Gotta have at least one. We'll make a NULL one */
|
|
||||||
logfiles = make_logfile("ignore", "", -1);
|
|
||||||
}
|
|
||||||
ast_destroy(cfg);
|
ast_destroy(cfg);
|
||||||
ast_mutex_unlock(&loglock);
|
ast_mutex_unlock(&loglock);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int reload_logger(int rotate)
|
int reload_logger(int rotate)
|
||||||
{
|
{
|
||||||
char old[AST_CONFIG_MAX_PATH];
|
char old[AST_CONFIG_MAX_PATH];
|
||||||
char new[AST_CONFIG_MAX_PATH];
|
char new[AST_CONFIG_MAX_PATH];
|
||||||
struct logfile *f;
|
struct logchannel *f;
|
||||||
FILE *myf;
|
FILE *myf;
|
||||||
|
|
||||||
int x;
|
int x;
|
||||||
|
|
||||||
ast_mutex_lock(&loglock);
|
ast_mutex_lock(&loglock);
|
||||||
if (eventlog)
|
if (eventlog)
|
||||||
fclose(eventlog);
|
fclose(eventlog);
|
||||||
else
|
else
|
||||||
rotate = 0;
|
rotate = 0;
|
||||||
eventlog = NULL;
|
eventlog = NULL;
|
||||||
|
|
||||||
|
|
||||||
@@ -205,51 +238,47 @@ int reload_logger(int rotate)
|
|||||||
|
|
||||||
if(rotate) {
|
if(rotate) {
|
||||||
for(x=0;;x++) {
|
for(x=0;;x++) {
|
||||||
snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
|
snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR, EVENTLOG,x);
|
||||||
myf = fopen((char *)new, "r");
|
myf = fopen((char *)new, "r");
|
||||||
if(myf)
|
if(myf)
|
||||||
fclose(myf);
|
fclose(myf);
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* do it */
|
/* do it */
|
||||||
if (rename(old,new))
|
if (rename(old,new))
|
||||||
fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
|
fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
|
||||||
}
|
}
|
||||||
|
|
||||||
eventlog = fopen(old, "a");
|
eventlog = fopen(old, "a");
|
||||||
|
|
||||||
f = logfiles;
|
f = logchannels;
|
||||||
while(f) {
|
while(f) {
|
||||||
if (f->f && (f->f != stdout) && (f->f != stderr)) {
|
if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
|
||||||
fclose(f->f);
|
fclose(f->fileptr);
|
||||||
f->f = NULL;
|
f->fileptr = NULL;
|
||||||
if(rotate) {
|
if(rotate) {
|
||||||
snprintf(old, sizeof(old), "%s/%s", (char *)ast_config_AST_LOG_DIR,f->fn);
|
strncpy(old, f->filename, sizeof(old));
|
||||||
|
|
||||||
for(x=0;;x++) {
|
for(x=0;;x++) {
|
||||||
snprintf(new, sizeof(new), "%s/%s.%d", (char *)ast_config_AST_LOG_DIR,f->fn,x);
|
snprintf(new, sizeof(new), "%s.%d", f->filename, x);
|
||||||
myf = fopen((char *)new, "r");
|
myf = fopen((char *)new, "r");
|
||||||
if(myf)
|
if (myf) {
|
||||||
fclose(myf);
|
fclose(myf);
|
||||||
else
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* do it */
|
/* do it */
|
||||||
if (rename(old,new))
|
if (rename(old,new))
|
||||||
fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
|
fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
f = f->next;
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
f = f->next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ast_mutex_unlock(&loglock);
|
ast_mutex_unlock(&loglock);
|
||||||
|
|
||||||
if (eventlog) {
|
if (eventlog) {
|
||||||
@@ -275,13 +304,6 @@ static int handle_logger_reload(int fd, int argc, char *argv[])
|
|||||||
return RESULT_SUCCESS;
|
return RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int handle_logger_rotate(int fd, int argc, char *argv[])
|
static int handle_logger_rotate(int fd, int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if(reload_logger(1))
|
if(reload_logger(1))
|
||||||
@@ -301,41 +323,33 @@ static struct verb {
|
|||||||
|
|
||||||
static char logger_reload_help[] =
|
static char logger_reload_help[] =
|
||||||
"Usage: logger reload\n"
|
"Usage: logger reload\n"
|
||||||
" Reopens the log files. Use after a rotating the log files\n";
|
" Reloads the logger subsystem state. Use after restarting syslogd(8)\n";
|
||||||
|
|
||||||
|
|
||||||
static char logger_rotate_help[] =
|
static char logger_rotate_help[] =
|
||||||
"Usage: logger reload\n"
|
"Usage: logger reload\n"
|
||||||
" Rotates and Reopens the log files.\n";
|
" Rotates and Reopens the log files.\n";
|
||||||
|
|
||||||
|
|
||||||
static struct ast_cli_entry reload_logger_cli =
|
static struct ast_cli_entry reload_logger_cli =
|
||||||
{ { "logger", "reload", NULL },
|
{ { "logger", "reload", NULL },
|
||||||
handle_logger_reload, "Reopens the log files",
|
handle_logger_reload, "Reopens the log files",
|
||||||
logger_reload_help };
|
logger_reload_help };
|
||||||
|
|
||||||
|
|
||||||
static struct ast_cli_entry rotate_logger_cli =
|
static struct ast_cli_entry rotate_logger_cli =
|
||||||
{ { "logger", "rotate", NULL },
|
{ { "logger", "rotate", NULL },
|
||||||
handle_logger_rotate, "Reopens the log files",
|
handle_logger_rotate, "Reopens the log files",
|
||||||
logger_rotate_help };
|
logger_rotate_help };
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int handle_SIGXFSZ(int sig) {
|
static int handle_SIGXFSZ(int sig) {
|
||||||
reload_logger(1);
|
reload_logger(1);
|
||||||
ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ\n");
|
ast_log(LOG_EVENT,"Rotated Logs Per SIGXFSZ\n");
|
||||||
if (option_verbose)
|
if (option_verbose)
|
||||||
ast_verbose("Rotated Logs Per SIGXFSZ\n");
|
ast_verbose("Rotated Logs Per SIGXFSZ\n");
|
||||||
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int init_logger(void)
|
int init_logger(void)
|
||||||
{
|
{
|
||||||
char tmp[AST_CONFIG_MAX_PATH];
|
char tmp[256];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* auto rotate if sig SIGXFSZ comes a-knockin */
|
/* auto rotate if sig SIGXFSZ comes a-knockin */
|
||||||
(void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
|
(void) signal(SIGXFSZ,(void *) handle_SIGXFSZ);
|
||||||
@@ -344,6 +358,7 @@ int init_logger(void)
|
|||||||
ast_cli_register(&reload_logger_cli);
|
ast_cli_register(&reload_logger_cli);
|
||||||
ast_cli_register(&rotate_logger_cli);
|
ast_cli_register(&rotate_logger_cli);
|
||||||
|
|
||||||
|
/* create the eventlog */
|
||||||
mkdir((char *)ast_config_AST_LOG_DIR, 0755);
|
mkdir((char *)ast_config_AST_LOG_DIR, 0755);
|
||||||
snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
|
snprintf(tmp, sizeof(tmp), "%s/%s", (char *)ast_config_AST_LOG_DIR, EVENTLOG);
|
||||||
eventlog = fopen((char *)tmp, "a");
|
eventlog = fopen((char *)tmp, "a");
|
||||||
@@ -355,89 +370,124 @@ int init_logger(void)
|
|||||||
return 0;
|
return 0;
|
||||||
} else
|
} else
|
||||||
ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
|
ast_log(LOG_ERROR, "Unable to create event log: %s\n", strerror(errno));
|
||||||
|
|
||||||
|
init_logger_chain();
|
||||||
|
|
||||||
|
/* create log channels */
|
||||||
init_logger_chain();
|
init_logger_chain();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void ast_log_vsyslog(int level, const char *file, int line, const char *function, const char *fmt, va_list args) {
|
||||||
|
char buf[BUFSIZ];
|
||||||
|
|
||||||
extern void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
|
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(file && line && function) {
|
||||||
|
snprintf(buf, sizeof(buf), "%s[%ld]: %s:%d in %s: ",
|
||||||
|
levels[level], (long)pthread_self(), file, line, function);
|
||||||
|
} else {
|
||||||
|
snprintf(buf, sizeof(buf), "VERBOSE[%ld]: ", (long)pthread_self());
|
||||||
|
level = __LOG_DEBUG;
|
||||||
|
}
|
||||||
|
vsnprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), fmt, args);
|
||||||
|
syslog(syslog_level_map[level], buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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, ...)
|
||||||
{
|
{
|
||||||
char date[256];
|
struct logchannel *chan;
|
||||||
char tmp[80];
|
char buf[BUFSIZ];
|
||||||
char tmp2[80];
|
time_t t;
|
||||||
char tmp3[80];
|
struct tm tm;
|
||||||
char tmp4[80];
|
char date[256];
|
||||||
char linestr[80];
|
|
||||||
time_t t;
|
va_list ap;
|
||||||
struct tm tm;
|
|
||||||
struct logfile *f;
|
if (!option_verbose && !option_debug && (level == __LOG_DEBUG)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* begin critical section */
|
||||||
|
ast_mutex_lock(&loglock);
|
||||||
|
|
||||||
|
if (level == __LOG_EVENT) {
|
||||||
|
time(&t);
|
||||||
|
localtime_r(&t,&tm);
|
||||||
|
if (&tm) {
|
||||||
|
/* Log events into the event log file, with a different format */
|
||||||
|
strftime(date, sizeof(date), "%b %e %T", &tm);
|
||||||
|
va_start(ap, fmt);
|
||||||
|
|
||||||
|
fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
|
||||||
|
vfprintf(eventlog, fmt, ap);
|
||||||
|
fflush(eventlog);
|
||||||
|
|
||||||
|
va_end(ap);
|
||||||
|
} else
|
||||||
|
/** Cannot use ast_log() from locked section of ast_log()!
|
||||||
|
ast_log(LOG_WARNING, "Unable to retrieve local time?\n"); **/
|
||||||
|
fprintf(stderr, "ast_log: Unable to retrieve local time for %ld?\n", (long)t);
|
||||||
|
ast_mutex_unlock(&loglock);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (logchannels) {
|
||||||
|
chan = logchannels;
|
||||||
|
while(chan) {
|
||||||
|
if (chan->syslog && (chan->logmask & (1 << level))) {
|
||||||
|
va_start(ap, fmt);
|
||||||
|
ast_log_vsyslog(level, file, line, function, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
} else if ((chan->logmask & (1 << level)) && (chan->console || chan->fileptr)) {
|
||||||
|
char linestr[128];
|
||||||
|
char tmp1[80], tmp2[80], tmp3[80], tmp4[80];
|
||||||
|
|
||||||
va_list ap;
|
|
||||||
if (!option_verbose && !option_debug && (!level)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ast_mutex_lock(&loglock);
|
|
||||||
if (level == 1 /* Event */) {
|
|
||||||
time(&t);
|
time(&t);
|
||||||
localtime_r(&t,&tm);
|
localtime_r(&t, &tm);
|
||||||
if (&tm) {
|
if(!&tm) {
|
||||||
/* Log events into the event log file, with a different format */
|
/** Cannot use ast_log() from locked section of ast_log()!
|
||||||
strftime(date, sizeof(date), "%b %e %T", &tm);
|
ast_log(AST_LOG_WARNING, "Unable to retrieve local time?\n"); **/
|
||||||
fprintf(eventlog, "%s asterisk[%d]: ", date, getpid());
|
fprintf(stderr, "ast_log: Unable to retrieve local time for %ld?\n", (long)t);
|
||||||
va_start(ap, fmt);
|
ast_mutex_unlock(&loglock);
|
||||||
vfprintf(eventlog, fmt, ap);
|
return;
|
||||||
va_end(ap);
|
|
||||||
fflush(eventlog);
|
|
||||||
} else
|
|
||||||
/** Cannot use ast_log() from locked section of ast_log()!
|
|
||||||
ast_log(LOG_WARNING, "Unable to retrieve local time?\n"); **/
|
|
||||||
fprintf(stderr, "ast_log: Unable to retrieve local time for %ld?\n", (long)t);
|
|
||||||
} else {
|
|
||||||
if (logfiles) {
|
|
||||||
f = logfiles;
|
|
||||||
while(f) {
|
|
||||||
if (f->logflags & (1 << level) && f->facility) {
|
|
||||||
time(&t);
|
|
||||||
localtime_r(&t,&tm);
|
|
||||||
strftime(date, sizeof(date), "%b %e %T", &tm);
|
|
||||||
|
|
||||||
openlog("asterisk_pbx",LOG_PID,f->facility);
|
|
||||||
syslog(LOG_INFO|f->facility,"%s %s[%ld]: File %s, Line %d (%s): ",date,
|
|
||||||
levels[level], (long)pthread_self(), file, line, function);
|
|
||||||
closelog();
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (f->logflags & (1 << level) && f->f) {
|
|
||||||
if ((f->f != stdout) && (f->f != stderr)) {
|
|
||||||
time(&t);
|
|
||||||
localtime_r(&t,&tm);
|
|
||||||
strftime(date, sizeof(date), "%b %e %T", &tm);
|
|
||||||
fprintf(f->f, "%s %s[%ld]: File %s, Line %d (%s): ", date, levels[level], (long)pthread_self(), file, line, function);
|
|
||||||
} else {
|
|
||||||
sprintf(linestr, "%d", line);
|
|
||||||
fprintf(f->f, "%s[%ld]: File %s, Line %s (%s): ",
|
|
||||||
term_color(tmp, levels[level], colors[level], 0, sizeof(tmp)),
|
|
||||||
(long)pthread_self(),
|
|
||||||
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)));
|
|
||||||
}
|
|
||||||
va_start(ap, fmt);
|
|
||||||
vfprintf(f->f, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
fflush(f->f);
|
|
||||||
}
|
|
||||||
f = f->next;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fprintf(stdout, "%s[%ld]: File %s, Line %d (%s): ", levels[level], (long)pthread_self(), file, line, function);
|
|
||||||
va_start(ap, fmt);
|
|
||||||
vfprintf(stdout, fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
fflush(stdout);
|
|
||||||
}
|
}
|
||||||
|
strftime(date, sizeof(date), "%b %e %T", &tm);
|
||||||
|
|
||||||
|
sprintf(linestr, "%d", line);
|
||||||
|
snprintf(buf, sizeof(buf), "%s %s[%ld]: %s:%s %s: ",
|
||||||
|
date,
|
||||||
|
term_color(tmp1, levels[level], colors[level], 0, sizeof(tmp1)),
|
||||||
|
(long)pthread_self(),
|
||||||
|
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)));
|
||||||
|
|
||||||
|
if (chan->console)
|
||||||
|
ast_console_puts(buf);
|
||||||
|
else
|
||||||
|
fprintf(chan->fileptr, buf);
|
||||||
|
va_start(ap, fmt);
|
||||||
|
vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
if (chan->console)
|
||||||
|
ast_console_puts(buf);
|
||||||
|
else
|
||||||
|
fprintf(chan->fileptr, buf);
|
||||||
|
|
||||||
|
}
|
||||||
|
chan = chan->next;
|
||||||
}
|
}
|
||||||
ast_mutex_unlock(&loglock);
|
}
|
||||||
|
|
||||||
|
ast_mutex_unlock(&loglock);
|
||||||
|
/* end critical section */
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void ast_verbose(const char *fmt, ...)
|
extern void ast_verbose(const char *fmt, ...)
|
||||||
@@ -498,6 +548,13 @@ extern void ast_verbose(const char *fmt, ...)
|
|||||||
else
|
else
|
||||||
replacelast = pos = 0;
|
replacelast = pos = 0;
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
if(stuff[strlen(stuff)-1] == '\n')
|
||||||
|
stuff[strlen(stuff)-1] = '\0';
|
||||||
|
ast_log_vsyslog(0, NULL, 0, NULL, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
ast_mutex_unlock(&msglist_lock);
|
ast_mutex_unlock(&msglist_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -903,6 +903,8 @@ static int handle_save_dialplan(int fd, int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
char filename[256];
|
char filename[256];
|
||||||
struct ast_context *c;
|
struct ast_context *c;
|
||||||
|
struct ast_config *cfg;
|
||||||
|
struct ast_variable *var;
|
||||||
int context_header_written;
|
int context_header_written;
|
||||||
int incomplete = 0; /* incomplete config write? */
|
int incomplete = 0; /* incomplete config write? */
|
||||||
FILE *output;
|
FILE *output;
|
||||||
@@ -922,6 +924,9 @@ static int handle_save_dialplan(int fd, int argc, char *argv[])
|
|||||||
return RESULT_FAILURE;
|
return RESULT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Load old file first */
|
||||||
|
cfg = ast_load(config);
|
||||||
|
|
||||||
/* have config path? */
|
/* have config path? */
|
||||||
if (argc == 3) {
|
if (argc == 3) {
|
||||||
/* is there extension.conf too? */
|
/* is there extension.conf too? */
|
||||||
@@ -946,6 +951,8 @@ static int handle_save_dialplan(int fd, int argc, char *argv[])
|
|||||||
if (ast_lock_contexts()) {
|
if (ast_lock_contexts()) {
|
||||||
ast_cli(fd, "Failed to lock contexts list\n");
|
ast_cli(fd, "Failed to lock contexts list\n");
|
||||||
ast_mutex_unlock(&save_dialplan_lock);
|
ast_mutex_unlock(&save_dialplan_lock);
|
||||||
|
if (cfg)
|
||||||
|
ast_destroy(cfg);
|
||||||
return RESULT_FAILURE;
|
return RESULT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -955,6 +962,8 @@ static int handle_save_dialplan(int fd, int argc, char *argv[])
|
|||||||
filename);
|
filename);
|
||||||
ast_unlock_contexts();
|
ast_unlock_contexts();
|
||||||
ast_mutex_unlock(&save_dialplan_lock);
|
ast_mutex_unlock(&save_dialplan_lock);
|
||||||
|
if (cfg)
|
||||||
|
ast_destroy(cfg);
|
||||||
return RESULT_FAILURE;
|
return RESULT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -963,6 +972,15 @@ static int handle_save_dialplan(int fd, int argc, char *argv[])
|
|||||||
static_config ? "yes" : "no",
|
static_config ? "yes" : "no",
|
||||||
write_protect_config ? "yes" : "no");
|
write_protect_config ? "yes" : "no");
|
||||||
|
|
||||||
|
var = ast_variable_walk(cfg, "globals");
|
||||||
|
if (var) {
|
||||||
|
fprintf(output, "[globals]\n");
|
||||||
|
while(var) {
|
||||||
|
fprintf(output, "%s => %s", var->name, var->value);
|
||||||
|
var = var->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* walk all contexts */
|
/* walk all contexts */
|
||||||
c = ast_walk_contexts(NULL);
|
c = ast_walk_contexts(NULL);
|
||||||
while (c) {
|
while (c) {
|
||||||
@@ -1079,6 +1097,8 @@ static int handle_save_dialplan(int fd, int argc, char *argv[])
|
|||||||
ast_unlock_contexts();
|
ast_unlock_contexts();
|
||||||
ast_mutex_unlock(&save_dialplan_lock);
|
ast_mutex_unlock(&save_dialplan_lock);
|
||||||
fclose(output);
|
fclose(output);
|
||||||
|
if (cfg)
|
||||||
|
ast_destroy(cfg);
|
||||||
|
|
||||||
if (incomplete) {
|
if (incomplete) {
|
||||||
ast_cli(fd, "Saved dialplan is incomplete\n");
|
ast_cli(fd, "Saved dialplan is incomplete\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user