diff --git a/daemon/call.c b/daemon/call.c index a49c946ce..9bbb29ac0 100644 --- a/daemon/call.c +++ b/daemon/call.c @@ -327,7 +327,7 @@ retry: for (i = 0; i < 100; i++) close(i); - if (!_log_stderr) { + if (!ilog_stderr) { openlog("rtpengine/child", LOG_PID | LOG_NDELAY, LOG_DAEMON); } ilog(LOG_INFO, "Initiating XMLRPC call for tag "STR_FORMAT"", STR_FMT(tag)); diff --git a/daemon/main.c b/daemon/main.c index 140dd222c..10217dc94 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -153,28 +153,6 @@ static void resources(void) { -static int parse_log_facility(char *name, int *dst) { - int i; - for (i = 0 ; _facilitynames[i].c_name; i++) { - if (strcmp(_facilitynames[i].c_name, name) == 0) { - *dst = _facilitynames[i].c_val; - return 1; - } - } - return 0; -} - -static void print_available_log_facilities () { - int i; - - fprintf(stderr, "available facilities:"); - for (i = 0 ; _facilitynames[i].c_name; i++) { - fprintf(stderr, " %s", _facilitynames[i].c_name); - } - fprintf(stderr, "\n"); -} - - static struct intf_config *if_addr_parse(char *s) { str name; char *c; @@ -268,7 +246,6 @@ static void options(int *argc, char ***argv) { char *graphite_prefix_s = NULL; char *redisps = NULL; char *redisps_write = NULL; - char *log_facility_s = NULL; char *log_facility_cdr_s = NULL; char *log_facility_rtcp_s = NULL; int sip_source = 0; @@ -300,10 +277,8 @@ static void options(int *argc, char ***argv) { { "redis-expires", 0, 0, G_OPTION_ARG_INT, &redis_expires, "Expire time in seconds for redis keys", "INT" }, { "no-redis-required", 'q', 0, G_OPTION_ARG_NONE, &no_redis_required, "Start no matter of redis connection state", NULL }, { "b2b-url", 'b', 0, G_OPTION_ARG_STRING, &b2b_url, "XMLRPC URL of B2B UA" , "STRING" }, - { "log-facility",0, 0, G_OPTION_ARG_STRING, &log_facility_s, "Syslog facility to use for logging", "daemon|local0|...|local7"}, { "log-facility-cdr",0, 0, G_OPTION_ARG_STRING, &log_facility_cdr_s, "Syslog facility to use for logging CDRs", "daemon|local0|...|local7"}, { "log-facility-rtcp",0, 0, G_OPTION_ARG_STRING, &log_facility_rtcp_s, "Syslog facility to use for logging RTCP", "daemon|local0|...|local7"}, - { "log-stderr", 'E', 0, G_OPTION_ARG_NONE, &_log_stderr, "Log on stderr instead of syslog", NULL }, { "xmlrpc-format",'x', 0, G_OPTION_ARG_INT, &xmlrpc_fmt, "XMLRPC timeout request format to use. 0: SEMS DI, 1: call-id only", "INT" }, { "num-threads", 0, 0, G_OPTION_ARG_INT, &num_threads, "Number of worker threads to create", "INT" }, { "delete-delay", 'd', 0, G_OPTION_ARG_INT, &delete_delay, "Delay for deleting a session from memory.", "INT" }, @@ -415,13 +390,6 @@ static void options(int *argc, char ***argv) { if ((log_level < LOG_EMERG) || (log_level > LOG_DEBUG)) die("Invalid log level (--log_level)"); - if (log_facility_s) { - if (!parse_log_facility(log_facility_s, &_log_facility)) { - print_available_log_facilities(); - die ("Invalid log facility '%s' (--log-facility)", log_facility_s); - } - } - if (log_facility_cdr_s) { if (!parse_log_facility(log_facility_cdr_s, &_log_facility_cdr)) { print_available_log_facilities(); @@ -436,11 +404,6 @@ static void options(int *argc, char ***argv) { } } - if (_log_stderr) { - write_log = log_to_stderr; - max_log_line_length = 0; - } - if (!sip_source) trust_address_def = 1; } @@ -491,7 +454,7 @@ static void early_init() { static void init_everything() { struct timespec ts; - log_init(); + log_init("rtpengine"); recording_fs_init(spooldir, rec_method, rec_format); clock_gettime(CLOCK_REALTIME, &ts); srandom(ts.tv_sec ^ ts.tv_nsec); @@ -507,8 +470,6 @@ static void init_everything() { g_type_init(); #endif - if (!_log_stderr) - openlog("rtpengine", LOG_PID | LOG_NDELAY, _log_facility); signals(); resources(); sdp_init(); diff --git a/lib/auxlib.c b/lib/auxlib.c index bd5fb9d8b..8f24fb0fb 100644 --- a/lib/auxlib.c +++ b/lib/auxlib.c @@ -12,6 +12,7 @@ static const char *config_file; static const char *config_section; static const char *pid_file; +static const char *log_facility; static int foreground; static int version; @@ -57,7 +58,9 @@ static const GOptionEntry shared_options[] = { { "version", 'v', 0, G_OPTION_ARG_NONE, &version, "Print build time and exit", NULL }, { "config-file", 0, 0, G_OPTION_ARG_STRING, &config_file, "Load config from this file", "FILE" }, { "config-section", 0, 0, G_OPTION_ARG_STRING, &config_section,"Config file section to use", "STRING" }, + { "log-facility", 0, 0, G_OPTION_ARG_STRING, &log_facility, "Syslog facility to use for logging", "daemon|local0|...|local7"}, { "log-level", 'L', 0, G_OPTION_ARG_INT, (void *)&log_level,"Mask log priorities above this level","INT" }, + { "log-stderr", 'E', 0, G_OPTION_ARG_NONE, &ilog_stderr, "Log on stderr instead of syslog", NULL }, { "pidfile", 'p', 0, G_OPTION_ARG_FILENAME, &pid_file, "Write PID to file", "FILE" }, { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground, "Don't fork to background", NULL }, { NULL, } @@ -149,6 +152,20 @@ out: exit(0); } + + if (log_facility) { + if (!parse_log_facility(log_facility, &ilog_facility)) { + print_available_log_facilities(); + die ("Invalid log facility '%s' (--log-facility)", log_facility); + } + } + + if (ilog_stderr) { + write_log = log_to_stderr; + max_log_line_length = 0; + } + + return; err: diff --git a/lib/loglib.c b/lib/loglib.c index 4b4dddf3c..2f80e4f41 100644 --- a/lib/loglib.c +++ b/lib/loglib.c @@ -7,6 +7,7 @@ #include #include #include +#include struct log_limiter_entry { @@ -14,6 +15,12 @@ struct log_limiter_entry { char *msg; }; +typedef struct _fac_code { + char *c_name; + int c_val; +} _fac_code_t; + + #ifndef __DEBUG volatile gint log_level = LOG_INFO; @@ -30,7 +37,7 @@ write_log_t *write_log = (write_log_t *) log_both; -const _fac_code_t _facilitynames[] = +static const _fac_code_t _facilitynames[] = { { "auth", LOG_AUTH }, { "authpriv", LOG_AUTHPRIV }, @@ -66,8 +73,8 @@ static const char* const prio_str[] = { "DEBUG" }; -gboolean _log_stderr = 0; -int _log_facility = LOG_DAEMON; +gboolean ilog_stderr = 0; +int ilog_facility = LOG_DAEMON; @@ -218,8 +225,32 @@ static int log_limiter_entry_equal(const void *a, const void *b) { return 1; } -void log_init() { +void log_init(const char *handle) { pthread_mutex_init(&__log_limiter_lock, NULL); __log_limiter = g_hash_table_new(log_limiter_entry_hash, log_limiter_entry_equal); __log_limiter_strings = g_string_chunk_new(1024); + + if (!ilog_stderr) + openlog(handle, LOG_PID | LOG_NDELAY, ilog_facility); +} + +int parse_log_facility(const char *name, int *dst) { + int i; + for (i = 0 ; _facilitynames[i].c_name; i++) { + if (strcmp(_facilitynames[i].c_name, name) == 0) { + *dst = _facilitynames[i].c_val; + return 1; + } + } + return 0; +} + +void print_available_log_facilities () { + int i; + + fprintf(stderr, "available facilities:"); + for (i = 0 ; _facilitynames[i].c_name; i++) { + fprintf(stderr, " %s", _facilitynames[i].c_name); + } + fprintf(stderr, "\n"); } diff --git a/lib/loglib.h b/lib/loglib.h index 26e1fe0c9..72aeffd67 100644 --- a/lib/loglib.h +++ b/lib/loglib.h @@ -8,16 +8,8 @@ #include "compat.h" -typedef struct _fac_code { - char *c_name; - int c_val; -} _fac_code_t; - -extern const _fac_code_t _facilitynames[]; - - -extern gboolean _log_stderr; -extern int _log_facility; +extern gboolean ilog_stderr; +extern int ilog_facility; extern volatile gint log_level; @@ -27,9 +19,12 @@ extern unsigned int max_log_line_length; typedef void write_log_t(int facility_priority, const char *format, ...) __attribute__ ((format (printf, 2, 3))); extern write_log_t *write_log; +int parse_log_facility(const char *name, int *dst); +void print_available_log_facilities (); + void log_to_stderr(int facility_priority, const char *format, ...) __attribute__ ((format (printf, 2, 3))); -void log_init(void); +void log_init(const char *); void __vpilog(int prio, const char *prefix, const char *fmt, va_list); void __ilog_np(int prio, const char *format, ...) __attribute__ ((format (printf, 2, 3))); diff --git a/recording-daemon/main.c b/recording-daemon/main.c index e0c267352..bdc996cd6 100644 --- a/recording-daemon/main.c +++ b/recording-daemon/main.c @@ -89,9 +89,7 @@ static void avlog_ilog(void *ptr, int loglevel, const char *fmt, va_list ap) { static void setup(void) { - openlog("rtpengine-recording", LOG_PID | LOG_NDELAY, LOG_DAEMON); - - log_init(); + log_init("rtpengine-recording"); av_register_all(); avcodec_register_all(); avfilter_register_all();