26 #include <arpa/inet.h>
29 #include <sys/select.h>
32 #include "rdk_dynamic_logger.h"
35 #define DL_SIGNATURE "COMC"
36 #define DL_SIGNATURE_LEN 4
38 static int g_dl_socket = -1;
39 extern char *__progname;
41 static char * rdk_dyn_log_logLevelToString(
unsigned char log_level)
44 unsigned char negate_mask = 0x80;
46 if(negate_mask == (log_level & negate_mask)) {
48 log_level = log_level & (~negate_mask) ;
53 return (negate) ?
"!FATAL":
"FATAL";
55 return (negate) ?
"!ERROR":
"ERROR";
57 return (negate) ?
"!WARNING":
"WARNING";
59 return (negate) ?
"!NOTICE":
"NOTICE";
61 return (negate) ?
"!INFO":
"INFO";
63 return (negate) ?
"!DEBUG":
"DEBUG";
65 return (negate) ?
"!TRACE1":
"TRACE1";
67 return (negate) ?
"!TRACE2":
"TRACE2";
69 return (negate) ?
"!TRACE3":
"TRACE3";
71 return (negate) ?
"!TRACE4":
"TRACE4";
73 return (negate) ?
"!TRACE5":
"TRACE5";
75 return (negate) ?
"!TRACE6":
"TRACE6";
77 return (negate) ?
"!TRACE7":
"TRACE7";
79 return (negate) ?
"!TRACE8":
"TRACE8";
81 return (negate) ?
"!TRACE9":
"TRACE9";
87 static void rdk_dyn_log_validateComponentName(
const unsigned char *buf)
89 unsigned char log_level = 0;
90 int app_len, comp_len, i = DL_SIGNATURE_LEN;
91 char *loggingLevel = NULL, comp_name[64] = {0};
93 if(0 != memcmp(buf,DL_SIGNATURE,i)) {
100 if(0 != memcmp(buf+(++i),__progname,app_len)) {
108 loggingLevel = rdk_dyn_log_logLevelToString(log_level);
109 if(NULL != loggingLevel) {
110 memcpy(comp_name,buf+(++i),comp_len);
111 RDK_LOG_ControlCB(comp_name, NULL, loggingLevel, 0);
112 fprintf(stderr,
"%s(): Set %s loglevel for the component %s of the process %s\n",__func__,loggingLevel,comp_name,__progname);
116 void rdk_dyn_log_processPendingRequest()
119 struct sockaddr_in sender_addr;
121 int numbytes, addr_len, ret, i = 0;
124 if(-1 == g_dl_socket)
127 memset(&sender_addr,0,
sizeof(sender_addr));
130 FD_SET(g_dl_socket, &rfds);
131 addr_len =
sizeof(sender_addr);
135 ret = select(g_dl_socket+1,&rfds,NULL,NULL,&tv);
139 if ((numbytes=recvfrom(g_dl_socket, buf,
sizeof(buf), 0, (
struct sockaddr *)&sender_addr, &addr_len)) == -1) {
140 fprintf(stderr,
"%s recvfrom failed %s\n",__func__,strerror(errno));
157 if((0 == strcmp(
"127.0.0.1",inet_ntoa(sender_addr.sin_addr))) &&
158 (numbytes == buf[4]+DL_SIGNATURE_LEN+1)) {
159 rdk_dyn_log_validateComponentName(buf);
164 void rdk_dyn_log_init()
166 struct sockaddr_in my_addr;
169 if ((g_dl_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
170 fprintf(stderr,
"%s() Socket creation failed. %s\n",__func__,strerror(errno));
174 memset(&my_addr,0,
sizeof(my_addr));
175 my_addr.sin_family = AF_INET;
176 my_addr.sin_port = htons(DL_PORT);
177 my_addr.sin_addr.s_addr = inet_addr(
"127.255.255.255");
179 if (-1 == setsockopt(g_dl_socket,SOL_SOCKET,SO_REUSEADDR,&opt,
sizeof(
int))) {
180 fprintf(stderr,
"%s() setsockopt error %s\n",__func__,strerror(errno));
186 if (-1 == bind(g_dl_socket, (
struct sockaddr *)&my_addr,
sizeof(
struct sockaddr))) {
187 fprintf(stderr,
"%s() bind error %s\n",__func__,strerror(errno));
193 fprintf(stderr,
"%sg_dl_socket = %d __progname = %s \n",__func__,g_dl_socket,__progname);
196 void rdk_dyn_log_deInit()