rllib  1
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
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.

{
  int val;
  debug = 0;
  first_line = current_line = NULL;
  fout = NULL;
  max_hours_per_file = maxHoursPerFile;
  if(max_hours_per_file <= 0) max_hours_per_file = 1;
  val = max_hours_per_file;
  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;
  if(max_lines_in_memory <= 0) max_lines_in_memory = 1;
  current_file = -1;
  csv_name = new char[strlen(csvName)+1];
  strcpy(csv_name,csvName);
  csv_file_name = new char[strlen(csvName)+132];
  time.getLocalTime();
  file_start_time.getLocalTime();
}
rlHistoryLogger::~rlHistoryLogger ( ) [virtual]

Definition at line 46 of file rlhistorylogger.cpp.

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

Member Function Documentation

const char * rlHistoryLogger::firstLine ( )

Definition at line 180 of file rlhistorylogger.cpp.

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

Definition at line 187 of file rlhistorylogger.cpp.

{
  if(current_line == NULL) return "";
  current_line = current_line->next;
  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.getFileModificationTime(csv_file_name) == 0)
      {
        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
    current_file++;
    if(current_file >= 10) current_file = 0;
    sprintf(csv_file_name,"%s%d.csv",csv_name,current_file);
    fout = fopen(csv_file_name,"w");
  }
  file_start_time.getLocalTime();
  return 0;
}
int rlHistoryLogger::pushLine ( const char *  text)

Definition at line 70 of file rlhistorylogger.cpp.

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

Definition at line 131 of file rlhistorylogger.cpp.

{
  if(fout == NULL) openFile();
  if((file_start_time + time_diff) < time)
  {
    if(fout != NULL) fclose(fout);
    fout = NULL;
    openFile();
  }
  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 = new rlHistoryLogLine;
    first_line->line = new char[strlen(line)+1];
    strcpy(first_line->line,line);
    first_line->next = NULL;
  }
  else
  {
    history_line = first_line;
    first_line = new rlHistoryLogLine;
    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;
      current_line = current_line->next;
      if(last_line != NULL)
      {
        delete [] last_line->line;
        delete last_line;
      }
    }
    history_line->next = NULL;
  }
  return 0;
}

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: