automotive-dlt
dlt_daemon_offline_logstorage.c
Go to the documentation of this file.
1 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <syslog.h>
27 
29 
30 
44 int dlt_logstorage_split_key(char *key, char *appid, char *ctxid)
45 {
46  char *tok = NULL;
47  int len = 0;
48  int ret = 0;
49  char *sep = NULL;
50 
51  if (key == NULL)
52  {
53  return -1;
54  }
55 
56  len = strlen(key);
57 
58  sep = strchr (key, ':');
59  if(sep == NULL)
60  {
61  return -1;
62  }
63 
64  /* key is context id only */
65  if(key[0] == ':')
66  {
67  if(len > (DLT_ID_SIZE+1))
68  return -1;
69 
70  strncpy(ctxid,(key+1),(len-1));
71  strncpy(appid, ".*",2);
72  }
73  /* key is application id only */
74  else if (key[len-1] == ':')
75  {
76  if(len > (DLT_ID_SIZE+1))
77  return -1;
78 
79  strncpy(appid,key,(len-1));
80  strncpy(ctxid, ".*",2);
81  }
82  /* key is appid:ctxid */
83  else
84  {
86  return -1;
87 
88  /* copy appid and ctxid */
89  tok = strtok(key, ":");
90  if (tok != NULL)
91  strncpy(appid,tok,DLT_ID_SIZE);
92  else
93  ret = -1;
94 
95  tok = strtok(NULL, ":");
96  if (tok != NULL)
97  strncpy(ctxid,tok,DLT_ID_SIZE);
98  else
99  ret = -1;
100  }
101 
102  return ret;
103 }
104 
121 int dlt_logstorage_update_all_contexts(DltDaemon *daemon, char *id, int curr_log_level, int cmp_flag, int verbose)
122 {
123  int i = 0;
124  char tmp_id[DLT_ID_SIZE+1];
125  int old_log_level = -1;
126 
127  if((daemon == 0) || (id == NULL))
128  return -1;
129 
130  if((cmp_flag < 0 ) || (cmp_flag > 2 ))
131  return -1;
132 
133  memset(tmp_id, 0, sizeof(tmp_id));
134 
135  for(i = 0; i < daemon->num_contexts; i++)
136  {
137  if(cmp_flag == 1)
138  dlt_set_id(tmp_id, daemon->contexts[i].apid);
139  else
140  dlt_set_id(tmp_id, daemon->contexts[i].ctid);
141 
142  if(strcmp(id, tmp_id) == 0)
143  {
144  if(curr_log_level > 0)
145  {
146  old_log_level = daemon->contexts[i].storage_log_level;
147 
148  daemon->contexts[i].storage_log_level = DLT_OFFLINE_LOGSTORAGE_MAX(curr_log_level, daemon->contexts[i].storage_log_level);
149 
150  if(daemon->contexts[i].storage_log_level > old_log_level)
151  {
152  if(dlt_daemon_user_send_log_level(daemon, &daemon->contexts[i], verbose) == -1)
153  {
154  dlt_log(LOG_ERR, "Unable to update loglevel\n");
155  return -1;
156  }
157  }
158  }
159  else /* The request is to reset log levels */
160  {
161  /* Set storage level to -1, to clear log levels */
162  daemon->contexts[i].storage_log_level = -1;
163 
164  if(curr_log_level == DLT_DAEMON_LOGSTORAGE_RESET_SEND_LOGLEVEL)
165  {
166  if(dlt_daemon_user_send_log_level(daemon, &daemon->contexts[i], verbose) == -1)
167  {
168  dlt_log(LOG_ERR, "Unable to reset loglevel\n");
169  return -1;
170  }
171  }
172 
173  }
174  }
175  }
176  return 0;
177 }
178 
193 int dlt_logstorage_update_context(DltDaemon *daemon, char *apid, char *ctxid, int curr_log_level , int verbose)
194 {
195  DltDaemonContext *context = NULL;
196  int old_log_level = -1;
197 
198  if((daemon == 0) || (apid == NULL) || (ctxid == NULL))
199  return -1;
200 
201  context = dlt_daemon_context_find(daemon, apid, ctxid, verbose);
202  if(context != NULL)
203  {
204  if(curr_log_level > 0)
205  {
206  old_log_level = context->storage_log_level;
207 
208  context->storage_log_level = DLT_OFFLINE_LOGSTORAGE_MAX(curr_log_level, context->storage_log_level);
209  if(context->storage_log_level > old_log_level)
210  {
211  if(dlt_daemon_user_send_log_level(daemon, context, verbose) == -1)
212  {
213  dlt_log(LOG_ERR, "Unable to update loglevel\n");
214  return -1;
215  }
216  }
217  }
218  else
219  {
220  context->storage_log_level = -1;
221 
222  if(curr_log_level == DLT_DAEMON_LOGSTORAGE_RESET_SEND_LOGLEVEL)
223  {
224  if(dlt_daemon_user_send_log_level(daemon, context, verbose) == -1)
225  {
226  dlt_log(LOG_ERR, "Unable to update loglevel\n");
227  return -1;
228  }
229  }
230 
231  }
232  }
233  return 0;
234 }
235 
247 int dlt_logstorage_update_context_loglevel(DltDaemon *daemon, char *key, int curr_log_level , int verbose)
248 {
249 
250  int cmp_flag=0;
251  char appid[DLT_ID_SIZE+1] = {'\0'};
252  char ctxid[DLT_ID_SIZE+1] = {'\0'};
253 
254  PRINT_FUNCTION_VERBOSE(verbose);
255 
256  if((daemon == 0) || (key == NULL))
257  return -1;
258 
259  memset(appid, 0, sizeof(appid));
260  memset(ctxid, 0, sizeof(ctxid));
261 
262  if(dlt_logstorage_split_key(key, appid, ctxid) != 0)
263  {
264  dlt_log(LOG_ERR, "Error while updating application log levels (splt key)\n");
265  return -1;
266  }
267 
268  if(strcmp(ctxid, ".*") == 0) /* wildcard for context id, find all contexts of given application id */
269  {
270  cmp_flag = 1;
271 
272  if(dlt_logstorage_update_all_contexts(daemon, appid, curr_log_level, cmp_flag, verbose) != 0)
273  return -1;
274  }
275  else if(strcmp(appid, ".*") == 0) /* wildcard for application id, find all contexts with context id */
276  {
277  cmp_flag = 2;
278 
279  if(dlt_logstorage_update_all_contexts(daemon, ctxid, curr_log_level, cmp_flag, verbose) != 0)
280  return -1;
281  }
282  else /* In case of given application id, context id pair, call available context find function */
283  {
284  if(dlt_logstorage_update_context(daemon, appid, ctxid, curr_log_level, verbose) != 0)
285  return -1;
286  }
287  return 0;
288 }
289 
303 void dlt_daemon_logstorage_reset_application_loglevel(DltDaemon *daemon, int dev_num, int max_device, int verbose)
304 {
305  DltLogStorage *handle = NULL;
306  int i = 0;
307  char key[DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN] = {'\0'};
308  int num_device_configured = 0;
309 
310  PRINT_FUNCTION_VERBOSE(verbose);
311 
312  if((daemon == 0) || (dev_num < 0))
313  {
314  dlt_log(LOG_ERR, "Invalid function parameters used for dlt_daemon_logstorage_reset_application_loglevel\n");
315  return;
316  }
317 
318  handle = &(daemon->storage_handle[dev_num]);
321  {
322  return;
323  }
324 
325  /* First, check number of devices configured */
326  for(i = 0; i<max_device; i++)
327  {
329  {
330  num_device_configured += 1;
331  }
332  }
333 
334  /* for all filters (keys) check if application context are already running and log level need to be reset*/
335  for(i = 0; i < handle->num_filter_keys; i++)
336  {
337  memset(key, 0, sizeof(key));
338 
339  strncpy(key, (handle->filter_keys + i * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN), DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN);
340 
341  if(num_device_configured == 1)
342  /* Reset context log level and send to application */
344  else
345  /* Reset context log level do not send to application as other devices can have same configuration */
347  }
348 
349  /* Re-run update log level for all other configured devices */
350  for(i=0; i<max_device; i++)
351  {
352  if(i == dev_num)
353  continue;
354 
356  {
358  }
359  }
360  return;
361 }
362 
374 void dlt_daemon_logstorage_update_application_loglevel(DltDaemon *daemon, int dev_num, int verbose)
375 {
376  DltLogStorage *handle = NULL;
377  int i = 0;
378  char key[DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN] = {'\0'};
379 
380 
381  PRINT_FUNCTION_VERBOSE(verbose);
382 
383  if((daemon == 0) || (dev_num < 0))
384  {
385  dlt_log(LOG_ERR, "Invalid function parameters used for dlt_daemon_logstorage_update_application_loglevel\n");
386  return;
387  }
388 
389  handle = &(daemon->storage_handle[dev_num]);
392  {
393  return;
394  }
395 
396  /* for all filters (keys) check if application or context already running and log level need to be updated*/
397  for(i = 0; i < handle->num_filter_keys; i++)
398  {
399  int log_level = -1;
400 
401  memset(key, 0, sizeof(key));
402 
403  strncpy(key, (handle->filter_keys + i * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN), DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN);
404 
405  /* Obtain storage configuration data */
406  log_level = dlt_logstorage_get_loglevel_by_key(handle, key);
407  if(log_level < 0)
408  {
409  dlt_log(LOG_ERR, "Failed to get log level by key \n");
410  return;
411  }
412 
413  /* Update context log level with storage configuration log level */
414  dlt_logstorage_update_context_loglevel(daemon, key, log_level, verbose);
415  }
416  return;
417 }
418 
431 int dlt_daemon_logstorage_get_loglevel(DltDaemon *daemon, int max_device, char *apid, char *ctid)
432 {
434  int i = 0;
435  int j = 0;
436  int8_t storage_loglevel = -1;
437  int8_t retrvd_loglevel = -1;
438  int num_config = 0;
439 
440  if((daemon == 0) || (max_device == 0) || (apid == NULL) || (ctid == NULL))
441  return -1;
442 
443  for(i = 0; i<max_device; i++)
444  {
446  {
447  config = dlt_logstorage_get_config(&(daemon->storage_handle[i]), apid, ctid, &num_config);
448  if(config != NULL)
449  {
450  for(j = 0; j<num_config; j++)
451  {
452  retrvd_loglevel = config[j]->log_level;
453  storage_loglevel = DLT_OFFLINE_LOGSTORAGE_MAX(retrvd_loglevel, storage_loglevel);
454  }
455  free(config);
456  }
457  }
458  }
459  return storage_loglevel;
460 }
461 
478  DltDaemonFlags *user_config,
479  unsigned char *data1,
480  int size1,
481  unsigned char *data2,
482  int size2,
483  unsigned char *data3,
484  int size3)
485 {
486  int i = 0;
487  DltLogStorageUserConfig file_config;
488 
489  if (daemon == NULL || (user_config->offlineLogstorageMaxDevices <= 0)
490  || data1 == NULL || data2 == NULL || data3 == NULL)
491  {
492  dlt_log(LOG_INFO,
493  "dlt_daemon_logstorage_write: message type is not log. "
494  "Skip storing.\n");
495  return;
496  }
497 
498  /* Copy user configuration */
499  file_config.logfile_timestamp = user_config->offlineLogstorageTimestamp;
500  file_config.logfile_delimiter = user_config->offlineLogstorageDelimiter;
501  file_config.logfile_maxcounter = user_config->offlineLogstorageMaxCounter;
502  file_config.logfile_counteridxlen =
503  user_config->offlineLogstorageMaxCounterIdx;
504 
505  for (i = 0; i < user_config->offlineLogstorageMaxDevices; i++)
506  {
507  if (daemon->storage_handle[i].config_status ==
509  {
510  if (dlt_logstorage_write(&(daemon->storage_handle[i]),
511  &file_config,
512  data1,
513  size1,
514  data2,
515  size2,
516  data3,
517  size3) != 0)
518  {
519  dlt_log(LOG_ERR,
520  "dlt_daemon_logstorage_write: failed. "
521  "Disable storage device\n");
522  /* DLT_OFFLINE_LOGSTORAGE_MAX_WRITE_ERRORS happened,
523  * therefore remove logstorage device */
525  &(daemon->storage_handle[i]),
527  }
528  }
529  }
530 }
531 
541 int dlt_daemon_logstorage_setup_internal_storage(DltDaemon *daemon, char *path, int verbose)
542 {
543  int ret = 0;
544 
545  if((path == NULL) || (daemon == NULL))
546  {
547  return -1;
548  }
549 
550  /* connect internal storage device */
551  /* Device index always used as 0 as it is setup on DLT daemon startup */
552  ret = dlt_logstorage_device_connected(&(daemon->storage_handle[0]), path);
553  if(ret != 0)
554  {
555  dlt_log(LOG_ERR,"dlt_daemon_logstorage_setup_emmc_support : Device connect failed\n");
556  return -1;
557  }
558 
559  /* setup logstorage with config file settings */
560  ret = dlt_logstorage_load_config(&(daemon->storage_handle[0]));
561  if(ret != 0)
562  {
563  dlt_log(LOG_ERR,"dlt_daemon_logstorage_setup_emmc_support : Loading configuration file failed\n");
564  return -1;
565  }
566 
567  /* check if log level of running application need an update */
569 
570  return ret;
571 }
572 
574 {
575  /* store given [KB] size in [Bytes] */
576  g_logstorage_cache_max = size * 1000;
577 }
578 
580  DltDaemonLocal *daemon_local,
581  int verbose)
582 {
583  int i = 0;
584 
585  PRINT_FUNCTION_VERBOSE(verbose);
586 
587  if (daemon == NULL || daemon_local == NULL)
588  {
589  return -1;
590  }
591 
592  for (i = 0; i < daemon_local->flags.offlineLogstorageMaxDevices; i++)
593  {
594  /* call disconnect on all currently connected devices */
595  if (daemon->storage_handle[i].connection_type ==
597  {
599  &daemon->storage_handle[i],
601  }
602  }
603 
604  return 0;
605 }
606 
608  DltDaemonLocal *daemon_local,
609  char *mnt_point,
610  int verbose)
611 {
612  int i = 0;
613  DltLogStorage *handle = NULL;
614 
615  PRINT_FUNCTION_VERBOSE(verbose);
616 
617  if (daemon == NULL || mnt_point == NULL)
618  {
619  return -1;
620  }
621 
622  if (strlen(mnt_point) > 0) /* mount point is given */
623  {
624  handle = dlt_daemon_logstorage_get_device(daemon,
625  daemon_local,
626  mnt_point,
627  verbose);
628  if (handle == NULL)
629  {
630  return -1;
631  }
632  else
633  {
634  if (dlt_logstorage_sync_caches(handle) != 0)
635  {
636  return -1;
637  }
638  }
639  }
640  else /* sync caches for all connected logstorage devices */
641  {
642  for(i=0; i < daemon_local->flags.offlineLogstorageMaxDevices; i++)
643  {
644  if (daemon->storage_handle[i].connection_type ==
646  {
647  if (dlt_logstorage_sync_caches(&daemon->storage_handle[i]) != 0)
648  {
649  return -1;
650  }
651  }
652  }
653  }
654 
655  return 0;
656 }
657 
659  DltDaemonLocal *daemon_local,
660  char *mnt_point,
661  int verbose)
662 {
663  int i = 0;
664  int len1 = 0;
665  int len2 = 0;
666 
667  PRINT_FUNCTION_VERBOSE(verbose);
668 
669  if (daemon == NULL || daemon_local == NULL || mnt_point == NULL)
670  {
671  return NULL;
672  }
673 
674  len1 = strlen(mnt_point);
675 
676  for(i = 0; i < daemon_local->flags.offlineLogstorageMaxDevices; i++)
677  {
678  len2 = strlen(daemon->storage_handle[i].device_mount_point);
679 
680  /* Check if the requested device path is already used as log storage
681  * device. Check for strlen first, to avoid comparison errors when
682  * final '/' is given or not */
683  if (strncmp(daemon->storage_handle[i].device_mount_point,
684  mnt_point,
685  len1 > len2 ? len2 : len1) == 0)
686  {
687  return &daemon->storage_handle[i];
688  }
689  }
690 
691  return NULL;
692 }
int dlt_logstorage_split_key(char *key, char *appid, char *ctxid)
#define PRINT_FUNCTION_VERBOSE(_verbose)
Definition: dlt_common.h:220
int dlt_logstorage_load_config(DltLogStorage *handle)
#define DLT_ID_SIZE
Definition: dlt_common.h:204
int dlt_logstorage_write(DltLogStorage *handle, DltLogStorageUserConfig *uconfig, unsigned char *data1, int size1, unsigned char *data2, int size2, unsigned char *data3, int size3)
DltKpiConfig config
Definition: dlt-kpi.c:37
void dlt_set_id(char *id, const char *text)
Definition: dlt_common.c:324
unsigned int connection_type
DltLogStorage * storage_handle
int dlt_daemon_logstorage_get_loglevel(DltDaemon *daemon, int max_device, char *apid, char *ctid)
unsigned int offlineLogstorageMaxCounterIdx
Definition: dlt-daemon.h:120
char offlineLogstorageDelimiter
Definition: dlt-daemon.h:118
int dlt_logstorage_device_disconnected(DltLogStorage *handle, int reason)
DltReturnValue dlt_log(int prio, char *s)
Definition: dlt_common.c:2029
#define DLT_DAEMON_LOGSTORAGE_RESET_SEND_LOGLEVEL
void dlt_daemon_logstorage_reset_application_loglevel(DltDaemon *daemon, int dev_num, int max_device, int verbose)
int dlt_logstorage_update_context(DltDaemon *daemon, char *apid, char *ctxid, int curr_log_level, int verbose)
void dlt_daemon_logstorage_write(DltDaemon *daemon, DltDaemonFlags *user_config, unsigned char *data1, int size1, unsigned char *data2, int size2, unsigned char *data3, int size3)
DltDaemonFlags flags
Definition: dlt-daemon.h:138
DltDaemonContext * contexts
char ctid[DLT_ID_SIZE]
DltLogStorage * dlt_daemon_logstorage_get_device(DltDaemon *daemon, DltDaemonLocal *daemon_local, char *mnt_point, int verbose)
#define DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED
#define DLT_OFFLINE_LOGSTORAGE_MAX(A, B)
void dlt_daemon_logstorage_set_logstorage_cache_size(unsigned int size)
DltLogStorageConfigData ** dlt_logstorage_get_config(DltLogStorage *handle, char *apid, char *ctid, int *num_config)
int dlt_logstorage_update_all_contexts(DltDaemon *daemon, char *id, int curr_log_level, int cmp_flag, int verbose)
int offlineLogstorageTimestamp
Definition: dlt-daemon.h:117
char apid[DLT_ID_SIZE]
char device_mount_point[DLT_MOUNT_PATH_MAX]
int dlt_daemon_user_send_log_level(DltDaemon *daemon, DltDaemonContext *context, int verbose)
unsigned int config_status
int offlineLogstorageMaxDevices
Definition: dlt-daemon.h:115
int dlt_logstorage_sync_caches(DltLogStorage *handle)
#define DLT_LOGSTORAGE_SYNC_ON_DEVICE_DISCONNECT
#define DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN
unsigned int g_logstorage_cache_max
unsigned int offlineLogstorageMaxCounter
Definition: dlt-daemon.h:119
#define DLT_LOGSTORAGE_SYNC_ON_DAEMON_EXIT
int dlt_daemon_logstorage_setup_internal_storage(DltDaemon *daemon, char *path, int verbose)
int dlt_logstorage_update_context_loglevel(DltDaemon *daemon, char *key, int curr_log_level, int verbose)
void dlt_daemon_logstorage_update_application_loglevel(DltDaemon *daemon, int dev_num, int verbose)
int dlt_logstorage_get_loglevel_by_key(DltLogStorage *handle, char *key)
int dlt_daemon_logstorage_cleanup(DltDaemon *daemon, DltDaemonLocal *daemon_local, int verbose)
int dlt_logstorage_device_connected(DltLogStorage *handle, char *mount_point)
#define DLT_DAEMON_LOGSTORAGE_RESET_LOGLEVEL
#define DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE
int dlt_daemon_logstorage_sync_cache(DltDaemon *daemon, DltDaemonLocal *daemon_local, char *mnt_point, int verbose)
DltDaemonContext * dlt_daemon_context_find(DltDaemon *daemon, char *apid, char *ctid, int verbose)
#define NULL
Definition: dlt_common.h:232