RDK Documentation (Open Sourced RDK Components)
Util.cpp
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 
21 
22 /**
23 * @defgroup hdmicec
24 * @{
25 * @defgroup ccec
26 * @{
27 **/
28 
29 
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <sys/stat.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
36 #include <time.h>
37 #include "ccec/Util.hpp"
38 
39 static int cec_log_level = LOG_INFO;
40 #define MAX_LOG_BUFF 500
41 
42 
43 #define __TIMESTAMP() do { /*YYMMDD-HH:MM:SS:usec*/ \
44  struct tm __tm; \
45  struct timeval __tv; \
46  gettimeofday(&__tv, NULL); \
47  localtime_r(&__tv.tv_sec, &__tm); \
48  printf("\r\n%02d%02d%02d-%02d:%02d:%02d:%06d ", \
49  __tm.tm_year+1900-2000, \
50  __tm.tm_mon+1, \
51  __tm.tm_mday, \
52  __tm.tm_hour, \
53  __tm.tm_min, \
54  __tm.tm_sec, \
55  (int)__tv.tv_usec); \
56 } while(0)
57 
58 static const char *logLevel[][2] =
59 { {"FATAL","0"},
60  {"ERROR","1"},
61  {"WARN","2"},
62  {"EXP","3"},
63  {"NOTICE","4"},
64  {"INFO","5"},
65  {"DEBUG","6"},
66  {"TRACE","7"}
67 };
68 
69 /**
70  * @brief This function is used to get the cec log from the log file and checks
71  * the level of log received.
72  *
73  * @return None
74  */
76 {
77  struct stat st;
78  FILE *fp;
79  const int buffer_length = 256;
80  char cecBuffer[buffer_length];
81  memset(&st,0,sizeof(st));
82  if(0 == stat("/tmp/cec_log_enabled",&st))
83  {
84  if(!S_ISREG(st.st_mode))
85  {
86  return;
87  }
88  if((fp = fopen("/tmp/cec_log_enabled","r")) == NULL)
89  {
90  printf("Error in opening cec_log_enabled filee \n");
91  return;
92  }
93  if ((fgets(cecBuffer,buffer_length,fp)) != NULL)
94  {
95  for (int i =0; i< LOG_MAX;i++)
96  {
97  if (strncmp(cecBuffer,logLevel[i][0],strlen(logLevel[i][0])) == 0)
98  {
99  cec_log_level = atoi(logLevel[i][1]);
100  break;
101  }
102  }
103  }
104  fclose(fp);
105  }
106  return;
107 }
108 
109 char _CEC_LOG_PREFIX[64];
110 
111 /**
112  * @brief This function is used to gets the logs depending on the level of log
113  * and print these to standard output.
114  *
115  * @param[in] level CEC Log level
116  * @param[in] format Format of the received data.
117  *
118  * @return None
119  */
120 void CCEC_LOG(int level, const char * format ...)
121 {
122  if ((level < LOG_MAX) && (level <= cec_log_level))
123  {
124  char tmp_buff[MAX_LOG_BUFF];
125  va_list args;
126  va_start(args, format);
127  vsnprintf(tmp_buff,MAX_LOG_BUFF-1,format, args);
128  va_end(args);
129  __TIMESTAMP();printf("[%s]%s", _CEC_LOG_PREFIX, tmp_buff);
130  }
131 }
132 
133 /**
134  * @brief This function is used to print the content of log buffer in hexadecimal
135  * format.
136  *
137  * @param[in] buf Buffer where CEC log is stored.
138  * @param[in] len Length of the buffer to be printed.
139  *
140  * @return None.
141  */
142 void dump_buffer(unsigned char * buf, int len)
143 {
144  if((cec_log_level < LOG_MAX) && (cec_log_level >= LOG_DEBUG))
145  {
146  for (int ii = 0; ii < len; ii++) {
147  printf("%02X ", buf[ii]);
148  }
149  }
150 }
151 
152 
153 /** @} */
154 /** @} */
dump_buffer
void dump_buffer(unsigned char *buf, int len)
This function is used to print the content of log buffer in hexadecimal format.
Definition: Util.cpp:142
check_cec_log_status
void check_cec_log_status(void)
This function is used to get the cec log from the log file and checks the level of log received.
Definition: Util.cpp:75
LOG_INFO
#define LOG_INFO(AAMP_JS_OBJECT, FORMAT,...)
Definition: jsutils.h:39
CCEC_LOG
void CCEC_LOG(int level, const char *format ...)
This function is used to gets the logs depending on the level of log and print these to standard outp...
Definition: Util.cpp:120