rlHistoryLogger Class Reference

#include <rlhistorylogger.h>

Collaboration diagram for rlHistoryLogger:
Collaboration graph
[legend]

List of all members.

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 ( const char *  csvName,
int  maxHoursPerFile,
int  maxLinesInMemory = 100 
)

Definition at line 20 of file rlhistorylogger.cpp.

00021 {
00022   int val;
00023   debug = 0;
00024   first_line = current_line = NULL;
00025   fout = NULL;
00026   max_hours_per_file = maxHoursPerFile;
00027   if(max_hours_per_file <= 0) max_hours_per_file = 1;
00028   val = max_hours_per_file;
00029   time_diff.hour  = val % 24;
00030   val = val / 24;
00031   time_diff.day   = val % 31; // we are on the save side if we assume a month with 31 days
00032   val = val / 31;
00033   time_diff.month = val % 12;
00034   val = val / 12;
00035   time_diff.year  = val;
00036   max_lines_in_memory = maxLinesInMemory;
00037   if(max_lines_in_memory <= 0) max_lines_in_memory = 1;
00038   current_file = -1;
00039   csv_name = new char[strlen(csvName)+1];
00040   strcpy(csv_name,csvName);
00041   csv_file_name = new char[strlen(csvName)+132];
00042   time.getLocalTime();
00043   file_start_time.getLocalTime();
00044 }

rlHistoryLogger::~rlHistoryLogger (  )  [virtual]

Definition at line 46 of file rlhistorylogger.cpp.

00047 {
00048   mutex.lock();
00049   if(fout != NULL) fclose(fout);
00050   delete [] csv_name;
00051   delete [] csv_file_name;
00052   if(first_line != NULL)
00053   {
00054     rlHistoryLogLine *last_line;
00055     current_line = first_line;
00056     while(current_line != NULL)
00057     {
00058       last_line = current_line;
00059       current_line = current_line->next;
00060       if(last_line != NULL)
00061       {
00062         delete [] last_line->line;
00063         delete last_line;
00064       }
00065     }
00066   }
00067   mutex.unlock();
00068 }


Member Function Documentation

const char * rlHistoryLogger::firstLine (  ) 

Definition at line 180 of file rlhistorylogger.cpp.

00181 {
00182   if(first_line == NULL) return "";
00183   current_line = first_line;
00184   return current_line->line;
00185 }

const char * rlHistoryLogger::nextLine (  ) 

Definition at line 187 of file rlhistorylogger.cpp.

00188 {
00189   if(current_line == NULL) return "";
00190   current_line = current_line->next;
00191   if(current_line == NULL) return "";
00192   return current_line->line;
00193 }

int rlHistoryLogger::openFile (  )  [private]

Definition at line 148 of file rlhistorylogger.cpp.

00149 {
00150   if(current_file == -1)
00151   {
00152     // find oldest file and open it for writing
00153     int i_oldest = 0;
00154     rlTime t,t_oldest;
00155     t_oldest.getLocalTime(); // this must be newer that any file time
00156     for(int i=0; i<10; i++)
00157     {
00158       sprintf(csv_file_name,"%s%d.csv",csv_name,i);
00159       if(t.getFileModificationTime(csv_file_name) == 0)
00160       {
00161         if(t < t_oldest) i_oldest = i;
00162       }
00163     }
00164     current_file = i_oldest;
00165     sprintf(csv_file_name,"%s%d.csv",csv_name,i_oldest);
00166     fout = fopen(csv_file_name,"w");
00167   }
00168   else
00169   {
00170     // open next file for writing
00171     current_file++;
00172     if(current_file >= 10) current_file = 0;
00173     sprintf(csv_file_name,"%s%d.csv",csv_name,current_file);
00174     fout = fopen(csv_file_name,"w");
00175   }
00176   file_start_time.getLocalTime();
00177   return 0;
00178 }

int rlHistoryLogger::pushLine ( const char *  text  ) 

Definition at line 70 of file rlhistorylogger.cpp.

00071 {
00072   mutex.lock();
00073   time.getLocalTime();
00074   char *line = new char[strlen(text)+132];
00075   sprintf(line,"%s\t%s",time.getTimeString(),text);
00076   if(debug) printf("pushLine=%s\n",line);
00077   pushLineToMemory(line);
00078   pushLineToFile(line);
00079   delete [] line;
00080   mutex.unlock();
00081   return 0;
00082 }

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

Definition at line 131 of file rlhistorylogger.cpp.

00132 {
00133   if(fout == NULL) openFile();
00134   if((file_start_time + time_diff) < time)
00135   {
00136     if(fout != NULL) fclose(fout);
00137     fout = NULL;
00138     openFile();
00139   }
00140   if(fout != NULL)
00141   {
00142     fprintf(fout,"%s\n",line);
00143     fflush(fout);
00144   }
00145   return 0;
00146 }

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

Definition at line 84 of file rlhistorylogger.cpp.

00085 {
00086   rlHistoryLogLine *history_line;
00087 
00088   // put line at 1 position
00089   if(first_line == NULL)
00090   {
00091     first_line = new rlHistoryLogLine;
00092     first_line->line = new char[strlen(line)+1];
00093     strcpy(first_line->line,line);
00094     first_line->next = NULL;
00095   }
00096   else
00097   {
00098     history_line = first_line;
00099     first_line = new rlHistoryLogLine;
00100     first_line->line = new char[strlen(line)+1];
00101     strcpy(first_line->line,line);
00102     first_line->next = history_line;
00103   }
00104 
00105   // limit tail of list
00106   history_line = first_line;
00107   for(int i=0; i<max_lines_in_memory; i++)
00108   {
00109     if(history_line == NULL) break;
00110     history_line = history_line->next;
00111   }
00112   if(history_line != NULL)
00113   {
00114     rlHistoryLogLine *last_line;
00115     current_line = history_line->next;
00116     while(current_line != NULL)
00117     {
00118       last_line = current_line;
00119       current_line = current_line->next;
00120       if(last_line != NULL)
00121       {
00122         delete [] last_line->line;
00123         delete last_line;
00124       }
00125     }
00126     history_line->next = NULL;
00127   }
00128   return 0;
00129 }


Member Data Documentation

Definition at line 53 of file rlhistorylogger.h.

char* rlHistoryLogger::csv_name [private]

Definition at line 53 of file rlhistorylogger.h.

Definition at line 52 of file rlhistorylogger.h.

Definition at line 49 of file rlhistorylogger.h.

Definition at line 44 of file rlhistorylogger.h.

Definition at line 50 of file rlhistorylogger.h.

Definition at line 49 of file rlhistorylogger.h.

FILE* rlHistoryLogger::fout [private]

Definition at line 51 of file rlhistorylogger.h.

Definition at line 52 of file rlhistorylogger.h.

Definition at line 52 of file rlhistorylogger.h.

Definition at line 43 of file rlhistorylogger.h.

Definition at line 50 of file rlhistorylogger.h.

Definition at line 50 of file rlhistorylogger.h.


The documentation for this class was generated from the following files:
Generated on Mon Aug 30 20:16:16 2010 for rllib by  doxygen 1.6.3