automotive-dlt
dlt-system-logfile.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2011-2015, BMW AG
6  *
7  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8  *
9  * This Source Code Form is subject to the terms of the
10  * Mozilla Public License (MPL), v. 2.0.
11  * If a copy of the MPL was not distributed with this file,
12  * You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  * For further information see http://www.genivi.org/.
15  * @licence end@
16  */
17 
27 /*******************************************************************************
28 ** **
29 ** SRC-MODULE: dlt-system-logfile.c **
30 ** **
31 ** TARGET : linux **
32 ** **
33 ** PROJECT : DLT **
34 ** **
35 ** AUTHOR : Lassi Marttala <lassi.lm.marttala@partner.bmw.de> **
36 ** Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
37 ** **
38 ** PURPOSE : **
39 ** **
40 ** REMARKS : **
41 ** **
42 ** PLATFORM DEPENDANT [yes/no]: yes **
43 ** **
44 ** TO BE CHANGED BY USER [yes/no]: no **
45 ** **
46 *******************************************************************************/
47 
48 
49 #include <pthread.h>
50 #include <unistd.h>
51 #include "dlt-system.h"
52 
53 // Modes of sending
54 #define SEND_MODE_OFF 0
55 #define SEND_MODE_ONCE 1
56 #define SEND_MODE_ON 2
57 
59 
61 DltContext logfileContext[DLT_SYSTEM_LOG_FILE_MAX];
62 
63 void send_file(LogFileOptions const *fileopt, int n)
64 {
65  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
66  DLT_STRING("dlt-system-logfile, sending file."));
67  FILE * pFile;
68  DltContext context = logfileContext[n];
69  char buffer[1024];
70  int bytes;
71  int seq = 1;
72 
73  pFile = fopen((*fileopt).Filename[n],"r");
74 
75  if(pFile != NULL)
76  {
77  while (!feof(pFile)) {
78  bytes = fread(buffer,1,sizeof(buffer)-1,pFile);
79  if(bytes>=0)
80  buffer[bytes] = 0;
81  else
82  buffer[0] = 0;
83 
84  if(feof(pFile)) {
85  DLT_LOG(context, DLT_LOG_INFO, DLT_INT(seq*-1), DLT_STRING(buffer));
86  break;
87  }
88  else {
89  DLT_LOG(context, DLT_LOG_INFO, DLT_INT(seq++), DLT_STRING(buffer));
90  }
91  }
92  fclose(pFile);
93  }
94  else
95  {
96  DLT_LOG(dltsystem, DLT_LOG_ERROR,
97  DLT_STRING("dlt-system-logfile, failed to open file."),
98  DLT_STRING((*fileopt).Filename[n]));
99  }
100 }
101 
102 void register_contexts(LogFileOptions const *fileopts)
103 {
104  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
105  DLT_STRING("dlt-system-logfile, registering file contexts."));
106  int i;
107  for(i = 0;i < (*fileopts).Count;i++)
108  {
109  DLT_REGISTER_CONTEXT(logfileContext[i], (*fileopts).ContextId[i],
110  (*fileopts).Filename[i]);
111  }
112 }
113 
114 void logfile_thread(void *v_conf)
115 {
116  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
117  DLT_STRING("dlt-system-logfile, in thread."));
119 
120  register_contexts(&(conf->LogFile));
121 
122  int logfile_delays[DLT_SYSTEM_LOG_FILE_MAX];
123  int i;
124  for(i = 0;i < conf->LogFile.Count;i++)
125  logfile_delays[i] = conf->LogFile.TimeDelay[i];
126 
127  while(!threads.shutdown)
128  {
129  sleep(1);
130  for(i = 0;i < conf->LogFile.Count;i++)
131  {
132  if(conf->LogFile.Mode[i] == SEND_MODE_OFF)
133  continue;
134 
135  if(logfile_delays[i] <= 0)
136  {
137  send_file(&(conf->LogFile), i);
138  logfile_delays[i] = conf->LogFile.TimeDelay[i];
139  if(conf->LogFile.Mode[i] == SEND_MODE_ONCE)
140  conf->LogFile.Mode[i] = SEND_MODE_OFF;
141  }
142  else
143  {
144  logfile_delays[i]--;
145  }
146  }
147  }
148 }
149 
151 {
152  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
153  DLT_STRING("dlt-system-logfile, starting."));
154  DLT_LOG(dltsystem,DLT_LOG_DEBUG,DLT_STRING("Starting thread for logfile"));
155  static pthread_attr_t t_attr;
156  static pthread_t pt;
157  pthread_create(&pt, &t_attr, (void *)logfile_thread, conf);
158  threads.threads[threads.count++] = pt;
159 }
int Mode[DLT_SYSTEM_LOG_FILE_MAX]
Definition: dlt-system.h:136
void start_logfile(DltSystemConfiguration *conf)
#define SEND_MODE_ONCE
#define DLT_INT(INT_VAR)
void logfile_thread(void *v_conf)
void register_contexts(LogFileOptions const *fileopts)
LogFileOptions LogFile
Definition: dlt-system.h:158
#define SEND_MODE_OFF
#define DLT_STRING(TEXT)
unsigned char buffer[BUFFER_SIZE]
Buffer for dlt file transfer. The size is defined by BUFFER_SIZE.
#define DLT_REGISTER_CONTEXT(CONTEXT, CONTEXTID, DESCRIPTION)
#define DLT_LOG(CONTEXT, LOGLEVEL, ARGS...)
DltSystemThreads threads
int TimeDelay[DLT_SYSTEM_LOG_FILE_MAX]
Definition: dlt-system.h:137
DLT_IMPORT_CONTEXT(dltsystem)
pthread_t threads[MAX_THREADS]
Definition: dlt-system.h:163
#define NULL
Definition: dlt_common.h:232
#define DLT_SYSTEM_LOG_FILE_MAX
Definition: dlt-system.h:65