rllib  1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
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 ( const char *  csvName,
int  maxHoursPerFile,
int  maxLinesInMemory = 100 
)

Definition at line 20 of file rlhistorylogger.cpp.

{
int val;
debug = 0;
fout = NULL;
max_hours_per_file = maxHoursPerFile;
time_diff.hour = val % 24;
val = val / 24;
time_diff.day = val % 31; // we are on the save side if we assume a month with 31 days
val = val / 31;
time_diff.month = val % 12;
val = val / 12;
time_diff.year = val;
max_lines_in_memory = maxLinesInMemory;
csv_name = new char[strlen(csvName)+1];
strcpy(csv_name,csvName);
csv_file_name = new char[strlen(csvName)+132];
}
rlHistoryLogger::~rlHistoryLogger ( )
virtual

Definition at line 46 of file rlhistorylogger.cpp.

{
if(fout != NULL) fclose(fout);
delete [] csv_name;
delete [] csv_file_name;
if(first_line != NULL)
{
rlHistoryLogLine *last_line;
while(current_line != NULL)
{
last_line = current_line;
if(last_line != NULL)
{
delete [] last_line->line;
delete last_line;
}
}
}
}

Member Function Documentation

const char * rlHistoryLogger::firstLine ( )

Definition at line 180 of file rlhistorylogger.cpp.

{
if(first_line == NULL) return "";
return current_line->line;
}
const char * rlHistoryLogger::nextLine ( )

Definition at line 187 of file rlhistorylogger.cpp.

{
if(current_line == NULL) return "";
if(current_line == NULL) return "";
return current_line->line;
}
int rlHistoryLogger::openFile ( )
private

Definition at line 148 of file rlhistorylogger.cpp.

{
if(current_file == -1)
{
// find oldest file and open it for writing
int i_oldest = 0;
rlTime t,t_oldest;
t_oldest.getLocalTime(); // this must be newer that any file time
for(int i=0; i<10; i++)
{
sprintf(csv_file_name,"%s%d.csv",csv_name,i);
{
if(t < t_oldest) i_oldest = i;
}
}
current_file = i_oldest;
sprintf(csv_file_name,"%s%d.csv",csv_name,i_oldest);
fout = fopen(csv_file_name,"w");
}
else
{
// open next file for writing
if(current_file >= 10) current_file = 0;
sprintf(csv_file_name,"%s%d.csv",csv_name,current_file);
fout = fopen(csv_file_name,"w");
}
return 0;
}
int rlHistoryLogger::pushLine ( const char *  text)

Definition at line 70 of file rlhistorylogger.cpp.

{
char *line = new char[strlen(text)+132];
sprintf(line,"%s\t%s",time.getTimeString(),text);
if(debug) printf("pushLine=%s\n",line);
delete [] line;
return 0;
}
int rlHistoryLogger::pushLineToFile ( const char *  line)
private

Definition at line 131 of file rlhistorylogger.cpp.

{
if(fout == NULL) openFile();
{
if(fout != NULL) fclose(fout);
fout = NULL;
}
if(fout != NULL)
{
fprintf(fout,"%s\n",line);
fflush(fout);
}
return 0;
}
int rlHistoryLogger::pushLineToMemory ( const char *  line)
private

Definition at line 84 of file rlhistorylogger.cpp.

{
rlHistoryLogLine *history_line;
// put line at 1 position
if(first_line == NULL)
{
first_line->line = new char[strlen(line)+1];
strcpy(first_line->line,line);
first_line->next = NULL;
}
else
{
history_line = first_line;
first_line->line = new char[strlen(line)+1];
strcpy(first_line->line,line);
first_line->next = history_line;
}
// limit tail of list
history_line = first_line;
for(int i=0; i<max_lines_in_memory; i++)
{
if(history_line == NULL) break;
history_line = history_line->next;
}
if(history_line != NULL)
{
rlHistoryLogLine *last_line;
current_line = history_line->next;
while(current_line != NULL)
{
last_line = current_line;
if(last_line != NULL)
{
delete [] last_line->line;
delete last_line;
}
}
history_line->next = NULL;
}
return 0;
}

Member Data Documentation

char * rlHistoryLogger::csv_file_name
private

Definition at line 53 of file rlhistorylogger.h.

char* rlHistoryLogger::csv_name
private

Definition at line 53 of file rlhistorylogger.h.

int rlHistoryLogger::current_file
private

Definition at line 52 of file rlhistorylogger.h.

rlHistoryLogLine * rlHistoryLogger::current_line
private

Definition at line 49 of file rlhistorylogger.h.

int rlHistoryLogger::debug

Definition at line 44 of file rlhistorylogger.h.

rlTime rlHistoryLogger::file_start_time
private

Definition at line 50 of file rlhistorylogger.h.

rlHistoryLogLine* rlHistoryLogger::first_line
private

Definition at line 49 of file rlhistorylogger.h.

FILE* rlHistoryLogger::fout
private

Definition at line 51 of file rlhistorylogger.h.

int rlHistoryLogger::max_hours_per_file
private

Definition at line 52 of file rlhistorylogger.h.

int rlHistoryLogger::max_lines_in_memory
private

Definition at line 52 of file rlhistorylogger.h.

rlMutex rlHistoryLogger::mutex

Definition at line 43 of file rlhistorylogger.h.

rlTime rlHistoryLogger::time
private

Definition at line 50 of file rlhistorylogger.h.

rlTime rlHistoryLogger::time_diff
private

Definition at line 50 of file rlhistorylogger.h.


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