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