Kajetan Johannes Hammerle 3 years ago
parent
commit
3d86c46953
4 changed files with 64 additions and 4 deletions
  1. 3 2
      meson.build
  2. 3 0
      utils/Logger.cpp
  3. 56 0
      utils/Logger.h
  4. 2 2
      utils/StringBuffer.h

+ 3 - 2
meson.build

@@ -24,6 +24,7 @@ sources = ['Main.cpp',
     'tests/FrustumTests.cpp',
     'math/Frustum.cpp',
     'utils/Size.cpp',
+    'utils/Logger.cpp',
     'tests/QuaternionTests.cpp',
     'math/Quaternion.cpp',
     'tests/UtilsTests.cpp',
@@ -53,7 +54,7 @@ sources = ['Main.cpp',
     'network/Packet.cpp',
     'network/Server.cpp',
     'network/Client.cpp',
-    'network/ENet.cpp']
+    'network/ENet.cpp',]
 
 threadDep = dependency('threads')
 glewDep = dependency('glew')
@@ -63,4 +64,4 @@ pngDep = dependency('libpng')
 executable('tests', 
     sources: sources,
     dependencies : [threadDep, glewDep, glfwDep, pngDep],
-    cpp_args: ['-Wall', '-Wextra', '-pedantic', '-Werror'])
+    cpp_args: ['-Wall', '-Wextra', '-pedantic', '-Werror', '-DLOG_LEVEL=4'])

+ 3 - 0
utils/Logger.cpp

@@ -0,0 +1,3 @@
+#include "utils/Logger.h"
+
+Logger::Level Logger::level = Logger::ERROR;

+ 56 - 0
utils/Logger.h

@@ -0,0 +1,56 @@
+#ifndef LOGGER_H
+#define LOGGER_H
+
+#include "utils/StringBuffer.h"
+
+namespace Logger {
+    enum Level { NONE, ERROR, WARNING, INFO, DEBUG };
+
+    extern Level level;
+
+    template<int N>
+    void error(const StringBuffer<N>& s) {
+#if LOG_LEVEL >= 1
+        if(level >= ERROR) {
+            std::cout << "\33[1;31m[ERROR] " << s << "\33[39;49m\n";
+        }
+#else
+        (void)s;
+#endif
+    }
+
+    template<int N>
+    void warn(const StringBuffer<N>& s) {
+#if LOG_LEVEL >= 2
+        if(level >= WARNING) {
+            std::cout << "\33[1;33m[WARNING] " << s << "\33[39;49m\n";
+        }
+#else
+        (void)s;
+#endif
+    }
+
+    template<int N>
+    void info(const StringBuffer<N>& s) {
+#if LOG_LEVEL >= 3
+        if(level >= INFO) {
+            std::cout << "\33[1;37m[INFO] " << s << "\33[39;49m\n";
+        }
+#else
+        (void)s;
+#endif
+    }
+
+    template<int N>
+    void debug(const StringBuffer<N>& s) {
+#if LOG_LEVEL >= 4
+        if(level >= DEBUG) {
+            std::cout << "\33[1;32m[DEBUG] " << s << "\33[39;49m\n";
+        }
+#else
+        (void)s;
+#endif
+    }
+}
+
+#endif

+ 2 - 2
utils/StringBuffer.h

@@ -129,11 +129,11 @@ public:
         return hash;
     }
 
-    void print() {
+    void print() const {
         std::cout << data;
     }
 
-    void printLine() {
+    void printLine() const {
         std::cout << data << '\n';
     }
 };