Guest
Public paste!

swarmapps

By: a guest | Nov 22nd, 2009 | Syntax: C++ | Size: 1.79 KB | Hits: 95 | Expires: Never
Copy text to clipboard
  1. // logger.h
  2. // By: Michael R. Rich, 2009
  3. // swarmapps.wordpress.com
  4. // This code is released into the public domain
  5.  
  6. #ifndef __EASYLOGGER_H
  7. #define __EASYLOGGER_H
  8.  
  9. #include <iostream>
  10.  
  11. // The following macro and class allow for a very quick, runtime-settable loging function
  12. // Essentially if the value passed into the macro as "TYPE" is true, it will then log the given text.
  13. /* code snippet example below
  14.  bool someLogType = true;
  15.  LOG(someLogType) << "someLogType.Log this please!\n"; // the text will be logged
  16.  */
  17.  
  18. #define LOG(TYPE) (TYPE) && logger::outstream
  19. #define LLOG(TYPE) LOG(TYPE) << #TYPE << ": "
  20. // The LLOG Macro will produce results like:
  21. // LLOG(MyLogType) << "A fancy log\n";
  22. // output = MyLogType: A fancy log
  23.  
  24. // This works because the boolean && operator stops at the first false value in the expression.
  25. // So, if the value TYPE is false, no further evaluation will occur and the rest of the line will be ignored
  26.  
  27. // Here are some standard logs I've used
  28. #define VERBOSE logger::verboseLog
  29. #define NORMAL logger::normalLog
  30. #define DEBUG logger::debugLog
  31. #define CONFIGTEST logger::configFileTest
  32. // add your own log types here and in the cpp file!
  33.  
  34. // To use, simply include the logger.h header file in your program, add the logger.cpp file to your target and use the code snippet:
  35. /*
  36.  NORMAL = true;
  37.  VERBOSE = false;
  38.  DEBUG = true;
  39.  
  40.  LOG(NORMAL) << "I'll print\n";
  41.  LOG(VERBOSE) << "I won't\n";
  42.  LOG(DEBUG) << "But I will!";
  43.  */
  44.  
  45.  
  46. class logger {
  47. public:
  48.         static bool verboseLog;
  49.         static bool normalLog;
  50.         static bool debugLog;
  51.         static bool configFileTest;
  52.         // Set up other log types here as necessary
  53.        
  54.         static std::ostream& outstream;
  55.        
  56.         static void setOutstream (std::ostream& newStream) {
  57.                 outstream.rdbuf(newStream.rdbuf());
  58.         }
  59.  
  60.  
  61. };
  62.  
  63. #endif