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 }
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 46 of file rlhistorylogger.cpp.

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 }
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 180 of file rlhistorylogger.cpp.

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

◆ nextLine()

const char * rlHistoryLogger::nextLine ( )

Definition at line 187 of file rlhistorylogger.cpp.

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

◆ openFile()

int rlHistoryLogger::openFile ( )
private

Definition at line 148 of file rlhistorylogger.cpp.

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 }
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 70 of file rlhistorylogger.cpp.

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 }
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 131 of file rlhistorylogger.cpp.

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 }

◆ pushLineToMemory()

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

Definition at line 84 of file rlhistorylogger.cpp.

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 }
_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: