Asterisk - The Open Source Telephony Project  GIT-master-4a4f1a5
logger.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  *
21  * \brief Asterisk Logger
22  *
23  * Logging routines
24  *
25  * \author Mark Spencer <markster@digium.com>
26  */
27 
28 /*! \li \ref logger.c uses the configuration file \ref logger.conf
29  * \addtogroup configuration_file Configuration Files
30  */
31 
32 /*!
33  * \page logger.conf logger.conf
34  * \verbinclude logger.conf.sample
35  */
36 
37 /*** MODULEINFO
38  <support_level>core</support_level>
39  ***/
40 
41 #include "asterisk.h"
42 
43 /* When we include logger.h again it will trample on some stuff in syslog.h, but
44  * nothing we care about in here. */
45 #include <syslog.h>
46 #include <signal.h>
47 #include <time.h>
48 #include <sys/stat.h>
49 #include <fcntl.h>
50 
51 #include "asterisk/_private.h"
52 #include "asterisk/module.h"
53 #include "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
54 #include "asterisk/logger.h"
56 #include "asterisk/lock.h"
57 #include "asterisk/channel.h"
58 #include "asterisk/config.h"
59 #include "asterisk/term.h"
60 #include "asterisk/cli.h"
61 #include "asterisk/utils.h"
62 #include "asterisk/manager.h"
63 #include "asterisk/astobj2.h"
64 #include "asterisk/threadstorage.h"
65 #include "asterisk/strings.h"
66 #include "asterisk/pbx.h"
67 #include "asterisk/app.h"
68 #include "asterisk/syslog.h"
69 #include "asterisk/buildinfo.h"
70 #include "asterisk/ast_version.h"
71 #include "asterisk/backtrace.h"
72 #include "asterisk/json.h"
73 
74 /*** DOCUMENTATION
75  ***/
76 
77 static int logger_register_level(const char *name);
78 static int logger_unregister_level(const char *name);
79 static int logger_get_dynamic_level(const char *name);
80 
81 static char dateformat[256] = "%b %e %T"; /* Original Asterisk Format */
82 
83 static char queue_log_name[256] = QUEUELOG;
84 static char exec_after_rotate[256] = "";
85 
87 static unsigned int global_logmask = 0xFFFF;
88 static int queuelog_init;
89 static int logger_initialized;
90 static volatile int next_unique_callid = 1; /* Used to assign unique call_ids to calls */
91 static int display_callids;
92 
94 
95 static int logger_queue_size;
96 static int logger_queue_limit = 1000;
98 static unsigned int high_water_alert;
99 
100 static enum rotatestrategy {
101  NONE = 0, /* Do not rotate log files at all, instead rely on external mechanisms */
102  SEQUENTIAL = 1 << 0, /* Original method - create a new file, in order */
103  ROTATE = 1 << 1, /* Rotate all files, such that the oldest file has the highest suffix */
104  TIMESTAMP = 1 << 2, /* Append the epoch timestamp onto the end of the archived file */
106 
107 static struct {
108  unsigned int queue_log:1;
109  unsigned int queue_log_to_file:1;
110  unsigned int queue_adaptive_realtime:1;
112 } logfiles = { 1 };
113 
116 
117 struct logchannel;
118 struct logmsg;
119 
120 struct logformatter {
121  /* The name of the log formatter */
122  const char *name;
123  /* Pointer to the function that will format the log */
124  int (* const format_log)(struct logchannel *channel, struct logmsg *msg, char *buf, size_t size);
125 };
126 
127 enum logtypes {
131 };
132 
133 struct logchannel {
134  /*! How the logs sent to this channel will be formatted */
135  struct logformatter formatter;
136  /*! What to log to this channel */
137  unsigned int logmask;
138  /*! If this channel is disabled or not */
139  int disabled;
140  /*! syslog facility */
141  int facility;
142  /*! Verbosity level. (-1 if use option_verbose for the level.) */
144  /*! Type of log channel */
145  enum logtypes type;
146  /*! logfile logging file pointer */
147  FILE *fileptr;
148  /*! Filename */
150  /*! field for linking to list */
152  /*! Line number from configuration file */
153  int lineno;
154  /*! Whether this log channel was created dynamically */
155  int dynamic;
156  /*! Components (levels) from last config load */
157  char components[0];
158 };
159 
161 
165 };
166 
167 struct logmsg {
168  enum logmsgtypes type;
169  int level;
170  int sublevel;
171  int line;
172  int lwp;
177  AST_STRING_FIELD(function);
180  );
182 };
183 
184 static void logmsg_free(struct logmsg *msg)
185 {
187  ast_free(msg);
188 }
189 
191 static pthread_t logthread = AST_PTHREADT_NULL;
193 static int close_logger_thread = 0;
194 
195 static FILE *qlog;
196 
197 /*! \brief Logging channels used in the Asterisk logging system
198  *
199  * The first 16 levels are reserved for system usage, and the remaining
200  * levels are reserved for usage by dynamic levels registered via
201  * ast_logger_register_level.
202  */
203 
204 /* Modifications to this array are protected by the rwlock in the
205  * logchannels list.
206  */
207 
208 static char *levels[NUMLOGLEVELS] = {
209  "DEBUG",
210  "TRACE",
211  "NOTICE",
212  "WARNING",
213  "ERROR",
214  "VERBOSE",
215  "DTMF",
216 };
217 
218 /*! \brief Custom dynamic logging levels added by the user
219  *
220  * The first 16 levels are reserved for system usage, and the remaining
221  * levels are reserved for usage by dynamic levels registered via
222  * ast_logger_register_level.
223  */
224 
226 
227 /*! \brief Colors used in the console for logging */
228 static const int colors[NUMLOGLEVELS] = {
230  COLOR_BRBLUE, /* no longer used */
231  COLOR_YELLOW,
232  COLOR_BRRED,
233  COLOR_RED,
234  COLOR_GREEN,
236  0,
237  0,
238  0,
239  0,
240  0,
241  0,
242  0,
243  0,
244  0,
245  COLOR_BRBLUE,
246  COLOR_BRBLUE,
247  COLOR_BRBLUE,
248  COLOR_BRBLUE,
249  COLOR_BRBLUE,
250  COLOR_BRBLUE,
251  COLOR_BRBLUE,
252  COLOR_BRBLUE,
253  COLOR_BRBLUE,
254  COLOR_BRBLUE,
255  COLOR_BRBLUE,
256  COLOR_BRBLUE,
257  COLOR_BRBLUE,
258  COLOR_BRBLUE,
259  COLOR_BRBLUE,
260  COLOR_BRBLUE,
261 };
262 
265 #define VERBOSE_BUF_INIT_SIZE 256
266 
268 #define LOG_BUF_INIT_SIZE 256
269 
270 static int format_log_json(struct logchannel *channel, struct logmsg *msg, char *buf, size_t size)
271 {
272  struct ast_json *json;
273  char *str;
274  char call_identifier_str[13];
275  size_t json_str_len;
276 
277  if (msg->callid) {
278  snprintf(call_identifier_str, sizeof(call_identifier_str), "[C-%08x]", msg->callid);
279  } else {
280  call_identifier_str[0] = '\0';
281  }
282 
283  json = ast_json_pack("{s: s, s: s, "
284  "s: {s: i, s: s} "
285  "s: {s: {s: s, s: s, s: i}, "
286  "s: s, s: s} }",
287  "hostname", ast_config_AST_SYSTEM_NAME,
288  "timestamp", msg->date,
289  "identifiers",
290  "lwp", msg->lwp,
291  "callid", S_OR(call_identifier_str, ""),
292  "logmsg",
293  "location",
294  "filename", msg->file,
295  "function", msg->function,
296  "line", msg->line,
297  "level", msg->level_name,
298  "message", msg->message);
299  if (!json) {
300  return -1;
301  }
302 
303  str = ast_json_dump_string(json);
304  if (!str) {
305  ast_json_unref(json);
306  return -1;
307  }
308 
309  ast_copy_string(buf, str, size);
310  json_str_len = strlen(str);
311  if (json_str_len > size - 1) {
312  json_str_len = size - 1;
313  }
314  buf[json_str_len] = '\n';
315  buf[json_str_len + 1] = '\0';
316 
317  term_strip(buf, buf, size);
318 
320  ast_json_unref(json);
321 
322  return 0;
323 }
324 
325 static struct logformatter logformatter_json = {
326  .name = "json",
327  .format_log = format_log_json
328 };
329 
330 static int logger_add_verbose_magic(struct logmsg *logmsg, char *buf, size_t size)
331 {
332  const char *p;
333  const char *fmt;
334  struct ast_str *prefixed;
335  signed char magic = logmsg->sublevel > 9 ? -10 : -logmsg->sublevel - 1; /* 0 => -1, 1 => -2, etc. Can't pass NUL, as it is EOS-delimiter */
336 
337  /* For compatibility with modules still calling ast_verbose() directly instead of using ast_verb() */
338  if (logmsg->sublevel < 0) {
339  if (!strncmp(logmsg->message, VERBOSE_PREFIX_4, strlen(VERBOSE_PREFIX_4))) {
340  magic = -5;
341  } else if (!strncmp(logmsg->message, VERBOSE_PREFIX_3, strlen(VERBOSE_PREFIX_3))) {
342  magic = -4;
343  } else if (!strncmp(logmsg->message, VERBOSE_PREFIX_2, strlen(VERBOSE_PREFIX_2))) {
344  magic = -3;
345  } else if (!strncmp(logmsg->message, VERBOSE_PREFIX_1, strlen(VERBOSE_PREFIX_1))) {
346  magic = -2;
347  } else {
348  magic = -1;
349  }
350  }
351 
353  return -1;
354  }
355 
356  ast_str_reset(prefixed);
357 
358  /* for every newline found in the buffer add verbose prefix data */
359  fmt = logmsg->message;
360  do {
361  if (!(p = strchr(fmt, '\n'))) {
362  p = strchr(fmt, '\0') - 1;
363  }
364  ++p;
365 
366  ast_str_append(&prefixed, 0, "%c", (char)magic);
367  ast_str_append_substr(&prefixed, 0, fmt, p - fmt);
368  fmt = p;
369  } while (p && *p);
370 
371  snprintf(buf, size, "%s", ast_str_buffer(prefixed));
372 
373  return 0;
374 }
375 
376 static int format_log_default(struct logchannel *chan, struct logmsg *msg, char *buf, size_t size)
377 {
378  char call_identifier_str[13];
379 
380  if (msg->callid) {
381  snprintf(call_identifier_str, sizeof(call_identifier_str), "[C-%08x]", msg->callid);
382  } else {
383  call_identifier_str[0] = '\0';
384  }
385 
386  switch (chan->type) {
387  case LOGTYPE_SYSLOG:
388  snprintf(buf, size, "%s[%d]%s: %s:%d in %s: %s",
389  levels[msg->level], msg->lwp, call_identifier_str, msg->file,
390  msg->line, msg->function, msg->message);
391  term_strip(buf, buf, size);
392  break;
393  case LOGTYPE_FILE:
394  snprintf(buf, size, "[%s] %s[%d]%s %s: %s",
395  msg->date, msg->level_name, msg->lwp, call_identifier_str,
396  msg->file, msg->message);
397  term_strip(buf, buf, size);
398  break;
399  case LOGTYPE_CONSOLE:
400  {
401  char linestr[32];
402  int has_file = !ast_strlen_zero(msg->file);
403  int has_line = (msg->line > 0);
404  int has_func = !ast_strlen_zero(msg->function);
405 
406  /*
407  * Verbose messages are interpreted by console channels in their own
408  * special way
409  */
410  if (msg->level == __LOG_VERBOSE) {
411  return logger_add_verbose_magic(msg, buf, size);
412  }
413 
414  /* Turn the numerical line number into a string */
415  snprintf(linestr, sizeof(linestr), "%d", msg->line);
416  /* Build string to print out */
417  snprintf(buf, size, "[%s] " COLORIZE_FMT "[%d]%s: " COLORIZE_FMT "%s" COLORIZE_FMT " " COLORIZE_FMT "%s %s",
418  msg->date,
419  COLORIZE(colors[msg->level], 0, msg->level_name),
420  msg->lwp,
421  call_identifier_str,
422  COLORIZE(COLOR_BRWHITE, 0, has_file ? msg->file : ""),
423  has_file ? ":" : "",
424  COLORIZE(COLOR_BRWHITE, 0, has_line ? linestr : ""),
425  COLORIZE(COLOR_BRWHITE, 0, has_func ? msg->function : ""),
426  has_func ? ":" : "",
427  msg->message);
428  }
429  break;
430  }
431 
432  return 0;
433 }
434 
435 static struct logformatter logformatter_default = {
436  .name = "default",
437  .format_log = format_log_default,
438 };
439 
440 static int format_log_plain(struct logchannel *chan, struct logmsg *msg, char *buf, size_t size)
441 {
442  char call_identifier_str[13];
443  char linestr[32];
444  int has_file = !ast_strlen_zero(msg->file);
445  int has_line = (msg->line > 0);
446  int has_func = !ast_strlen_zero(msg->function);
447 
448  if (msg->callid) {
449  snprintf(call_identifier_str, sizeof(call_identifier_str), "[C-%08x]", msg->callid);
450  } else {
451  call_identifier_str[0] = '\0';
452  }
453 
454  switch (chan->type) {
455  case LOGTYPE_SYSLOG:
456  snprintf(buf, size, "%s[%d]%s: %s:%d in %s: %s",
457  levels[msg->level], msg->lwp, call_identifier_str, msg->file,
458  msg->line, msg->function, msg->message);
459  term_strip(buf, buf, size);
460  break;
461  case LOGTYPE_FILE:
462  case LOGTYPE_CONSOLE:
463  /* Turn the numerical line number into a string */
464  snprintf(linestr, sizeof(linestr), "%d", msg->line);
465  /* Build string to print out */
466  snprintf(buf, size, "[%s] %s[%d]%s: %s%s%s%s%s%s%s",
467  msg->date,
468  msg->level_name,
469  msg->lwp,
470  call_identifier_str,
471  has_file ? msg->file : "",
472  has_file ? ":" : "",
473  has_line ? linestr : "",
474  has_line ? " " : "",
475  has_func ? msg->function : "",
476  has_func ? ": " : "",
477  msg->message);
478  term_strip(buf, buf, size);
479  break;
480  }
481 
482  return 0;
483 }
484 
485 static struct logformatter logformatter_plain = {
486  .name = "plain",
487  .format_log = format_log_plain,
488 };
489 
490 static void make_components(struct logchannel *chan)
491 {
492  char *w;
493  unsigned int logmask = 0;
494  char *stringp = ast_strdupa(chan->components);
495  unsigned int x;
496  unsigned int verb_level;
497 
498  /* Default to using option_verbose as the verbosity level of the logging channel. */
499  verb_level = -1;
500 
501  w = strchr(stringp, '[');
502  if (w) {
503  char *end = strchr(w + 1, ']');
504  if (!end) {
505  fprintf(stderr, "Logger Warning: bad formatter definition for %s in logger.conf\n", chan->filename);
506  } else {
507  char *formatter_name = w + 1;
508 
509  *end = '\0';
510  stringp = end + 1;
511 
512  if (!strcasecmp(formatter_name, "json")) {
513  memcpy(&chan->formatter, &logformatter_json, sizeof(chan->formatter));
514  } else if (!strcasecmp(formatter_name, "default")) {
515  memcpy(&chan->formatter, &logformatter_default, sizeof(chan->formatter));
516  } else if (!strcasecmp(formatter_name, "plain")) {
517  memcpy(&chan->formatter, &logformatter_plain, sizeof(chan->formatter));
518  } else {
519  fprintf(stderr, "Logger Warning: Unknown formatter definition %s for %s in logger.conf; using 'default'\n",
520  formatter_name, chan->filename);
521  memcpy(&chan->formatter, &logformatter_default, sizeof(chan->formatter));
522  }
523  }
524  }
525 
526  if (!chan->formatter.name) {
527  memcpy(&chan->formatter, &logformatter_default, sizeof(chan->formatter));
528  }
529 
530  while ((w = strsep(&stringp, ","))) {
531  w = ast_strip(w);
532  if (ast_strlen_zero(w)) {
533  continue;
534  }
535  if (!strcmp(w, "*")) {
536  logmask = 0xFFFFFFFF;
537  } else if (!strncasecmp(w, "verbose(", 8)) {
538  if (levels[__LOG_VERBOSE] && sscanf(w + 8, "%30u)", &verb_level) == 1) {
539  logmask |= (1 << __LOG_VERBOSE);
540  }
541  } else {
542  for (x = 0; x < ARRAY_LEN(levels); ++x) {
543  if (levels[x] && !strcasecmp(w, levels[x])) {
544  logmask |= (1 << x);
545  break;
546  }
547  }
548  }
549  }
550  if (chan->type == LOGTYPE_CONSOLE) {
551  /*
552  * Force to use the root console verbose level so if the
553  * user specified any verbose level then it does not interfere
554  * with calculating the ast_verb_sys_level value.
555  */
556  chan->verbosity = -1;
557  logmask |= (1 << __LOG_VERBOSE);
558  } else {
559  chan->verbosity = verb_level;
560  }
561  chan->logmask = logmask;
562 }
563 
564 /*!
565  * \brief create the filename that will be used for a logger channel.
566  *
567  * \param channel The name of the logger channel
568  * \param[out] filename The filename for the logger channel
569  * \param size The size of the filename buffer
570  */
571 static void make_filename(const char *channel, char *filename, size_t size)
572 {
573  const char *log_dir_prefix = "";
574  const char *log_dir_separator = "";
575 
576  *filename = '\0';
577 
578  if (!strcasecmp(channel, "console")) {
579  return;
580  }
581 
582  if (!strncasecmp(channel, "syslog", 6)) {
583  ast_copy_string(filename, channel, size);
584  return;
585  }
586 
587  /* It's a filename */
588 
589  if (channel[0] != '/') {
590  log_dir_prefix = ast_config_AST_LOG_DIR;
591  log_dir_separator = "/";
592  }
593 
594  if (!ast_strlen_zero(hostname)) {
595  snprintf(filename, size, "%s%s%s.%s",
596  log_dir_prefix, log_dir_separator, channel, hostname);
597  } else {
598  snprintf(filename, size, "%s%s%s",
599  log_dir_prefix, log_dir_separator, channel);
600  }
601 }
602 
603 /*!
604  * \brief Find a particular logger channel by name
605  *
606  * \pre logchannels list is locked
607  *
608  * \param channel The name of the logger channel to find
609  * \retval non-NULL The corresponding logger channel
610  * \retval NULL Unable to find a logger channel with that particular name
611  */
612 static struct logchannel *find_logchannel(const char *channel)
613 {
614  char filename[PATH_MAX];
615  struct logchannel *chan;
616 
617  make_filename(channel, filename, sizeof(filename));
618 
620  if (!strcmp(chan->filename, filename)) {
621  return chan;
622  }
623  }
624 
625  return NULL;
626 }
627 
628 static struct logchannel *make_logchannel(const char *channel, const char *components, int lineno, int dynamic)
629 {
630  struct logchannel *chan;
631  char *facility;
632  struct ast_tm tm;
633  struct timeval now = ast_tvnow();
634  char datestring[256];
635 
636  if (ast_strlen_zero(channel) || !(chan = ast_calloc(1, sizeof(*chan) + strlen(components) + 1)))
637  return NULL;
638 
639  strcpy(chan->components, components);
640  chan->lineno = lineno;
641  chan->dynamic = dynamic;
642 
643  make_filename(channel, chan->filename, sizeof(chan->filename));
644 
645  if (!strcasecmp(channel, "console")) {
646  chan->type = LOGTYPE_CONSOLE;
647  } else if (!strncasecmp(channel, "syslog", 6)) {
648  /*
649  * syntax is:
650  * syslog.facility => level,level,level
651  */
652  facility = strchr(channel, '.');
653  if (!facility++ || !facility) {
654  facility = "local0";
655  }
656 
657  chan->facility = ast_syslog_facility(facility);
658 
659  if (chan->facility < 0) {
660  fprintf(stderr, "Logger Warning: bad syslog facility in logger.conf\n");
661  ast_free(chan);
662  return NULL;
663  }
664 
665  chan->type = LOGTYPE_SYSLOG;
666  openlog("asterisk", LOG_PID, chan->facility);
667  } else {
668  if (!(chan->fileptr = fopen(chan->filename, "a"))) {
669  /* Can't do real logging here since we're called with a lock
670  * so log to any attached consoles */
671  ast_console_puts_mutable("ERROR: Unable to open log file '", __LOG_ERROR);
676  ast_free(chan);
677  return NULL;
678  } else {
679  /* Create our date/time */
680  ast_localtime(&now, &tm, NULL);
681  ast_strftime(datestring, sizeof(datestring), dateformat, &tm);
682 
683  fprintf(chan->fileptr, "[%s] Asterisk %s built by %s @ %s on a %s running %s on %s\n",
686  fflush(chan->fileptr);
687  }
688  chan->type = LOGTYPE_FILE;
689  }
690  make_components(chan);
691 
692  return chan;
693 }
694 
696 {
697  struct ast_config *cfg;
698  const char *s;
699  struct ast_flags config_flags = { 0 };
700 
701  if (!(cfg = ast_config_load2("logger.conf", "logger", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
702  return;
703  }
704 
705  if ((s = ast_variable_retrieve(cfg, "general", "dateformat"))) {
707  }
708 
709  ast_config_destroy(cfg);
710 }
711 
712 /*!
713  * \brief Checks if level exists in array of level names
714  * \param levels Array of level names
715  * \param level Name to search for
716  * \len Size of levels
717  *
718  * \retval 1 Found
719  * \retval 0 Not Found
720  */
721 static int custom_level_still_exists(char **levels, char *level, size_t len)
722 {
723  int i;
724  for (i = 0; i < len; i++) {
725  if (!strcmp(levels[i], level)) {
726  return 1;
727  }
728  }
729  return 0;
730 }
731 
732 /*!
733  * \brief Read config, setup channels.
734  * \param altconf Alternate configuration file to read.
735  *
736  * \pre logchannels list is write locked
737  *
738  * \retval 0 Success
739  * \retval -1 No config found or Failed
740  */
741 static int init_logger_chain(const char *altconf)
742 {
743  struct logchannel *chan;
744  struct ast_config *cfg;
745  struct ast_variable *var;
746  const char *s;
747  struct ast_flags config_flags = { 0 };
748 
749  if (!(cfg = ast_config_load2(S_OR(altconf, "logger.conf"), "logger", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
750  cfg = NULL;
751  }
752 
753  /* Set defaults */
754  hostname[0] = '\0';
755  display_callids = 1;
756  memset(&logfiles, 0, sizeof(logfiles));
757  logfiles.queue_log = 1;
758  ast_copy_string(dateformat, "%b %e %T", sizeof(dateformat));
760  exec_after_rotate[0] = '\0';
762 
763  /* delete our list of log channels */
764  while ((chan = AST_RWLIST_REMOVE_HEAD(&logchannels, list))) {
765  ast_free(chan);
766  }
767  global_logmask = 0;
768 
769  errno = 0;
770  /* close syslog */
771  closelog();
772 
773  /* If no config file, we're fine, set default options. */
774  if (!cfg) {
775  chan = make_logchannel("console", "error,warning,notice,verbose", 0, 0);
776  if (!chan) {
777  fprintf(stderr, "ERROR: Failed to initialize default logging\n");
778  return -1;
779  }
780 
781  AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
782  global_logmask |= chan->logmask;
783 
784  return -1;
785  }
786 
787  if ((s = ast_variable_retrieve(cfg, "general", "appendhostname"))) {
788  if (ast_true(s)) {
789  if (gethostname(hostname, sizeof(hostname) - 1)) {
790  ast_copy_string(hostname, "unknown", sizeof(hostname));
791  fprintf(stderr, "What box has no hostname???\n");
792  }
793  }
794  }
795  if ((s = ast_variable_retrieve(cfg, "general", "display_callids"))) {
797  }
798  if ((s = ast_variable_retrieve(cfg, "general", "dateformat"))) {
800  }
801  if ((s = ast_variable_retrieve(cfg, "general", "queue_log"))) {
802  logfiles.queue_log = ast_true(s);
803  }
804  if ((s = ast_variable_retrieve(cfg, "general", "queue_log_to_file"))) {
805  logfiles.queue_log_to_file = ast_true(s);
806  }
807  if ((s = ast_variable_retrieve(cfg, "general", "queue_log_name"))) {
809  }
810  if ((s = ast_variable_retrieve(cfg, "general", "queue_log_realtime_use_gmt"))) {
811  logfiles.queue_log_realtime_use_gmt = ast_true(s);
812  }
813  if ((s = ast_variable_retrieve(cfg, "general", "exec_after_rotate"))) {
815  }
816  if ((s = ast_variable_retrieve(cfg, "general", "rotatestrategy"))) {
817  if (strcasecmp(s, "timestamp") == 0) {
819  } else if (strcasecmp(s, "rotate") == 0) {
821  } else if (strcasecmp(s, "sequential") == 0) {
823  } else if (strcasecmp(s, "none") == 0) {
825  } else {
826  fprintf(stderr, "Unknown rotatestrategy: %s\n", s);
827  }
828  } else {
829  if ((s = ast_variable_retrieve(cfg, "general", "rotatetimestamp"))) {
831  fprintf(stderr, "rotatetimestamp option has been deprecated. Please use rotatestrategy instead.\n");
832  }
833  }
834  if ((s = ast_variable_retrieve(cfg, "general", "logger_queue_limit"))) {
835  if (sscanf(s, "%30d", &logger_queue_limit) != 1) {
836  fprintf(stderr, "logger_queue_limit has an invalid value. Leaving at default of %d.\n",
838  }
839  if (logger_queue_limit < 10) {
840  fprintf(stderr, "logger_queue_limit must be >= 10. Setting to 10.\n");
841  logger_queue_limit = 10;
842  }
843  }
844 
845  /* Custom dynamic logging levels defined by user */
846  if ((s = ast_variable_retrieve(cfg, "general", "custom_levels"))) {
847  char *customlogs = ast_strdupa(s);
848  char *logfile;
849  char *new_custom_levels[16] = { };
850  unsigned int level, new_level = 0;
851 
852  /* get the custom levels we need to register or reload */
853  while ((logfile = strsep(&customlogs, ","))) {
854  new_custom_levels[new_level++] = logfile;
855  }
856 
857  /* unregister existing custom levels, if they're not still
858  specified in customlogs, to make room for new levels */
859  for (level = 16; level < ARRAY_LEN(levels); level++) {
860  if (levels[level] && custom_dynamic_levels[level] &&
861  !custom_level_still_exists(new_custom_levels, levels[level], ARRAY_LEN(new_custom_levels))) {
863  custom_dynamic_levels[level] = 0;
864  }
865  }
866 
867  new_level = 0;
868  while ((logfile = new_custom_levels[new_level++])) {
869  /* Lock already held, so directly register the level,
870  unless it's already registered (as during reload) */
871  if (logger_get_dynamic_level(logfile) == -1) {
872  int custom_level = logger_register_level(logfile);
873  custom_dynamic_levels[custom_level] = logfile;
874  }
875  }
876  }
877 
878  var = ast_variable_browse(cfg, "logfiles");
879  for (; var; var = var->next) {
880  chan = make_logchannel(var->name, var->value, var->lineno, 0);
881  if (!chan) {
882  /* Print error message directly to the consoles since the lock is held
883  * and we don't want to unlock with the list partially built */
884  ast_console_puts_mutable("ERROR: Unable to create log channel '", __LOG_ERROR);
887  continue;
888  }
889  AST_RWLIST_INSERT_HEAD(&logchannels, chan, list);
890  global_logmask |= chan->logmask;
891  }
892 
893  if (qlog) {
894  fclose(qlog);
895  qlog = NULL;
896  }
897 
898  ast_config_destroy(cfg);
899 
900  return 0;
901 }
902 
903 void ast_child_verbose(int level, const char *fmt, ...)
904 {
905  char *msg = NULL, *emsg = NULL, *sptr, *eptr;
906  va_list ap, aq;
907  int size;
908 
909  va_start(ap, fmt);
910  va_copy(aq, ap);
911  if ((size = vsnprintf(msg, 0, fmt, ap)) < 0) {
912  va_end(ap);
913  va_end(aq);
914  return;
915  }
916  va_end(ap);
917 
918  if (!(msg = ast_malloc(size + 1))) {
919  va_end(aq);
920  return;
921  }
922 
923  vsnprintf(msg, size + 1, fmt, aq);
924  va_end(aq);
925 
926  if (!(emsg = ast_malloc(size * 2 + 1))) {
927  ast_free(msg);
928  return;
929  }
930 
931  for (sptr = msg, eptr = emsg; ; sptr++) {
932  if (*sptr == '"') {
933  *eptr++ = '\\';
934  }
935  *eptr++ = *sptr;
936  if (*sptr == '\0') {
937  break;
938  }
939  }
940  ast_free(msg);
941 
942  fprintf(stdout, "verbose \"%s\" %d\n", emsg, level);
943  fflush(stdout);
944  ast_free(emsg);
945 }
946 
947 void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
948 {
949  va_list ap;
950  struct timeval tv;
951  struct ast_tm tm;
952  char qlog_msg[8192];
953  int qlog_len;
954  char time_str[30];
955 
956  if (!logger_initialized) {
957  /* You are too early. We are not open yet! */
958  return;
959  }
960  if (!queuelog_init) {
961  /* We must initialize now since someone is trying to log something. */
963  }
964 
965  if (ast_check_realtime("queue_log")) {
966  tv = ast_tvnow();
967  ast_localtime(&tv, &tm, logfiles.queue_log_realtime_use_gmt ? "GMT" : NULL);
968  ast_strftime(time_str, sizeof(time_str), "%F %T.%6q", &tm);
969  va_start(ap, fmt);
970  vsnprintf(qlog_msg, sizeof(qlog_msg), fmt, ap);
971  va_end(ap);
972  if (logfiles.queue_adaptive_realtime) {
974  AST_APP_ARG(data)[5];
975  );
976  AST_NONSTANDARD_APP_ARGS(args, qlog_msg, '|');
977  /* Ensure fields are large enough to receive data */
978  ast_realtime_require_field("queue_log",
979  "data1", RQ_CHAR, strlen(S_OR(args.data[0], "")),
980  "data2", RQ_CHAR, strlen(S_OR(args.data[1], "")),
981  "data3", RQ_CHAR, strlen(S_OR(args.data[2], "")),
982  "data4", RQ_CHAR, strlen(S_OR(args.data[3], "")),
983  "data5", RQ_CHAR, strlen(S_OR(args.data[4], "")),
984  SENTINEL);
985 
986  /* Store the log */
987  ast_store_realtime("queue_log", "time", time_str,
988  "callid", callid,
989  "queuename", queuename,
990  "agent", agent,
991  "event", event,
992  "data1", S_OR(args.data[0], ""),
993  "data2", S_OR(args.data[1], ""),
994  "data3", S_OR(args.data[2], ""),
995  "data4", S_OR(args.data[3], ""),
996  "data5", S_OR(args.data[4], ""),
997  SENTINEL);
998  } else {
999  ast_store_realtime("queue_log", "time", time_str,
1000  "callid", callid,
1001  "queuename", queuename,
1002  "agent", agent,
1003  "event", event,
1004  "data", qlog_msg,
1005  SENTINEL);
1006  }
1007 
1008  if (!logfiles.queue_log_to_file) {
1009  return;
1010  }
1011  }
1012 
1013  if (qlog) {
1014  va_start(ap, fmt);
1015  qlog_len = snprintf(qlog_msg, sizeof(qlog_msg), "%ld|%s|%s|%s|%s|", (long)time(NULL), callid, queuename, agent, event);
1016  vsnprintf(qlog_msg + qlog_len, sizeof(qlog_msg) - qlog_len, fmt, ap);
1017  va_end(ap);
1019  if (qlog) {
1020  fprintf(qlog, "%s\n", qlog_msg);
1021  fflush(qlog);
1022  }
1024  }
1025 }
1026 
1027 static int rotate_file(const char *filename)
1028 {
1029  char old[PATH_MAX];
1030  char new[PATH_MAX];
1031  int x, y, which, found, res = 0, fd;
1032  char *suffixes[4] = { "", ".gz", ".bz2", ".Z" };
1033 
1034  switch (rotatestrategy) {
1035  case NONE:
1036  /* No rotation */
1037  break;
1038  case SEQUENTIAL:
1039  for (x = 0; ; x++) {
1040  snprintf(new, sizeof(new), "%s.%d", filename, x);
1041  fd = open(new, O_RDONLY);
1042  if (fd > -1)
1043  close(fd);
1044  else
1045  break;
1046  }
1047  if (rename(filename, new)) {
1048  fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
1049  res = -1;
1050  } else {
1051  filename = new;
1052  }
1053  break;
1054  case TIMESTAMP:
1055  snprintf(new, sizeof(new), "%s.%ld", filename, (long)time(NULL));
1056  if (rename(filename, new)) {
1057  fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
1058  res = -1;
1059  } else {
1060  filename = new;
1061  }
1062  break;
1063  case ROTATE:
1064  /* Find the next empty slot, including a possible suffix */
1065  for (x = 0; ; x++) {
1066  found = 0;
1067  for (which = 0; which < ARRAY_LEN(suffixes); which++) {
1068  snprintf(new, sizeof(new), "%s.%d%s", filename, x, suffixes[which]);
1069  fd = open(new, O_RDONLY);
1070  if (fd > -1) {
1071  close(fd);
1072  found = 1;
1073  break;
1074  }
1075  }
1076  if (!found) {
1077  break;
1078  }
1079  }
1080 
1081  /* Found an empty slot */
1082  for (y = x; y > 0; y--) {
1083  for (which = 0; which < ARRAY_LEN(suffixes); which++) {
1084  snprintf(old, sizeof(old), "%s.%d%s", filename, y - 1, suffixes[which]);
1085  fd = open(old, O_RDONLY);
1086  if (fd > -1) {
1087  /* Found the right suffix */
1088  close(fd);
1089  snprintf(new, sizeof(new), "%s.%d%s", filename, y, suffixes[which]);
1090  if (rename(old, new)) {
1091  fprintf(stderr, "Unable to rename file '%s' to '%s'\n", old, new);
1092  res = -1;
1093  }
1094  break;
1095  }
1096  }
1097  }
1098 
1099  /* Finally, rename the current file */
1100  snprintf(new, sizeof(new), "%s.0", filename);
1101  if (rename(filename, new)) {
1102  fprintf(stderr, "Unable to rename file '%s' to '%s'\n", filename, new);
1103  res = -1;
1104  } else {
1105  filename = new;
1106  }
1107  }
1108 
1111  char buf[512];
1112 
1113  pbx_builtin_setvar_helper(c, "filename", filename);
1115  if (c) {
1116  c = ast_channel_unref(c);
1117  }
1118  if (ast_safe_system(buf) == -1) {
1119  ast_log(LOG_WARNING, "error executing '%s'\n", buf);
1120  }
1121  }
1122  return res;
1123 }
1124 
1125 /*!
1126  * \internal
1127  * \brief Start the realtime queue logging if configured.
1128  *
1129  * \retval TRUE if not to open queue log file.
1130  */
1131 static int logger_queue_rt_start(void)
1132 {
1133  if (ast_check_realtime("queue_log")) {
1134  if (!ast_realtime_require_field("queue_log",
1135  "time", RQ_DATETIME, 26,
1136  "data1", RQ_CHAR, 20,
1137  "data2", RQ_CHAR, 20,
1138  "data3", RQ_CHAR, 20,
1139  "data4", RQ_CHAR, 20,
1140  "data5", RQ_CHAR, 20,
1141  SENTINEL)) {
1142  logfiles.queue_adaptive_realtime = 1;
1143  } else {
1144  logfiles.queue_adaptive_realtime = 0;
1145  }
1146 
1147  if (!logfiles.queue_log_to_file) {
1148  /* Don't open the log file. */
1149  return 1;
1150  }
1151  }
1152  return 0;
1153 }
1154 
1155 /*!
1156  * \internal
1157  * \brief Rotate the queue log file and restart.
1158  *
1159  * \param queue_rotate Log queue rotation mode.
1160  *
1161  * \note Assumes logchannels is write locked on entry.
1162  *
1163  * \retval 0 on success.
1164  * \retval -1 on error.
1165  */
1166 static int logger_queue_restart(int queue_rotate)
1167 {
1168  int res = 0;
1169  char qfname[PATH_MAX];
1170 
1171  if (logger_queue_rt_start()) {
1172  return res;
1173  }
1174 
1175  snprintf(qfname, sizeof(qfname), "%s/%s", ast_config_AST_LOG_DIR, queue_log_name);
1176  if (qlog) {
1177  /* Just in case it was still open. */
1178  fclose(qlog);
1179  qlog = NULL;
1180  }
1181  if (queue_rotate) {
1182  rotate_file(qfname);
1183  }
1184 
1185  /* Open the log file. */
1186  qlog = fopen(qfname, "a");
1187  if (!qlog) {
1188  ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
1189  res = -1;
1190  }
1191  return res;
1192 }
1193 
1194 static int reload_logger(int rotate, const char *altconf)
1195 {
1196  int queue_rotate = rotate;
1197  struct logchannel *f;
1198  int res = 0;
1199 
1201 
1202  if (qlog) {
1203  if (rotate < 0) {
1204  /* Check filesize - this one typically doesn't need an auto-rotate */
1205  if (ftello(qlog) > 0x40000000) { /* Arbitrarily, 1 GB */
1206  fclose(qlog);
1207  qlog = NULL;
1208  } else {
1209  queue_rotate = 0;
1210  }
1211  } else {
1212  fclose(qlog);
1213  qlog = NULL;
1214  }
1215  } else {
1216  queue_rotate = 0;
1217  }
1218 
1220 
1222  if (f->disabled) {
1223  f->disabled = 0; /* Re-enable logging at reload */
1224  /*** DOCUMENTATION
1225  <managerEventInstance>
1226  <synopsis>Raised when a logging channel is re-enabled after a reload operation.</synopsis>
1227  <syntax>
1228  <parameter name="Channel">
1229  <para>The name of the logging channel.</para>
1230  </parameter>
1231  </syntax>
1232  </managerEventInstance>
1233  ***/
1234  manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n", f->filename);
1235  }
1236  if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
1237  int rotate_this = 0;
1238  if (rotatestrategy != NONE && ftello(f->fileptr) > 0x40000000) { /* Arbitrarily, 1 GB */
1239  /* Be more proactive about rotating massive log files */
1240  rotate_this = 1;
1241  }
1242  fclose(f->fileptr); /* Close file */
1243  f->fileptr = NULL;
1244  if (rotate || rotate_this) {
1245  rotate_file(f->filename);
1246  }
1247  }
1248  }
1249 
1251 
1252  init_logger_chain(altconf);
1253 
1254  ast_unload_realtime("queue_log");
1255  if (logfiles.queue_log) {
1256  res = logger_queue_restart(queue_rotate);
1258  ast_verb_update();
1259  ast_queue_log("NONE", "NONE", "NONE", "CONFIGRELOAD", "%s", "");
1260  ast_verb(1, "Asterisk Queue Logger restarted\n");
1261  } else {
1263  ast_verb_update();
1264  }
1265 
1266  return res;
1267 }
1268 
1269 static char *handle_logger_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1270 {
1271  switch (cmd) {
1272  case CLI_INIT:
1273  e->command = "logger reload";
1274  e->usage =
1275  "Usage: logger reload [<alt-conf>]\n"
1276  " Reloads the logger subsystem state. Use after restarting syslogd(8) if you are using syslog logging.\n";
1277  return NULL;
1278  case CLI_GENERATE:
1279  return NULL;
1280  }
1281  if (reload_logger(0, a->argc == 3 ? a->argv[2] : NULL)) {
1282  ast_cli(a->fd, "Failed to reload the logger\n");
1283  return CLI_FAILURE;
1284  }
1285  return CLI_SUCCESS;
1286 }
1287 
1288 static char *handle_logger_rotate(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1289 {
1290  switch (cmd) {
1291  case CLI_INIT:
1292  e->command = "logger rotate";
1293  e->usage =
1294  "Usage: logger rotate\n"
1295  " Rotates and Reopens the log files.\n";
1296  return NULL;
1297  case CLI_GENERATE:
1298  return NULL;
1299  }
1300  if (reload_logger(1, NULL)) {
1301  ast_cli(a->fd, "Failed to reload the logger and rotate log files\n");
1302  return CLI_FAILURE;
1303  }
1304  return CLI_SUCCESS;
1305 }
1306 
1308 {
1309  return reload_logger(1, NULL);
1310 }
1311 
1312 int ast_logger_rotate_channel(const char *log_channel)
1313 {
1314  struct logchannel *f;
1315  int success = AST_LOGGER_FAILURE;
1316  char filename[PATH_MAX];
1317 
1318  make_filename(log_channel, filename, sizeof(filename));
1319 
1321 
1323 
1325  if (f->disabled) {
1326  f->disabled = 0; /* Re-enable logging at reload */
1327  manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: Yes\r\n",
1328  f->filename);
1329  }
1330  if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
1331  fclose(f->fileptr); /* Close file */
1332  f->fileptr = NULL;
1333  if (strcmp(filename, f->filename) == 0) {
1334  rotate_file(f->filename);
1335  success = AST_LOGGER_SUCCESS;
1336  }
1337  }
1338  }
1339 
1341 
1343 
1344  return success;
1345 }
1346 
1347 static char *handle_logger_set_level(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1348 {
1349  int x;
1350  int state;
1351  int level = -1;
1352 
1353  switch (cmd) {
1354  case CLI_INIT:
1355  e->command = "logger set level {DEBUG|TRACE|NOTICE|WARNING|ERROR|VERBOSE|DTMF} {on|off}";
1356  e->usage =
1357  "Usage: logger set level {DEBUG|TRACE|NOTICE|WARNING|ERROR|VERBOSE|DTMF} {on|off}\n"
1358  " Set a specific log level to enabled/disabled for this console.\n";
1359  return NULL;
1360  case CLI_GENERATE:
1361  return NULL;
1362  }
1363 
1364  if (a->argc < 5)
1365  return CLI_SHOWUSAGE;
1366 
1368 
1369  for (x = 0; x < ARRAY_LEN(levels); x++) {
1370  if (levels[x] && !strcasecmp(a->argv[3], levels[x])) {
1371  level = x;
1372  break;
1373  }
1374  }
1375 
1377 
1378  state = ast_true(a->argv[4]) ? 1 : 0;
1379 
1380  if (level != -1) {
1381  ast_console_toggle_loglevel(a->fd, level, state);
1382  ast_cli(a->fd, "Logger status for '%s' has been set to '%s'.\n", levels[level], state ? "on" : "off");
1383  } else
1384  return CLI_SHOWUSAGE;
1385 
1386  return CLI_SUCCESS;
1387 }
1388 
1389 int ast_logger_get_channels(int (*logentry)(const char *channel, const char *type,
1390  const char *status, const char *configuration, void *data), void *data)
1391 {
1392  struct logchannel *chan;
1393  struct ast_str *configs = ast_str_create(64);
1394  int res = AST_LOGGER_SUCCESS;
1395 
1396  if (!configs) {
1397  return AST_LOGGER_ALLOC_ERROR;
1398  }
1399 
1401  AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
1402  unsigned int level;
1403 
1405 
1406  for (level = 0; level < ARRAY_LEN(levels); level++) {
1407  if ((chan->logmask & (1 << level)) && levels[level]) {
1408  ast_str_append(&configs, 0, "%s ", levels[level]);
1409  }
1410  }
1411 
1412  res = logentry(chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" :
1413  (chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"), chan->disabled ?
1414  "Disabled" : "Enabled", ast_str_buffer(configs), data);
1415 
1416  if (res) {
1418  ast_free(configs);
1419  configs = NULL;
1420  return AST_LOGGER_FAILURE;
1421  }
1422  }
1424 
1425  ast_free(configs);
1426  configs = NULL;
1427 
1428  return AST_LOGGER_SUCCESS;
1429 }
1430 
1431 /*! \brief CLI command to show logging system configuration */
1432 static char *handle_logger_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1433 {
1434 #define FORMATL "%-35.35s %-8.8s %-10.10s %-9.9s "
1435  struct logchannel *chan;
1436  switch (cmd) {
1437  case CLI_INIT:
1438  e->command = "logger show channels";
1439  e->usage =
1440  "Usage: logger show channels\n"
1441  " List configured logger channels.\n";
1442  return NULL;
1443  case CLI_GENERATE:
1444  return NULL;
1445  }
1446  ast_cli(a->fd, "Logger queue limit: %d\n\n", logger_queue_limit);
1447  ast_cli(a->fd, FORMATL, "Channel", "Type", "Formatter", "Status");
1448  ast_cli(a->fd, "Configuration\n");
1449  ast_cli(a->fd, FORMATL, "-------", "----", "---------", "------");
1450  ast_cli(a->fd, "-------------\n");
1453  unsigned int level;
1454 
1455  ast_cli(a->fd, FORMATL, chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" : (chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"),
1456  chan->formatter.name,
1457  chan->disabled ? "Disabled" : "Enabled");
1458  ast_cli(a->fd, " - ");
1459  for (level = 0; level < ARRAY_LEN(levels); level++) {
1460  if ((chan->logmask & (1 << level)) && levels[level]) {
1461  ast_cli(a->fd, "%s ", levels[level]);
1462  }
1463  }
1464  ast_cli(a->fd, "\n");
1465  }
1467  ast_cli(a->fd, "\n");
1468 
1469  return CLI_SUCCESS;
1470 }
1471 
1472 /*! \brief CLI command to show logging levels */
1473 static char *handle_logger_show_levels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1474 {
1475 #define FORMATL2 "%5s %s\n"
1476  unsigned int level;
1477  switch (cmd) {
1478  case CLI_INIT:
1479  e->command = "logger show levels";
1480  e->usage =
1481  "Usage: logger show levels\n"
1482  " List configured logger levels.\n";
1483  return NULL;
1484  case CLI_GENERATE:
1485  return NULL;
1486  }
1487  ast_cli(a->fd, FORMATL2, "Level", "Name");
1488  ast_cli(a->fd, FORMATL2, "-----", "----");
1490  for (level = 0; level < ARRAY_LEN(levels); level++) {
1491  if (levels[level]) {
1492  ast_cli(a->fd, "%5d %s\n", level, levels[level]);
1493  }
1494  }
1496  ast_cli(a->fd, "\n");
1497 
1498  return CLI_SUCCESS;
1499 }
1500 
1501 int ast_logger_create_channel(const char *log_channel, const char *components)
1502 {
1503  struct logchannel *chan;
1504 
1505  if (ast_strlen_zero(components)) {
1506  return AST_LOGGER_DECLINE;
1507  }
1508 
1510 
1511  chan = find_logchannel(log_channel);
1512  if (chan) {
1514  return AST_LOGGER_FAILURE;
1515  }
1516 
1517  chan = make_logchannel(log_channel, components, 0, 1);
1518  if (!chan) {
1520  return AST_LOGGER_ALLOC_ERROR;
1521  }
1522 
1524  global_logmask |= chan->logmask;
1525 
1527 
1528  return AST_LOGGER_SUCCESS;
1529 }
1530 
1531 static char *handle_logger_add_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1532 {
1533  switch (cmd) {
1534  case CLI_INIT:
1535  e->command = "logger add channel";
1536  e->usage =
1537  "Usage: logger add channel <name> <levels>\n"
1538  " Adds a temporary logger channel. This logger channel\n"
1539  " will exist until removed or until Asterisk is restarted.\n"
1540  " <levels> is a comma-separated list of desired logger\n"
1541  " levels such as: verbose,warning,error\n"
1542  " An optional formatter may be specified with the levels;\n"
1543  " valid values are '[json]' and '[default]'.\n";
1544  return NULL;
1545  case CLI_GENERATE:
1546  return NULL;
1547  }
1548 
1549  if (a->argc < 5) {
1550  return CLI_SHOWUSAGE;
1551  }
1552 
1553  switch (ast_logger_create_channel(a->argv[3], a->argv[4])) {
1554  case AST_LOGGER_SUCCESS:
1555  return CLI_SUCCESS;
1556  case AST_LOGGER_FAILURE:
1557  ast_cli(a->fd, "Logger channel '%s' already exists\n", a->argv[3]);
1558  return CLI_SUCCESS;
1559  case AST_LOGGER_DECLINE:
1561  default:
1562  ast_cli(a->fd, "ERROR: Unable to create log channel '%s'\n", a->argv[3]);
1563  return CLI_FAILURE;
1564  }
1565 }
1566 
1567 int ast_logger_remove_channel(const char *log_channel)
1568 {
1569  struct logchannel *chan;
1570 
1572 
1573  chan = find_logchannel(log_channel);
1574  if (chan && chan->dynamic) {
1576  } else {
1578  return AST_LOGGER_FAILURE;
1579  }
1581 
1582  if (chan->fileptr) {
1583  fclose(chan->fileptr);
1584  chan->fileptr = NULL;
1585  }
1586  ast_free(chan);
1587  chan = NULL;
1588 
1589  return AST_LOGGER_SUCCESS;
1590 }
1591 
1592 static char *handle_logger_remove_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1593 {
1594  struct logchannel *chan;
1595  int gen_count = 0;
1596  char *gen_ret = NULL;
1597 
1598  switch (cmd) {
1599  case CLI_INIT:
1600  e->command = "logger remove channel";
1601  e->usage =
1602  "Usage: logger remove channel <name>\n"
1603  " Removes a temporary logger channel.\n";
1604  return NULL;
1605  case CLI_GENERATE:
1606  if (a->argc > 4 || (a->argc == 4 && a->pos > 3)) {
1607  return NULL;
1608  }
1611  if (chan->dynamic && (ast_strlen_zero(a->argv[3])
1612  || !strncmp(a->argv[3], chan->filename, strlen(a->argv[3])))) {
1613  if (gen_count == a->n) {
1614  gen_ret = ast_strdup(chan->filename);
1615  break;
1616  }
1617  gen_count++;
1618  }
1619  }
1621  return gen_ret;
1622  }
1623 
1624  if (a->argc < 4) {
1625  return CLI_SHOWUSAGE;
1626  }
1627 
1628  switch (ast_logger_remove_channel(a->argv[3])) {
1629  case AST_LOGGER_SUCCESS:
1630  ast_cli(a->fd, "Removed dynamic logger channel '%s'\n", a->argv[3]);
1631  return CLI_SUCCESS;
1632  case AST_LOGGER_FAILURE:
1633  ast_cli(a->fd, "Unable to find dynamic logger channel '%s'\n", a->argv[3]);
1634  return CLI_SUCCESS;
1635  default:
1636  ast_cli(a->fd, "Internal failure attempting to delete dynamic logger channel '%s'\n", a->argv[3]);
1637  return CLI_FAILURE;
1638  }
1639 }
1640 
1641 static struct ast_cli_entry cli_logger[] = {
1642  AST_CLI_DEFINE(handle_logger_show_channels, "List configured log channels"),
1643  AST_CLI_DEFINE(handle_logger_show_levels, "List configured log levels"),
1644  AST_CLI_DEFINE(handle_logger_reload, "Reopens the log files"),
1645  AST_CLI_DEFINE(handle_logger_rotate, "Rotates and reopens the log files"),
1646  AST_CLI_DEFINE(handle_logger_set_level, "Enables/Disables a specific logging level for this console"),
1647  AST_CLI_DEFINE(handle_logger_add_channel, "Adds a new logging channel"),
1648  AST_CLI_DEFINE(handle_logger_remove_channel, "Removes a logging channel"),
1649 };
1650 
1651 static void _handle_SIGXFSZ(int sig)
1652 {
1653  /* Indicate need to reload */
1655 }
1656 
1657 static struct sigaction handle_SIGXFSZ = {
1658  .sa_handler = _handle_SIGXFSZ,
1659  .sa_flags = SA_RESTART,
1660 };
1661 
1662 /*! \brief Print a normal log message to the channels */
1663 static void logger_print_normal(struct logmsg *logmsg)
1664 {
1665  struct logchannel *chan = NULL;
1666  char buf[BUFSIZ];
1667  int level = 0;
1668 
1670  if (!AST_RWLIST_EMPTY(&logchannels)) {
1672 
1673  /* If the channel is disabled, then move on to the next one */
1674  if (chan->disabled) {
1675  continue;
1676  }
1677  if (logmsg->level == __LOG_VERBOSE
1678  && (((chan->verbosity < 0) ? option_verbose : chan->verbosity)) < level) {
1679  continue;
1680  }
1681 
1682  if (!(chan->logmask & (1 << logmsg->level))) {
1683  continue;
1684  }
1685 
1686  switch (chan->type) {
1687  case LOGTYPE_SYSLOG:
1688  {
1689  int syslog_level = ast_syslog_priority_from_loglevel(logmsg->level);
1690 
1691  if (syslog_level < 0) {
1692  /* we are locked here, so cannot ast_log() */
1693  fprintf(stderr, "ast_log_vsyslog called with bogus level: %d\n", logmsg->level);
1694  continue;
1695  }
1696 
1697  /* Don't use LOG_MAKEPRI because it's broken in glibc<2.17 */
1698  syslog_level = chan->facility | syslog_level; /* LOG_MAKEPRI(chan->facility, syslog_level); */
1699  if (!chan->formatter.format_log(chan, logmsg, buf, BUFSIZ)) {
1700  syslog(syslog_level, "%s", buf);
1701  }
1702  }
1703  break;
1704  case LOGTYPE_CONSOLE:
1705  if (!chan->formatter.format_log(chan, logmsg, buf, BUFSIZ)) {
1707  }
1708  break;
1709  case LOGTYPE_FILE:
1710  {
1711  int res = 0;
1712 
1713  if (!chan->fileptr) {
1714  continue;
1715  }
1716 
1717  if (chan->formatter.format_log(chan, logmsg, buf, BUFSIZ)) {
1718  continue;
1719  }
1720 
1721  /* Print out to the file */
1722  res = fprintf(chan->fileptr, "%s", buf);
1723  if (res > 0) {
1724  fflush(chan->fileptr);
1725  } else if (res <= 0 && !ast_strlen_zero(logmsg->message)) {
1726  fprintf(stderr, "**** Asterisk Logging Error: ***********\n");
1727  if (errno == ENOMEM || errno == ENOSPC) {
1728  fprintf(stderr, "Asterisk logging error: Out of disk space, can't log to log file %s\n", chan->filename);
1729  } else {
1730  fprintf(stderr, "Logger Warning: Unable to write to log file '%s': %s (disabled)\n", chan->filename, strerror(errno));
1731  }
1732 
1733  /*** DOCUMENTATION
1734  <managerEventInstance>
1735  <synopsis>Raised when a logging channel is disabled.</synopsis>
1736  <syntax>
1737  <parameter name="Channel">
1738  <para>The name of the logging channel.</para>
1739  </parameter>
1740  </syntax>
1741  </managerEventInstance>
1742  ***/
1743  manager_event(EVENT_FLAG_SYSTEM, "LogChannel", "Channel: %s\r\nEnabled: No\r\nReason: %d - %s\r\n", chan->filename, errno, strerror(errno));
1744  chan->disabled = 1;
1745  }
1746  }
1747  break;
1748  }
1749  }
1750  } else if (logmsg->level != __LOG_VERBOSE || option_verbose >= logmsg->sublevel) {
1751  fputs(logmsg->message, stdout);
1752  }
1753 
1755 
1756  /* If we need to reload because of the file size, then do so */
1757  if (filesize_reload_needed) {
1758  reload_logger(-1, NULL);
1759  ast_verb(1, "Rotated Logs Per SIGXFSZ (Exceeded file size limit)\n");
1760  }
1761 
1762  return;
1763 }
1764 
1765 static struct logmsg * __attribute__((format(printf, 7, 0))) format_log_message_ap(int level,
1766  int sublevel, const char *file, int line, const char *function, ast_callid callid,
1767  const char *fmt, va_list ap)
1768 {
1769  struct logmsg *logmsg = NULL;
1770  struct ast_str *buf = NULL;
1771  struct ast_tm tm;
1772  struct timeval now = ast_tvnow();
1773  int res = 0;
1774  char datestring[256];
1775 
1777  return NULL;
1778  }
1779 
1780  /* Build string */
1781  res = ast_str_set_va(&buf, BUFSIZ, fmt, ap);
1782 
1783  /* If the build failed, then abort and free this structure */
1784  if (res == AST_DYNSTR_BUILD_FAILED) {
1785  return NULL;
1786  }
1787 
1788  /* Automatically add a newline to format strings that don't have one */
1789  if (!ast_ends_with(ast_str_buffer(buf), "\n")) {
1790  ast_str_append(&buf, 0, "\n");
1791  }
1792 
1793  /* Create a new logging message */
1794  if (!(logmsg = ast_calloc_with_stringfields(1, struct logmsg, res + 128))) {
1795  return NULL;
1796  }
1797 
1798  /* Copy string over */
1800 
1801  /* Set type */
1802  if (level == __LOG_VERBOSE) {
1804  } else {
1806  }
1807 
1808  if (display_callids && callid) {
1809  logmsg->callid = callid;
1810  }
1811 
1812  /* Create our date/time */
1813  ast_localtime(&now, &tm, NULL);
1814  ast_strftime(datestring, sizeof(datestring), dateformat, &tm);
1815  ast_string_field_set(logmsg, date, datestring);
1816 
1817  /* Copy over data */
1818  logmsg->level = level;
1819  logmsg->sublevel = sublevel;
1820  logmsg->line = line;
1821  ast_string_field_set(logmsg, level_name, levels[level]);
1823  ast_string_field_set(logmsg, function, function);
1824  logmsg->lwp = ast_get_tid();
1825 
1826  return logmsg;
1827 }
1828 
1829 static struct logmsg * __attribute__((format(printf, 7, 0))) format_log_message(int level,
1830  int sublevel, const char *file, int line, const char *function, ast_callid callid,
1831  const char *fmt, ...)
1832 {
1833  struct logmsg *logmsg;
1834  va_list ap;
1835 
1836  va_start(ap, fmt);
1837  logmsg = format_log_message_ap(level, sublevel, file, line, function, callid, fmt, ap);
1838  va_end(ap);
1839 
1840  return logmsg;
1841 }
1842 
1843 /*! \brief Actual logging thread */
1844 static void *logger_thread(void *data)
1845 {
1846  struct logmsg *next = NULL, *msg = NULL;
1847 
1848  for (;;) {
1849  /* We lock the message list, and see if any message exists... if not we wait on the condition to be signalled */
1851  if (AST_LIST_EMPTY(&logmsgs)) {
1852  if (close_logger_thread) {
1854  break;
1855  } else {
1857  }
1858  }
1859 
1860  if (high_water_alert) {
1861  msg = format_log_message(__LOG_WARNING, 0, "logger", 0, "***", 0,
1862  "Logging resumed. %d message%s discarded.\n",
1864  if (msg) {
1866  }
1867  high_water_alert = 0;
1869  }
1870 
1873  logger_queue_size = 0;
1875 
1876  /* Otherwise go through and process each message in the order added */
1877  while ((msg = next)) {
1878  /* Get the next entry now so that we can free our current structure later */
1879  next = AST_LIST_NEXT(msg, list);
1880 
1881  /* Depending on the type, send it to the proper function */
1882  logger_print_normal(msg);
1883 
1884  /* Free the data since we are done */
1885  logmsg_free(msg);
1886  }
1887  }
1888 
1889  return NULL;
1890 }
1891 
1892 /*!
1893  * \internal
1894  * \brief Initialize the logger queue.
1895  *
1896  * \note Assumes logchannels is write locked on entry.
1897  *
1898  * \return Nothing
1899  */
1900 static void logger_queue_init(void)
1901 {
1902  ast_unload_realtime("queue_log");
1903  if (logfiles.queue_log) {
1904  char qfname[PATH_MAX];
1905 
1906  if (logger_queue_rt_start()) {
1907  return;
1908  }
1909 
1910  /* Open the log file. */
1911  snprintf(qfname, sizeof(qfname), "%s/%s", ast_config_AST_LOG_DIR,
1912  queue_log_name);
1913  if (qlog) {
1914  /* Just in case it was already open. */
1915  fclose(qlog);
1916  }
1917  qlog = fopen(qfname, "a");
1918  if (!qlog) {
1919  ast_log(LOG_ERROR, "Unable to create queue log: %s\n", strerror(errno));
1920  }
1921  }
1922 }
1923 
1925 {
1926  return logger_initialized;
1927 }
1928 
1929 /*!
1930  * \brief Start the ast_queue_log() logger.
1931  *
1932  * \note Called when the system is fully booted after startup
1933  * so preloaded realtime modules can get up.
1934  *
1935  * \return Nothing
1936  */
1938 {
1939  /* Must not be called before the logger is initialized. */
1941 
1943  if (!queuelog_init) {
1945  queuelog_init = 1;
1947  ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
1948  } else {
1950  }
1951 }
1952 
1953 int init_logger(void)
1954 {
1955  int res;
1956  /* auto rotate if sig SIGXFSZ comes a-knockin */
1957  sigaction(SIGXFSZ, &handle_SIGXFSZ, NULL);
1958 
1959  /* Re-initialize the logmsgs mutex. The recursive mutex can be accessed prior
1960  * to Asterisk being forked into the background, which can cause the thread
1961  * ID tracked by the underlying pthread mutex to be different than the ID of
1962  * the thread that unlocks the mutex. Since init_logger is called after the
1963  * fork, it is safe to initialize the mutex here for future accesses.
1964  */
1968 
1969  /* start logger thread */
1972  return -1;
1973  }
1974 
1975  /* register the logger cli commands */
1977 
1979 
1980  /* create log channels */
1982  res = init_logger_chain(NULL);
1984  ast_verb_update();
1985  logger_initialized = 1;
1986  if (res) {
1987  ast_log(LOG_ERROR, "Errors detected in logger.conf. Default console logging is being used.\n");
1988  }
1989 
1991 
1992  return 0;
1993 }
1994 
1995 void close_logger(void)
1996 {
1997  struct logchannel *f = NULL;
1998 
2000 
2002 
2003  logger_initialized = 0;
2004 
2005  /* Stop logger thread */
2007  close_logger_thread = 1;
2010 
2011  if (logthread != AST_PTHREADT_NULL) {
2012  pthread_join(logthread, NULL);
2013  }
2014 
2016 
2017  if (qlog) {
2018  fclose(qlog);
2019  qlog = NULL;
2020  }
2021 
2022  while ((f = AST_LIST_REMOVE_HEAD(&logchannels, list))) {
2023  if (f->fileptr && (f->fileptr != stdout) && (f->fileptr != stderr)) {
2024  fclose(f->fileptr);
2025  f->fileptr = NULL;
2026  }
2027  ast_free(f);
2028  }
2029 
2030  closelog(); /* syslog */
2031 
2033 }
2034 
2035 void ast_callid_strnprint(char *buffer, size_t buffer_size, ast_callid callid)
2036 {
2037  snprintf(buffer, buffer_size, "[C-%08x]", callid);
2038 }
2039 
2041 {
2043 }
2044 
2046 {
2047  ast_callid *callid;
2048 
2049  callid = ast_threadstorage_get(&unique_callid, sizeof(*callid));
2050 
2051  return callid ? *callid : 0;
2052 }
2053 
2055 {
2056  ast_callid *id = ast_threadstorage_get(&unique_callid, sizeof(*id));
2057 
2058  if (!id) {
2059  return -1;
2060  }
2061 
2062  *id = callid;
2063 
2064  return 0;
2065 }
2066 
2068 {
2069  ast_callid *pointing;
2070 
2071  pointing = ast_threadstorage_get(&unique_callid, sizeof(*pointing));
2072  if (!pointing) {
2073  return -1;
2074  }
2075 
2076  if (*pointing) {
2077  ast_log(LOG_ERROR, "ast_callid_threadassoc_add(C-%08x) on thread "
2078  "already associated with callid [C-%08x].\n", callid, *pointing);
2079  return 1;
2080  }
2081 
2082  *pointing = callid;
2083  return 0;
2084 }
2085 
2087 {
2088  ast_callid *pointing;
2089 
2090  pointing = ast_threadstorage_get(&unique_callid, sizeof(*pointing));
2091  if (!pointing) {
2092  return -1;
2093  }
2094 
2095  if (*pointing) {
2096  *pointing = 0;
2097  return 0;
2098  }
2099 
2100  return -1;
2101 }
2102 
2104 {
2105  ast_callid tmp;
2106 
2107  /* Start by trying to see if a callid is available from thread storage */
2109  if (tmp) {
2110  *callid = tmp;
2111  return 0;
2112  }
2113 
2114  /* If that failed, try to create a new one and bind it. */
2115  *callid = ast_create_callid();
2116  if (*callid) {
2117  ast_callid_threadassoc_add(*callid);
2118  return 1;
2119  }
2120 
2121  /* If neither worked, then something must have gone wrong. */
2122  return -1;
2123 }
2124 
2125 void ast_callid_threadstorage_auto_clean(ast_callid callid, int callid_created)
2126 {
2127  if (callid && callid_created) {
2128  /* If the callid was created rather than simply grabbed from the thread storage, we need to unbind here. */
2130  }
2131 }
2132 
2133 /*!
2134  * \brief send log messages to syslog and/or the console
2135  */
2136 static void __attribute__((format(printf, 7, 0))) ast_log_full(int level, int sublevel,
2137  const char *file, int line, const char *function, ast_callid callid,
2138  const char *fmt, va_list ap)
2139 {
2140  struct logmsg *logmsg = NULL;
2141 
2143  return;
2144  }
2145 
2150  logmsg = format_log_message(__LOG_WARNING, 0, "logger", 0, "***", 0,
2151  "Log queue threshold (%d) exceeded. Discarding new messages.\n", logger_queue_limit);
2153  high_water_alert = 1;
2155  }
2157  return;
2158  }
2160 
2161  logmsg = format_log_message_ap(level, sublevel, file, line, function, callid, fmt, ap);
2162  if (!logmsg) {
2163  return;
2164  }
2165 
2166  /* If the logger thread is active, append it to the tail end of the list - otherwise skip that step */
2167  if (logthread != AST_PTHREADT_NULL) {
2169  if (close_logger_thread) {
2170  /* Logger is either closing or closed. We cannot log this message. */
2172  } else {
2176  }
2178  } else {
2181  }
2182 }
2183 
2184 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
2185 {
2186  va_list ap;
2187 
2188  va_start(ap, fmt);
2189  ast_log_ap(level, file, line, function, fmt, ap);
2190  va_end(ap);
2191 }
2192 
2193 void ast_log_ap(int level, const char *file, int line, const char *function, const char *fmt, va_list ap)
2194 {
2196 
2198 
2199  if (level == __LOG_VERBOSE) {
2200  __ast_verbose_ap(file, line, function, 0, callid, fmt, ap);
2201  } else {
2202  ast_log_full(level, -1, file, line, function, callid, fmt, ap);
2203  }
2204 }
2205 
2206 void ast_log_safe(int level, const char *file, int line, const char *function, const char *fmt, ...)
2207 {
2208  va_list ap;
2209  void *recursed = ast_threadstorage_get_ptr(&in_safe_log);
2211 
2212  if (recursed) {
2213  return;
2214  }
2215 
2216  if (ast_threadstorage_set_ptr(&in_safe_log, &(int) { 1 })) {
2217  /* We've failed to set the flag that protects against
2218  * recursion, so bail. */
2219  return;
2220  }
2221 
2223 
2224  va_start(ap, fmt);
2225  ast_log_full(level, -1, file, line, function, callid, fmt, ap);
2226  va_end(ap);
2227 
2228  /* Clear flag so the next allocation failure can be logged. */
2229  ast_threadstorage_set_ptr(&in_safe_log, NULL);
2230 }
2231 
2232 void ast_log_callid(int level, const char *file, int line, const char *function, ast_callid callid, const char *fmt, ...)
2233 {
2234  va_list ap;
2235  va_start(ap, fmt);
2236  ast_log_full(level, -1, file, line, function, callid, fmt, ap);
2237  va_end(ap);
2238 }
2239 
2240 
2242 {
2243 #ifdef HAVE_BKTR
2244  struct ast_bt *bt;
2245  int i = 0;
2246  struct ast_vector_string *strings;
2247 
2248  if (!(bt = ast_bt_create())) {
2249  ast_log(LOG_WARNING, "Unable to allocate space for backtrace structure\n");
2250  return;
2251  }
2252 
2253  if ((strings = ast_bt_get_symbols(bt->addresses, bt->num_frames))) {
2254  int count = AST_VECTOR_SIZE(strings);
2255  struct ast_str *buf = ast_str_create(bt->num_frames * 64);
2256 
2257  if (buf) {
2258  ast_str_append(&buf, 0, "Got %d backtrace record%c\n", count - 3, count - 3 != 1 ? 's' : ' ');
2259  for (i = 3; i < AST_VECTOR_SIZE(strings); i++) {
2260  ast_str_append(&buf, 0, "#%2d: %s\n", i - 3, AST_VECTOR_GET(strings, i));
2261  }
2263  ast_free(buf);
2264  }
2265 
2266  ast_bt_free_symbols(strings);
2267  } else {
2268  ast_log(LOG_ERROR, "Could not allocate memory for backtrace\n");
2269  }
2270  ast_bt_destroy(bt);
2271 #else
2272  ast_log(LOG_WARNING, "Must run configure with '--with-execinfo' for stack backtraces.\n");
2273 #endif /* defined(HAVE_BKTR) */
2274 }
2275 
2276 void __ast_verbose_ap(const char *file, int line, const char *func, int level, ast_callid callid, const char *fmt, va_list ap)
2277 {
2278  ast_log_full(__LOG_VERBOSE, level, file, line, func, callid, fmt, ap);
2279 }
2280 
2281 void __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt, ...)
2282 {
2283  ast_callid callid;
2284  va_list ap;
2285 
2286  callid = ast_read_threadstorage_callid();
2287 
2288  va_start(ap, fmt);
2289  __ast_verbose_ap(file, line, func, level, callid, fmt, ap);
2290  va_end(ap);
2291 }
2292 
2293 void __ast_verbose_callid(const char *file, int line, const char *func, int level, ast_callid callid, const char *fmt, ...)
2294 {
2295  va_list ap;
2296  va_start(ap, fmt);
2297  __ast_verbose_ap(file, line, func, level, callid, fmt, ap);
2298  va_end(ap);
2299 }
2300 
2301 /*! Console verbosity level node. */
2303  /*! List node link */
2305  /*! Console verbosity level. */
2306  int *level;
2307 };
2308 
2309 /*! Registered console verbosity levels */
2311 
2312 /*! ast_verb_update() reentrancy protection lock. */
2314 
2316 {
2317  struct logchannel *log;
2318  struct verb_console *console;
2319  int verb_level;
2320 
2322 
2324 
2325  /* Default to the root console verbosity. */
2326  verb_level = option_verbose;
2327 
2328  /* Determine max remote console level. */
2330  if (verb_level < *console->level) {
2331  verb_level = *console->level;
2332  }
2333  }
2335 
2336  /* Determine max logger channel level. */
2338  AST_RWLIST_TRAVERSE(&logchannels, log, list) {
2339  if (verb_level < log->verbosity) {
2340  verb_level = log->verbosity;
2341  }
2342  }
2344 
2345  ast_verb_sys_level = verb_level;
2346 
2348 }
2349 
2350 /*!
2351  * \internal
2352  * \brief Unregister a console verbose level.
2353  *
2354  * \param console Which console to unregister.
2355  *
2356  * \return Nothing
2357  */
2359 {
2363  if (console) {
2364  ast_verb_update();
2365  }
2366 }
2367 
2368 static void verb_console_free(void *v_console)
2369 {
2370  struct verb_console *console = v_console;
2371 
2373  ast_free(console);
2374 }
2375 
2376 /*! Thread specific console verbosity level node. */
2378 
2380 {
2381  struct verb_console *console;
2382 
2384  if (!console || !level) {
2385  return;
2386  }
2387  console->level = level;
2388 
2392  ast_verb_update();
2393 }
2394 
2396 {
2397  struct verb_console *console;
2398 
2400  if (!console) {
2401  return;
2402  }
2404 }
2405 
2407 {
2408  struct verb_console *console;
2409  int verb_level;
2410 
2413  if (!console) {
2414  verb_level = 0;
2415  } else if (console->level) {
2416  verb_level = *console->level;
2417  } else {
2418  verb_level = option_verbose;
2419  }
2421  return verb_level;
2422 }
2423 
2424 void ast_verb_console_set(int verb_level)
2425 {
2426  struct verb_console *console;
2427 
2429  if (!console) {
2430  return;
2431  }
2432 
2434  if (console->level) {
2435  *console->level = verb_level;
2436  } else {
2437  option_verbose = verb_level;
2438  }
2440  ast_verb_update();
2441 }
2442 
2443 static void update_logchannels(void)
2444 {
2445  struct logchannel *cur;
2446 
2447  global_logmask = 0;
2448 
2450  make_components(cur);
2451  global_logmask |= cur->logmask;
2452  }
2453 }
2454 
2455 #ifdef AST_DEVMODE
2456 
2457 AST_THREADSTORAGE_RAW(trace_indent);
2458 #define LOTS_O_SPACES " "
2459 
2460 unsigned long _ast_trace_get_indent(void)
2461 {
2462  return (unsigned long)ast_threadstorage_get_ptr(&trace_indent);
2463 }
2464 
2465 void _ast_trace_set_indent(unsigned long indent)
2466 {
2467  ast_threadstorage_set_ptr(&trace_indent, (void*)indent);
2468 }
2469 
2470 unsigned long _ast_trace_inc_indent(void)
2471 {
2472  unsigned long indent = (unsigned long)ast_threadstorage_get_ptr(&trace_indent);
2473  indent++;
2474  ast_threadstorage_set_ptr(&trace_indent, (void*)indent);
2475 
2476  return indent;
2477 }
2478 
2479 unsigned long _ast_trace_dec_indent(void)
2480 {
2481  unsigned long indent = (unsigned long)ast_threadstorage_get_ptr(&trace_indent);
2482  indent--;
2483  ast_threadstorage_set_ptr(&trace_indent, (void*)indent);
2484 
2485  return indent;
2486 }
2487 
2488 void __ast_trace(const char *file, int line, const char *func, enum ast_trace_indent_type indent_type,
2489  unsigned long new_indent, const char* format, ...)
2490 {
2491  va_list ap;
2492  unsigned long indent = (unsigned long)ast_threadstorage_get_ptr(&trace_indent);
2493  struct ast_str *fmt = ast_str_create(128);
2494  const char *direction = "";
2495 
2496  if (!fmt) {
2497  return;
2498  }
2499 
2500  if (indent_type == AST_TRACE_INDENT_PROVIDED) {
2501  indent = new_indent;
2502  ast_threadstorage_set_ptr(&trace_indent, (void*)indent);
2503  } else if (indent_type == AST_TRACE_INDENT_INC_BEFORE) {
2504  indent++;
2505  ast_threadstorage_set_ptr(&trace_indent, (void*)indent);
2506  } else if (indent_type == AST_TRACE_INDENT_DEC_BEFORE) {
2507  indent--;
2508  ast_threadstorage_set_ptr(&trace_indent, (void*)indent);
2509  }
2510 
2511  switch(indent_type) {
2512  case AST_TRACE_INDENT_NONE:
2513  case AST_TRACE_INDENT_SAME:
2514  direction = "";
2515  break;
2519  direction = "--> ";
2520  break;
2523  direction = "<-- ";
2524  break;
2525  }
2526 
2527  ast_str_set(&fmt, 0, "%2d %-.*s%s%s:%d %s: %s", (int)indent, (indent_type == AST_TRACE_INDENT_NONE ? 0 : (int)(indent * 4)),
2528  LOTS_O_SPACES, direction, file, line, func, S_OR(ast_skip_blanks(format), "\n"));
2529 
2530  if (indent_type == AST_TRACE_INDENT_INC_AFTER || indent_type == AST_TRACE_INDENT_PROVIDED) {
2531  indent++;
2532  ast_threadstorage_set_ptr(&trace_indent, (void*)indent);
2533  }
2534  if (indent_type == AST_TRACE_INDENT_DEC_AFTER) {
2535  indent--;
2536  ast_threadstorage_set_ptr(&trace_indent, (void*)indent);
2537  }
2538 
2539  va_start(ap, format);
2540  ast_log_full(__LOG_TRACE, -1, NULL, 0, NULL, 0, ast_str_buffer(fmt), ap);
2541  va_end(ap);
2542  ast_free(fmt);
2543 }
2544 #endif
2545 
2546 /* Lock should be held before calling this function */
2547 static int logger_register_level(const char *name)
2548 {
2549  unsigned int level;
2550  unsigned int available = 0;
2551 
2552  for (level = 0; level < ARRAY_LEN(levels); level++) {
2553  if ((level >= 16) && !available && !levels[level]) {
2554  available = level;
2555  continue;
2556  }
2557 
2558  if (levels[level] && !strcasecmp(levels[level], name)) {
2560  "Unable to register dynamic logger level '%s': a standard logger level uses that name.\n",
2561  name);
2562 
2563  return -1;
2564  }
2565  }
2566 
2567  if (!available) {
2569  "Unable to register dynamic logger level '%s'; maximum number of levels registered.\n",
2570  name);
2571 
2572  return -1;
2573  }
2574 
2576 
2577  ast_debug(1, "Registered dynamic logger level '%s' with index %u.\n", name, available);
2578 
2580 
2581  return available;
2582 }
2583 
2585 {
2586  int available = 0;
2587 
2591 
2592  return available;
2593 }
2594 
2595 static int logger_get_dynamic_level(const char *name)
2596 {
2597  int level = -1;
2598  unsigned int x;
2599 
2600  for (x = 16; x < ARRAY_LEN(levels); x++) {
2601  if (!levels[x]) {
2602  continue;
2603  }
2604  if (!strcasecmp(levels[x], name)) {
2605  level = x;
2606  break;
2607  }
2608  }
2609 
2610  return level;
2611 }
2612 
2614 {
2615  int level = -1;
2616 
2618 
2619  level = logger_get_dynamic_level(name);
2620 
2622 
2623  return level;
2624 }
2625 
2626 static int logger_unregister_level(const char *name) {
2627  unsigned int x;
2628 
2630  if (x == -1) {
2631  return 0;
2632  }
2633  /* take this level out of the global_logmask, to ensure that no new log messages
2634  * will be queued for it
2635  */
2636  global_logmask &= ~(1 << x);
2637  ast_free(levels[x]);
2638  levels[x] = NULL;
2639  return x;
2640 }
2641 
2643 {
2644  int x;
2645 
2648 
2649  if (x) {
2651  }
2652 
2654 
2655  if (x) {
2656  ast_debug(1, "Unregistered dynamic logger level '%s' with index %u.\n", name, x);
2657  }
2658 }
2659 
2660 const char *ast_logger_get_dateformat(void)
2661 {
2662  return dateformat;
2663 }
2664 
2665 void ast_logger_set_queue_limit(int queue_limit)
2666 {
2667  logger_queue_limit = queue_limit;
2668 }
2669 
2671 {
2672  return logger_queue_limit;
2673 }
2674 
2675 static int reload_module(void)
2676 {
2677  return reload_logger(0, NULL);
2678 }
2679 
2680 static int unload_module(void)
2681 {
2682  return 0;
2683 }
2684 
2685 static int load_module(void)
2686 {
2687  return AST_MODULE_LOAD_SUCCESS;
2688 }
2689 
2690 /* Logger is initialized separate from the module loader, only reload_module does anything. */
2692  .support_level = AST_MODULE_SUPPORT_CORE,
2693  .load = load_module,
2694  .unload = unload_module,
2695  /* This reload does not support realtime so it does not require "extconfig". */
2696  .reload = reload_module,
2697  .load_pri = 0,
2698 );
Prototypes for public functions only of internal interest,.
jack_status_t status
Definition: app_jack.c:146
const char * str
Definition: app_jack.c:147
#define var
Definition: ast_expr2f.c:614
Asterisk version information.
const char * ast_get_version(void)
Retrieve the Asterisk version string.
Asterisk main include file. File version handling, generic pbx functions.
#define PATH_MAX
Definition: asterisk.h:40
#define ast_free(a)
Definition: astmm.h:182
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
Asterisk backtrace generation.
#define ast_bt_free_symbols(string_vector)
Definition: backtrace.h:42
#define ast_bt_get_symbols(addresses, num_frames)
Definition: backtrace.h:41
#define ast_bt_create()
Definition: backtrace.h:39
#define ast_bt_destroy(bt)
Definition: backtrace.h:40
static int tmp()
Definition: bt_open.c:389
const char * ast_build_os
Definition: buildinfo.c:32
const char * ast_build_machine
Definition: buildinfo.c:31
const char * ast_build_hostname
Definition: buildinfo.c:29
const char * ast_build_user
Definition: buildinfo.c:34
const char * ast_build_date
Definition: buildinfo.c:33
static snd_pcm_format_t format
Definition: chan_alsa.c:106
static int available(struct dahdi_pvt **pvt, int is_specific_channel)
Definition: chan_dahdi.c:13027
static const char type[]
Definition: chan_ooh323.c:109
enum sip_cc_notify_state state
Definition: chan_sip.c:963
General Asterisk PBX channel definitions.
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2924
#define ast_dummy_channel_alloc()
Create a fake channel structure.
Definition: channel.h:1282
Standard Command Line Interface.
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_SUCCESS
Definition: cli.h:44
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
#define CLI_FAILURE
Definition: cli.h:46
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
#define SENTINEL
Definition: compiler.h:87
char * end
Definition: eagi_proxy.c:73
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static const char name[]
Definition: format_mp3.c:68
direction
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
int ast_verb_sys_level
Definition: options.c:64
int option_verbose
Definition: options.c:67
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
int ast_safe_system(const char *s)
Safely spawn an OS shell command while closing file descriptors.
Definition: extconf.c:829
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the 'nonstandard' argument separation process for an application.
char * strsep(char **str, const char *delims)
Configuration File Parser.
int ast_realtime_require_field(const char *family,...) attribute_sentinel
Inform realtime what fields that may be stored.
Definition: main/config.c:3382
int ast_unload_realtime(const char *family)
Release any resources cached for a realtime family.
Definition: main/config.c:3406
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
#define CONFIG_STATUS_FILEINVALID
int ast_check_realtime(const char *family)
Check if realtime engine is configured for family.
Definition: main/config.c:3363
const char * ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
Definition: main/config.c:694
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:3154
int ast_store_realtime(const char *family,...) attribute_sentinel
Create realtime configuration.
Definition: main/config.c:3570
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1216
Asterisk JSON abstraction layer.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
void ast_json_free(void *p)
Asterisk's custom JSON allocator. Exposed for use by unit tests.
Definition: json.c:52
#define ast_json_dump_string(root)
Encode a JSON value to a compact string.
Definition: json.h:780
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
#define AST_RWLIST_EMPTY
Definition: linkedlists.h:452
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
Definition: linkedlists.h:681
#define AST_LIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a list of specified type, statically initialized.
Definition: linkedlists.h:291
#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
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized.
Definition: linkedlists.h:333
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:844
#define AST_LIST_EMPTY(head)
Checks whether the specified list contains any entries.
Definition: linkedlists.h:450
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
#define AST_RWLIST_REMOVE
Definition: linkedlists.h:885
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:410
#define AST_LIST_LOCK(head)
Locks a list.
Definition: linkedlists.h:40
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:494
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:718
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
#define AST_LIST_UNLOCK(head)
Attempts to unlock a list.
Definition: linkedlists.h:140
#define AST_LIST_FIRST(head)
Returns the first entry contained in a list.
Definition: linkedlists.h:421
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:439
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
Asterisk locking-related definitions:
#define ast_cond_destroy(cond)
Definition: lock.h:200
#define ast_cond_wait(cond, mutex)
Definition: lock.h:203
#define AST_PTHREADT_NULL
Definition: lock.h:66
#define ast_cond_init(cond, attr)
Definition: lock.h:199
#define ast_mutex_init(pmutex)
Definition: lock.h:184
#define ast_mutex_unlock(a)
Definition: lock.h:188
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:755
pthread_cond_t ast_cond_t
Definition: lock.h:176
#define ast_mutex_destroy(a)
Definition: lock.h:186
#define ast_mutex_lock(a)
Definition: lock.h:187
#define AST_MUTEX_DEFINE_STATIC(mutex)
Definition: lock.h:518
#define ast_cond_signal(cond)
Definition: lock.h:201
unsigned int queue_log_to_file
Definition: logger.c:109
int ast_logger_get_dynamic_level(const char *name)
Retrieve dynamic logging level id.
Definition: logger.c:2613
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:571
static int format_log_json(struct logchannel *channel, struct logmsg *msg, char *buf, size_t size)
Definition: logger.c:270
static int init_logger_chain(const char *altconf)
Read config, setup channels.
Definition: logger.c:741
int ast_logger_get_queue_limit(void)
Get the maximum number of messages allowed in the processing queue.
Definition: logger.c:2670
static unsigned int global_logmask
Definition: logger.c:87
void __ast_verbose_ap(const char *file, int line, const char *func, int level, ast_callid callid, const char *fmt, va_list ap)
Definition: logger.c:2276
static int format_log_plain(struct logchannel *chan, struct logmsg *msg, char *buf, size_t size)
Definition: logger.c:440
int ast_logger_rotate_channel(const char *log_channel)
Rotate the specified log channel.
Definition: logger.c:1312
void ast_callid_strnprint(char *buffer, size_t buffer_size, ast_callid callid)
copy a string representation of the callid into a target string
Definition: logger.c:2035
AST_THREADSTORAGE_RAW(in_safe_log)
static void verb_console_free(void *v_console)
Definition: logger.c:2368
static char dateformat[256]
Definition: logger.c:81
static int queuelog_init
Definition: logger.c:88
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...
Definition: logger.c:2125
void logger_queue_start(void)
Start the ast_queue_log() logger.
Definition: logger.c:1937
void ast_verb_console_unregister(void)
Unregister this thread's console verbosity level.
Definition: logger.c:2395
logmsgtypes
Definition: logger.c:162
@ LOGMSG_NORMAL
Definition: logger.c:163
@ LOGMSG_VERBOSE
Definition: logger.c:164
static char * levels[NUMLOGLEVELS]
Logging channels used in the Asterisk logging system.
Definition: logger.c:208
static int filesize_reload_needed
Definition: logger.c:86
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:721
int ast_logger_create_channel(const char *log_channel, const char *components)
Create a log channel.
Definition: logger.c:1501
static void _handle_SIGXFSZ(int sig)
Definition: logger.c:1651
void ast_verb_console_register(int *level)
Register this thread's console verbosity level pointer.
Definition: logger.c:2379
static struct logmsg * format_log_message(int level, int sublevel, const char *file, int line, const char *function, ast_callid callid, const char *fmt,...)
Definition: logger.c:1829
#define FORMATL
static struct @384 logfiles
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:2136
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 th...
Definition: logger.c:2054
static int close_logger_thread
Definition: logger.c:193
rotatestrategy
Definition: logger.c:100
@ ROTATE
Definition: logger.c:103
@ TIMESTAMP
Definition: logger.c:104
@ SEQUENTIAL
Definition: logger.c:102
@ NONE
Definition: logger.c:101
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.
Definition: logger.c:2206
static char * custom_dynamic_levels[NUMLOGLEVELS]
Custom dynamic logging levels added by the user.
Definition: logger.c:225
int ast_logger_rotate()
Reload logger while rotating log files.
Definition: logger.c:1307
int ast_logger_remove_channel(const char *log_channel)
Delete the specified log channel.
Definition: logger.c:1567
#define FORMATL2
static int logger_queue_rt_start(void)
Definition: logger.c:1131
void close_logger(void)
Definition: logger.c:1995
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.
Definition: logger.c:1389
static int reload_logger(int rotate, const char *altconf)
Definition: logger.c:1194
int ast_is_logger_initialized(void)
Test if logger is initialized.
Definition: logger.c:1924
static char * handle_logger_rotate(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: logger.c:1288
static FILE * qlog
Definition: logger.c:195
static struct ast_threadstorage verbose_buf
Definition: logger.c:263
static void update_logchannels(void)
Definition: logger.c:2443
ast_callid ast_read_threadstorage_callid(void)
extracts the callerid from the thread
Definition: logger.c:2045
void ast_init_logger_for_socket_console(void)
load logger.conf configuration for console socket connections
Definition: logger.c:695
static int display_callids
Definition: logger.c:91
static int logger_initialized
Definition: logger.c:89
static char * handle_logger_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: logger.c:1269
static char exec_after_rotate[256]
Definition: logger.c:84
static pthread_t logthread
Definition: logger.c:191
int ast_callid_threadassoc_add(ast_callid callid)
Adds a known callid to thread storage of the calling thread.
Definition: logger.c:2067
ast_callid ast_create_callid(void)
factory function to create a new uniquely identifying callid.
Definition: logger.c:2040
static char * handle_logger_show_levels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
CLI command to show logging levels.
Definition: logger.c:1473
static int logger_queue_limit
Definition: logger.c:96
static struct logformatter logformatter_json
Definition: logger.c:325
const char * ast_logger_get_dateformat(void)
Get the logger configured date format.
Definition: logger.c:2660
static int logger_queue_size
Definition: logger.c:93
static struct logformatter logformatter_default
Definition: logger.c:435
unsigned int queue_log_realtime_use_gmt
Definition: logger.c:111
static int reload_module(void)
Definition: logger.c:2675
static int logger_add_verbose_magic(struct logmsg *logmsg, char *buf, size_t size)
Definition: logger.c:330
logtypes
Definition: logger.c:127
@ LOGTYPE_CONSOLE
Definition: logger.c:130
@ LOGTYPE_FILE
Definition: logger.c:129
@ LOGTYPE_SYSLOG
Definition: logger.c:128
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.
Definition: logger.c:2293
static unsigned int high_water_alert
Definition: logger.c:98
static char queue_log_name[256]
Definition: logger.c:83
static int logger_register_level(const char *name)
Definition: logger.c:2547
#define LOG_BUF_INIT_SIZE
Definition: logger.c:268
static ast_mutex_t verb_update_lock
Definition: logger.c:2313
static void make_components(struct logchannel *chan)
Definition: logger.c:490
void __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt,...)
Send a verbose message (based on verbose level)
Definition: logger.c:2281
void ast_log_ap(int level, const char *file, int line, const char *function, const char *fmt, va_list ap)
Definition: logger.c:2193
static char * handle_logger_set_level(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: logger.c:1347
static const int colors[NUMLOGLEVELS]
Colors used in the console for logging.
Definition: logger.c:228
static int format_log_default(struct logchannel *chan, struct logmsg *msg, char *buf, size_t size)
Definition: logger.c:376
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:2184
static struct sigaction handle_SIGXFSZ
Definition: logger.c:1657
static struct ast_threadstorage unique_callid
Definition: logger.c:93
static char * handle_logger_add_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: logger.c:1531
unsigned int queue_log
Definition: logger.c:108
int ast_callid_threadassoc_remove(void)
Removes callid from thread storage of the calling thread.
Definition: logger.c:2086
void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt,...)
Definition: logger.c:947
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 b...
Definition: logger.c:2103
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.
Definition: logger.c:1432
int ast_logger_register_level(const char *name)
Register a new logger level.
Definition: logger.c:2584
int init_logger(void)
Definition: logger.c:1953
void ast_logger_set_queue_limit(int queue_limit)
Set the maximum number of messages allowed in the processing queue.
Definition: logger.c:2665
void ast_verb_update(void)
Re-evaluate the system max verbosity level (ast_verb_sys_level).
Definition: logger.c:2315
static struct logformatter logformatter_plain
Definition: logger.c:485
static struct logmsg * format_log_message_ap(int level, int sublevel, const char *file, int line, const char *function, ast_callid callid, const char *fmt, va_list ap)
Definition: logger.c:1765
static struct logchannel * make_logchannel(const char *channel, const char *components, int lineno, int dynamic)
Definition: logger.c:628
static ast_cond_t logcond
Definition: logger.c:192
#define VERBOSE_BUF_INIT_SIZE
Definition: logger.c:265
void ast_child_verbose(int level, const char *fmt,...)
Definition: logger.c:903
static int logger_unregister_level(const char *name)
Definition: logger.c:2626
static struct ast_threadstorage my_verb_console
Definition: logger.c:2377
static void logger_print_normal(struct logmsg *logmsg)
Print a normal log message to the channels.
Definition: logger.c:1663
static int load_module(void)
Definition: logger.c:2685
static void * logger_thread(void *data)
Actual logging thread.
Definition: logger.c:1844
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 funct...
Definition: logger.c:2232
static struct ast_threadstorage log_buf
Definition: logger.c:267
static int unload_module(void)
Definition: logger.c:2680
static void logger_queue_init(void)
Definition: logger.c:1900
static struct ast_cli_entry cli_logger[]
Definition: logger.c:1641
static int logger_queue_restart(int queue_rotate)
Definition: logger.c:1166
int ast_verb_console_get(void)
Get this thread's console verbosity level.
Definition: logger.c:2406
void ast_logger_unregister_level(const char *name)
Unregister a previously registered logger level.
Definition: logger.c:2642
static char * handle_logger_remove_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: logger.c:1592
unsigned int queue_adaptive_realtime
Definition: logger.c:110
static int logger_messages_discarded
Definition: logger.c:97
static int rotate_file(const char *filename)
Definition: logger.c:1027
static volatile int next_unique_callid
Definition: logger.c:90
static char hostname[MAXHOSTNAMELEN]
Definition: logger.c:114
static struct logchannel * find_logchannel(const char *channel)
Find a particular logger channel by name.
Definition: logger.c:612
void ast_log_backtrace(void)
Log a backtrace of the current thread's execution stack to the Asterisk log.
Definition: logger.c:2241
void ast_verb_console_set(int verb_level)
Set this thread's console verbosity level.
Definition: logger.c:2424
static void verb_console_unregister(struct verb_console *console)
Definition: logger.c:2358
static int logger_get_dynamic_level(const char *name)
Definition: logger.c:2595
static void logmsg_free(struct logmsg *msg)
Definition: logger.c:184
static struct ast_threadstorage verbose_build_buf
Definition: logger.c:264
Support for logging to various files, console and syslog Configuration in file logger....
#define __LOG_ERROR
Definition: logger.h:285
#define VERBOSE_PREFIX_3
Definition: logger.h:44
#define NUMLOGLEVELS
Definition: logger.h:315
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:453
#define __LOG_TRACE
Definition: logger.h:252
void ast_console_toggle_loglevel(int fd, int level, int state)
enables or disables logging of a specified level to the console fd specifies the index of the console...
Definition: asterisk.c:1209
#define VERBOSE_PREFIX_2
Definition: logger.h:43
void ast_console_puts_mutable(const char *string, int level)
log the string to the console, and all attached console clients
Definition: asterisk.c:1274
unsigned int ast_callid
Definition: logger.h:88
#define QUEUELOG
Definition: logger.h:36
ast_trace_indent_type
Controls if and when indenting is applied.
Definition: logger.h:647
@ AST_TRACE_INDENT_INC_BEFORE
Definition: logger.h:651
@ AST_TRACE_INDENT_SAME
Definition: logger.h:649
@ AST_TRACE_INDENT_PROVIDED
Definition: logger.h:659
@ AST_TRACE_INDENT_NONE
Definition: logger.h:661
@ AST_TRACE_INDENT_INC_AFTER
Definition: logger.h:653
@ AST_TRACE_INDENT_DEC_BEFORE
Definition: logger.h:655
@ AST_TRACE_INDENT_DEC_AFTER
Definition: logger.h:657
#define VERBOSE_PREFIX_4
Definition: logger.h:45
#define __LOG_VERBOSE
Definition: logger.h:296
@ AST_LOGGER_DECLINE
Definition: logger.h:52
@ AST_LOGGER_FAILURE
Definition: logger.h:51
@ AST_LOGGER_SUCCESS
Definition: logger.h:50
@ AST_LOGGER_ALLOC_ERROR
Definition: logger.h:53
#define VERBOSE_PREFIX_1
Definition: logger.h:42
#define __LOG_WARNING
Definition: logger.h:274
#define LOG_ERROR
Definition: logger.h:286
#define ast_verb(level,...)
Definition: logger.h:464
#define LOG_WARNING
Definition: logger.h:275
void ast_console_puts_mutable_full(const char *message, int level, int sublevel)
log the string to the console, and all attached console clients
Definition: asterisk.c:1281
int ast_logger_category_load(void)
Load/Initialize system wide logger category functionality.
int ast_logger_category_unload(void)
Unload system wide logger category functionality.
int errno
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
#define manager_event(category, event, contents,...)
External routines may send asterisk manager events this way.
Definition: manager.h:248
#define EVENT_FLAG_SYSTEM
Definition: manager.h:71
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:317
@ AST_MODFLAG_GLOBAL_SYMBOLS
Definition: module.h:316
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
#define MAXHOSTNAMELEN
Definition: network.h:69
#define ast_opt_remote
Definition: options.h:112
#define ast_opt_exec
Definition: options.h:113
Asterisk file paths, configured in asterisk.conf.
const char * ast_config_AST_SYSTEM_NAME
Definition: options.c:170
const char * ast_config_AST_LOG_DIR
Definition: options.c:159
Core PBX routines and definitions.
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
Definition: ael_main.c:211
static int reload(void)
#define NULL
Definition: resample.c:96
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
Definition: stringfields.h:337
#define ast_calloc_with_stringfields(n, type, size)
Allocate a structure with embedded stringfields in a single allocation.
Definition: stringfields.h:426
#define AST_STRING_FIELD(name)
Declare a string field.
Definition: stringfields.h:299
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:514
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:368
String manipulation functions.
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:1104
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:727
int ast_str_set_va(struct ast_str **buf, ssize_t max_len, const char *fmt, va_list ap)
Set a dynamic string from a va_list.
Definition: strings.h:995
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:79
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: main/utils.c:1951
static int force_inline attribute_pure ast_ends_with(const char *str, const char *suffix)
Definition: strings.h:112
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
char * ast_str_append_substr(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc)
Append a non-NULL terminated substring to the end of a dynamic string.
Definition: strings.h:1027
@ AST_DYNSTR_BUILD_FAILED
Definition: strings.h:908
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:666
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:633
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1078
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:219
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Definition: strings.h:157
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
Definition: strings.h:874
void * addresses[AST_MAX_BT_FRAMES]
Definition: backtrace.h:52
int num_frames
Definition: backtrace.h:54
Main Channel structure associated with a channel.
descriptor for a cli entry.
Definition: cli.h:171
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
Structure used to handle boolean flags.
Definition: utils.h:199
Abstract JSON element (object, array, string, int, ...).
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:597
Structure for variables, used for configurations and for channel variables.
Definition: astman.c:222
struct logchannel::@385 list
int verbosity
Definition: logger.c:143
int facility
Definition: logger.c:141
struct logformatter formatter
Definition: logger.c:135
int dynamic
Definition: logger.c:155
int lineno
Definition: logger.c:153
enum logtypes type
Definition: logger.c:145
unsigned int logmask
Definition: logger.c:137
char filename[PATH_MAX]
Definition: logger.c:149
FILE * fileptr
Definition: logger.c:147
int disabled
Definition: logger.c:139
char components[0]
Definition: logger.c:157
const char * name
Definition: logger.c:122
int(*const format_log)(struct logchannel *channel, struct logmsg *msg, char *buf, size_t size)
Definition: logger.c:124
Definition: logger.c:167
const ast_string_field level_name
Definition: logger.c:180
int lwp
Definition: logger.c:172
const ast_string_field message
Definition: logger.c:180
struct logmsg::@386 list
int line
Definition: logger.c:171
ast_callid callid
Definition: logger.c:173
int sublevel
Definition: logger.c:170
struct logmsg * next
Definition: logger.c:181
const ast_string_field file
Definition: logger.c:180
const ast_string_field function
Definition: logger.c:180
enum logmsgtypes type
Definition: logger.c:168
int level
Definition: logger.c:169
const ast_string_field date
Definition: logger.c:180
ast_mutex_t lock
Definition: logger.c:190
Definition: test_heap.c:38
struct verb_console::@387 node
int * level
Definition: logger.c:2306
Syslog support functions for Asterisk logging.
int ast_syslog_priority_from_loglevel(int level)
Maps an Asterisk log level (i.e. LOG_ERROR) to a syslog priority constant.
Definition: syslog.c:162
int ast_syslog_facility(const char *facility)
Maps a syslog facility name from a string to a syslog facility constant.
Definition: syslog.c:85
Handy terminal functions for vt* terms.
#define COLOR_BRGREEN
Definition: term.h:52
#define COLOR_BRRED
Definition: term.h:50
char * term_strip(char *outbuf, const char *inbuf, int maxout)
Remove colorings from a specified string.
Definition: term.c:362
#define COLOR_YELLOW
Definition: term.h:54
#define COLOR_BRWHITE
Definition: term.h:62
#define COLOR_RED
Definition: term.h:49
#define COLORIZE(fg, bg, str)
Definition: term.h:68
#define COLORIZE_FMT
Shortcut macros for coloring a set of text.
Definition: term.h:67
#define COLOR_GREEN
Definition: term.h:51
#define COLOR_BRBLUE
Definition: term.h:56
const char * args
static struct test_val a
static struct test_val c
Definitions to aid in the use of thread local storage.
int ast_threadstorage_set_ptr(struct ast_threadstorage *ts, void *ptr)
Set a raw pointer from threadstorage.
void * ast_threadstorage_get_ptr(struct ast_threadstorage *ts)
Retrieve a raw pointer from threadstorage.
#define AST_THREADSTORAGE_CUSTOM(a, b, c)
Define a thread storage variable, with custom initialization and cleanup.
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:86
Time-related functions and macros.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
Utility functions.
#define ast_assert(a)
Definition: utils.h:710
#define ast_pthread_create(a, b, c, d)
Definition: utils.h:559
int ast_get_tid(void)
Get current thread ID.
Definition: main/utils.c:2504
int ast_mkdir(const char *path, int mode)
Recursively create directory path.
Definition: main/utils.c:2231
#define ARRAY_LEN(a)
Definition: utils.h:639
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:682