RTS API Documentation  1.10.11
Data Structures | Macros | Typedefs | Functions | Variables
switch_log.c File Reference
#include <switch.h>
#include "private/switch_core_pvt.h"
+ Include dependency graph for switch_log.c:

Go to the source code of this file.

Data Structures

struct  switch_log_binding
 

Macros

#define do_mods   (LOG_QUEUE && THREAD_RUNNING)
 

Typedefs

typedef struct switch_log_binding switch_log_binding_t
 

Functions

cJSONswitch_log_node_to_json (const switch_log_node_t *node, int log_level, switch_log_json_format_t *json_format, switch_event_t *chan_vars)
 Convert a log node to JSON object. Destroy JSON object when finished. More...
 
static switch_log_node_tswitch_log_node_alloc (void)
 
switch_log_node_tswitch_log_node_dup (const switch_log_node_t *node)
 
void switch_log_node_free (switch_log_node_t **pnode)
 
const char * switch_log_level2str (switch_log_level_t level)
 
static int switch_log_to_mask (switch_log_level_t level)
 
uint32_t switch_log_str2mask (const char *str)
 
switch_log_level_t switch_log_str2level (const char *str)
 
switch_status_t switch_log_unbind_logger (switch_log_function_t function)
 
switch_status_t switch_log_bind_logger (switch_log_function_t function, switch_log_level_t level, switch_bool_t is_console)
 
static void *SWITCH_THREAD_FUNC log_thread (switch_thread_t *t, void *obj)
 
void switch_log_meta_printf (switch_text_channel_t channel, const char *file, const char *func, int line, const char *userdata, switch_log_level_t level, cJSON **meta, const char *fmt,...)
 
void switch_log_printf (switch_text_channel_t channel, const char *file, const char *func, int line, const char *userdata, switch_log_level_t level, const char *fmt,...)
 
void switch_log_vprintf (switch_text_channel_t channel, const char *file, const char *func, int line, const char *userdata, switch_log_level_t level, const char *fmt, va_list ap)
 
void switch_log_meta_vprintf (switch_text_channel_t channel, const char *file, const char *func, int line, const char *userdata, switch_log_level_t level, cJSON **meta, const char *fmt, va_list ap)
 
switch_status_t switch_log_init (switch_memory_pool_t *pool, switch_bool_t colorize)
 
void switch_core_memory_reclaim_logger (void)
 
switch_status_t switch_log_shutdown (void)
 Shut down the logging engine. More...
 

Variables

static const char * LEVELS []
 
static switch_memory_pool_tLOG_POOL = NULL
 
static switch_log_binding_tBINDINGS = NULL
 
static switch_mutex_tBINDLOCK = NULL
 
static switch_queue_tLOG_QUEUE = NULL
 
static int8_t THREAD_RUNNING = 0
 
static uint8_t MAX_LEVEL = 0
 
static int mods_loaded = 0
 
static int console_mods_loaded = 0
 
static switch_bool_t COLORIZE = SWITCH_FALSE
 
static int64_t log_sequence = 0
 
static const char * COLORS []
 
static switch_thread_tthread
 

Macro Definition Documentation

◆ do_mods

#define do_mods   (LOG_QUEUE && THREAD_RUNNING)

Definition at line 548 of file switch_log.c.

Referenced by switch_log_meta_vprintf().

Typedef Documentation

◆ switch_log_binding_t

Definition at line 56 of file switch_log.c.

Function Documentation

◆ log_thread()

static void* SWITCH_THREAD_FUNC log_thread ( switch_thread_t t,
void *  obj 
)
static

Definition at line 488 of file switch_log.c.

References BINDLOCK, switch_log_binding::function, switch_log_binding::level, switch_log_node_t::level, log_sequence, switch_log_binding::next, switch_log_node_t::sequence, SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, switch_log_node_free(), switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_queue_pop(), SWITCH_STATUS_SUCCESS, and THREAD_RUNNING.

Referenced by switch_log_init().

489 {
490 
491  if (!obj) {
492  obj = NULL;
493  }
494  THREAD_RUNNING = 1;
495 
496  while (THREAD_RUNNING == 1) {
497  void *pop = NULL;
498  switch_log_node_t *node = NULL;
499  switch_log_binding_t *binding;
500 
502  break;
503  }
504 
505  if (!pop) {
506  THREAD_RUNNING = -1;
507  break;
508  }
509 
510  node = (switch_log_node_t *) pop;
512  node->sequence = ++log_sequence;
513  for (binding = BINDINGS; binding; binding = binding->next) {
514  if (binding->level >= node->level) {
515  binding->function(node, node->level);
516  }
517  }
519 
520  switch_log_node_free(&node);
521 
522  }
523 
524  THREAD_RUNNING = 0;
526  return NULL;
527 }
Log Data.
Definition: switch_log.h:49
#define SWITCH_CHANNEL_LOG
switch_log_function_t function
Definition: switch_log.c:50
static int64_t log_sequence
Definition: switch_log.c:71
struct switch_log_binding * next
Definition: switch_log.c:53
void switch_log_printf(switch_text_channel_t channel, const char *file, const char *func, int line, const char *userdata, switch_log_level_t level, const char *fmt,...)
Definition: switch_log.c:538
switch_status_t switch_queue_pop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1243
static switch_log_binding_t * BINDINGS
Definition: switch_log.c:59
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
static switch_queue_t * LOG_QUEUE
Definition: switch_log.c:61
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
static int8_t THREAD_RUNNING
Definition: switch_log.c:65
static switch_mutex_t * BINDLOCK
Definition: switch_log.c:60
int64_t sequence
Definition: switch_log.h:70
void switch_log_node_free(switch_log_node_t **pnode)
Definition: switch_log.c:300
switch_log_level_t level
Definition: switch_log.c:51
switch_log_level_t level
Definition: switch_log.h:59

◆ switch_log_bind_logger()

switch_status_t switch_log_bind_logger ( switch_log_function_t  function,
switch_log_level_t  level,
switch_bool_t  is_console 
)

Definition at line 452 of file switch_log.c.

References BINDLOCK, console_mods_loaded, switch_log_binding::function, switch_log_binding::is_console, switch_log_binding::level, LOG_POOL, MAX_LEVEL, mods_loaded, switch_log_binding::next, switch_assert, switch_core_alloc, switch_mutex_lock(), switch_mutex_unlock(), SWITCH_STATUS_MEMERR, and SWITCH_STATUS_SUCCESS.

453 {
454  switch_log_binding_t *binding = NULL, *ptr = NULL;
455  switch_assert(function != NULL);
456 
457  if (!(binding = switch_core_alloc(LOG_POOL, sizeof(*binding)))) {
458  return SWITCH_STATUS_MEMERR;
459  }
460 
461  if ((uint8_t) level > MAX_LEVEL) {
462  MAX_LEVEL = level;
463  }
464 
465  binding->function = function;
466  binding->level = level;
467  binding->is_console = is_console;
468 
470  for (ptr = BINDINGS; ptr && ptr->next; ptr = ptr->next);
471 
472  if (ptr) {
473  ptr->next = binding;
474  } else {
475  BINDINGS = binding;
476  }
477  if (is_console) {
479  }
480  mods_loaded++;
482 
483  return SWITCH_STATUS_SUCCESS;
484 }
static int console_mods_loaded
Definition: switch_log.c:68
static uint8_t MAX_LEVEL
Definition: switch_log.c:66
switch_log_function_t function
Definition: switch_log.c:50
static switch_memory_pool_t * LOG_POOL
Definition: switch_log.c:58
struct switch_log_binding * next
Definition: switch_log.c:53
static int mods_loaded
Definition: switch_log.c:67
static switch_log_binding_t * BINDINGS
Definition: switch_log.c:59
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
static switch_mutex_t * BINDLOCK
Definition: switch_log.c:60
switch_log_level_t level
Definition: switch_log.c:51
#define switch_assert(expr)

◆ switch_log_init()

switch_status_t switch_log_init ( switch_memory_pool_t pool,
switch_bool_t  colorize 
)

Definition at line 745 of file switch_log.c.

References BINDLOCK, COLORIZE, LOG_POOL, log_thread(), pool, switch_assert, switch_cond_next(), switch_core_get_console(), SWITCH_CORE_QUEUE_LEN, switch_mutex_init(), SWITCH_MUTEX_NESTED, switch_queue_create(), SWITCH_STATUS_SUCCESS, switch_thread_create(), SWITCH_THREAD_STACKSIZE, switch_threadattr_create(), switch_threadattr_stacksize_set(), SWITCH_TRUE, thread, and THREAD_RUNNING.

746 {
747  switch_threadattr_t *thd_attr;;
748 
749  switch_assert(pool != NULL);
750 
751  LOG_POOL = pool;
752 
754 
756 #ifdef SWITCH_LOG_RECYCLE
757  switch_queue_create(&LOG_RECYCLE_QUEUE, SWITCH_CORE_QUEUE_LEN, LOG_POOL);
758 #endif
761  switch_thread_create(&thread, thd_attr, log_thread, NULL, LOG_POOL);
762 
763  while (!THREAD_RUNNING) {
765  }
766 
767  if (colorize) {
768 #ifdef WIN32
769  hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
770  if (switch_core_get_console() == stdout && hStdout != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) {
771  wOldColorAttrs = csbiInfo.wAttributes;
773  }
774 #else
776 #endif
777  }
778 
779 
780  return SWITCH_STATUS_SUCCESS;
781 }
static switch_memory_pool_t * LOG_POOL
Definition: switch_log.c:58
switch_status_t switch_threadattr_stacksize_set(switch_threadattr_t *attr, switch_size_t stacksize)
Definition: switch_apr.c:683
switch_memory_pool_t * pool
static switch_thread_t * thread
Definition: switch_log.c:486
#define SWITCH_MUTEX_NESTED
Definition: switch_apr.h:318
static void *SWITCH_THREAD_FUNC log_thread(switch_thread_t *t, void *obj)
Definition: switch_log.c:488
#define SWITCH_CORE_QUEUE_LEN
Definition: switch_types.h:597
static switch_queue_t * LOG_QUEUE
Definition: switch_log.c:61
#define SWITCH_THREAD_STACKSIZE
Definition: switch_types.h:588
static int8_t THREAD_RUNNING
Definition: switch_log.c:65
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:293
void switch_cond_next(void)
Definition: switch_time.c:658
static switch_bool_t COLORIZE
Definition: switch_log.c:69
static switch_mutex_t * BINDLOCK
Definition: switch_log.c:60
switch_status_t switch_threadattr_create(switch_threadattr_t **new_attr, switch_memory_pool_t *pool)
Definition: switch_apr.c:665
switch_status_t switch_thread_create(switch_thread_t **new_thread, switch_threadattr_t *attr, switch_thread_start_t func, void *data, switch_memory_pool_t *cont)
Definition: switch_apr.c:698
FILE * switch_core_get_console(void)
Get the output console.
Definition: switch_core.c:253
switch_status_t switch_queue_create(switch_queue_t **queue, unsigned int queue_capacity, switch_memory_pool_t *pool)
Definition: switch_apr.c:1233
#define switch_assert(expr)

◆ switch_log_level2str()

const char* switch_log_level2str ( switch_log_level_t  level)

Definition at line 331 of file switch_log.c.

References LEVELS, and SWITCH_LOG_DEBUG.

Referenced by switch_log_meta_vprintf().

332 {
333  if (level > SWITCH_LOG_DEBUG) {
334  level = SWITCH_LOG_DEBUG;
335  }
336  return LEVELS[level + 1];
337 }
static const char * LEVELS[]
Definition: switch_log.c:36

◆ switch_log_meta_printf()

void switch_log_meta_printf ( switch_text_channel_t  channel,
const char *  file,
const char *  func,
int  line,
const char *  userdata,
switch_log_level_t  level,
cJSON **  meta,
const char *  fmt,
  ... 
)

Definition at line 529 of file switch_log.c.

References switch_log_meta_vprintf().

531 {
532  va_list ap;
533  va_start(ap, fmt);
534  switch_log_meta_vprintf(channel, file, func, line, userdata, level, meta, fmt, ap);
535  va_end(ap);
536 }
int cJSON_bool fmt
Definition: switch_cJSON.h:150
void switch_log_meta_vprintf(switch_text_channel_t channel, const char *file, const char *func, int line, const char *userdata, switch_log_level_t level, cJSON **meta, const char *fmt, va_list ap)
Definition: switch_log.c:554

◆ switch_log_meta_vprintf()

void switch_log_meta_vprintf ( switch_text_channel_t  channel,
const char *  file,
const char *  func,
int  line,
const char *  userdata,
switch_log_level_t  level,
cJSON **  meta,
const char *  fmt,
va_list  ap 
)

Definition at line 554 of file switch_log.c.

References switch_log_node_t::channel, COLORIZE, COLORS, console_mods_loaded, switch_log_node_t::content, switch_log_node_t::data, switch_runtime::debug_level, do_mods, switch_log_node_t::file, switch_log_node_t::func, switch_runtime::hard_log_level, switch_log_binding::level, switch_log_node_t::level, switch_log_node_t::line, switch_core_session::loglevel, MAX_LEVEL, memset(), switch_log_node_t::meta, runtime, switch_log_node_t::slevel, switch_assert, switch_channel_get_log_tags(), SWITCH_CHANNEL_ID_EVENT, SWITCH_CHANNEL_ID_LOG_CLEAN, SWITCH_CHANNEL_ID_SESSION, switch_core_data_channel(), switch_core_idle_cpu(), switch_core_session_get_channel(), switch_core_session_get_uuid(), switch_cut_path(), switch_event_add_header(), switch_event_add_header_string(), switch_event_create, switch_event_fire, SWITCH_EVENT_LOG, switch_event_running(), SWITCH_LOG_DISABLE, SWITCH_LOG_INVALID, switch_log_level2str(), switch_log_node_alloc(), switch_log_node_free(), SWITCH_LOG_UNINIT, switch_micro_time_now(), switch_queue_trypush(), switch_safe_free, SWITCH_SEQ_DEFAULT_COLOR, switch_set_string, switch_snprintf(), SWITCH_STACK_BOTTOM, SWITCH_STATUS_SUCCESS, switch_time_exp_lt(), switch_vasprintf(), switch_log_node_t::tags, switch_log_node_t::timestamp, switch_time_exp_t::tm_hour, switch_time_exp_t::tm_mday, switch_time_exp_t::tm_min, switch_time_exp_t::tm_mon, switch_time_exp_t::tm_sec, switch_time_exp_t::tm_usec, switch_time_exp_t::tm_year, switch_log_node_t::userdata, and zstr.

Referenced by switch_log_meta_printf(), switch_log_printf(), and switch_log_vprintf().

556 {
557  cJSON *log_meta = NULL;
558  char *data = NULL;
559  char *new_fmt = NULL;
560  int ret = 0;
561  FILE *handle;
562  const char *filep = (file ? switch_cut_path(file) : "");
563  const char *funcp = (func ? func : "");
564  char *content = NULL;
566  uint32_t len;
567 #ifdef SWITCH_FUNC_IN_LOG
568  const char *extra_fmt = "%s [%s] %s:%d %s()%c%s";
569 #else
570  const char *extra_fmt = "%s [%s] %s:%d%c%s";
571 #endif
573  switch_log_level_t special_level = SWITCH_LOG_UNINIT;
574 
575  if (meta && *meta) {
576  log_meta = *meta;
577  *meta = NULL;
578  }
579 
580  if (limit_level == SWITCH_LOG_DISABLE) {
581  goto end;
582  }
583 
584  if (channel == SWITCH_CHANNEL_ID_SESSION && userdata) {
586  special_level = session->loglevel;
587  if (limit_level < session->loglevel) {
588  limit_level = session->loglevel;
589  }
590  }
591 
592  if (level > 100) {
593  if ((uint32_t) (level - 100) > runtime.debug_level) {
594  goto end;
595  }
596 
597  level = 1;
598  }
599 
600  if (level > limit_level) {
601  goto end;
602  }
603 
605 
606  handle = switch_core_data_channel(channel);
607 
608  if (channel != SWITCH_CHANNEL_ID_LOG_CLEAN) {
609  char date[80] = "";
610  //switch_size_t retsize;
612 
613  switch_time_exp_lt(&tm, now);
614  switch_snprintf(date, sizeof(date), "%0.4d-%0.2d-%0.2d %0.2d:%0.2d:%0.2d.%0.6d %0.2f%%%%",
615  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec, switch_core_idle_cpu());
616 
617  //switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm);
618 
619 #ifdef SWITCH_FUNC_IN_LOG
620  len = (uint32_t) (strlen(extra_fmt) + strlen(date) + strlen(filep) + 32 + strlen(funcp) + strlen(fmt));
621 #else
622  len = (uint32_t) (strlen(extra_fmt) + strlen(date) + strlen(filep) + 32 + strlen(fmt));
623 #endif
624  new_fmt = malloc(len + 1);
625  switch_assert(new_fmt);
626 #ifdef SWITCH_FUNC_IN_LOG
627  switch_snprintf(new_fmt, len, extra_fmt, date, switch_log_level2str(level), filep, line, funcp, 128, fmt);
628 #else
629  switch_snprintf(new_fmt, len, extra_fmt, date, switch_log_level2str(level), filep, line, 128, fmt);
630 #endif
631 
632  fmt = new_fmt;
633  }
634 
635  ret = switch_vasprintf(&data, fmt, ap);
636 
637  if (ret == -1) {
638  fprintf(stderr, "Memory Error\n");
639  goto end;
640  }
641 
642  if (channel == SWITCH_CHANNEL_ID_LOG_CLEAN) {
643  content = data;
644  } else {
645  if ((content = strchr(data, 128))) {
646  *content = ' ';
647  }
648  }
649 
650  if (channel == SWITCH_CHANNEL_ID_EVENT) {
651  switch_event_t *event;
653  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-Data", data);
654  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-File", filep);
655  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-Function", funcp);
656  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Line", "%d", line);
657  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Level", "%d", (int) level);
658  if (!zstr(userdata)) {
659  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "User-Data", userdata);
660  }
661  switch_event_fire(&event);
662  data = NULL;
663  }
664 
665  goto end;
666  }
667 
668  if (console_mods_loaded == 0 || !do_mods) {
669  if (handle) {
670  int aok = 1;
671 #ifndef WIN32
672 
673  fd_set can_write;
674  int fd;
675  struct timeval to;
676 
677  fd = fileno(handle);
678  memset(&to, 0, sizeof(to));
679  FD_ZERO(&can_write);
680  FD_SET(fd, &can_write);
681  to.tv_sec = 0;
682  to.tv_usec = 100000;
683  if (select(fd + 1, NULL, &can_write, NULL, &to) > 0) {
684  aok = FD_ISSET(fd, &can_write);
685  } else {
686  aok = 0;
687  }
688 #endif
689  if (aok) {
690  if (COLORIZE) {
691 
692 #ifdef WIN32
693  SetConsoleTextAttribute(hStdout, COLORS[level]);
694  WriteFile(hStdout, data, (DWORD) strlen(data), NULL, NULL);
695  SetConsoleTextAttribute(hStdout, wOldColorAttrs);
696 #else
697  fprintf(handle, "%s%s%s", COLORS[level], data, SWITCH_SEQ_DEFAULT_COLOR);
698 #endif
699  } else {
700  fprintf(handle, "%s", data);
701  }
702  }
703  }
704  }
705 
706  if (do_mods && level <= MAX_LEVEL) {
708 
709  node->data = data;
710  data = NULL;
711  switch_set_string(node->file, filep);
712  switch_set_string(node->func, funcp);
713  node->line = line;
714  node->level = level;
715  node->slevel = special_level;
716  node->content = content;
717  node->timestamp = now;
718  node->channel = channel;
719  node->tags = NULL;
720  node->meta = log_meta;
721  log_meta = NULL;
722  if (channel == SWITCH_CHANNEL_ID_SESSION) {
723  switch_core_session_t *session = (switch_core_session_t *) userdata;
724  node->userdata = userdata ? strdup(switch_core_session_get_uuid(session)) : NULL;
725  if (session) {
727  }
728  } else {
729  node->userdata = !zstr(userdata) ? strdup(userdata) : NULL;
730  }
731 
733  switch_log_node_free(&node);
734  }
735  }
736 
737  end:
738 
739  cJSON_Delete(log_meta);
740  switch_safe_free(data);
741  switch_safe_free(new_fmt);
742 
743 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:413
switch_xml_t switch_status_t switch_event_running(void)
Determine if the event system has been initialized.
Definition: switch_event.c:425
static int console_mods_loaded
Definition: switch_log.c:68
Log Data.
Definition: switch_log.h:49
static uint8_t MAX_LEVEL
Definition: switch_log.c:66
switch_event_t * tags
Definition: switch_log.h:68
#define do_mods
Definition: switch_log.c:548
cJSON *const to
switch_status_t switch_event_add_header(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *fmt,...) PRINTF_FUNCTION(4
Add a header to an event.
Representation of an event.
Definition: switch_event.h:80
switch_status_t switch_time_exp_lt(switch_time_exp_t *result, switch_time_t input)
Definition: switch_apr.c:346
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
struct switch_runtime runtime
Definition: switch_core.c:86
#define zstr(x)
Definition: switch_utils.h:314
int cJSON_bool fmt
Definition: switch_cJSON.h:150
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
int64_t switch_time_t
Definition: switch_apr.h:188
double switch_core_idle_cpu(void)
static switch_queue_t * LOG_QUEUE
Definition: switch_log.c:61
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
switch_core_session_t * session
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
switch_log_level_t slevel
Definition: switch_log.h:67
int switch_vasprintf(_Out_opt_ char **buf, _In_z_ _Printf_format_string_ const char *format, _In_ va_list ap)
FILE * switch_core_data_channel(switch_text_channel_t channel)
Retrieve a FILE stream of a given text channel name.
Definition: switch_core.c:286
static const char * COLORS[]
Definition: switch_log.c:86
static switch_bool_t COLORIZE
Definition: switch_log.c:69
const char * switch_log_level2str(switch_log_level_t level)
Definition: switch_log.c:331
switch_time_t timestamp
Definition: switch_log.h:61
char * switch_core_session_get_uuid(_In_ switch_core_session_t *session)
Retrieve the unique identifier from a session.
static switch_log_node_t * switch_log_node_alloc(void)
Definition: switch_log.c:249
switch_log_level_t hard_log_level
#define SWITCH_SEQ_DEFAULT_COLOR
Definition: switch_types.h:70
#define switch_event_create(event, id)
Create a new event assuming it will not be custom event and therefore hiding the unused parameters...
Definition: switch_event.h:384
#define switch_set_string(_dst, _src)
Definition: switch_utils.h:734
switch_status_t switch_queue_trypush(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1279
void switch_log_node_free(switch_log_node_t **pnode)
Definition: switch_log.c:300
uint32_t debug_level
switch_log_level_t loglevel
switch_text_channel_t channel
Definition: switch_log.h:66
switch_status_t switch_channel_get_log_tags(switch_channel_t *channel, switch_event_t **log_tags)
#define switch_assert(expr)
const char * switch_cut_path(const char *in)
Create a pointer to the file name in a given file path eliminating the directory name.
memset(buf, 0, buflen)
switch_log_level_t
Log Level Enumeration.
switch_log_level_t level
Definition: switch_log.h:59

◆ switch_log_node_alloc()

static switch_log_node_t* switch_log_node_alloc ( void  )
static

Definition at line 249 of file switch_log.c.

References switch_assert, switch_queue_trypop(), and SWITCH_STATUS_SUCCESS.

Referenced by switch_log_meta_vprintf(), and switch_log_node_dup().

250 {
251  switch_log_node_t *node = NULL;
252 #ifdef SWITCH_LOG_RECYCLE
253  void *pop = NULL;
254 
255  if (switch_queue_trypop(LOG_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
256  node = (switch_log_node_t *) pop;
257  } else {
258 #endif
259  node = malloc(sizeof(*node));
260  switch_assert(node);
261 #ifdef SWITCH_LOG_RECYCLE
262  }
263 #endif
264  return node;
265 }
Log Data.
Definition: switch_log.h:49
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1264
#define switch_assert(expr)

◆ switch_log_printf()

void switch_log_printf ( switch_text_channel_t  channel,
const char *  file,
const char *  func,
int  line,
const char *  userdata,
switch_log_level_t  level,
const char *  fmt,
  ... 
)

Definition at line 538 of file switch_log.c.

References switch_log_meta_vprintf().

Referenced by log_thread(), and switch_core_memory_reclaim_logger().

540 {
541  va_list ap;
542 
543  va_start(ap, fmt);
544  switch_log_meta_vprintf(channel, file, func, line, userdata, level, NULL, fmt, ap);
545  va_end(ap);
546 }
int cJSON_bool fmt
Definition: switch_cJSON.h:150
void switch_log_meta_vprintf(switch_text_channel_t channel, const char *file, const char *func, int line, const char *userdata, switch_log_level_t level, cJSON **meta, const char *fmt, va_list ap)
Definition: switch_log.c:554

◆ switch_log_str2level()

switch_log_level_t switch_log_str2level ( const char *  str)

Definition at line 393 of file switch_log.c.

References switch_log_binding::level, LEVELS, switch_is_number(), and SWITCH_LOG_INVALID.

Referenced by switch_log_str2mask().

394 {
395  int x = 0;
397 
398  if (switch_is_number(str)) {
399  x = atoi(str);
400 
401  if (x > SWITCH_LOG_INVALID) {
402  return SWITCH_LOG_INVALID - 1;
403  } else if (x < 0) {
404  return 0;
405  } else {
406  return x;
407  }
408  }
409 
410 
411  for (x = 0;; x++) {
412  if (!LEVELS[x]) {
413  break;
414  }
415 
416  if (!strcasecmp(LEVELS[x], str)) {
417  level = (switch_log_level_t)(x - 1);
418  break;
419  }
420  }
421 
422  return level;
423 }
switch_bool_t switch_is_number(const char *str)
static const char * LEVELS[]
Definition: switch_log.c:36
switch_log_level_t
Log Level Enumeration.

◆ switch_log_str2mask()

uint32_t switch_log_str2mask ( const char *  str)

Definition at line 363 of file switch_log.c.

References switch_log_binding::level, switch_assert, SWITCH_LOG_INVALID, switch_log_str2level(), switch_log_to_mask(), and switch_separate_string().

364 {
365  int argc = 0, x = 0;
366  char *argv[10] = { 0 };
367  uint32_t mask = 0;
368  char *p = strdup(str);
370 
371  switch_assert(p);
372 
373  if ((argc = switch_separate_string(p, ',', argv, (sizeof(argv) / sizeof(argv[0]))))) {
374  for (x = 0; x < argc && argv[x]; x++) {
375  if (!strcasecmp(argv[x], "all")) {
376  mask = 0xFF;
377  break;
378  } else {
379  level = switch_log_str2level(argv[x]);
380 
381  if (level != SWITCH_LOG_INVALID) {
382  mask |= switch_log_to_mask(level);
383  }
384  }
385  }
386  }
387 
388  free(p);
389 
390  return mask;
391 }
unsigned int switch_separate_string(_In_ char *buf, char delim, _Post_count_(return) char **array, unsigned int arraylen)
Separate a string into an array based on a character delimiter.
static int switch_log_to_mask(switch_log_level_t level)
Definition: switch_log.c:339
switch_log_level_t switch_log_str2level(const char *str)
Definition: switch_log.c:393
#define switch_assert(expr)
switch_log_level_t
Log Level Enumeration.

◆ switch_log_to_mask()

static int switch_log_to_mask ( switch_log_level_t  level)
static

Definition at line 339 of file switch_log.c.

References SWITCH_LOG_ALERT, SWITCH_LOG_CONSOLE, SWITCH_LOG_CRIT, SWITCH_LOG_DEBUG, SWITCH_LOG_ERROR, SWITCH_LOG_INFO, SWITCH_LOG_NOTICE, and SWITCH_LOG_WARNING.

Referenced by switch_log_str2mask().

340 {
341  switch (level) {
342  case SWITCH_LOG_DEBUG:
343  return (1<<7);
344  case SWITCH_LOG_INFO:
345  return (1<<6);
346  case SWITCH_LOG_NOTICE:
347  return (1<<5);
348  case SWITCH_LOG_WARNING:
349  return (1<<4);
350  case SWITCH_LOG_ERROR:
351  return (1<<3);
352  case SWITCH_LOG_CRIT:
353  return (1<<2);
354  case SWITCH_LOG_ALERT:
355  return (1<<1);
356  case SWITCH_LOG_CONSOLE:
357  return (1<<0);
358  default:
359  return 0;
360  }
361 }

◆ switch_log_unbind_logger()

switch_status_t switch_log_unbind_logger ( switch_log_function_t  function)

Definition at line 425 of file switch_log.c.

References BINDLOCK, console_mods_loaded, switch_log_binding::function, switch_log_binding::is_console, mods_loaded, switch_log_binding::next, switch_mutex_lock(), switch_mutex_unlock(), SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

426 {
427  switch_log_binding_t *ptr = NULL, *last = NULL;
429 
431  for (ptr = BINDINGS; ptr; ptr = ptr->next) {
432  if (ptr->function == function) {
433  if (last) {
434  last->next = ptr->next;
435  } else {
436  BINDINGS = ptr->next;
437  }
438  status = SWITCH_STATUS_SUCCESS;
439  mods_loaded--;
440  if (ptr->is_console) {
442  }
443  break;
444  }
445  last = ptr;
446  }
448 
449  return status;
450 }
static int console_mods_loaded
Definition: switch_log.c:68
switch_log_function_t function
Definition: switch_log.c:50
struct switch_log_binding * next
Definition: switch_log.c:53
static int mods_loaded
Definition: switch_log.c:67
static switch_log_binding_t * BINDINGS
Definition: switch_log.c:59
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
static switch_mutex_t * BINDLOCK
Definition: switch_log.c:60
switch_status_t
Common return values.

◆ switch_log_vprintf()

void switch_log_vprintf ( switch_text_channel_t  channel,
const char *  file,
const char *  func,
int  line,
const char *  userdata,
switch_log_level_t  level,
const char *  fmt,
va_list  ap 
)

Definition at line 549 of file switch_log.c.

References switch_log_meta_vprintf().

551 {
552  switch_log_meta_vprintf(channel, file, func, line, userdata, level, NULL, fmt, ap);
553 }
int cJSON_bool fmt
Definition: switch_cJSON.h:150
void switch_log_meta_vprintf(switch_text_channel_t channel, const char *file, const char *func, int line, const char *userdata, switch_log_level_t level, cJSON **meta, const char *fmt, va_list ap)
Definition: switch_log.c:554

Variable Documentation

◆ BINDINGS

switch_log_binding_t* BINDINGS = NULL
static

Definition at line 59 of file switch_log.c.

◆ BINDLOCK

switch_mutex_t* BINDLOCK = NULL
static

◆ COLORIZE

switch_bool_t COLORIZE = SWITCH_FALSE
static

Definition at line 69 of file switch_log.c.

Referenced by switch_log_init(), and switch_log_meta_vprintf().

◆ COLORS

const char* COLORS[]
static
Initial value:
=
#define SWITCH_SEQ_FMAGEN
Definition: switch_types.h:97
#define SWITCH_SEQ_FYELLOW
Definition: switch_types.h:95
#define SWITCH_SEQ_FCYAN
Definition: switch_types.h:98
#define SWITCH_SEQ_FRED
Definition: switch_types.h:93
#define SWITCH_SEQ_DEFAULT_COLOR
Definition: switch_types.h:70
#define SWITCH_SEQ_FGREEN
Definition: switch_types.h:94

Definition at line 86 of file switch_log.c.

Referenced by switch_log_meta_vprintf().

◆ console_mods_loaded

int console_mods_loaded = 0
static

◆ LEVELS

const char* LEVELS[]
static
Initial value:
= {
"DISABLE",
"CONSOLE",
"ALERT",
"CRIT",
"ERR",
"WARNING",
"NOTICE",
"INFO",
"DEBUG",
NULL
}

Definition at line 36 of file switch_log.c.

Referenced by switch_log_level2str(), and switch_log_str2level().

◆ LOG_POOL

switch_memory_pool_t* LOG_POOL = NULL
static

Definition at line 58 of file switch_log.c.

Referenced by switch_log_bind_logger(), and switch_log_init().

◆ LOG_QUEUE

switch_queue_t* LOG_QUEUE = NULL
static

Definition at line 61 of file switch_log.c.

◆ log_sequence

int64_t log_sequence = 0
static

Definition at line 71 of file switch_log.c.

Referenced by log_thread().

◆ MAX_LEVEL

uint8_t MAX_LEVEL = 0
static

Definition at line 66 of file switch_log.c.

Referenced by switch_log_bind_logger(), and switch_log_meta_vprintf().

◆ mods_loaded

int mods_loaded = 0
static

Definition at line 67 of file switch_log.c.

Referenced by switch_log_bind_logger(), and switch_log_unbind_logger().

◆ thread

switch_thread_t* thread
static

◆ THREAD_RUNNING

int8_t THREAD_RUNNING = 0
static

Definition at line 65 of file switch_log.c.

Referenced by log_thread(), switch_log_init(), and switch_log_shutdown().