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 }
45 
47 {
48  mutex.lock();
49  if(fout != NULL) fclose(fout);
50  delete [] csv_name;
51  delete [] csv_file_name;
52  if(first_line != NULL)
53  {
54  rlHistoryLogLine *last_line;
56  while(current_line != NULL)
57  {
58  last_line = current_line;
60  if(last_line != NULL)
61  {
62  delete [] last_line->line;
63  delete last_line;
64  }
65  }
66  }
67  mutex.unlock();
68 }
69 
70 int rlHistoryLogger::pushLine(const char *text)
71 {
72  mutex.lock();
74  char *line = new char[strlen(text)+132];
75  sprintf(line,"%s\t%s",time.getTimeString(),text);
76  if(debug) printf("pushLine=%s\n",line);
77  pushLineToMemory(line);
78  pushLineToFile(line);
79  delete [] line;
80  mutex.unlock();
81  return 0;
82 }
83 
84 int rlHistoryLogger::pushLineToMemory(const char *line)
85 {
86  rlHistoryLogLine *history_line;
87 
88  // put line at 1 position
89  if(first_line == NULL)
90  {
92  first_line->line = new char[strlen(line)+1];
93  strcpy(first_line->line,line);
94  first_line->next = NULL;
95  }
96  else
97  {
98  history_line = first_line;
100  first_line->line = new char[strlen(line)+1];
101  strcpy(first_line->line,line);
102  first_line->next = history_line;
103  }
104 
105  // limit tail of list
106  history_line = first_line;
107  for(int i=0; i<max_lines_in_memory; i++)
108  {
109  if(history_line == NULL) break;
110  history_line = history_line->next;
111  }
112  if(history_line != NULL)
113  {
114  rlHistoryLogLine *last_line;
115  current_line = history_line->next;
116  while(current_line != NULL)
117  {
118  last_line = current_line;
120  if(last_line != NULL)
121  {
122  delete [] last_line->line;
123  delete last_line;
124  }
125  }
126  history_line->next = NULL;
127  }
128  return 0;
129 }
130 
131 int rlHistoryLogger::pushLineToFile(const char *line)
132 {
133  if(fout == NULL) openFile();
134  if((file_start_time + time_diff) < time)
135  {
136  if(fout != NULL) fclose(fout);
137  fout = NULL;
138  openFile();
139  }
140  if(fout != NULL)
141  {
142  fprintf(fout,"%s\n",line);
143  fflush(fout);
144  }
145  return 0;
146 }
147 
149 {
150  if(current_file == -1)
151  {
152  // find oldest file and open it for writing
153  int i_oldest = 0;
154  rlTime t,t_oldest;
155  t_oldest.getLocalTime(); // this must be newer that any file time
156  for(int i=0; i<10; i++)
157  {
158  sprintf(csv_file_name,"%s%d.csv",csv_name,i);
160  {
161  if(t < t_oldest) i_oldest = i;
162  }
163  }
164  current_file = i_oldest;
165  sprintf(csv_file_name,"%s%d.csv",csv_name,i_oldest);
166  fout = fopen(csv_file_name,"w");
167  }
168  else
169  {
170  // open next file for writing
171  current_file++;
172  if(current_file >= 10) current_file = 0;
173  sprintf(csv_file_name,"%s%d.csv",csv_name,current_file);
174  fout = fopen(csv_file_name,"w");
175  }
177  return 0;
178 }
179 
181 {
182  if(first_line == NULL) return "";
184  return current_line->line;
185 }
186 
188 {
189  if(current_line == NULL) return "";
191  if(current_line == NULL) return "";
192  return current_line->line;
193 }
194 
195 //#define TESTING_HISTORYLOGGER
196 #ifdef TESTING_HISTORYLOGGER
197 int main()
198 {
199  char text[1024];
200  int val;
201  rlHistoryLogger logger("test", 1);
202  logger.debug = 1;
203  val = 0;
204  while(val >= 0)
205  {
206  if((val % 10) == 0)
207  {
208  const char *cptr;
209  logger.mutex.lock();
210  cptr = logger.firstLine();
211  while(*cptr != '\0')
212  {
213  printf("mem=%s\n",cptr);
214  cptr = logger.nextLine();
215  }
216  logger.mutex.unlock();
217  }
218  sprintf(text,"%d\t%d\t%d",val,val+1,val+2);
219  logger.pushLine(text);
220  val++;
221  rlsleep(1000);
222  }
223  return 0;
224 }
225 #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()