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