RDK Documentation (Open Sourced RDK Components)
rdk_logger_util.c
Go to the documentation of this file.
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  * @file rdk_logger_util.c
22  * This source file contains the APIs for RDK logger util.
23  */
24 
25 /*
26  * This file provides the rdk_logger_ utility APIs.
27  */
28 
29 /* Header Files */
30 #include <rdk_logger_types.h> /* Resolve basic type references. */
31 #include "rdk_debug.h" /* Resolved RDK_LOG support. */
32 #include "rdk_error.h"
33 #include "rdk_utils.h"
34 
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <string.h>
39 
40 /*Resolve reboot and related macros.*/
41 #include <unistd.h>
42 #include <sys/reboot.h>
43 #include <linux/reboot.h>
44 
45 
46 typedef struct EnvVarNode
47 {
48  int number;
49  char* name;
50  char* value;
51  struct EnvVarNode *next;
52 } EnvVarNode;
53 
54 /** Global count for the modules */
56 
57 /* Env var cache */
58 static EnvVarNode *g_envCache = NULL;
59 
60 static void trim(char *instr, char* outstr)
61 {
62  char *ptr = instr;
63  char *endptr = instr + strlen(instr)-1;
64  int length;
65 
66  /* Advance pointer to first non-whitespace char */
67  while (isspace(*ptr))
68  ptr++;
69 
70  if (ptr > endptr)
71  {
72  /*
73  * avoid breaking things when there are
74  * no non-space characters in instr (JIRA OCORI-2028)
75  */
76  outstr[0] = '\0';
77  return;
78  }
79 
80  /* Move end pointer toward the front to first non-whitespace char */
81  while (isspace(*endptr))
82  endptr--;
83 
84  length = endptr + 1 - ptr;
85  strncpy(outstr,ptr,length);
86  outstr[length] = '\0';
87 
88 }
89 
90 /**
91  * @brief This Function sets up the environment variable cache by parsing configuration file and adding
92  * each name/value pairs to the list.
93  *
94  * @param[in] path Path of the configuration file.
95  * @return Returns Returns RDK_SUCCESS if the setting of environment variable is successful else it returns -1.
96  */
97 rdk_Error rdk_logger_env_add_conf_file( const char * path)
98 {
99  const int line_buf_len = 256;
100  static int number = 0;
101 
102  FILE* f;
103  char lineBuffer[line_buf_len];
104 
105  /* Open the env file */
106  if ((f = fopen( path,"r")) == NULL)
107  {
108  printf("***************************************************\n");
109  printf("***************************************************\n");
110  printf("** ERROR! Could not open configuration file! **\n");
111  printf("***************************************************\n");
112  printf("***************************************************\n");
113  printf("(Tried %s\n", path);
114  return -1;
115  }
116  printf("Conf file %s open success\n", path);
117 
118  /* Read each line of the file */
119  while (fgets(lineBuffer,line_buf_len,f) != NULL)
120  {
121  char name[line_buf_len];
122  char value[line_buf_len];
123  char trimname[line_buf_len];
124  char trimvalue[line_buf_len];
125  EnvVarNode *node;
126  EnvVarNode *tmp_node;
127  char *equals;
128  int length;
129 
130  /* Ignore comment lines */
131  if (lineBuffer[0] == '#')
132  continue;
133 
134  /* Ignore lines that do not have an '=' char */
135  if ((equals = strchr(lineBuffer,'=')) == NULL)
136  continue;
137 
138  /* Read the property and store in the cache */
139  length = equals - lineBuffer;
140  strncpy( name,lineBuffer,length);
141  name[ length] = '\0'; /* have to null-term */
142 
143  length = lineBuffer + strlen(lineBuffer) - equals + 1;
144  strncpy( value,equals+1,length);
145  value[ length] = '\0' ;
146 
147  /* Trim all whitespace from name and value strings */
148  trim( name,trimname);
149  trim( value,trimvalue);
150 
151  tmp_node = g_envCache;
152  while(tmp_node)
153  {
154  if(strcmp(tmp_node->name, trimname) == 0)
155  {
156  break;
157  }
158  tmp_node = tmp_node->next;
159  }
160 
161  if(!tmp_node)
162  {
163  node = ( EnvVarNode*)malloc(sizeof(EnvVarNode));
164  node->name = strdup( trimname);
165  node->value = strdup( trimvalue);
166  }
167  else
168  {
169  free(tmp_node->value);
170  tmp_node->value = strdup( trimvalue);
171  continue;
172  }
173 
174  /** Update number only for the modules, not for environment variable */
175  if ((strcmp("LOG.RDK.DEFAULT",node->name) != 0) &&
176  (strcmp("EnableMPELog",node->name) != 0) &&
177  (strcmp("SEPARATE.LOGFILE.SUPPORT",node->name) != 0))
178  {
179  number++;
180  node->number = number;
181  } else
182  node->number = 0;
183 
184  /* Insert at the front of the list */
185  node->next = g_envCache;
186  g_envCache = node;
187  }
188 
189  global_count = number;
190 
191  fclose( f);
192  return RDK_SUCCESS;
193 }
194 
195 /**
196  * @brief This function will get value of the specified environment variable.
197  *
198  * @param[in] name It is a pointer to the name of the target environment variable.
199  *
200  * @return Returns a pointer to the associated string value of the target environment
201  * variable or return NULL if the variable can't be found.
202  */
203 const char* rdk_logger_envGet(const char *name)
204 {
205  EnvVarNode *node = g_envCache;
206 
207  while (node != NULL)
208  {
209  /* Env var name match */
210  if (strcmp(name,node->name) == 0)
211  {
212  /* return the value */
213  return node->value;
214  }
215 
216  node = node->next;
217  }
218 
219  /* Not found */
220  return NULL;
221 }
222 
223 /**
224  * @brief This function is used to get the value of the specified environment variable based
225  * on its registered number.
226  *
227  * @param[in] number Is a registered number of the target environment variable.
228  * @return Returns a pointer to the associated string value of the target environment.
229  * variable or return NULL in failure condition.
230  */
231 const char* rdk_logger_envGetValueFromNum(int number)
232 {
233  EnvVarNode *node = g_envCache;
234 
235  while (node != NULL)
236  {
237  /* Env var name match */
238  if (number == node->number)
239  {
240  /* return the value */
241  return node->value;
242  }
243 
244  node = node->next;
245  }
246 
247  /* Not found */
248  return NULL;
249 }
250 
251 /**
252  * @brief Function will give the registered number of the specified environment variable.
253  *
254  * @param[in] mod It is a pointer to the name of the target environment variable.
255  * @return Returns an integer value if the call is success otherwise returns -1.
256  */
257 int rdk_logger_envGetNum(const char * mod)
258 {
259  EnvVarNode *node = g_envCache;
260 
261  while (node != NULL)
262  {
263  /* Env var name match */
264  if (strcmp(mod,node->name) == 0)
265  {
266  /* return the value */
267  return node->number;
268  }
269 
270  node = node->next;
271  }
272 
273  /* Not found */
274  return -1;
275 }
276 
277 /**
278  * @brief This function is used to get the name of the specified environment variable based
279  * on its registered number.
280  *
281  * @param[in] Num Is a registered number of the target environment variable.
282  * @return Returns a pointer to the associated string value of the target environment variable associated
283  * for registered number. Return NULL if the environment variable can't be found for registered number.
284  */
285 const char* rdk_logger_envGetModFromNum(int Num)
286 {
287  EnvVarNode *node = g_envCache;
288 
289  while (node != NULL)
290  {
291  /* Env var name match */
292  if (Num == node->number)
293  {
294  /* return the value */
295  return node->name;
296  }
297 
298  node = node->next;
299  }
300 
301  /* Not found */
302  return NULL;
303 }
304 
rdk_logger_envGetValueFromNum
const char * rdk_logger_envGetValueFromNum(int number)
This function is used to get the value of the specified environment variable based on its registered ...
Definition: rdk_logger_util.c:231
rdk_debug.h
EnvVarNode
Definition: rdk_logger_util.c:46
rdk_utils.h
trim
void trim(std::string &src)
Trim a string.
Definition: AampUtils.cpp:710
rdk_logger_envGet
const char * rdk_logger_envGet(const char *name)
This function will get value of the specified environment variable.
Definition: rdk_logger_util.c:203
global_count
int global_count
Definition: rdk_logger_util.c:55
rdk_logger_env_add_conf_file
rdk_Error rdk_logger_env_add_conf_file(const char *path)
This Function sets up the environment variable cache by parsing configuration file and adding each na...
Definition: rdk_logger_util.c:97
rdk_logger_envGetNum
int rdk_logger_envGetNum(const char *mod)
Function will give the registered number of the specified environment variable.
Definition: rdk_logger_util.c:257
rdk_logger_envGetModFromNum
const char * rdk_logger_envGetModFromNum(int Num)
This function is used to get the name of the specified environment variable based on its registered n...
Definition: rdk_logger_util.c:285