From 25b87e0d02f285fa8bacc4369f1d9251cf815a40 Mon Sep 17 00:00:00 2001
From: Bjørn Lindeijer <bjorn@lindeijer.nl>
Date: Sun, 13 Feb 2005 23:22:52 +0000
Subject: Now the resource manager will log a bit about images and cleanup.

---
 src/log.cpp                       | 63 +++++++++++++++++++++++----------------
 src/log.h                         | 28 ++++++++++++++++-
 src/resources/image.cpp           |  7 +++--
 src/resources/resourcemanager.cpp |  8 ++---
 4 files changed, 73 insertions(+), 33 deletions(-)

diff --git a/src/log.cpp b/src/log.cpp
index 25d94faf..4a66a223 100644
--- a/src/log.cpp
+++ b/src/log.cpp
@@ -26,26 +26,21 @@
 
 FILE* logfile;
 
+#define LOG_FILE "tmw.log"
 
-#define LOG_FILE "./docs/tmw.log"
 
-
-/*
- * Initializes log file by opening it for writing.
- */
-void init_log() {
+void init_log()
+{
     logfile = fopen(LOG_FILE, "w");
     if (!logfile) {
         printf("Warning: error while opening log file.\n");
     }
 }
 
-/*
- * Enter a message in the log with a certain category. The message will be
- * timestamped.
- */
-void log(const char *category, const char *log_text, ...) {
-    if (logfile) {
+void log(const char *category, const char *log_text, ...)
+{
+    if (logfile)
+    {
         char* buf = new char[1024];
         va_list ap;
         time_t t;
@@ -78,11 +73,33 @@ void log(const char *category, const char *log_text, ...) {
     }
 }
 
-/*
- * Log an error and quit. The error will pop-up in Windows and will be printed
- * to standard error everywhere else. 
- */
-void error(const std::string error_text) {
+void log(const std::string &text)
+{
+    if (logfile)
+    {
+        // Get the current system time
+        time_t t;
+        time(&t);
+
+        // Print the log entry
+        fprintf(logfile,
+                "[%s%d:%s%d:%s%d] %s\n",
+                (((t / 60) / 60) % 24 < 10) ? "0" : "",
+                (int)(((t / 60) / 60) % 24),
+                ((t / 60) % 60 < 10) ? "0" : "",
+                (int)((t / 60) % 60),
+                (t % 60 < 10) ? "0" : "",
+                (int)(t % 60),
+                text.c_str()
+               );
+
+        // Flush the log file
+        fflush(logfile);
+    }
+}
+
+void error(const std::string &error_text)
+{
     log("Error", error_text.c_str());
 
 #ifdef WIN32
@@ -93,16 +110,12 @@ void error(const std::string error_text) {
     exit(1);
 }
 
-/*
- * Shortcut to log a warning.
- */
-void warning(const char *warning_text) {
+void warning(const char *warning_text)
+{
     log("Warning", warning_text);
 }
 
-/*
- * Shortcut to log a status update, will only really be logged in debug mode.
- */
-void status(const char *status_text) {
+void status(const char *status_text)
+{
     log("Status", status_text);
 }
diff --git a/src/log.h b/src/log.h
index aedba824..9d9cfb72 100644
--- a/src/log.h
+++ b/src/log.h
@@ -28,10 +28,36 @@
 #include <time.h>
 #include <string>
 
+/**
+ * Initializes log file by opening it for writing.
+ */
 void init_log();
+
+/**
+ * Enters a message in the log with a certain category. The message will be
+ * timestamped.
+ */
 void log(const char *category, const char *log_text, ...);
-void error(std::string error_text);
+
+/**
+ * Enters a message in the log. The message will be timestamped.
+ */
+void log(const std::string &text);
+
+/**
+ * Log an error and quit. The error will pop-up in Windows and will be printed
+ * to standard error everywhere else. 
+ */
+void error(const std::string &error_text);
+
+/**
+ * Shortcut to log a warning.
+ */
 void warning(const char *warning_text);
+
+/**
+ * Shortcut to log a status update.
+ */
 void status(const char *status_text);
 
 #endif
diff --git a/src/resources/image.cpp b/src/resources/image.cpp
index f84826bb..dc5df41c 100644
--- a/src/resources/image.cpp
+++ b/src/resources/image.cpp
@@ -40,9 +40,10 @@ Image::~Image()
 
 Image* Image::load(const std::string &filePath, int flags)
 {
-#ifdef __DEBUG
-    std::cout << "Image::load(" << filePath << ")\n";
-#endif
+    std::stringstream msg;
+    msg << "Image::load(" << filePath << ")";
+    log(msg.str());
+
     // Attempt to use SDL_Image to load the file.
     SDL_Surface *tmpImage = IMG_Load(filePath.c_str());
     SDL_SetColorKey(tmpImage, SDL_SRCCOLORKEY | SDL_RLEACCEL,
diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp
index 1a2c7a5f..582bc613 100644
--- a/src/resources/resourcemanager.cpp
+++ b/src/resources/resourcemanager.cpp
@@ -62,11 +62,11 @@ ResourceManager::~ResourceManager()
     }
     resources.clear();
 
-#ifdef __DEBUG
-    std::cout << "ResourceManager::~ResourceManager() cleaned up " <<
+    std::stringstream msg;
+    msg << "ResourceManager::~ResourceManager() cleaned up " <<
         danglingReferences << " references to " << danglingResources <<
-        " resources\n";
-#endif
+        " resources";
+    log(msg.str());
 }
 
 Resource* ResourceManager::get(const E_RESOURCE_TYPE &type,
-- 
cgit v1.2.3-70-g09d2