Main Page | Namespace List | Alphabetical List | Class List | Directories | File List | Class Members | File Members

LogWriter.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   SpikeStream Library                                                   *
00003  *   Copyright (C) 2007 by David Gamez                                     *
00004  *   david@davidgamez.eu                                                   *
00005  *   Version 0.1                                                           *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 
00023 //SpikeStream includes
00024 #include "LogWriter.h"
00025 #include "Utilities.h"
00026 
00027 //Other includes
00028 #include <ctime>
00029 #include <iostream>
00030 using namespace std;
00031 
00032 
00033 /* Declare and initialise static variables. */
00034 int LogWriter::logLevel = 0; //default log level
00035 char LogWriter::logPath[255];
00036 
00037 
00038 //--------------------------------------------------------------------------
00039 //------------------------- PUBLIC STATIC METHODS --------------------------
00040 //--------------------------------------------------------------------------
00041 
00042 /*! Add log to the log file. */
00043 void LogWriter::addLog(char * data){
00044         if(logLevel != 0){
00045                 ofstream logOut(logPath, ios::app);
00046                 logOut<<data<<endl<<endl;
00047                 logOut.close();
00048         }
00049 }
00050 
00051 
00052 /*! Add log to the log file. */
00053 void LogWriter::addLog(char*  data1, char* data2){
00054         if(logLevel != 0){
00055                 ofstream logOut(logPath, ios::app);
00056                 logOut<<data1<<" "<<data2<<endl<<endl;
00057                 logOut.close();
00058         }
00059 }
00060 
00061 
00062 /*! Add log to the log file. */
00063 void LogWriter::addLog(const char*  data1, const char* data2){
00064         if(logLevel != 0){
00065                 ofstream logOut(logPath, ios::app);
00066                 logOut<<data1<<" "<<data2<<endl<<endl;
00067                 logOut.close();
00068         }
00069 }
00070 
00071 
00072 /*! Add log to the log file. */
00073 void LogWriter::addLog(string data){
00074         if(logLevel != 0){
00075                 ofstream logOut(logPath, ios::app);
00076                 logOut<<data<<endl<<endl;
00077                 logOut.close();
00078         }
00079 }
00080 
00081 
00082 /*! Add log to the log file. */
00083 void LogWriter::addLog(string data1, string data2){
00084         if(logLevel != 0){
00085                 ofstream logOut(logPath, ios::app);
00086                 logOut<<data1<<" "<<data2<<endl<<endl;
00087                 logOut.close();
00088         }
00089 }
00090 
00091 
00092 /*! Switche logging off. */
00093 void LogWriter::disableLogging(){
00094         logLevel = 0;
00095 }
00096 
00097 
00098 /*! Returns the log level. */
00099 int LogWriter::getLogLevel(){
00100         return logLevel;
00101 }
00102 
00103 
00104 /*! Returns true if logging is enabled. */
00105 bool LogWriter::loggingEnabled(){
00106         if(logLevel == 0)
00107                 return false;
00108         return true;
00109 }
00110 
00111 
00112 /*! Sets the log level. */
00113 void LogWriter::setLogLevel(int logLev){
00114         logLevel = logLev;
00115 }
00116 
00117 
00118 /*! Sets the location of the log path. */
00119 void LogWriter::setLogPath(const char* lp){
00120         Utilities::safeCStringCopy(logPath, lp, 100);
00121 }
00122 
00123 
00124 /*! Writes the date and time to the log. */
00125 void LogWriter::writeDate(){
00126         if(logLevel != 0)//Check to see if logging is enabled
00127                 return;
00128                 
00129         //Get the time and date
00130         time_t timeNow = time(NULL);
00131         struct tm *locTm = localtime(&timeNow);
00132         
00133         //Write the time and date
00134         ofstream logOut(logPath, ios::app);//Should create a new file if one does not exist already
00135         if(!logOut){//No point in throwing an exception since this is a non critical function
00136                 cout<<"No log file available. Logging disabled."<<endl;
00137                 logLevel = 0;
00138                 return;
00139         }
00140         logOut<<locTm->tm_mday<<"/"<<(locTm->tm_mon +1)<<"/"<<(locTm->tm_year + 1900)<<" "<<locTm->tm_hour<<":"<<locTm->tm_min<<":"<<locTm->tm_sec<<endl;
00141 }
00142 

Generated on Mon Sep 3 22:18:50 2007 for SpikeStream Library by  doxygen 1.4.4