Log.h
Upload User: sbftbdw
Upload Date: 2007-01-03
Package Size: 379k
Code Size: 2k
Category:

Remote Control

Development Platform:

Visual C++

  1. // This is an object and macros which provide general logging and debugging functions.
  2. // It can log to a file, to a new console, and/or to debug - others maybe to follow.
  3. // Every log object has a logging level (which can be changed).
  4. // Only log requests with a high enough level attached get logged. So the
  5. // level can be thought of as 'amount of detail'.
  6. // We use Unicode-portable stuff here for compatibility with WinCE.
  7. //
  8. // Typical use:
  9. //
  10. //       Log log;
  11. //       log.SetFile( _T("myapp.log") );
  12. //       ...
  13. //       log.Print(2, _T("x = %dn"), x);
  14. //
  15. #pragma once
  16. #include <stdarg.h>
  17. class Log  
  18. {
  19. public:
  20.     // Logging mode flags:
  21.     static const int ToDebug;
  22.     static const int ToFile;
  23.     static const int ToConsole;
  24.     // Create a new log object.
  25.     // Parameters as follows:
  26.     //    mode     - specifies where output should go, using combination
  27.     //               of flags above. ToConsole won't do anything on CE.
  28.     //    level    - the default level
  29.     //    filename - if flag Log::ToFile is specified in the type,
  30.     //               a filename must be specified here.
  31.     //    append   - if logging to a file, whether or not to append to any
  32.     //               existing log.
  33. Log(int mode = ToDebug, int level = 1, LPTSTR filename = NULL, bool append = false);
  34.     inline void Print(int level, LPTSTR format, ...) {
  35.         if (level > m_level) return;
  36.         va_list ap;
  37.         va_start(ap, format);
  38.         ReallyPrint(format, ap);
  39.         va_end(ap);
  40.     }
  41.     
  42.     // Change the log level
  43.     void SetLevel(int level);
  44.     // Change the logging mode
  45.     void SetMode(int mode);
  46.     // Change or set the logging filename.  This enables ToFile mode if
  47.     // not already enabled.
  48.     void SetFile(LPTSTR filename, bool append = false);
  49. virtual ~Log();
  50. private:
  51.     void ReallyPrint(LPTSTR format, va_list ap);
  52.     void CloseFile();
  53.     bool m_tofile, m_todebug, m_toconsole;
  54.     int m_level;
  55.     HANDLE hlogfile;
  56. };