Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Macros | Enumerations | Functions | Variables
logger.c File Reference

Asterisk Logger. More...

#include "asterisk.h"
#include <syslog.h>
#include <signal.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "asterisk/_private.h"
#include "asterisk/module.h"
#include "asterisk/paths.h"
#include "asterisk/logger.h"
#include "asterisk/logger_category.h"
#include "asterisk/lock.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"
#include "asterisk/astobj2.h"
#include "asterisk/threadstorage.h"
#include "asterisk/strings.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/buildinfo.h"
#include "asterisk/ast_version.h"
#include "asterisk/backtrace.h"
#include "asterisk/json.h"

Go to the source code of this file.

Data Structures

struct  chan_group_lock
 map call ID to group More...
 
struct  chan_group_lock_list
 
struct  logchannel
 
struct  logchannels
 
struct  logformatter
 
struct  logmsg
 
struct  logmsgs
 
struct  verb_console
 
struct  verb_consoles
 

Macros

#define FORMATL   "%-35.35s %-8.8s %-10.10s %-9.9s "
 
#define FORMATL2   "%5s %s\n"
 
#define LOG_BUF_INIT_SIZE   256
 
#define LOGMSG_SIZE   MAX(BUFSIZ, 8192)
 
#define VERBOSE_BUF_INIT_SIZE   256
 

Enumerations

enum  logmsgtypes { LOGMSG_NORMAL = 0, LOGMSG_VERBOSE }
 
enum  logtypes { LOGTYPE_SYSLOG, LOGTYPE_FILE, LOGTYPE_CONSOLE }
 
enum  rotatestrategy { NONE = 0, SEQUENTIAL = 1 << 0, ROTATE = 1 << 1, TIMESTAMP = 1 << 2 }
 

Functions

void __ast_verbose (const char *file, int line, const char *func, int level, const char *fmt,...)
 Send a verbose message (based on verbose level) More...
 
void __ast_verbose_ap (const char *file, int line, const char *func, int level, ast_callid callid, const char *fmt, va_list ap)
 
void __ast_verbose_callid (const char *file, int line, const char *func, int level, ast_callid callid, const char *fmt,...)
 Send a verbose message (based on verbose level) with deliberately specified callid. More...
 
static void __init_callid_group_name (void)
 
static void __init_log_buf (void)
 
static void __init_my_verb_console (void)
 
static void __init_unique_callid (void)
 
static void __init_verbose_buf (void)
 
static void __init_verbose_build_buf (void)
 
static void __reg_module (void)
 
static void __unreg_module (void)
 
static void _handle_SIGXFSZ (int sig)
 
void ast_callid_strnprint (char *buffer, size_t buffer_size, ast_callid callid)
 copy a string representation of the callid into a target string More...
 
int ast_callid_threadassoc_add (ast_callid callid)
 Adds a known callid to thread storage of the calling thread. More...
 
int ast_callid_threadassoc_change (ast_callid callid)
 Sets what is stored in the thread storage to the given callid if it does not match what is already there. More...
 
int ast_callid_threadassoc_remove (void)
 Removes callid from thread storage of the calling thread. More...
 
int ast_callid_threadstorage_auto (ast_callid *callid)
 Checks thread storage for a callid and stores a reference if it exists. If not, then a new one will be created, bound to the thread, and a reference to it will be stored. More...
 
void ast_callid_threadstorage_auto_clean (ast_callid callid, int callid_created)
 Use in conjunction with ast_callid_threadstorage_auto. Cleans up the references and if the callid was created by threadstorage_auto, unbinds the callid from the threadstorage. More...
 
void ast_child_verbose (int level, const char *fmt,...)
 
ast_callid ast_create_callid (void)
 factory function to create a new uniquely identifying callid. More...
 
void ast_init_logger_for_socket_console (void)
 load logger.conf configuration for console socket connections
 
int ast_is_logger_initialized (void)
 Test if logger is initialized. More...
 
void ast_log (int level, const char *file, int line, const char *function, const char *fmt,...)
 Used for sending a log message This is the standard logger function. Probably the only way you will invoke it would be something like this: ast_log(AST_LOG_WHATEVER, "Problem with the %s Captain. We should get some more. Will %d be enough?\n", "flux capacitor", 10); where WHATEVER is one of ERROR, DEBUG, EVENT, NOTICE, or WARNING depending on which log you wish to output to. These are implemented as macros, that will provide the function with the needed arguments. More...
 
void ast_log_ap (int level, const char *file, int line, const char *function, const char *fmt, va_list ap)
 
void ast_log_backtrace (void)
 Log a backtrace of the current thread's execution stack to the Asterisk log.
 
void ast_log_callid (int level, const char *file, int line, const char *function, ast_callid callid, const char *fmt,...)
 Used for sending a log message with a known call_id This is a modified logger function which is functionally identical to the above logger function, it just include a call_id argument as well. If NULL is specified here, no attempt will be made to join the log message with a call_id. More...
 
static void ast_log_full (int level, int sublevel, const char *file, int line, const char *function, ast_callid callid, const char *fmt, va_list ap)
 send log messages to syslog and/or the console
 
void ast_log_safe (int level, const char *file, int line, const char *function, const char *fmt,...)
 Used for sending a log message with protection against recursion. More...
 
int ast_logger_create_channel (const char *log_channel, const char *components)
 Create a log channel. More...
 
int ast_logger_get_channels (int(*logentry)(const char *channel, const char *type, const char *status, const char *configuration, void *data), void *data)
 Retrieve the existing log channels. More...
 
const char * ast_logger_get_dateformat (void)
 Get the logger configured date format. More...
 
int ast_logger_get_dynamic_level (const char *name)
 Retrieve dynamic logging level id. More...
 
int ast_logger_get_queue_limit (void)
 Get the maximum number of messages allowed in the processing queue. More...
 
int ast_logger_register_level (const char *name)
 Register a new logger level. More...
 
int ast_logger_remove_channel (const char *log_channel)
 Delete the specified log channel. More...
 
int ast_logger_rotate ()
 Reload logger while rotating log files.
 
int ast_logger_rotate_channel (const char *log_channel)
 Rotate the specified log channel. More...
 
void ast_logger_set_queue_limit (int queue_limit)
 Set the maximum number of messages allowed in the processing queue. More...
 
void ast_logger_unregister_level (const char *name)
 Unregister a previously registered logger level. More...
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
void ast_queue_log (const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt,...)
 
ast_callid ast_read_threadstorage_callid (void)
 extracts the callerid from the thread More...
 
 AST_THREADSTORAGE_RAW (in_safe_log)
 
int ast_verb_console_get (void)
 Get this thread's console verbosity level. More...
 
void ast_verb_console_register (int *level)
 Register this thread's console verbosity level pointer. More...
 
void ast_verb_console_set (int verb_level)
 Set this thread's console verbosity level. More...
 
void ast_verb_console_unregister (void)
 Unregister this thread's console verbosity level.
 
void ast_verb_update (void)
 Re-evaluate the system max verbosity level (ast_verb_sys_level).
 
static int callid_group_remove_filters (void)
 
static int callid_group_set_filter (const char *group, int enabled)
 
static int callid_logging_enabled (void)
 
static int callid_set_chanloggroup (const char *group)
 
void close_logger (void)
 
static int custom_level_still_exists (char **levels, char *level, size_t len)
 Checks if level exists in array of level names. More...
 
static struct logchannelfind_logchannel (const char *channel)
 Find a particular logger channel by name. More...
 
static int format_log_default (struct logchannel *chan, struct logmsg *msg, char *buf, size_t size)
 
static int format_log_json (struct logchannel *channel, struct logmsg *msg, char *buf, size_t size)
 
static struct logmsgformat_log_message (int level, int sublevel, const char *file, int line, const char *function, ast_callid callid, const char *fmt,...)
 
static struct logmsgformat_log_message_ap (int level, int sublevel, const char *file, int line, const char *function, ast_callid callid, const char *fmt, va_list ap)
 
static int format_log_plain (struct logchannel *chan, struct logmsg *msg, char *buf, size_t size)
 
static const char * get_callid_group (void)
 
static char * handle_logger_add_channel (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_logger_chanloggroup_filter (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_logger_filter_reset (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_logger_filter_show (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_logger_reload (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_logger_remove_channel (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_logger_rotate (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_logger_set_level (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_logger_show_channels (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 CLI command to show logging system configuration.
 
static char * handle_logger_show_levels (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 CLI command to show logging levels.
 
int init_logger (void)
 
static int init_logger_chain (const char *altconf)
 Read config, setup channels. More...
 
static int load_module (void)
 
static int log_group_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 
static int logger_add_verbose_magic (struct logmsg *logmsg, char *buf, size_t size)
 
static int logger_get_dynamic_level (const char *name)
 
static void logger_print_normal (struct logmsg *logmsg)
 Print a normal log message to the channels.
 
static void logger_queue_init (void)
 
static int logger_queue_restart (int queue_rotate)
 
static int logger_queue_rt_start (void)
 
void logger_queue_start (void)
 Start the ast_queue_log() logger. More...
 
static int logger_register_level (const char *name)
 
static void * logger_thread (void *data)
 Actual logging thread.
 
static int logger_unregister_level (const char *name)
 
static void logmsg_free (struct logmsg *msg)
 
static void make_components (struct logchannel *chan)
 
static void make_filename (const char *channel, char *filename, size_t size)
 create the filename that will be used for a logger channel. More...
 
static struct logchannelmake_logchannel (const char *channel, const char *components, int lineno, int dynamic)
 
static int reload_logger (int rotate, const char *altconf)
 
static int reload_module (void)
 
static int rotate_file (const char *filename)
 
static int unload_module (void)
 
static void update_logchannels (void)
 
static void verb_console_free (void *v_console)
 
static void verb_console_unregister (struct verb_console *console)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER , .description = "Logger" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "da6642af068ee5e6490c5b1d2cc1d238" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload_module, .load_pri = 0, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static int callid_filtering = 0
 
static struct ast_threadstorage callid_group_name = { .once = PTHREAD_ONCE_INIT , .key_init = __init_callid_group_name , .custom_init = NULL , }
 
struct chan_group_lock_list chan_group_lock_list = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
 
static struct ast_cli_entry cli_logger []
 
static int close_logger_thread = 0
 
static const int colors [NUMLOGLEVELS]
 Colors used in the console for logging.
 
static char * custom_dynamic_levels [NUMLOGLEVELS]
 Custom dynamic logging levels added by the user. More...
 
static char dateformat [256] = "%b %e %T"
 
static int display_callids
 
static char exec_after_rotate [256] = ""
 
static int filesize_reload_needed
 
static unsigned int global_logmask = 0xFFFF
 
static struct sigaction handle_SIGXFSZ
 
static unsigned int high_water_alert
 
static char hostname [MAXHOSTNAMELEN]
 
static char * levels [NUMLOGLEVELS]
 Logging channels used in the Asterisk logging system. More...
 
static struct ast_threadstorage log_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_log_buf , .custom_init = NULL , }
 
static struct ast_custom_function log_group_function
 
static struct logchannels logchannels = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
 
static ast_cond_t logcond
 
struct {
   unsigned int   queue_adaptive_realtime:1
 
   unsigned int   queue_log:1
 
   unsigned int   queue_log_realtime_use_gmt:1
 
   unsigned int   queue_log_to_file:1
 
logfiles = { 1 }
 
static struct logformatter logformatter_default
 
static struct logformatter logformatter_json
 
static struct logformatter logformatter_plain
 
static int logger_initialized
 
static int logger_messages_discarded
 
static int logger_queue_limit = 1000
 
static int logger_queue_size
 
static struct logmsgs logmsgs = { .first = NULL, .last = NULL, .lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } , }
 
static pthread_t logthread = AST_PTHREADT_NULL
 
static struct ast_threadstorage my_verb_console = { .once = PTHREAD_ONCE_INIT , .key_init = __init_my_verb_console , .custom_init = NULL , }
 
static volatile int next_unique_callid = 1
 
static FILE * qlog
 
static char queue_log_name [256] = QUEUELOG
 
static int queuelog_init
 
static enum rotatestrategy rotatestrategy = SEQUENTIAL
 
static struct ast_threadstorage unique_callid = { .once = PTHREAD_ONCE_INIT , .key_init = __init_unique_callid , .custom_init = NULL , }
 
static struct verb_consoles verb_consoles = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
 
static ast_mutex_t verb_update_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
 
static struct ast_threadstorage verbose_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_verbose_buf , .custom_init = NULL , }
 
static struct ast_threadstorage verbose_build_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_verbose_build_buf , .custom_init = NULL , }
 

Detailed Description

Asterisk Logger.

Logging routines

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file logger.c.

Function Documentation

void __ast_verbose ( const char *  file,
int  line,
const char *  func,
int  level,
const char *  fmt,
  ... 
)

Send a verbose message (based on verbose level)

This works like ast_log, but prints verbose messages to the console depending on verbosity level set.

ast_verbose(VERBOSE_PREFIX_3 "Whatever %s is happening\n", "nothing");

This will print the message to the console if the verbose level is set to a level >= 3

Note the absence of a comma after the VERBOSE_PREFIX_3. This is important. VERBOSE_PREFIX_1 through VERBOSE_PREFIX_10 are defined.

Version
11 added level parameter

Definition at line 2550 of file logger.c.

References ast_read_threadstorage_callid().

2551 {
2552  ast_callid callid;
2553  va_list ap;
2554 
2555  callid = ast_read_threadstorage_callid();
2556 
2557  va_start(ap, fmt);
2558  __ast_verbose_ap(file, line, func, level, callid, fmt, ap);
2559  va_end(ap);
2560 }
ast_callid ast_read_threadstorage_callid(void)
extracts the callerid from the thread
Definition: logger.c:2298
void __ast_verbose_callid ( const char *  file,
int  line,
const char *  func,
int  level,
ast_callid  callid,
const char *  fmt,
  ... 
)

Send a verbose message (based on verbose level) with deliberately specified callid.

just like __ast_verbose, only __ast_verbose_callid allows you to specify which callid is being used for the log without needing to bind it to a thread. NULL is a valid argument for this function and will allow you to specify that a log will never display a call id even when there is a call id bound to the thread.

Definition at line 2562 of file logger.c.

2563 {
2564  va_list ap;
2565  va_start(ap, fmt);
2566  __ast_verbose_ap(file, line, func, level, callid, fmt, ap);
2567  va_end(ap);
2568 }
static void __init_my_verb_console ( void  )
static

Thread specific console verbosity level node.

Definition at line 2644 of file logger.c.

2647 {
void ast_callid_strnprint ( char *  buffer,
size_t  buffer_size,
ast_callid  callid 
)

copy a string representation of the callid into a target string

Parameters
bufferdestination of callid string (should be able to store 13 characters or more)
buffer_sizemaximum writable length of the string (Less than 13 will result in truncation)
callidCallid for which string is being requested

Definition at line 2288 of file logger.c.

Referenced by ast_channel_callid_set(), and handle_showchan().

2289 {
2290  snprintf(buffer, buffer_size, "[C-%08x]", callid);
2291 }
int ast_callid_threadassoc_add ( ast_callid  callid)

Adds a known callid to thread storage of the calling thread.

Return values
0- success
non-zero- failure

Definition at line 2320 of file logger.c.

References ast_log(), and ast_threadstorage_get().

Referenced by __ast_pbx_run(), ast_callid_threadstorage_auto(), async_dial(), attended_transfer_monitor_thread(), bridge_channel_depart_thread(), bridge_channel_ind_thread(), jingle_action_hook(), and jingle_outgoing_hook().

2321 {
2322  ast_callid *pointing;
2323 
2324  pointing = ast_threadstorage_get(&unique_callid, sizeof(*pointing));
2325  if (!pointing) {
2326  return -1;
2327  }
2328 
2329  if (*pointing) {
2330  ast_log(LOG_ERROR, "ast_callid_threadassoc_add(C-%08x) on thread "
2331  "already associated with callid [C-%08x].\n", callid, *pointing);
2332  return 1;
2333  }
2334 
2335  *pointing = callid;
2336  return 0;
2337 }
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:2453
int ast_callid_threadassoc_change ( ast_callid  callid)

Sets what is stored in the thread storage to the given callid if it does not match what is already there.

Return values
0- success
non-zero- failure

Definition at line 2307 of file logger.c.

References ast_threadstorage_get().

2308 {
2309  ast_callid *id = ast_threadstorage_get(&unique_callid, sizeof(*id));
2310 
2311  if (!id) {
2312  return -1;
2313  }
2314 
2315  *id = callid;
2316 
2317  return 0;
2318 }
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
int ast_callid_threadassoc_remove ( void  )

Removes callid from thread storage of the calling thread.

Return values
0- success
non-zero- failure

Definition at line 2339 of file logger.c.

References ast_threadstorage_get().

Referenced by ast_callid_threadstorage_auto_clean(), attended_transfer_monitor_thread(), jingle_action_hook(), and jingle_outgoing_hook().

2340 {
2341  ast_callid *pointing;
2342 
2343  pointing = ast_threadstorage_get(&unique_callid, sizeof(*pointing));
2344  if (!pointing) {
2345  return -1;
2346  }
2347 
2348  if (*pointing) {
2349  *pointing = 0;
2350  return 0;
2351  }
2352 
2353  return -1;
2354 }
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
int ast_callid_threadstorage_auto ( ast_callid *  callid)

Checks thread storage for a callid and stores a reference if it exists. If not, then a new one will be created, bound to the thread, and a reference to it will be stored.

Parameters
callidpointer to store the callid
Return values
0- callid was found
1- callid was created
-1- the function failed somehow (presumably memory problems)

Definition at line 2356 of file logger.c.

References ast_callid_threadassoc_add(), ast_create_callid(), and ast_read_threadstorage_callid().

Referenced by __analog_handle_event().

2357 {
2358  ast_callid tmp;
2359 
2360  /* Start by trying to see if a callid is available from thread storage */
2362  if (tmp) {
2363  *callid = tmp;
2364  return 0;
2365  }
2366 
2367  /* If that failed, try to create a new one and bind it. */
2368  *callid = ast_create_callid();
2369  if (*callid) {
2370  ast_callid_threadassoc_add(*callid);
2371  return 1;
2372  }
2373 
2374  /* If neither worked, then something must have gone wrong. */
2375  return -1;
2376 }
ast_callid ast_read_threadstorage_callid(void)
extracts the callerid from the thread
Definition: logger.c:2298
int ast_callid_threadassoc_add(ast_callid callid)
Adds a known callid to thread storage of the calling thread.
Definition: logger.c:2320
ast_callid ast_create_callid(void)
factory function to create a new uniquely identifying callid.
Definition: logger.c:2293
void ast_callid_threadstorage_auto_clean ( ast_callid  callid,
int  callid_created 
)

Use in conjunction with ast_callid_threadstorage_auto. Cleans up the references and if the callid was created by threadstorage_auto, unbinds the callid from the threadstorage.

Parameters
callidThe callid set by ast_callid_threadstorage_auto
callid_createdThe integer returned through ast_callid_threadstorage_auto

Definition at line 2378 of file logger.c.

References ast_callid_threadassoc_remove().

Referenced by __analog_handle_event().

2379 {
2380  if (callid && callid_created) {
2381  /* If the callid was created rather than simply grabbed from the thread storage, we need to unbind here. */
2383  }
2384 }
int ast_callid_threadassoc_remove(void)
Removes callid from thread storage of the calling thread.
Definition: logger.c:2339
ast_callid ast_create_callid ( void  )

factory function to create a new uniquely identifying callid.

Returns
The call id

Definition at line 2293 of file logger.c.

References ast_atomic_fetchadd_int().

Referenced by __ast_pbx_run(), ast_callid_threadstorage_auto(), and jingle_alloc().

2294 {
2295  return ast_atomic_fetchadd_int(&next_unique_callid, +1);
2296 }
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:757
int ast_is_logger_initialized ( void  )

Test if logger is initialized.

Return values
trueif the logger is initialized

Definition at line 2175 of file logger.c.

2176 {
2177  return logger_initialized;
2178 }
void ast_log ( int  level,
const char *  file,
int  line,
const char *  function,
const char *  fmt,
  ... 
)

Used for sending a log message This is the standard logger function. Probably the only way you will invoke it would be something like this: ast_log(AST_LOG_WHATEVER, "Problem with the %s Captain. We should get some more. Will %d be enough?\n", "flux capacitor", 10); where WHATEVER is one of ERROR, DEBUG, EVENT, NOTICE, or WARNING depending on which log you wish to output to. These are implemented as macros, that will provide the function with the needed arguments.

Parameters
levelType of log event
fileWill be provided by the AST_LOG_* macro
lineWill be provided by the AST_LOG_* macro
functionWill be provided by the AST_LOG_* macro
fmtThis is what is important. The format is the same as your favorite breed of printf. You know how that works, right? :-)

Definition at line 2453 of file logger.c.

Referenced by ast_callid_threadassoc_add(), ast_log_backtrace(), and init_logger().

2454 {
2455  va_list ap;
2456 
2457  va_start(ap, fmt);
2458  ast_log_ap(level, file, line, function, fmt, ap);
2459  va_end(ap);
2460 }
void ast_log_callid ( int  level,
const char *  file,
int  line,
const char *  function,
ast_callid  callid,
const char *  fmt,
  ... 
)

Used for sending a log message with a known call_id This is a modified logger function which is functionally identical to the above logger function, it just include a call_id argument as well. If NULL is specified here, no attempt will be made to join the log message with a call_id.

Parameters
levelType of log event
fileWill be provided by the AST_LOG_* macro
lineWill be provided by the AST_LOG_* macro
functionWill be provided by the AST_LOG_* macro
callidThis is the ast_callid that is associated with the log message. May be NULL.
fmtThis is what is important. The format is the same as your favorite breed of printf. You know how that works, right? :-)

Definition at line 2501 of file logger.c.

References ast_log_full().

Referenced by ast_channel_destructor().

2502 {
2503  va_list ap;
2504  va_start(ap, fmt);
2505  ast_log_full(level, -1, file, line, function, callid, fmt, ap);
2506  va_end(ap);
2507 }
static void ast_log_full(int level, int sublevel, const char *file, int line, const char *function, ast_callid callid, const char *fmt, va_list ap)
send log messages to syslog and/or the console
Definition: logger.c:2389
void ast_log_safe ( int  level,
const char *  file,
int  line,
const char *  function,
const char *  fmt,
  ... 
)

Used for sending a log message with protection against recursion.

Note
This function should be used by all error messages that might be directly or indirectly caused by logging.
See also
ast_log for documentation on the parameters.

Definition at line 2475 of file logger.c.

References ast_log_full(), ast_read_threadstorage_callid(), ast_threadstorage_get_ptr(), and ast_threadstorage_set_ptr().

Referenced by __ast_str_helper(), and ast_log_backtrace().

2476 {
2477  va_list ap;
2478  void *recursed = ast_threadstorage_get_ptr(&in_safe_log);
2479  ast_callid callid;
2480 
2481  if (recursed) {
2482  return;
2483  }
2484 
2485  if (ast_threadstorage_set_ptr(&in_safe_log, &(int) { 1 })) {
2486  /* We've failed to set the flag that protects against
2487  * recursion, so bail. */
2488  return;
2489  }
2490 
2491  callid = ast_read_threadstorage_callid();
2492 
2493  va_start(ap, fmt);
2494  ast_log_full(level, -1, file, line, function, callid, fmt, ap);
2495  va_end(ap);
2496 
2497  /* Clear flag so the next allocation failure can be logged. */
2498  ast_threadstorage_set_ptr(&in_safe_log, NULL);
2499 }
int ast_threadstorage_set_ptr(struct ast_threadstorage *ts, void *ptr)
Set a raw pointer from threadstorage.
ast_callid ast_read_threadstorage_callid(void)
extracts the callerid from the thread
Definition: logger.c:2298
void * ast_threadstorage_get_ptr(struct ast_threadstorage *ts)
Retrieve a raw pointer from threadstorage.
static void ast_log_full(int level, int sublevel, const char *file, int line, const char *function, ast_callid callid, const char *fmt, va_list ap)
send log messages to syslog and/or the console
Definition: logger.c:2389
int ast_logger_create_channel ( const char *  log_channel,
const char *  components 
)

Create a log channel.

Parameters
log_channelLog channel to create
componentsLogging config levels to add to the log channel

Definition at line 1521 of file logger.c.

References AST_LOGGER_ALLOC_ERROR, AST_LOGGER_DECLINE, AST_LOGGER_FAILURE, AST_LOGGER_SUCCESS, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, find_logchannel(), logchannel::list, and logchannel::logmask.

Referenced by ast_ari_asterisk_add_log().

1522 {
1523  struct logchannel *chan;
1524 
1525  if (ast_strlen_zero(components)) {
1526  return AST_LOGGER_DECLINE;
1527  }
1528 
1530 
1531  chan = find_logchannel(log_channel);
1532  if (chan) {
1534  return AST_LOGGER_FAILURE;
1535  }
1536 
1537  chan = make_logchannel(log_channel, components, 0, 1);
1538  if (!chan) {
1540  return AST_LOGGER_ALLOC_ERROR;
1541  }
1542 
1543  AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
1544  global_logmask |= chan->logmask;
1545 
1547 
1548  return AST_LOGGER_SUCCESS;
1549 }
char components[0]
Definition: logger.c:162
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
unsigned int logmask
Definition: logger.c:142
static struct logchannel * find_logchannel(const char *channel)
Find a particular logger channel by name.
Definition: logger.c:630
struct logchannel::@372 list
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
int ast_logger_get_channels ( int(*)(const char *channel, const char *type, const char *status, const char *configuration, void *data)  logentry,
void *  data 
)

Retrieve the existing log channels.

Parameters
logentryA callback to an updater function
dataData passed into the callback for manipulation

For each of the logging channels, logentry will be executed with the channel file name, log type, status of the log, and configuration levels.

Return values
0on success
1on failure
-2on allocation error

Definition at line 1409 of file logger.c.

References AST_LOGGER_ALLOC_ERROR, AST_LOGGER_FAILURE, AST_LOGGER_SUCCESS, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_str_append(), ast_str_buffer(), ast_str_create, ast_str_reset(), logchannel::disabled, logchannel::filename, logchannel::list, logchannel::logmask, and logchannel::type.

Referenced by ast_ari_asterisk_list_log_channels().

1411 {
1412  struct logchannel *chan;
1413  struct ast_str *configs = ast_str_create(64);
1414  int res = AST_LOGGER_SUCCESS;
1415 
1416  if (!configs) {
1417  return AST_LOGGER_ALLOC_ERROR;
1418  }
1419 
1421  AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
1422  unsigned int level;
1423 
1424  ast_str_reset(configs);
1425 
1426  for (level = 0; level < ARRAY_LEN(levels); level++) {
1427  if ((chan->logmask & (1 << level)) && levels[level]) {
1428  ast_str_append(&configs, 0, "%s ", levels[level]);
1429  }
1430  }
1431 
1432  res = logentry(chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" :
1433  (chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"), chan->disabled ?
1434  "Disabled" : "Enabled", ast_str_buffer(configs), data);
1435 
1436  if (res) {
1438  ast_free(configs);
1439  configs = NULL;
1440  return AST_LOGGER_FAILURE;
1441  }
1442  }
1444 
1445  ast_free(configs);
1446  configs = NULL;
1447 
1448  return AST_LOGGER_SUCCESS;
1449 }
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
static char * levels[NUMLOGLEVELS]
Logging channels used in the Asterisk logging system.
Definition: logger.c:214
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
unsigned int logmask
Definition: logger.c:142
int disabled
Definition: logger.c:144
Support for dynamic strings.
Definition: strings.h:623
enum logtypes type
Definition: logger.c:150
char filename[PATH_MAX]
Definition: logger.c:154
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition: strings.h:693
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
const char* ast_logger_get_dateformat ( void  )

Get the logger configured date format.

Returns
The date format string
Since
13.0.0

Definition at line 2927 of file logger.c.

2928 {
2929  return dateformat;
2930 }
int ast_logger_get_dynamic_level ( const char *  name)

Retrieve dynamic logging level id.

Parameters
nameThe name of the level
Returns
The unique integer id for the given level
Return values
-1if level name not found

Definition at line 2880 of file logger.c.

References AST_RWLIST_RDLOCK, and AST_RWLIST_UNLOCK.

2881 {
2882  int level = -1;
2883 
2885 
2886  level = logger_get_dynamic_level(name);
2887 
2889 
2890  return level;
2891 }
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
int ast_logger_get_queue_limit ( void  )

Get the maximum number of messages allowed in the processing queue.

Returns
Queue limit

Definition at line 2937 of file logger.c.

2938 {
2939  return logger_queue_limit;
2940 }
int ast_logger_register_level ( const char *  name)

Register a new logger level.

Parameters
nameThe name of the level to be registered
Return values
-1if an error occurs
non-zerolevel to be used with ast_log for sending messages to this level
Since
1.8

Definition at line 2851 of file logger.c.

References AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

Referenced by load_module().

2852 {
2853  int available = 0;
2854 
2856  available = logger_register_level(name);
2858 
2859  return available;
2860 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
int ast_logger_remove_channel ( const char *  log_channel)

Delete the specified log channel.

Parameters
log_channelThe log channel to delete

Definition at line 1587 of file logger.c.

References AST_LOGGER_FAILURE, AST_LOGGER_SUCCESS, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, logchannel::dynamic, logchannel::fileptr, find_logchannel(), and logchannel::list.

Referenced by ast_ari_asterisk_delete_log().

1588 {
1589  struct logchannel *chan;
1590 
1592 
1593  chan = find_logchannel(log_channel);
1594  if (chan && chan->dynamic) {
1595  AST_RWLIST_REMOVE(&logchannels, chan, list);
1596  } else {
1598  return AST_LOGGER_FAILURE;
1599  }
1601 
1602  if (chan->fileptr) {
1603  fclose(chan->fileptr);
1604  chan->fileptr = NULL;
1605  }
1606  ast_free(chan);
1607  chan = NULL;
1608 
1609  return AST_LOGGER_SUCCESS;
1610 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
int dynamic
Definition: logger.c:160
static struct logchannel * find_logchannel(const char *channel)
Find a particular logger channel by name.
Definition: logger.c:630
struct logchannel::@372 list
FILE * fileptr
Definition: logger.c:152
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
int ast_logger_rotate_channel ( const char *  log_channel)

Rotate the specified log channel.

Parameters
log_channelThe log channel to rotate

Definition at line 1332 of file logger.c.

References AST_LOGGER_FAILURE, AST_LOGGER_SUCCESS, ast_mkdir(), AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, logchannel::disabled, logchannel::filename, logchannel::fileptr, init_logger_chain(), logchannel::list, make_filename(), and manager_event.

Referenced by ast_ari_asterisk_rotate_log().

1333 {
1334  struct logchannel *f;
1335  int success = AST_LOGGER_FAILURE;
1336  char filename[PATH_MAX];
1337 
1338  make_filename(log_channel, filename, sizeof(filename));
1339 
1341 
1342  ast_mkdir(ast_config_AST_LOG_DIR, 0644);
1343 
1344  AST_RWLIST_TRAVERSE(&logchannels, f, list) {
1345  if (f->disabled) {
1346  f->disabled = 0; /* Re-enable logging at reload */
1347  manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n",
1348  f->filename);
1349  }
1350  if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
1351  fclose(f->fileptr); /* Close file */
1352  f->fileptr = NULL;
1353  if (strcmp(filename, f->filename) == 0) {
1354  rotate_file(f->filename);
1355  success = AST_LOGGER_SUCCESS;
1356  }
1357  }
1358  }
1359 
1360  init_logger_chain(NULL);
1361 
1363 
1364  return success;
1365 }
static int init_logger_chain(const char *altconf)
Read config, setup channels.
Definition: logger.c:759
static void make_filename(const char *channel, char *filename, size_t size)
create the filename that will be used for a logger channel.
Definition: logger.c:589
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
int disabled
Definition: logger.c:144
struct logchannel::@372 list
FILE * fileptr
Definition: logger.c:152
char filename[PATH_MAX]
Definition: logger.c:154
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define manager_event(category, event, contents,...)
External routines may send asterisk manager events this way.
Definition: manager.h:253
int ast_mkdir(const char *path, int mode)
Recursively create directory path.
Definition: utils.c:2479
void ast_logger_set_queue_limit ( int  queue_limit)

Set the maximum number of messages allowed in the processing queue.

Parameters
queue_limit

Definition at line 2932 of file logger.c.

2933 {
2934  logger_queue_limit = queue_limit;
2935 }
void ast_logger_unregister_level ( const char *  name)

Unregister a previously registered logger level.

Parameters
nameThe name of the level to be unregistered
Since
1.8

Definition at line 2909 of file logger.c.

References ast_debug, AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

Referenced by unload_module().

2910 {
2911  int x;
2912 
2914  x = logger_unregister_level(name);
2915 
2916  if (x) {
2917  update_logchannels();
2918  }
2919 
2921 
2922  if (x) {
2923  ast_debug(1, "Unregistered dynamic logger level '%s' with index %u.\n", name, x);
2924  }
2925 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define ast_debug(level,...)
Log a DEBUG message.
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
ast_callid ast_read_threadstorage_callid ( void  )

extracts the callerid from the thread

Return values
Non-zeroCall id related to the thread
0if no call_id is present in the thread

Definition at line 2298 of file logger.c.

References ast_threadstorage_get().

Referenced by __ast_pbx_run(), __ast_verbose(), ast_callid_threadstorage_auto(), ast_dial_run(), ast_log_safe(), jingle_alloc(), and local_request_with_stream_topology().

2299 {
2300  ast_callid *callid;
2301 
2302  callid = ast_threadstorage_get(&unique_callid, sizeof(*callid));
2303 
2304  return callid ? *callid : 0;
2305 }
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
int ast_verb_console_get ( void  )

Get this thread's console verbosity level.

Returns
verbosity level of the console.

Definition at line 2673 of file logger.c.

References AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_threadstorage_get(), verb_console::level, and option_verbose.

Referenced by handle_show_settings().

2674 {
2675  struct verb_console *console;
2676  int verb_level;
2677 
2678  console = ast_threadstorage_get(&my_verb_console, sizeof(*console));
2680  if (!console) {
2681  verb_level = 0;
2682  } else if (console->level) {
2683  verb_level = *console->level;
2684  } else {
2685  verb_level = option_verbose;
2686  }
2688  return verb_level;
2689 }
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
int option_verbose
Definition: options.c:67
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
int * level
Definition: logger.c:2575
void ast_verb_console_register ( int *  level)

Register this thread's console verbosity level pointer.

Parameters
levelWhere the verbose level value is.

Definition at line 2646 of file logger.c.

References AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_threadstorage_get(), ast_verb_update(), and verb_console::level.

2647 {
2648  struct verb_console *console;
2649 
2650  console = ast_threadstorage_get(&my_verb_console, sizeof(*console));
2651  if (!console || !level) {
2652  return;
2653  }
2654  console->level = level;
2655 
2657  AST_RWLIST_INSERT_HEAD(&verb_consoles, console, node);
2659  ast_verb_update();
2660 }
Definition: test_heap.c:38
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
void ast_verb_update(void)
Re-evaluate the system max verbosity level (ast_verb_sys_level).
Definition: logger.c:2584
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
int * level
Definition: logger.c:2575
void ast_verb_console_set ( int  verb_level)

Set this thread's console verbosity level.

Parameters
verb_levelNew level to set.

Definition at line 2691 of file logger.c.

References AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_threadstorage_get(), ast_verb_update(), verb_console::level, and option_verbose.

2692 {
2693  struct verb_console *console;
2694 
2695  console = ast_threadstorage_get(&my_verb_console, sizeof(*console));
2696  if (!console) {
2697  return;
2698  }
2699 
2701  if (console->level) {
2702  *console->level = verb_level;
2703  } else {
2704  option_verbose = verb_level;
2705  }
2707  ast_verb_update();
2708 }
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
void ast_verb_update(void)
Re-evaluate the system max verbosity level (ast_verb_sys_level).
Definition: logger.c:2584
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
int option_verbose
Definition: options.c:67
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
int * level
Definition: logger.c:2575
void close_logger ( void  )

Provided by logger.c

Definition at line 2245 of file logger.c.

References ast_cli_unregister_multiple(), ast_custom_function_unregister(), AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, logchannel::fileptr, and logchannel::list.

Referenced by really_quit().

2246 {
2247  struct logchannel *f = NULL;
2248 
2249  ast_logger_category_unload();
2250 
2251  ast_custom_function_unregister(&log_group_function);
2252  ast_cli_unregister_multiple(cli_logger, ARRAY_LEN(cli_logger));
2253 
2254  logger_initialized = 0;
2255 
2256  /* Stop logger thread */
2258  close_logger_thread = 1;
2259  ast_cond_signal(&logcond);
2261 
2262  if (logthread != AST_PTHREADT_NULL) {
2263  pthread_join(logthread, NULL);
2264  }
2265 
2267 
2268  if (qlog) {
2269  fclose(qlog);
2270  qlog = NULL;
2271  }
2272 
2273  while ((f = AST_LIST_REMOVE_HEAD(&logchannels, list))) {
2274  if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
2275  fclose(f->fileptr);
2276  f->fileptr = NULL;
2277  }
2278  ast_free(f);
2279  }
2280 
2281  callid_group_remove_filters();
2282 
2283  closelog(); /* syslog */
2284 
2286 }
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:40
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:140
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
struct logchannel::@372 list
FILE * fileptr
Definition: logger.c:152
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
static int custom_level_still_exists ( char **  levels,
char *  level,
size_t  len 
)
static

Checks if level exists in array of level names.

Parameters
levelsArray of level names
levelName to search for
lenSize of levels
Return values
1Found
0Not Found

Definition at line 739 of file logger.c.

Referenced by init_logger_chain().

740 {
741  int i;
742  for (i = 0; i < len; i++) {
743  if (!strcmp(levels[i], level)) {
744  return 1;
745  }
746  }
747  return 0;
748 }
static char * levels[NUMLOGLEVELS]
Logging channels used in the Asterisk logging system.
Definition: logger.c:214
static struct logchannel* find_logchannel ( const char *  channel)
static

Find a particular logger channel by name.

Precondition
logchannels list is locked
Parameters
channelThe name of the logger channel to find
Return values
non-NULLThe corresponding logger channel
NULLUnable to find a logger channel with that particular name

Definition at line 630 of file logger.c.

References logchannel::filename, logchannel::list, and make_filename().

Referenced by ast_logger_create_channel(), and ast_logger_remove_channel().

631 {
632  char filename[PATH_MAX];
633  struct logchannel *chan;
634 
635  make_filename(channel, filename, sizeof(filename));
636 
637  AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
638  if (!strcmp(chan->filename, filename)) {
639  return chan;
640  }
641  }
642 
643  return NULL;
644 }
static void make_filename(const char *channel, char *filename, size_t size)
create the filename that will be used for a logger channel.
Definition: logger.c:589
struct logchannel::@372 list
char filename[PATH_MAX]
Definition: logger.c:154
int init_logger ( void  )

Provided by logger.c

Definition at line 2202 of file logger.c.

References ast_cli_register_multiple, ast_custom_function_register, ast_log(), ast_mkdir(), AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_verb_update(), init_logger_chain(), and logger_thread().

2203 {
2204  int res;
2205  /* auto rotate if sig SIGXFSZ comes a-knockin */
2206  sigaction(SIGXFSZ, &handle_SIGXFSZ, NULL);
2207 
2208  /* Re-initialize the logmsgs mutex. The recursive mutex can be accessed prior
2209  * to Asterisk being forked into the background, which can cause the thread
2210  * ID tracked by the underlying pthread mutex to be different than the ID of
2211  * the thread that unlocks the mutex. Since init_logger is called after the
2212  * fork, it is safe to initialize the mutex here for future accesses.
2213  */
2214  ast_mutex_destroy(&logmsgs.lock);
2215  ast_mutex_init(&logmsgs.lock);
2216  ast_cond_init(&logcond, NULL);
2217 
2218  /* start logger thread */
2219  if (ast_pthread_create(&logthread, NULL, logger_thread, NULL) < 0) {
2220  ast_cond_destroy(&logcond);
2221  return -1;
2222  }
2223 
2224  /* register the logger cli commands */
2225  ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
2226  ast_custom_function_register(&log_group_function);
2227 
2228  ast_mkdir(ast_config_AST_LOG_DIR, 0777);
2229 
2230  /* create log channels */
2232  res = init_logger_chain(NULL);
2234  ast_verb_update();
2235  logger_initialized = 1;
2236  if (res) {
2237  ast_log(LOG_ERROR, "Errors detected in logger.conf. Default console logging is being used.\n");
2238  }
2239 
2240  ast_logger_category_load();
2241 
2242  return 0;
2243 }
static int init_logger_chain(const char *altconf)
Read config, setup channels.
Definition: logger.c:759
void ast_verb_update(void)
Re-evaluate the system max verbosity level (ast_verb_sys_level).
Definition: logger.c:2584
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static void * logger_thread(void *data)
Actual logging thread.
Definition: logger.c:2097
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:2453
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558
int ast_mkdir(const char *path, int mode)
Recursively create directory path.
Definition: utils.c:2479
static int init_logger_chain ( const char *  altconf)
static

Read config, setup channels.

Parameters
altconfAlternate configuration file to read.
Precondition
logchannels list is write locked
Return values
0Success
-1No config found or Failed

Definition at line 759 of file logger.c.

References ast_config_destroy(), ast_config_load2(), ast_console_puts_mutable(), ast_copy_string(), ast_strdupa, ast_true(), custom_level_still_exists(), logchannel::list, logchannel::logmask, ast_variable::name, ast_variable::next, S_OR, and ast_variable::value.

Referenced by ast_logger_rotate_channel(), and init_logger().

760 {
761  struct logchannel *chan;
762  struct ast_config *cfg;
763  struct ast_variable *var;
764  const char *s;
765  struct ast_flags config_flags = { 0 };
766 
767  if (!(cfg = ast_config_load2(S_OR(altconf, "logger.conf"), "logger", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
768  cfg = NULL;
769  }
770 
771  /* Set defaults */
772  hostname[0] = '\0';
773  display_callids = 1;
774  memset(&logfiles, 0, sizeof(logfiles));
775  logfiles.queue_log = 1;
776  ast_copy_string(dateformat, "%b %e %T", sizeof(dateformat));
777  ast_copy_string(queue_log_name, QUEUELOG, sizeof(queue_log_name));
778  exec_after_rotate[0] = '\0';
779  rotatestrategy = SEQUENTIAL;
780 
781  /* delete our list of log channels */
782  while ((chan = AST_RWLIST_REMOVE_HEAD(&logchannels, list))) {
783  ast_free(chan);
784  }
785  global_logmask = 0;
786 
787  errno = 0;
788  /* close syslog */
789  closelog();
790 
791  /* If no config file, we're fine, set default options. */
792  if (!cfg) {
793  chan = make_logchannel("console", "error,warning,notice,verbose", 0, 0);
794  if (!chan) {
795  fprintf(stderr, "ERROR: Failed to initialize default logging\n");
796  return -1;
797  }
798 
799  AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
800  global_logmask |= chan->logmask;
801 
802  return -1;
803  }
804 
805  if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
806  if (ast_true(s)) {
807  if (gethostname(hostname, sizeof(hostname) - 1)) {
808  ast_copy_string(hostname, "unknown", sizeof(hostname));
809  fprintf(stderr, "What box has no hostname???\n");
810  }
811  }
812  }
813  if ((s = ast_variable_retrieve(cfg, "general", "display_callids"))) {
814  display_callids = ast_true(s);
815  }
816  if ((s = ast_variable_retrieve(cfg, "general", "dateformat"))) {
817  ast_copy_string(dateformat, s, sizeof(dateformat));
818  }
819  if ((s = ast_variable_retrieve(cfg, "general", "queue_log"))) {
820  logfiles.queue_log = ast_true(s);
821  }
822  if ((s = ast_variable_retrieve(cfg, "general", "queue_log_to_file"))) {
823  logfiles.queue_log_to_file = ast_true(s);
824  }
825  if ((s = ast_variable_retrieve(cfg, "general", "queue_log_name"))) {
826  ast_copy_string(queue_log_name, s, sizeof(queue_log_name));
827  }
828  if ((s = ast_variable_retrieve(cfg, "general", "queue_log_realtime_use_gmt"))) {
829  logfiles.queue_log_realtime_use_gmt = ast_true(s);
830  }
831  if ((s = ast_variable_retrieve(cfg, "general", "exec_after_rotate"))) {
832  ast_copy_string(exec_after_rotate, s, sizeof(exec_after_rotate));
833  }
834  if ((s = ast_variable_retrieve(cfg, "general", "rotatestrategy"))) {
835  if (strcasecmp(s, "timestamp") == 0) {
836  rotatestrategy = TIMESTAMP;
837  } else if (strcasecmp(s, "rotate") == 0) {
838  rotatestrategy = ROTATE;
839  } else if (strcasecmp(s, "sequential") == 0) {
840  rotatestrategy = SEQUENTIAL;
841  } else if (strcasecmp(s, "none") == 0) {
842  rotatestrategy = NONE;
843  } else {
844  fprintf(stderr, "Unknown rotatestrategy: %s\n", s);
845  }
846  } else {
847  if ((s = ast_variable_retrieve(cfg, "general", "rotatetimestamp"))) {
848  rotatestrategy = ast_true(s) ? TIMESTAMP : SEQUENTIAL;
849  fprintf(stderr, "rotatetimestamp option has been deprecated. Please use rotatestrategy instead.\n");
850  }
851  }
852  if ((s = ast_variable_retrieve(cfg, "general", "logger_queue_limit"))) {
853  if (sscanf(s, "%30d", &logger_queue_limit) != 1) {
854  fprintf(stderr, "logger_queue_limit has an invalid value. Leaving at default of %d.\n",
855  logger_queue_limit);
856  }
857  if (logger_queue_limit < 10) {
858  fprintf(stderr, "logger_queue_limit must be >= 10. Setting to 10.\n");
859  logger_queue_limit = 10;
860  }
861  }
862 
863  /* Custom dynamic logging levels defined by user */
864  if ((s = ast_variable_retrieve(cfg, "general", "custom_levels"))) {
865  char *customlogs = ast_strdupa(s);
866  char *logfile;
867  char *new_custom_levels[16] = { };
868  unsigned int level, new_level = 0;
869 
870  /* get the custom levels we need to register or reload */
871  while ((logfile = strsep(&customlogs, ","))) {
872  new_custom_levels[new_level++] = logfile;
873  }
874 
875  /* unregister existing custom levels, if they're not still
876  specified in customlogs, to make room for new levels */
877  for (level = 16; level < ARRAY_LEN(levels); level++) {
878  if (levels[level] && custom_dynamic_levels[level] &&
879  !custom_level_still_exists(new_custom_levels, levels[level], ARRAY_LEN(new_custom_levels))) {
880  logger_unregister_level(levels[level]);
881  custom_dynamic_levels[level] = 0;
882  }
883  }
884 
885  new_level = 0;
886  while ((logfile = new_custom_levels[new_level++])) {
887  /* Lock already held, so directly register the level,
888  unless it's already registered (as during reload) */
889  if (logger_get_dynamic_level(logfile) == -1) {
890  int custom_level = logger_register_level(logfile);
891  custom_dynamic_levels[custom_level] = logfile;
892  }
893  }
894  }
895 
896  var = ast_variable_browse(cfg, "logfiles");
897  for (; var; var = var->next) {
898  chan = make_logchannel(var->name, var->value, var->lineno, 0);
899  if (!chan) {
900  /* Print error message directly to the consoles since the lock is held
901  * and we don't want to unlock with the list partially built */
902  ast_console_puts_mutable("ERROR: Unable to create log channel '", __LOG_ERROR);
903  ast_console_puts_mutable(var->name, __LOG_ERROR);
904  ast_console_puts_mutable("'\n", __LOG_ERROR);
905  continue;
906  }
907  AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
908  global_logmask |= chan->logmask;
909  }
910 
911  if (qlog) {
912  fclose(qlog);
913  qlog = NULL;
914  }
915 
916  ast_config_destroy(cfg);
917 
918  return 0;
919 }
struct ast_variable * next
struct ast_config * ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
Load a config file.
Definition: main/config.c:3321
Structure for variables, used for configurations and for channel variables.
static char * custom_dynamic_levels[NUMLOGLEVELS]
Custom dynamic logging levels added by the user.
Definition: logger.c:231
static char * levels[NUMLOGLEVELS]
Logging channels used in the Asterisk logging system.
Definition: logger.c:214
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
static int custom_level_still_exists(char **levels, char *level, size_t len)
Checks if level exists in array of level names.
Definition: logger.c:739
unsigned int logmask
Definition: logger.c:142
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
Definition: utils.c:2199
Structure used to handle boolean flags.
Definition: utils.h:199
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:80
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
void ast_console_puts_mutable(const char *string, int level)
log the string to the console, and all attached console clients
Definition: asterisk.c:1316
void logger_queue_start ( void  )

Start the ast_queue_log() logger.

Note
Called when the system is fully booted after startup so preloaded realtime modules can get up.

Definition at line 2186 of file logger.c.

References AST_RWLIST_UNLOCK, and AST_RWLIST_WRLOCK.

2187 {
2188  /* Must not be called before the logger is initialized. */
2189  ast_assert(logger_initialized);
2190 
2192  if (!queuelog_init) {
2193  logger_queue_init();
2194  queuelog_init = 1;
2196  ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
2197  } else {
2199  }
2200 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
static void make_filename ( const char *  channel,
char *  filename,
size_t  size 
)
static

create the filename that will be used for a logger channel.

Parameters
channelThe name of the logger channel
[out]filenameThe filename for the logger channel
sizeThe size of the filename buffer

Definition at line 589 of file logger.c.

References ast_copy_string().

Referenced by ast_logger_rotate_channel(), ast_read_image(), and find_logchannel().

590 {
591  const char *log_dir_prefix = "";
592  const char *log_dir_separator = "";
593 
594  *filename = '\0';
595 
596  if (!strcasecmp(channel, "console")) {
597  return;
598  }
599 
600  if (!strncasecmp(channel, "syslog", 6)) {
601  ast_copy_string(filename, channel, size);
602  return;
603  }
604 
605  /* It's a filename */
606 
607  if (channel[0] != '/') {
608  log_dir_prefix = ast_config_AST_LOG_DIR;
609  log_dir_separator = "/";
610  }
611 
612  if (!ast_strlen_zero(hostname)) {
613  snprintf(filename, size, "%s%s%s.%s",
614  log_dir_prefix, log_dir_separator, channel, hostname);
615  } else {
616  snprintf(filename, size, "%s%s%s",
617  log_dir_prefix, log_dir_separator, channel);
618  }
619 }
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425

Variable Documentation

char* custom_dynamic_levels[NUMLOGLEVELS]
static

Custom dynamic logging levels added by the user.

The first 16 levels are reserved for system usage, and the remaining levels are reserved for usage by dynamic levels registered via ast_logger_register_level.

Definition at line 231 of file logger.c.

struct sigaction handle_SIGXFSZ
static
Initial value:
= {
.sa_handler = _handle_SIGXFSZ,
.sa_flags = SA_RESTART,
}

Definition at line 1910 of file logger.c.

char* levels[NUMLOGLEVELS]
static

Logging channels used in the Asterisk logging system.

The first 16 levels are reserved for system usage, and the remaining levels are reserved for usage by dynamic levels registered via ast_logger_register_level.

Examples:
app_skel.c.

Definition at line 214 of file logger.c.

Referenced by ast_network_puts_mutable().

struct ast_custom_function log_group_function
static
Initial value:
= {
.name = "LOG_GROUP",
.write = log_group_write,
}

Definition at line 1801 of file logger.c.

struct logformatter logformatter_default
static
Initial value:
= {
.name = "default",
.format_log = format_log_default,
}

Definition at line 453 of file logger.c.

struct logformatter logformatter_json
static
Initial value:
= {
.name = "json",
.format_log = format_log_json
}

Definition at line 331 of file logger.c.

struct logformatter logformatter_plain
static
Initial value:
= {
.name = "plain",
.format_log = format_log_plain,
}

Definition at line 503 of file logger.c.

ast_mutex_t verb_update_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
static

ast_verb_update() reentrancy protection lock.

Definition at line 2582 of file logger.c.

Referenced by ast_verb_update().