42 #include <sys/syscall.h>
45 #include <sys/ioctl.h>
46 #include <arpa/inet.h>
50 #include "rdk_logger_types.h"
51 #include "rdk_error.h"
52 #define RDK_DEBUG_DEFINE_STRINGS
53 #include "rdk_debug_priv.h"
54 #include "rdk_dynamic_logger.h"
58 #ifdef SYSTEMD_JOURNAL
59 #include <systemd/sd-journal.h>
60 #endif //SYSTEMD_JOURNAL
63 #ifdef SYSTEMD_SYSLOG_HELPER
64 #include "syslog_helper_ifc.h"
76 #define WANT_LOG(mod, lvl) ( ( ((mod) >= 0) && ((mod) < RDK_MAX_MOD_COUNT) ) ? (rdk_g_logControlTbl[(mod)] & (1 << (lvl))) : 0 )
80 #define SKIPWHITE(cptr) while ((*cptr != '\0') && isspace(*cptr)) cptr++
83 #define LOG_TRACE ( (1 << RDK_LOG_TRACE1) | (1 << RDK_LOG_TRACE2) | (1 << RDK_LOG_TRACE3) \
84 | (1 << RDK_LOG_TRACE4) | (1 << RDK_LOG_TRACE5) | (1 << RDK_LOG_TRACE6) \
85 | (1 << RDK_LOG_TRACE7) | (1 << RDK_LOG_TRACE8) | (1 << RDK_LOG_TRACE9) )
88 #define LOG_ALL ( (1 << RDK_LOG_FATAL) | (1 << RDK_LOG_ERROR) | \
89 (1 << RDK_LOG_WARN) | (1 << RDK_LOG_INFO) | \
90 (1 << RDK_LOG_NOTICE))
95 #define HOSTADDR_STR_MAX 255
98 #define MAX_LOGLINE_LENGTH 4096
102 static const char* dated_format_nocr(
const log4c_layout_t* a_layout,
103 const log4c_logging_event_t*a_event);
104 static const char* basic_format_nocr(
const log4c_layout_t* a_layout,
105 const log4c_logging_event_t*a_event);
106 static const char* comcast_dated_format_nocr(
const log4c_layout_t* a_layout,
107 log4c_logging_event_t*a_event);
108 static int stream_env_overwrite_open(log4c_appender_t * appender);
109 static int stream_env_append_open(log4c_appender_t * appender);
110 static int stream_env_append(log4c_appender_t* appender,
const log4c_logging_event_t* event);
111 static int stream_env_plus_stdout_append(log4c_appender_t* appender,
const log4c_logging_event_t* event);
112 static int stream_env_close(log4c_appender_t * appender);
116 static rdk_logger_Bool g_initialized = FALSE;
125 struct sockaddr_in dbg_logHostAddr;
127 log4c_category_t* stackCat = NULL;
140 static const char *errorMsgs[] =
141 {
"Error: Invalid module name.",
"Warning: Ignoring invalid log name(s)." };
148 static log4c_category_t* glibCategory = NULL;
150 static const log4c_layout_type_t log4c_layout_type_dated_nocr =
151 {
"dated_nocr", dated_format_nocr, };
153 static const log4c_layout_type_t log4c_layout_type_basic_nocr =
154 {
"basic_nocr", basic_format_nocr, };
156 static const log4c_layout_type_t log4c_layout_type_comcast_dated_nocr =
157 {
"comcast_dated_nocr", comcast_dated_format_nocr, };
159 static const log4c_appender_type_t
160 log4c_appender_type_stream_env =
161 {
"stream_env", stream_env_overwrite_open, stream_env_append,
164 static const log4c_appender_type_t log4c_appender_type_stream_env_append =
165 {
"stream_env_append", stream_env_append_open, stream_env_append,
168 static const log4c_appender_type_t log4c_appender_type_stream_env_plus_stdout =
169 {
"stream_env_plus_stdout", stream_env_overwrite_open,
170 stream_env_plus_stdout_append, stream_env_close, };
172 static const log4c_appender_type_t log4c_appender_type_stream_env_append_plus_stdout =
173 {
"stream_env_append_plus_stdout", stream_env_append_open,
174 stream_env_plus_stdout_append, stream_env_close, };
176 void rdk_dbg_priv_Init()
182 fprintf(stderr,
"%s -- initLogger failure?!\n", __FUNCTION__);
185 stackCat = log4c_category_get(
"RI.Stack");
208 *token = (char) toupper(*token);
223 while (i < ENUM_RDK_LOG_COUNT)
247 const char *src = *srcStr;
248 while (*src && !isspace(*src))
270 uint32_t config = *configEntry;
271 char logTypeName[128] =
281 *msg =
"Warning: Empty log level confguration.";
285 while (*cfgStr !=
'\0')
289 memset(logTypeName, 0,
sizeof(logTypeName));
295 if (strcmp(logTypeName,
"ALL") == 0)
299 else if (strcmp(logTypeName,
"NONE") == 0)
303 else if (strcmp(logTypeName,
"TRACE") == 0)
307 else if (strcmp(logTypeName,
"!TRACE") == 0)
315 const char *name = logTypeName;
318 if (logTypeName[0] ==
'!')
321 name = &logTypeName[1];
329 config &= ~(1 << logLevel);
333 config |= (1 << logLevel);
338 *msg = errorMsgs[ERR_INVALID_LOG_NAME];
347 *configEntry = config;
351 static void printTime(
const struct tm *pTm,
char *pBuff)
353 sprintf(pBuff,
"%02d%02d%02d-%02d:%02d:%02d",pTm->tm_year + 1900 - 2000, pTm->tm_mon + 1, pTm->tm_mday, pTm->tm_hour, pTm->tm_min, pTm->tm_sec);
368 char envVarName[128] =
370 const char *envVarValue = NULL;
371 uint32_t defaultConfig = 0;
373 const char *msg =
"";
381 strcpy(envVarName,
"LOG.RDK.DEFAULT");
383 if ((envVarValue != NULL) && (envVarValue[0] != 0))
399 if ((envVarValue != NULL) && (envVarValue[0] !=
'\0'))
433 const char *envVarName;
434 uint32_t defaultConfig = 0;
436 const char *msg =
"";
438 if ((pszModuleName != NULL) && (pszLogLevels != NULL))
441 if(0 == strcmp(pszModuleName,
"LOG.RDK.DEFAULT"))
448 printf(
"rdk_dbg_priv_SetLogLevelString: Set Logging Level for '%s' to '%s'\n", pszModuleName, pszLogLevels);
458 if(0 == strcmp(pszModuleName, envVarName))
460 if ((pszLogLevels != NULL) && (pszLogLevels[0] !=
'\0'))
463 printf(
"rdk_dbg_priv_SetLogLevelString: Set Logging Level for '%s' to '%s'\n", pszModuleName, pszLogLevels);
469 printf(
"rdk_dbg_priv_SetLogLevelString: Failed to set Logging Level for '%s' to '%s'\n", pszModuleName, pszLogLevels);
493 char *name = modName;
500 assert(cfgStrMaxLen > 32);
512 return "Unknown module specified.";
520 strcpy(cfgStr,
"NONE");
530 strcpy(cfgStr,
" ALL");
536 strcat(cfgStr,
" TRACE");
543 for (level = 0; level < ENUM_RDK_LOG_COUNT; level++)
545 if (modCfg & (1 << level))
551 > (
size_t) cfgStrMaxLen)
553 return "Warning: Config string too long, config concatenated.";
565 int module,
const char *module_name,
const char* format, va_list args)
568 static log4c_category_t *cat_cache[RDK_MAX_MOD_COUNT] = {NULL};
569 char cat_name[64] = {
'\0'};
570 log4c_category_t* cat = NULL;
573 rdk_dyn_log_processPendingRequest();
580 char *parent_cat_name = (
char *)log4c_category_get_name(stackCat);
581 snprintf(cat_name,
sizeof(cat_name)-1,
"%s.%s", parent_cat_name == NULL ?
"" : parent_cat_name, module_name);
583 if((module >= 0) && (module < RDK_MAX_MOD_COUNT))
585 if (cat_cache[module] == NULL) {
587 cat_cache[module] = log4c_category_get(cat_name);
590 cat = cat_cache[module];
594 cat = log4c_category_get(cat_name);
600 log4c_category_vlog(cat, LOG4C_PRIORITY_FATAL, format, args);
603 log4c_category_vlog(cat, LOG4C_PRIORITY_ERROR, format, args);
606 log4c_category_vlog(cat, LOG4C_PRIORITY_WARN, format, args);
609 log4c_category_vlog(cat, LOG4C_PRIORITY_NOTICE, format, args);
612 log4c_category_vlog(cat, LOG4C_PRIORITY_INFO, format, args);
615 log4c_category_vlog(cat, LOG4C_PRIORITY_DEBUG, format, args);
626 log4c_category_vlog(cat, LOG4C_PRIORITY_TRACE, format, args);
629 log4c_category_vlog(cat, LOG4C_PRIORITY_DEBUG, format, args);
635 void RDK_LOG_ControlCB(
const char *moduleName,
const char *subComponentName,
const char *loggingLevel,
int log_status)
637 char logTypeName[20] = {0};
639 if ((NULL == moduleName) || (NULL == loggingLevel))
644 strncpy(logTypeName, loggingLevel,
sizeof(logTypeName));
645 if (logTypeName[0] ==
'~')
647 logTypeName[0] =
'!';
666 (void) log4c_appender_type_set(&log4c_appender_type_stream_env);
667 (void) log4c_appender_type_set(&log4c_appender_type_stream_env_append);
668 (void) log4c_appender_type_set(&log4c_appender_type_stream_env_plus_stdout);
669 (void) log4c_appender_type_set(
670 &log4c_appender_type_stream_env_append_plus_stdout);
671 (void) log4c_layout_type_set(&log4c_layout_type_dated_nocr);
672 (void) log4c_layout_type_set(&log4c_layout_type_basic_nocr);
673 (void) log4c_layout_type_set(&log4c_layout_type_comcast_dated_nocr);
677 fprintf(stderr,
"log4c_init() failed?!");
683 glibCategory = log4c_category_get(
"RI.GLib");
693 static const char* dated_format_nocr(
const log4c_layout_t* layout,
694 const log4c_logging_event_t* event)
699 gmtime_r(&event->evt_timestamp.tv_sec, &tm);
701 memset(&timeBuff,0,40);
703 printTime(&tm,timeBuff);
705 (void) snprintf(event->evt_buffer.buf_data, event->evt_buffer.buf_size,
706 "%s.%03ld %-8s %s- %s", timeBuff,
707 event->evt_timestamp.tv_usec / 1000, log4c_priority_to_string(
708 event->evt_priority),
event->evt_category,
710 if (event->evt_buffer.buf_size > 0 && event->evt_buffer.buf_data != NULL)
712 event->evt_buffer.buf_data[
event->evt_buffer.buf_size - 1] = 0;
714 return event->evt_buffer.buf_data;
720 static const char* basic_format_nocr(
const log4c_layout_t* layout,
721 const log4c_logging_event_t* event)
723 (void) snprintf(event->evt_buffer.buf_data, event->evt_buffer.buf_size,
"%-8s %s - %s",
724 log4c_priority_to_string(event->evt_priority),
725 event->evt_category,
event->evt_msg);
727 if (event->evt_buffer.buf_size > 0 && event->evt_buffer.buf_data != NULL)
729 event->evt_buffer.buf_data[
event->evt_buffer.buf_size - 1] = 0;
732 return event->evt_buffer.buf_data;
735 #define COMCAST_DATAED_BUFF_SIZE 40
737 static const char* comcast_dated_format_nocr(
const log4c_layout_t* layout,
738 log4c_logging_event_t*event)
742 char timeBuff[COMCAST_DATAED_BUFF_SIZE] = {0};
744 gmtime_r(&event->evt_timestamp.tv_sec, &tm);
746 printTime(&tm,timeBuff);
749 char *p= (
char *)(event->evt_category);
752 p = (
char*)
"UNKNOWN";
757 if ( len > 0 && *p !=
'.' && *(p+len-1) !=
'.')
760 while (p != (
char *)(event->evt_category) && *p !=
'.') p--;
766 p = (
char*)
"UNKNOWN";
770 n = snprintf(event->evt_buffer.buf_data, event->evt_buffer.buf_size,
771 "%s.%06ld [mod=%s, lvl=%s] [tid=%ld] %s",timeBuff,
772 event->evt_timestamp.tv_usec,
773 p, log4c_priority_to_string(event->evt_priority), syscall(SYS_gettid),
775 if (n > -1 && n > event->evt_buffer.buf_size && event->evt_buffer.buf_maxsize == 0) {
776 event->evt_buffer.buf_size = n + COMCAST_DATAED_BUFF_SIZE + 1;
777 event->evt_buffer.buf_data = (
char *) realloc (event->evt_buffer.buf_data, event->evt_buffer.buf_size);
779 n = snprintf(event->evt_buffer.buf_data, event->evt_buffer.buf_size,
780 "%s.%06ld [mod=%s, lvl=%s] [tid=%ld] %s",timeBuff,
781 event->evt_timestamp.tv_usec,
782 p, log4c_priority_to_string(event->evt_priority), syscall(SYS_gettid),
785 if (event->evt_buffer.buf_size > 0 && event->evt_buffer.buf_data != NULL)
787 event->evt_buffer.buf_data[
event->evt_buffer.buf_size - 1] = 0;
789 return event->evt_buffer.buf_data;
795 static int stream_env_open(log4c_appender_t* appender,
int append)
797 FILE* fp = (FILE*)log4c_appender_get_udata(appender);
798 char* name = strdup(log4c_appender_get_name(appender));
799 int nameLen = strlen(name);
801 char *varBegin, *varEnd;
803 const int MAX_VAR_LEN = 1024;
804 char newName[MAX_VAR_LEN+1];
816 while ((varBegin = strchr(temp,
'$')) != NULL)
819 if (((varBegin - name + 1) >= nameLen) || (*(varBegin+1) !=
'('))
823 strncat(newName, temp, varBegin-temp);
824 newNameLen += varBegin-temp;
825 if (newNameLen > MAX_VAR_LEN)
830 if ((varEnd = strchr(varBegin,
')')) == NULL)
834 if ((envVar = getenv(varBegin)) == NULL)
838 strncat(newName, envVar, strlen(envVar));
839 newNameLen += strlen(envVar);
840 if (newNameLen >MAX_VAR_LEN)
847 strncat(newName, temp, (name + nameLen) - temp);
848 newNameLen += (name + nameLen) - temp;
849 if (newNameLen >MAX_VAR_LEN)
854 if (!strcmp(newName,
"stderr"))
856 else if (!strcmp(newName,
"stdout"))
860 printf(
"****Opening %s in append mode\n", newName);
861 if ((fp = fopen(newName,
"a")) == NULL)
866 printf(
"****Opening %s in write mode\n", newName);
867 if ((fp = fopen(newName,
"w")) == NULL)
874 (void)log4c_appender_set_udata(appender, fp);
878 fprintf(stderr,
"*(*(*(*( log4c appender stream_env, %s -- Illegal env var name or format! %s\n",
880 (void)fflush(stderr);
885 fprintf(stderr,
"*(*(*(*( log4c appender stream_env, %s -- Path is too long! %s\n",
887 (void)fflush(stderr);
892 static int stream_env_overwrite_open(log4c_appender_t* appender)
894 return stream_env_open(appender, 0);
897 static int stream_env_append_open(log4c_appender_t* appender)
899 return stream_env_open(appender, 1);
902 #ifdef SYSTEMD_JOURNAL
903 static int stream_env_append_get_priority(
int log4c_pr)
908 case LOG4C_PRIORITY_FATAL:
909 priority = LOG_EMERG;
911 case LOG4C_PRIORITY_ERROR:
914 case LOG4C_PRIORITY_WARN:
915 priority = LOG_WARNING;
917 case LOG4C_PRIORITY_NOTICE:
918 priority = LOG_NOTICE;
920 case LOG4C_PRIORITY_INFO:
923 case LOG4C_PRIORITY_DEBUG:
924 case LOG4C_PRIORITY_TRACE:
926 priority = LOG_DEBUG;
933 static int stream_env_append(log4c_appender_t* appender,
934 const log4c_logging_event_t* event)
937 FILE* fp = (FILE*)log4c_appender_get_udata(appender);
939 #if defined(SYSTEMD_SYSLOG_HELPER)
940 send_logs_to_syslog(event->evt_rendered_msg);
941 #elif defined(SYSTEMD_JOURNAL)
942 if (fp == stdout || fp == stderr)
944 retval = sd_journal_print(stream_env_append_get_priority(event->evt_priority),
"%s",event->evt_rendered_msg);
948 retval = fprintf(fp,
"%s", event->evt_rendered_msg);
952 retval = fprintf(fp,
"%s", event->evt_rendered_msg);
961 static int stream_env_plus_stdout_append(log4c_appender_t* appender,
962 const log4c_logging_event_t* event)
965 FILE* fp = (FILE*)log4c_appender_get_udata(appender);
967 #if defined(SYSTEMD_SYSLOG_HELPER)
968 send_logs_to_syslog(event->evt_rendered_msg);
969 #elif defined(SYSTEMD_JOURNAL)
970 if (fp != stdout || fp != stderr)
972 retval = fprintf(fp,
"%s", event->evt_rendered_msg);
976 retval = sd_journal_print(stream_env_append_get_priority(event->evt_priority),
"%s",event->evt_rendered_msg);
980 retval = fprintf(fp,
"%s", event->evt_rendered_msg);
981 fprintf(stdout,
"%s", event->evt_rendered_msg);
983 (void)fflush(stdout);
991 static int stream_env_close(log4c_appender_t* appender)
993 FILE* fp = (FILE*)log4c_appender_get_udata(appender);
995 if (!fp || fp == stdout || fp == stderr)