Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

switch_log.c File Reference


Data Structures

struct  switch_log_binding

Typedefs

typedef switch_log_binding switch_log_binding_t

Functions

const char * switch_log_level2str (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_bind_logger (switch_log_function_t function, switch_log_level_t level)
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,...)
switch_status_t switch_log_init (switch_memory_pool_t *pool)
void switch_core_memory_reclaim_logger (void)
switch_status_t switch_log_shutdown (void)
 Shut down the logging engine.


Typedef Documentation

typedef struct switch_log_binding switch_log_binding_t
 


Function Documentation

switch_status_t switch_log_bind_logger switch_log_function_t  function,
switch_log_level_t  level
 

00120 {
00121         switch_log_binding_t *binding = NULL, *ptr = NULL;
00122         switch_assert(function != NULL);
00123 
00124         if (!(binding = switch_core_alloc(LOG_POOL, sizeof(*binding)))) {
00125                 return SWITCH_STATUS_MEMERR;
00126         }
00127 
00128         if ((uint8_t) level > MAX_LEVEL) {
00129                 MAX_LEVEL = level;
00130         }
00131 
00132         binding->function = function;
00133         binding->level = level;
00134 
00135         switch_mutex_lock(BINDLOCK);
00136         for (ptr = BINDINGS; ptr && ptr->next; ptr = ptr->next);
00137 
00138         if (ptr) {
00139                 ptr->next = binding;
00140         } else {
00141                 BINDINGS = binding;
00142         }
00143         switch_mutex_unlock(BINDLOCK);
00144 
00145         return SWITCH_STATUS_SUCCESS;
00146 }

Here is the call graph for this function:

switch_status_t switch_log_init switch_memory_pool_t pool  ) 
 

00322 {
00323         switch_thread_t *thread;
00324         switch_threadattr_t *thd_attr;;
00325 
00326         switch_assert(pool != NULL);
00327 
00328         LOG_POOL = pool;
00329 
00330         switch_threadattr_create(&thd_attr, LOG_POOL);
00331         switch_threadattr_detach_set(thd_attr, 1);
00332 
00333 
00334         switch_queue_create(&LOG_QUEUE, SWITCH_CORE_QUEUE_LEN, LOG_POOL);
00335         switch_queue_create(&LOG_RECYCLE_QUEUE, SWITCH_CORE_QUEUE_LEN, LOG_POOL);
00336         switch_mutex_init(&BINDLOCK, SWITCH_MUTEX_NESTED, LOG_POOL);
00337         switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
00338         switch_thread_create(&thread, thd_attr, log_thread, NULL, LOG_POOL);
00339 
00340         while (!THREAD_RUNNING) {
00341                 switch_yield(1000);
00342         }
00343         return SWITCH_STATUS_SUCCESS;
00344 }

Here is the call graph for this function:

const char* switch_log_level2str switch_log_level_t  level  ) 
 

00065 {
00066         if (level > SWITCH_LOG_DEBUG) {
00067                 level = SWITCH_LOG_DEBUG;
00068         }
00069         return LEVELS[level];
00070 }

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,
  ...
 

00191 {
00192         char *data = NULL;
00193         char *new_fmt = NULL;
00194         int ret = 0;
00195         va_list ap;
00196         FILE *handle;
00197         const char *filep = (file ? switch_cut_path(file) : "");
00198         const char *funcp = (func ? func : "");
00199         char *content = NULL;
00200         switch_time_t now = switch_timestamp_now();
00201         uint32_t len;
00202         const char *extra_fmt = "%s [%s] %s:%d %s()%c%s";
00203 
00204         if (level > runtime.hard_log_level) {
00205                 return;
00206         }
00207 
00208         va_start(ap, fmt);
00209 
00210         handle = switch_core_data_channel(channel);
00211 
00212         if (channel != SWITCH_CHANNEL_ID_LOG_CLEAN) {
00213                 char date[80] = "";
00214                 switch_size_t retsize;
00215                 switch_time_exp_t tm;
00216 
00217                 switch_time_exp_lt(&tm, now);
00218                 switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm);
00219 
00220                 len = (uint32_t) (strlen(extra_fmt) + strlen(date) + strlen(filep) + 32 + strlen(funcp) + strlen(fmt));
00221                 new_fmt = malloc(len + 1);
00222                 switch_assert(new_fmt);
00223                 switch_snprintf(new_fmt, len, extra_fmt, date, switch_log_level2str(level), filep, line, funcp, 128, fmt);
00224                 fmt = new_fmt;
00225         }
00226 
00227         ret = switch_vasprintf(&data, fmt, ap);
00228         va_end(ap);
00229 
00230         if (ret == -1) {
00231                 fprintf(stderr, "Memory Error\n");
00232                 goto end;
00233         }
00234 
00235         if (channel == SWITCH_CHANNEL_ID_LOG_CLEAN) {
00236                 content = data;
00237         } else {
00238                 if ((content = strchr(data, 128))) {
00239                         *content = ' ';
00240                 }
00241         }
00242 
00243         if (channel == SWITCH_CHANNEL_ID_EVENT) {
00244                 switch_event_t *event;
00245                 if (switch_event_running() == SWITCH_STATUS_SUCCESS && switch_event_create(&event, SWITCH_EVENT_LOG) == SWITCH_STATUS_SUCCESS) {
00246                         switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Data", "%s", data);
00247                         switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-File", "%s", filep);
00248                         switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Function", "%s", funcp);
00249                         switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Line", "%d", line);
00250                         switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Level", "%d", (int) level);
00251                         switch_event_fire(&event);
00252                         data = NULL;
00253                 }
00254 
00255                 goto end;
00256         }
00257 
00258         if (level == SWITCH_LOG_CONSOLE || !LOG_QUEUE || !THREAD_RUNNING) {
00259                 if (handle) {
00260                         int aok = 1;
00261 #ifndef WIN32
00262 
00263                         fd_set can_write;
00264                         int fd;
00265                         struct timeval to;
00266                                         
00267                         fd = fileno(handle);
00268                         memset(&to, 0, sizeof(to));
00269                         FD_SET(fd, &can_write);
00270                         to.tv_sec = 0;
00271                         to.tv_usec = 100000;
00272                         if (select(fd+1, NULL, &can_write, NULL, &to) > 0) {
00273                                 aok = FD_ISSET(fd, &can_write);
00274                         } else {
00275                                 aok = 0;
00276                         }
00277 #endif
00278                         if (aok) {
00279                                 fprintf(handle, "%s", data);
00280                         }
00281                 }
00282         } else if (level <= MAX_LEVEL) {
00283                 switch_log_node_t *node;
00284                 void *pop = NULL;
00285 
00286                 if (switch_queue_trypop(LOG_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
00287                         node = (switch_log_node_t *) pop;
00288                 } else {
00289                         node = malloc(sizeof(*node));
00290                         switch_assert(node);                    
00291                 }
00292 
00293                 node->data = data;
00294                 data = NULL;
00295                 switch_set_string(node->file, filep);
00296                 switch_set_string(node->func, funcp);
00297                 node->line = line;
00298                 node->level = level;
00299                 node->content = content;
00300                 node->timestamp = now;
00301 
00302 
00303                 if (switch_queue_trypush(LOG_QUEUE, node) != SWITCH_STATUS_SUCCESS) {
00304                         free(node->data);
00305                         if (switch_queue_trypush(LOG_RECYCLE_QUEUE, node) != SWITCH_STATUS_SUCCESS) {
00306                                 free(node);
00307                         }
00308                         node = NULL;
00309                 }
00310         }
00311         
00312  end:
00313 
00314         switch_safe_free(data);
00315         switch_safe_free(new_fmt);
00316         if (handle) {
00317                 fflush(handle);
00318         }
00319 }

Here is the call graph for this function:

switch_log_level_t switch_log_str2level const char *  str  ) 
 

00102 {
00103         int x = 0;
00104         switch_log_level_t level = SWITCH_LOG_INVALID;
00105 
00106         for (x = 0;; x++) {
00107                 if (!LEVELS[x]) {
00108                         break;
00109                 }
00110                 if (!strcasecmp(LEVELS[x], str)) {
00111                         level = (switch_log_level_t) x;
00112                         break;
00113                 }
00114         }
00115 
00116         return level;
00117 }

uint32_t switch_log_str2mask const char *  str  ) 
 

00073 {
00074         int argc = 0, x = 0;
00075         char *argv[10] = { 0 };
00076         uint32_t mask = 0;
00077         char *p = strdup(str);
00078         switch_log_level_t level;
00079 
00080         switch_assert(p);
00081 
00082         if ((argc = switch_separate_string(p, ',', argv, (sizeof(argv) / sizeof(argv[0]))))) {
00083                 for (x = 0; x < argc && argv[x]; x++) {
00084                         if (!strcasecmp(argv[x], "all")) {
00085                                 mask = 0xFF;
00086                                 break;
00087                         } else {
00088                                 level = switch_log_str2level(argv[x]);
00089                                 if (level != SWITCH_LOG_INVALID) {
00090                                         mask |= (1 << level);
00091                                 }
00092                         }
00093                 }
00094         }
00095 
00096         free(p);
00097 
00098         return mask;
00099 }

Here is the call graph for this function:


Generated on Mon May 26 22:06:52 2008 for FreeSWITCH by  doxygen 1.3.9.1