rllib  1
rlhistorylogger.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlhistorylogger.cpp - description
3  -------------------
4  begin : Wed Dec 06 2006
5  copyright : (C) 2006 by R. Lehrig
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include "rlhistorylogger.h"
17 #include "rlcutil.h"
18 #include <string.h>
19 
20 rlHistoryLogger::rlHistoryLogger(const char *csvName, int maxHoursPerFile, int maxLinesInMemory)
21 {
22  int val;
23  debug = 0;
24  first_line = current_line = NULL;
25  fout = NULL;
26  max_hours_per_file = maxHoursPerFile;
28  val = max_hours_per_file;
29  time_diff.hour = val % 24;
30  val = val / 24;
31  time_diff.day = val % 31; // we are on the save side if we assume a month with 31 days
32  val = val / 31;
33  time_diff.month = val % 12;
34  val = val / 12;
35  time_diff.year = val;
36  max_lines_in_memory = maxLinesInMemory;
38  current_file = -1;
39  csv_name = new char[strlen(csvName)+1];
40  strcpy(csv_name,csvName);
41  csv_file_name = new char[strlen(csvName)+132];
44  openFile(); // jun 2021, hint by george zempekis
45 }
46 
48 {
49  mutex.lock();
50  if(fout != NULL) fclose(fout);
51  delete [] csv_name;
52  delete [] csv_file_name;
53  if(first_line != NULL)
54  {
55  rlHistoryLogLine *last_line;
57  while(current_line != NULL)
58  {
59  last_line = current_line;
61  if(last_line != NULL)
62  {
63  delete [] last_line->line;
64  delete last_line;
65  }
66  }
67  }
68  mutex.unlock();
69 }
70 
71 int rlHistoryLogger::pushLine(const char *text)
72 {
73  mutex.lock();
75  char *line = new char[strlen(text)+132];
76  sprintf(line,"%s\t%s",time.getTimeString(),text);
77  if(debug) printf("pushLine=%s\n",line);
78  pushLineToMemory(line);
79  pushLineToFile(line);
80  delete [] line;
81  mutex.unlock();
82  return 0;
83 }
84 
85 int rlHistoryLogger::pushLineToMemory(const char *line)
86 {
87  rlHistoryLogLine *history_line;
88 
89  // put line at 1 position
90  if(first_line == NULL)
91  {
93  first_line->line = new char[strlen(line)+1];
94  strcpy(first_line->line,line);
95  first_line->next = NULL;
96  }
97  else
98  {
99  history_line = first_line;
101  first_line->line = new char[strlen(line)+1];
102  strcpy(first_line->line,line);
103  first_line->next = history_line;
104  }
105 
106  // limit tail of list
107  history_line = first_line;
108  for(int i=0; i<max_lines_in_memory; i++)
109  {
110  if(history_line == NULL) break;
111  history_line = history_line->next;
112  }
113  if(history_line != NULL)
114  {
115  rlHistoryLogLine *last_line;
116  current_line = history_line->next;
117  while(current_line != NULL)
118  {
119  last_line = current_line;
121  if(last_line != NULL)
122  {
123  delete [] last_line->line;
124  delete last_line;
125  }
126  }
127  history_line->next = NULL;
128  }
129  return 0;
130 }
131 
132 int rlHistoryLogger::pushLineToFile(const char *line)
133 {
134  if(fout == NULL) openFile();
135  if((file_start_time + time_diff) < time)
136  {
137  if(fout != NULL) fclose(fout);
138  fout = NULL;
139  openFile();
140  }
141  if(fout != NULL)
142  {
143  fprintf(fout,"%s\n",line);
144  fflush(fout);
145  }
146  return 0;
147 }
148 
150 {
151  if(current_file == -1)
152  {
153  // find oldest file and open it for writing
154  int i_oldest = 0;
155  rlTime t,t_oldest;
156  t_oldest.getLocalTime(); // this must be newer that any file time
157  for(int i=0; i<10; i++)
158  {
159  sprintf(csv_file_name,"%s%d.csv",csv_name,i);
161  {
162  if(t < t_oldest) i_oldest = i;
163  }
164  }
165  current_file = i_oldest;
166  sprintf(csv_file_name,"%s%d.csv",csv_name,i_oldest);
167  fout = fopen(csv_file_name,"w");
168  }
169  else
170  {
171  // open next file for writing
172  current_file++;
173  if(current_file >= 10) current_file = 0;
174  sprintf(csv_file_name,"%s%d.csv",csv_name,current_file);
175  fout = fopen(csv_file_name,"w");
176  }
178  return 0;
179 }
180 
182 {
183  if(first_line == NULL) return "";
185  return current_line->line;
186 }
187 
189 {
190  if(current_line == NULL) return "";
192  if(current_line == NULL) return "";
193  return current_line->line;
194 }
195 
196 //#define TESTING_HISTORYLOGGER
197 #ifdef TESTING_HISTORYLOGGER
198 int main()
199 {
200  char text[1024];
201  int val;
202  rlHistoryLogger logger("test", 1);
203  logger.debug = 1;
204  val = 0;
205  while(val >= 0)
206  {
207  if((val % 10) == 0)
208  {
209  const char *cptr;
210  logger.mutex.lock();
211  cptr = logger.firstLine();
212  while(*cptr != '\0')
213  {
214  printf("mem=%s\n",cptr);
215  cptr = logger.nextLine();
216  }
217  logger.mutex.unlock();
218  }
219  sprintf(text,"%d\t%d\t%d",val,val+1,val+2);
220  logger.pushLine(text);
221  val++;
222  rlsleep(1000);
223  }
224  return 0;
225 }
226 #endif
const char * getTimeString()
Definition: rltime.cpp:106
void getLocalTime()
Definition: rltime.cpp:342
int year
Definition: rltime.h:53
int hour
Definition: rltime.h:56
int lock()
Definition: rlthread.cpp:105
_rlHistoryLogLine_ * next
int month
Definition: rltime.h:54
const char * firstLine()
virtual ~rlHistoryLogger()
rlHistoryLogLine * current_line
int day
Definition: rltime.h:55
rlHistoryLogLine * first_line
int pushLineToMemory(const char *line)
int main()
Definition: rlcorba.cpp:18
int pushLine(const char *text)
void rlsleep(long msec)
Definition: rlwthread.cpp:396
int unlock()
Definition: rlthread.cpp:110
int getFileModificationTime(const char *filename)
Definition: rltime.cpp:392
rlHistoryLogger(const char *csvName, int maxHoursPerFile, int maxLinesInMemory=100)
struct _rlHistoryLogLine_ rlHistoryLogLine
int pushLineToFile(const char *line)
Definition: rltime.h:25
const char * nextLine()