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