BWAPI
SPAR/AIModule/SparAIModule/Utils/Logger.h
Go to the documentation of this file.
00001 #pragma once
00002 #include <stdio.h>
00003 #define WIN32_LEAN_AND_MEAN
00004 #include <windows.h>
00005 #undef min
00006 #undef max
00007 #include <time.h>
00008 
00009 struct LogTypes
00010 {
00011   class EventType
00012   {
00013   public:
00014     const unsigned int code;
00015     const WORD color;
00016     static const WORD DEFAULT_COLOR;
00017     const unsigned int htmlColor;
00018     static const unsigned int DEFAULT_HTML_COLOR;
00019     operator unsigned int() const 
00020     {
00021       return code;
00022     }
00023   protected:
00024     friend struct LogTypes;
00025     EventType(unsigned int _code, WORD _color, unsigned int _htmlColor)
00026       : code(_code), color(_color), htmlColor(_htmlColor)
00027     {
00028     }
00029     EventType(unsigned int _code)
00030       : code(_code), color(DEFAULT_COLOR), htmlColor(DEFAULT_HTML_COLOR)
00031     {
00032     }
00033   };
00034 
00035   static const EventType
00036     ON_FRAME,
00037     ON_MORPH,
00038     ON_RENEGADE,
00039     ON_DESTROY,
00040     ON_DISCOVER,
00041     ON_EVADE,
00042     ON_SHOW,
00043     ON_HIDE,
00044     ON_CREATE,
00045     GENERAL,
00046     LAYER_1_DECISION,
00047     LAYER_2_DECISION,
00048     LAYER_3_DECISION,
00049     LAYER_4_DECISION,
00050     LAYER_1_SITUATION_ANALYSIS,
00051     LAYER_2_SITUATION_ANALYSIS,
00052     LAYER_3_SITUATION_ANALYSIS,
00053     LAYER_4_SITUATION_ANALYSIS,
00054     PERCEPTUAL_STATE,
00055     GENERAL_ERROR; // Can't name that ERROR due to stupid windows.h
00056 
00057   static const unsigned int
00058     CALLBACKS,
00059     LAYERS,
00060     ALL;
00061 };
00062 
00063 class SparLogger
00064 {
00065 private:
00066   FILE * m_sparLogFile;
00067   unsigned int m_enabledLogger;
00068 
00069   static SparLogger s_logger;
00070   SparLogger(int enabledLogger);
00071   ~SparLogger();
00072   void CreateConsoleLog(const char *winTitle);
00073 public:
00074   static SparLogger* get()
00075   {
00076     return &s_logger;
00077   }
00078   FILE * getFile()
00079   {
00080     return m_sparLogFile;
00081   }
00082   void activate(unsigned int mask)
00083   {
00084     m_enabledLogger |= mask;
00085   }
00086   void deactivate(unsigned int mask)
00087   {
00088     m_enabledLogger &= ~mask;
00089   }
00090   bool isActive(unsigned int mask)
00091   {
00092     return (m_enabledLogger & mask) == mask;
00093   }
00094 };
00095 
00096 #ifdef USE_SPAR_LOG
00097   #define SPAR_LOGGER_ACTIVATE_EVENT(mask) SparLogger::get()->activate(mask)
00098   #define SPAR_LOGGER_DEACTIVATE_EVENT(mask) SparLogger::get()->deactivate(mask)
00099   #define SPAR_LOG(eventType, format, ...) \
00100     do{ \
00101       if(SparLogger::get()->isActive(eventType)) \
00102       { \
00103         time_t rawtime; \
00104         time(&rawtime); \
00105         struct tm timeinfo; \
00106         localtime_s(&timeinfo, &rawtime); \
00107         char buffer [80]; \
00108         strftime(buffer, 80, "%H:%M:%S", &timeinfo); \
00109         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), LogTypes::EventType::DEFAULT_COLOR); \
00110         printf("%s (#%d) - ", buffer, BWAPI::Broodwar->getFrameCount()); \
00111         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), eventType.color); \
00112         printf(format, __VA_ARGS__); \
00113         printf("\n"); \
00114         fprintf(SparLogger::get()->getFile(), "<font color=\"#%x\">%s (#%d) - </font><font color=\"#%x\">", \
00115                                                   LogTypes::EventType::DEFAULT_HTML_COLOR, \
00116                                                   buffer, \
00117                                                   BWAPI::Broodwar->getFrameCount(), \
00118                                                   eventType.htmlColor); \
00119         fprintf(SparLogger::get()->getFile(), format, __VA_ARGS__); \
00120         fprintf(SparLogger::get()->getFile(), "</font></br>\n"); \
00121       } \
00122     } while(false)
00123 #else
00124   #define SPAR_LOGGER_ACTIVATE_EVENT(type) 
00125   #define SPAR_LOGGER_DEACTIVATE_EVENT(type) 
00126   #define SPAR_LOG(format, ...) 
00127 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines