Browse Source

Update logger headers and remove the useless log vector.

Godzil 1 year ago
parent
commit
b36b03edbe
2 changed files with 19 additions and 45 deletions
  1. 7 18
      source/include/logger.h
  2. 12 27
      source/logger.cpp

+ 7 - 18
source/include/logger.h

@@ -1,17 +1,16 @@
 /*
- * 2D Game Engine 
- * Logger.h: 
- * Based on pikuma.com 2D game engine in C++ and Lua course
- * Copyright (c) 2021 986-Studio. All rights reserved.
+ * 2D Physic Engine
+ * logger.h: Header for the logger
+ * Based on pikuma.com Learn Game Physics Engine Programming course.
+ * Copyright (c) 2021-2022 986-Studio. All rights reserved.
  *
  * Created by Manoël Trapier on 11/02/2021.
  */
 
-#ifndef GAMEENGINE_LOGGER_H
-#define GAMEENGINE_LOGGER_H
+#ifndef PHYSICENGINE_LOGGER_H
+#define PHYSICENGINE_LOGGER_H
 
 #include <time.h>
-#include <vector>
 
 typedef enum LogType
 {
@@ -23,19 +22,9 @@ typedef enum LogType
     LOG_DEBUG,
 } LogType;
 
-typedef struct LogEntry
-{
-    LogType type;
-    time_t timestamp;
-    char *message;
-} LogEntry;
-
 class Logger
 {
 public:
-    /* C++ is stupid. */
-    static std::vector<LogEntry> messages;
-
     static void Log(const char *message, ...);
     static void Error(const char *message, ...);
     static void Warning(const char *message, ...);
@@ -44,4 +33,4 @@ public:
     static void Debug(const char *message, ...);
 };
 
-#endif /* GAMEENGINE_LOGGER_H */
+#endif /* PHYSICENGINE_LOGGER_H */

+ 12 - 27
source/logger.cpp

@@ -1,18 +1,19 @@
 /*
- * 2D Game Engine 
- * Logger.cpp:
- * Based on pikuma.com 2D game engine in C++ and Lua course
- * Copyright (c) 2021 986-Studio. All rights reserved.
+ * 2D Physic Engine
+ * logger.cpp: The logger
+ * Based on pikuma.com Learn Game Physics Engine Programming course.
+ * Copyright (c) 2021-2022 986-Studio. All rights reserved.
  *
  * Created by Manoël Trapier on 11/02/2021.
  */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <stdarg.h>
 #include <time.h>
 
-#include <Logger.h>
+#include <logger.h>
 
 typedef struct logData
 {
@@ -30,37 +31,21 @@ static logData dataTable[] =
     [LOG_DEBUG] = { "DEBUG", 35 },
 };
 
-std::vector<LogEntry> Logger::messages;
-
-static void printLastLog()
+static void addLog(LogType type, const char *message, va_list params)
 {
-    LogEntry entry = Logger::messages.back();
+    time_t curtime = time(NULL);
 
     char buf[30];
-    struct tm *p = localtime(&entry.timestamp);
-    int typeColour = dataTable[entry.type].colour;
-    const char *typeName = dataTable[entry.type].name;
+    struct tm *p = localtime(&curtime);
+    int typeColour = dataTable[type].colour;
+    const char *typeName = dataTable[type].name;
 
     strftime(buf, 38, "%d-%b-%Y %H:%M:%S", p);
     fprintf(stderr, "\x1B[%d;1m%-6s | %-20s - ", typeColour, typeName, buf);
-    fprintf(stderr, "%s", entry.message);
+    vfprintf(stderr, message, params);
     fprintf(stderr, "\x1B[0m\n");
 }
 
-static void addLog(LogType type, const char *message, va_list params)
-{
-    LogEntry entry;
-    time_t curtime = time(NULL);
-
-    entry.message = (char *)calloc(512, sizeof(char));
-    vsnprintf(entry.message, 510, message, params);
-    entry.timestamp = curtime;
-    entry.type = type;
-
-    Logger::messages.push_back(entry);
-    printLastLog();
-}
-
 void Logger::Log(const char *message, ...)
 {
     va_list list;