rllib  1
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
rlHistoryLogger Class Reference

#include <rlhistorylogger.h>

Collaboration diagram for rlHistoryLogger:
Collaboration graph
[legend]

Public Member Functions

 rlHistoryLogger (const char *csvName, int maxHoursPerFile, int maxLinesInMemory=100)
 
virtual ~rlHistoryLogger ()
 
int pushLine (const char *text)
 
const char * firstLine ()
 
const char * nextLine ()
 

Public Attributes

rlMutex mutex
 
int debug
 

Private Member Functions

int pushLineToMemory (const char *line)
 
int pushLineToFile (const char *line)
 
int openFile ()
 

Private Attributes

rlHistoryLogLinefirst_line
 
rlHistoryLogLinecurrent_line
 
rlTime time
 
rlTime file_start_time
 
rlTime time_diff
 
FILE * fout
 
int max_hours_per_file
 
int max_lines_in_memory
 
int current_file
 
char * csv_name
 
char * csv_file_name
 

Detailed Description

This class logs tab separated text including time stamp in 10 csv files + actual values in memory
This is for archiveing historical data with time stamp.
You should separate the text in pushLine with tab.

Definition at line 35 of file rlhistorylogger.h.

Constructor & Destructor Documentation

◆ rlHistoryLogger()

rlHistoryLogger::rlHistoryLogger ( const char *  csvName,
int  maxHoursPerFile,
int  maxLinesInMemory = 100 
)

Definition at line 20 of file rlhistorylogger.cpp.

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 }
void getLocalTime()
Definition: rltime.cpp:342
int year
Definition: rltime.h:53
int hour
Definition: rltime.h:56
int month
Definition: rltime.h:54
rlHistoryLogLine * current_line
int day
Definition: rltime.h:55
rlHistoryLogLine * first_line

◆ ~rlHistoryLogger()

rlHistoryLogger::~rlHistoryLogger ( )
virtual

Definition at line 47 of file rlhistorylogger.cpp.

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 }
int lock()
Definition: rlthread.cpp:105
_rlHistoryLogLine_ * next
rlHistoryLogLine * current_line
rlHistoryLogLine * first_line
int unlock()
Definition: rlthread.cpp:110

Member Function Documentation

◆ firstLine()

const char * rlHistoryLogger::firstLine ( )

Definition at line 181 of file rlhistorylogger.cpp.

182 {
183  if(first_line == NULL) return "";
185  return current_line->line;
186 }
rlHistoryLogLine * current_line
rlHistoryLogLine * first_line

◆ nextLine()

const char * rlHistoryLogger::nextLine ( )

Definition at line 188 of file rlhistorylogger.cpp.

189 {
190  if(current_line == NULL) return "";
192  if(current_line == NULL) return "";
193  return current_line->line;
194 }
_rlHistoryLogLine_ * next
rlHistoryLogLine * current_line

◆ openFile()

int rlHistoryLogger::openFile ( )
private

Definition at line 149 of file rlhistorylogger.cpp.

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 }
void getLocalTime()
Definition: rltime.cpp:342
int getFileModificationTime(const char *filename)
Definition: rltime.cpp:392
Definition: rltime.h:25

◆ pushLine()

int rlHistoryLogger::pushLine ( const char *  text)

Definition at line 71 of file rlhistorylogger.cpp.

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 }
const char * getTimeString()
Definition: rltime.cpp:106
void getLocalTime()
Definition: rltime.cpp:342
int lock()
Definition: rlthread.cpp:105
int pushLineToMemory(const char *line)
int unlock()
Definition: rlthread.cpp:110
int pushLineToFile(const char *line)

◆ pushLineToFile()

int rlHistoryLogger::pushLineToFile ( const char *  line)
private

Definition at line 132 of file rlhistorylogger.cpp.

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 }

◆ pushLineToMemory()

int rlHistoryLogger::pushLineToMemory ( const char *  line)
private

Definition at line 85 of file rlhistorylogger.cpp.

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 }
_rlHistoryLogLine_ * next
rlHistoryLogLine * current_line
rlHistoryLogLine * first_line
struct _rlHistoryLogLine_ rlHistoryLogLine

Member Data Documentation

◆ csv_file_name

char * rlHistoryLogger::csv_file_name
private

Definition at line 53 of file rlhistorylogger.h.

◆ csv_name

char* rlHistoryLogger::csv_name
private

Definition at line 53 of file rlhistorylogger.h.

◆ current_file

int rlHistoryLogger::current_file
private

Definition at line 52 of file rlhistorylogger.h.

◆ current_line

rlHistoryLogLine * rlHistoryLogger::current_line
private

Definition at line 49 of file rlhistorylogger.h.

◆ debug

int rlHistoryLogger::debug

Definition at line 44 of file rlhistorylogger.h.

◆ file_start_time

rlTime rlHistoryLogger::file_start_time
private

Definition at line 50 of file rlhistorylogger.h.

◆ first_line

rlHistoryLogLine* rlHistoryLogger::first_line
private

Definition at line 49 of file rlhistorylogger.h.

◆ fout

FILE* rlHistoryLogger::fout
private

Definition at line 51 of file rlhistorylogger.h.

◆ max_hours_per_file

int rlHistoryLogger::max_hours_per_file
private

Definition at line 52 of file rlhistorylogger.h.

◆ max_lines_in_memory

int rlHistoryLogger::max_lines_in_memory
private

Definition at line 52 of file rlhistorylogger.h.

◆ mutex

rlMutex rlHistoryLogger::mutex

Definition at line 43 of file rlhistorylogger.h.

◆ time

rlTime rlHistoryLogger::time
private

Definition at line 50 of file rlhistorylogger.h.

◆ time_diff

rlTime rlHistoryLogger::time_diff
private

Definition at line 50 of file rlhistorylogger.h.


The documentation for this class was generated from the following files: