Commits

Sylvain Rouquette committed efbaf2f

added Manager to Log

Comments (0)

Files changed (6)

game/inc/common/log.h

 #if defined(DEBUG)
 
 
-#define LOG_CREATE(context)     { log_ = new common::Log(context); }
-#define LOG_DELETE()            { delete log_; log_ = NULL; }
+#include <map>
+#include <string>
+
+
 #define LOG(...)                { log_->write(__VA_ARGS__); }
 
 
         LOG_ERROR
     };
 
+    static Log* get(const char* context);
+    static void clear();
+
     void write(const char* format, ...);
     //void write(int level, const char* format, ...);
 
 private:
     char*   buffer_;
     int     context_length_;
+
+    typedef log_map_t   std::map<std::string, Log*>;
+    static log_map_t    allocated_;
 };
 
 
 
 #else // defined(DEBUG)
 
-#define LOG_CREATE(context) {}
-#define LOG_DELETE()        {}
-#define LOG(...)            {}
+#define LOG(...)                {}
 
 #endif // defined(DEBUG)
 

game/inc/platform/application.h

     GameLoop* game_loop_;
 
 #if defined(DEBUG)
-    common::Log*    log_;
+    static common::Log*    log_;
 #endif
 
     bool exit_requested_;

game/src/common/log.cpp

 }
 
 
+void Log::get(const char* context) {
+    log_map_t::iterator it = allocated_.find(context);
+    if (it != allocated_.end)
+        return it;
+
+    Log* log = new Log(context);
+    allocated_[context] = log;
+    return log;
+}
+
+
+void Log::clear() {
+    for (log_map_t::iterator it = allocated_.begin(); it != allocated_.end(); ++it)
+        delete it->second;
+    allocated_.clear();
+}
+
+
 void Log::write(const char* format, ...) {
     va_list arg_list;
     va_start(arg_list, format);
               format, arg_list);
     va_end(arg_list);
     printf("%s", buffer_);
+    fflush(stdout);
 }
 
 

game/src/main/main_win32.cpp

     LPSTR lpCmdLine, int nCmdShow)
 {
 #if defined(DEBUG)
-    common::Log* log_;
+    common::Log* log_ = common::Log::get("main");
 #endif
-    LOG_CREATE("main");
     LOG("starting game...");
 
     game::GameLoopSample game_loop;
 
     delete game;
 
-    LOG_DELETE();
+#if defined(DEBUG)
+    common::Log::clear();
+#endif
 
     return 0;
 }

game/src/main/main_x11.cpp

 
 int main()
 {
+#if defined(DEBUG)
+    common::Log* log_ = common::Log::get("main");
+#endif
+    LOG("starting game...");
+
     game::GameLoopSample game_loop;
     game::GameX11 *game;
     game = new game::GameX11(&game_loop);
     game->run();
 
     delete game;
+
+#if defined(DEBUG)
+    common::Log::clear();
+#endif
+
     return 0;
 }

game/src/platform/application.cpp

 #include "state/state_manager.h"
 
 
-namespace platform
-{
+namespace platform {
+
+
+#if defined(DEBUG)
+Application::log_ = NULL;
+#endif
 
 
 Application::Application(Platform* platform, GameLoop* game_loop) :
     game_loop_(game_loop),
     exit_requested_(false)
 {
-    LOG_CREATE("platform");
+#if defined(DEBUG)
+    if (!log_)
+        log_ = common::Log::get("platform");
+#endif
 }
 
 
     finish();
     delete platform_;
     LOG("Platform terminated\n");
-    LOG_DELETE();
 }