Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members

FXLogger.h

Go to the documentation of this file.
00001 /*********************************************************************************
00002 *                                                                               *
00003 *                  Log file object                                              *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2003 by Mathew Robertson.   All Rights Reserved.                *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (at your option) any later version.            *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************/
00022 #ifndef FXLOGGER_H
00023 #define FXLOGGER_H
00024 
00025 #ifndef FXFILESTREAM_H
00026 #include <fox/FXStream.h>
00027 using namespace FX;
00028 #endif
00029 namespace FXEX {
00030 
00031 /**
00032  * Simple defines, used to simplify the logging syntax
00033  * Takes FXString's, FXchar*'s or FXException's
00034  */
00035 #ifndef NDEBUG
00036 #  define DBG(argument)     FXLogger::instance().debug  (argument)
00037 #  define LOG(argument)     FXLogger::instance().log    (argument)
00038 #  define WRN(argument)     FXLogger::instance().warning(argument)
00039 #  define ERR(argument)     FXLogger::instance().error  (argument)
00040 #  define STATUS(arg1,arg2) FXLogger::instance().status (arg1,arg2)
00041 #else
00042 #  define DBG(argument)
00043 #  define LOG(argument)
00044 #  define WRN(argument)
00045 #  define ERR(argument)
00046 #  define STATUS(arg1,arg2)
00047 #endif
00048 
00049 /**
00050  * A log file manager to allow applications to log entries to a file.
00051  *
00052  * The good stuff:
00053  * - 'stream'ing works (mostly), but it is not automatically new-line terminated (use endl)
00054  * - Use the #define's (LOG,DBG,WRN,ERR,STATUS) to make application logging simple
00055  * - Logging of exceptions are supported
00056  * - No need to add a newline to the end of the string, though you can if you wish.
00057  *
00058  * The bad stuff:
00059  * - The base class for this is currently FXFileStream, it should really be FXFileIO
00060  * - FXFileStream has no append mode, so each time the app is run it opens a new file
00061  *   destroying the old one, rather than appending to it - this is rather annoying...
00062  * - stream operator<< doesn't like FXStrings - will fix one day (not soon)
00063  */
00064 class FXAPI FXLogger : public FXFileStream {
00065 private:
00066   static FXLogger* thisLog;   // singleton handle
00067   FXString         filename;  // the log file
00068   FXlong           filesize;  // max size of log file
00069   FXuint           filemode;  // the permisions of the file
00070   FXbool           enabled;   // indication of whether the logger is enabled
00071   FXbool           stamp;     // should we add a date stamp to the log entry
00072   FXbool           nextStamp; // indication of whether the next stream operation will cause a timestamp to be written
00073 
00074 protected:
00075   /// opens the log file
00076   FXbool open();
00077 
00078   /// closes the log file
00079   FXbool close();
00080 
00081   /**
00082    * Ensures that the log file is no bigger than size
00083    * Returns state whether the log file is open
00084    */
00085   FXbool trim();
00086 
00087   /// Add timestamp when using the stream operator '<<'
00088   void checkStreamStamp(const FXchar& v);
00089 
00090 public:
00091   /// Give me a log file
00092   FXLogger(const FXString& file,const FXlong size=100000,const FXuint mode=0,const FXbool timestamp=TRUE);
00093 
00094   /// Use the current instance
00095   static FXLogger& instance();
00096 
00097   /// Set the size of the log file - check on next log entry
00098   void size(const FXlong size) { filesize=size; }
00099 
00100   /// Get the size of the log file
00101   FXlong size() { return filesize; }
00102 
00103   /// Change the location of the log file - change is immediate
00104   FXbool name(const FXString& file);
00105 
00106   /// get the current filename
00107   FXString name() { return filename; }
00108 
00109   /// indicates whether the log file can/will be written to
00110   FXbool isEnabled() { return enabled; }
00111 
00112   /// This is for logging output to a file
00113   /// It is automatically new-line terminated
00114   void log(const FXchar* msg, ...);
00115   void log(const FXString& msg);
00116   void status(const FXString& code, const FXString& msg);
00117   void warning(const FXString& msg);
00118   void error(const FXString& msg);
00119   void debug(const FXString& msg);
00120   void log(const FXException* e);
00121   void log(const FXExtendedException* e);
00122 
00123   /// Save to stream
00124   virtual FXStream& operator<<(const FXchar& v);
00125 
00126   /// load from stream
00127   virtual FXStream& operator<<(const FXuchar& v);
00128 
00129   /// done
00130   virtual ~FXLogger();
00131   };
00132 
00133 } // namespace FXEX
00134 #endif // FXLOGGER_H