浏览代码

all logging uses the logger

Kajetan Johannes Hammerle 3 年之前
父节点
当前提交
c11be95ad0
共有 12 个文件被更改,包括 124 次插入66 次删除
  1. 2 14
      Main.cpp
  2. 31 16
      images/PNGReader.cpp
  3. 0 1
      input/TextInput.cpp
  4. 0 2
      rendering/Framebuffer.h
  5. 3 3
      rendering/Shader.cpp
  6. 5 4
      tests/HashMapTests.cpp
  7. 27 5
      tests/Test.cpp
  8. 13 4
      tests/Test.h
  9. 4 4
      utils/Logger.h
  10. 0 1
      utils/Random.cpp
  11. 24 3
      utils/StringBuffer.h
  12. 15 9
      wrapper/GL.cpp

+ 2 - 14
Main.cpp

@@ -1,5 +1,3 @@
-#include <iostream>
-
 #include "rendering/FileTexture.h"
 #include "rendering/Framebuffer.h"
 #include "rendering/Window.h"
@@ -31,7 +29,7 @@
 
 int main(int argAmount, char** args) {
     if(argAmount < 2) {
-        std::cout << "missing path to images\n";
+        LOG_ERROR("missing path to images");
         return 0;
     }
     ArrayTests::test();
@@ -76,20 +74,10 @@ int main(int argAmount, char** args) {
     WindowOptions options(4, 3, size, false, "Test");
     Window w(input, options);
     if(w.getError().has()) {
-        std::cout << w.getError().message << "\n";
+        LOG_WARNING(w.getError().message);
         return 0;
     }
     Game game;
     w.run(game, 10'000'000);
-
-    LOG_ERROR("HI");
-    LOG_ERROR(StringBuffer<50>(" dfgdf hi"));
-    LOG_WARNING("HI");
-    LOG_WARNING(StringBuffer<50>(" dfgdf hi"));
-    LOG_INFO("HI");
-    LOG_INFO(StringBuffer<50>(" dfgdf hi"));
-    LOG_DEBUG("HI");
-    LOG_DEBUG(StringBuffer<50>(" dfgdf hi"));
-
     return 0;
 }

+ 31 - 16
images/PNGReader.cpp

@@ -1,16 +1,18 @@
-#include <cstring>
-#include <iostream>
 #include <libpng16/png.h>
 
 #include "images/PNGReader.h"
+#include "utils/Logger.h"
+#include "utils/StringBuffer.h"
 
 PNGReader::PNGReader(const char* path)
     : path(path), width(0), height(0), channels(0), bitDepth(0), rowBytes(0),
       file(fopen(path, "r")), read(nullptr), info(nullptr),
       rowPointers(nullptr) {
     if(file == nullptr) {
-        std::cout << "file '" << path << "' cannot be read: " << strerror(errno)
-                  << "\n";
+        LOG_WARNING(StringBuffer<100>("file '")
+                        .append(path)
+                        .append("' cannot be read: ")
+                        .append(static_cast<const char*>(strerror(errno))));
         return;
     }
     if(checkSignature()) {
@@ -19,16 +21,18 @@ PNGReader::PNGReader(const char* path)
     read = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr,
                                   nullptr);
     if(read == nullptr) {
-        std::cout << "cannot create png read data structure\n";
+        LOG_WARNING("cannot create png read data structure");
         return;
     }
     info = png_create_info_struct(read);
     if(info == nullptr) {
-        std::cout << "cannot create png info structure\n";
+        LOG_WARNING("cannot create png info structure");
         return;
     }
     if(setjmp(png_jmpbuf(read))) {
-        std::cout << "png file '" << path << "' has used error callback\n";
+        LOG_WARNING(StringBuffer<100>("png file '")
+                        .append(path)
+                        .append("' has used error callback"));
         return;
     }
     png_init_io(read, file);
@@ -75,18 +79,24 @@ int PNGReader::getBufferSize() const {
 
 bool PNGReader::hasError() const {
     if(channels < 1 || channels > 4) {
-        std::cout << "'" << path
-                  << "' has unsupported number of channels: " << channels
-                  << "\n";
+        LOG_WARNING(StringBuffer<100>("'")
+                        .append(path)
+                        .append("' has unsupported number of channels: ")
+                        .append(channels));
         return true;
     } else if(width < 1 || width > 2048 || height < 1 || height > 2048) {
-        std::cout << "width and height of '" << path << "' are too big\n";
+        LOG_WARNING(StringBuffer<100>("width and height of '")
+                        .append(path)
+                        .append("' are too big"));
         return true;
     } else if(bitDepth != 8 && bitDepth != 16) {
-        std::cout << "bit depth of '" << path << "' is neither 8 or 16\n";
+        LOG_WARNING(StringBuffer<100>("bit depth of '")
+                        .append(path)
+                        .append("' is neither 8 or 16"));
         return true;
     } else if(getBufferSize() != (rowBytes * height * 8 / bitDepth)) {
-        std::cout << "'" << path << "' needs an unexpected buffer size\n";
+        LOG_WARNING(StringBuffer<100>("'").append(path).append(
+            "' needs an unexpected buffer size"));
         return true;
     }
     return false;
@@ -95,11 +105,14 @@ bool PNGReader::hasError() const {
 bool PNGReader::checkSignature() {
     png_byte buffer[8];
     if(fread(buffer, sizeof(png_byte), 8, file) != 8) {
-        std::cout << "cannot read signature of file '" << path << "'\n";
+        LOG_WARNING(StringBuffer<100>("cannot read signature of file '")
+                        .append(path)
+                        .append("'"));
         return true;
     }
     if(png_sig_cmp(buffer, 0, 8)) {
-        std::cout << "file '" << path << "' is not a png\n";
+        LOG_WARNING(
+            StringBuffer<100>("file '").append(path).append("' is not a png"));
         return true;
     }
     return false;
@@ -107,7 +120,9 @@ bool PNGReader::checkSignature() {
 
 bool PNGReader::readData(ColorChannel* buffer) {
     if(setjmp(png_jmpbuf(read))) {
-        std::cout << "png file '" << path << "' has used error callback\n";
+        LOG_WARNING(StringBuffer<100>("png file '")
+                        .append(path)
+                        .append("' has used error callback"));
         return true;
     }
     rowPointers = static_cast<ColorChannel**>(

+ 0 - 1
input/TextInput.cpp

@@ -1,4 +1,3 @@
-#include <iostream>
 #include <utility>
 
 #include <GLFW/glfw3.h>

+ 0 - 2
rendering/Framebuffer.h

@@ -1,8 +1,6 @@
 #ifndef FRAMEBUFFER_H
 #define FRAMEBUFFER_H
 
-#include <iostream>
-
 #include "rendering/Texture.h"
 #include "utils/ArrayList.h"
 #include "utils/Size.h"

+ 3 - 3
rendering/Shader.cpp

@@ -1,7 +1,7 @@
 #include <fstream>
-#include <iostream>
 
 #include "rendering/Shader.h"
+#include "utils/Logger.h"
 #include "wrapper/GL.h"
 
 Shader::Shader(const char* vertexPath, const char* fragmentPath,
@@ -44,7 +44,7 @@ void Shader::clean() {
 }
 
 bool Shader::compile(const char* path, GL::Shader& s, GL::ShaderType st) {
-    std::cout << "shader: " << path << '\n';
+    LOG_DEBUG(StringBuffer<50>("shader: ").append(path));
     List<char> code;
     if(readFile(code, path)) {
         return true;
@@ -56,7 +56,7 @@ bool Shader::readFile(List<char>& code, const char* path) const {
     std::ifstream in;
     in.open(path);
     if(!in.good()) {
-        std::cout << "cannot read file\n";
+        LOG_ERROR("cannot read file");
         return true;
     }
     while(true) {

+ 5 - 4
tests/HashMapTests.cpp

@@ -84,11 +84,12 @@ struct A {
     bool operator==(const A& other) const {
         return a == other.a && b == other.b;
     }
-};
 
-std::ostream& operator<<(std::ostream& os, const A& a) {
-    return os << "A(" << a.a << ", " << a.b << ")";
-}
+    template<int L>
+    void toString(StringBuffer<L>& s) const {
+        s.append("A(").append(a).append(", ").append(b).append(")");
+    }
+};
 
 static void testEmplace(Test& test) {
     HashMap<int, A> map;

+ 27 - 5
tests/Test.cpp

@@ -4,13 +4,27 @@ Test::Test(const char* name) : tests(0), successTests(0), name(name) {
 }
 
 void Test::finalize() {
-    std::cout << ((successTests == tests) ? GREEN : RED);
-    std::cout << name << " Tests: " << successTests << " / " << tests << " succeeded\n" << RESET;
+    if(successTests == tests) {
+        LOG_DEBUG(StringBuffer<256>(name)
+                      .append(" Tests: ")
+                      .append(successTests)
+                      .append(" / ")
+                      .append(tests)
+                      .append(" succeeded"));
+    } else {
+        LOG_ERROR(StringBuffer<256>(name)
+                      .append(" Tests: ")
+                      .append(successTests)
+                      .append(" / ")
+                      .append(tests)
+                      .append(" succeeded"));
+    }
     tests = 0;
     successTests = 0;
 }
 
-void Test::checkFloat(float wanted, float actual, float error, const char* text) {
+void Test::checkFloat(float wanted, float actual, float error,
+                      const char* text) {
     float diff = wanted - actual;
     diff = diff < 0.0f ? -diff : diff;
     if(diff < error) {
@@ -18,7 +32,15 @@ void Test::checkFloat(float wanted, float actual, float error, const char* text)
         successTests++;
     } else {
         tests++;
-        std::cout << RED << name << " Test " << tests << ": " << text << " - " << RESET;
-        std::cout << RED << "expected '" << wanted << "' got '" << actual << "'\n" << RESET;
+        LOG_ERROR(StringBuffer<256>(name)
+                      .append(" Test ")
+                      .append(tests)
+                      .append(": ")
+                      .append(text)
+                      .append(" - expected '")
+                      .append(wanted)
+                      .append("' got '")
+                      .append(actual)
+                      .append("'"));
     }
 }

+ 13 - 4
tests/Test.h

@@ -1,7 +1,8 @@
 #ifndef TEST_H
 #define TEST_H
 
-#include <iostream>
+#include "utils/Logger.h"
+#include "utils/StringBuffer.h"
 
 class Test final {
     static constexpr const char* RED = "\033[0;31m";
@@ -23,11 +24,19 @@ public:
             successTests++;
         } else {
             tests++;
-            std::cout << RED << name << " Test " << tests << ": " << text << " - " << RESET;
-            std::cout << RED << "expected '" << wanted << "' got '" << actual << "'\n" << RESET;
+            LOG_ERROR(StringBuffer<256>(name)
+                          .append(" Test ")
+                          .append(tests)
+                          .append(": ")
+                          .append(text)
+                          .append(" - expected '")
+                          .append(wanted)
+                          .append("' got '")
+                          .append(actual)
+                          .append("'"));
         }
     }
-    
+
     void checkFloat(float wanted, float actual, float error, const char* text);
 };
 

+ 4 - 4
utils/Logger.h

@@ -14,7 +14,7 @@ namespace Logger {
         std::cout << "\33[1;31m[ERROR] " << (text) << "\33[39;49m\n";          \
     }
 #else
-#define LOG_ERROR(text)
+#define LOG_ERROR(text) (void)text;
 #endif
 
 #if LOG_LEVEL >= 2
@@ -23,7 +23,7 @@ namespace Logger {
         std::cout << "\33[1;33m[WARNING] " << (text) << "\33[39;49m\n";        \
     }
 #else
-#define LOG_WARNING(text)
+#define LOG_WARNING(text) (void)text;
 #endif
 
 #if LOG_LEVEL >= 3
@@ -32,7 +32,7 @@ namespace Logger {
         std::cout << "\33[1;37m[INFO] " << (text) << "\33[39;49m\n";           \
     }
 #else
-#define LOG_INFO(text)
+#define LOG_INFO(text) (void)text;
 #endif
 
 #if LOG_LEVEL >= 4
@@ -41,7 +41,7 @@ namespace Logger {
         std::cout << "\33[1;32m[DEBUG] " << (text) << "\33[39;49m\n";          \
     }
 #else
-#define LOG_DEBUG(text)
+#define LOG_DEBUG(text) (void)text;
 #endif
 
 #endif

+ 0 - 1
utils/Random.cpp

@@ -1,5 +1,4 @@
 #include <chrono>
-#include <iostream>
 
 #include "utils/Random.h"
 

+ 24 - 3
utils/StringBuffer.h

@@ -73,6 +73,10 @@ public:
         return append(static_cast<char>(c));
     }
 
+    StringBuffer& append(unsigned char c) {
+        return append(static_cast<char>(c));
+    }
+
     StringBuffer& append(const char* str) {
         return appendString<const char*>(str);
     }
@@ -99,7 +103,15 @@ public:
         return *this;
     }
 
-    StringBuffer& append(int i) {
+    StringBuffer& append(signed short s) {
+        return append("%hd", s);
+    }
+
+    StringBuffer& append(unsigned short s) {
+        return append("%hu", s);
+    }
+
+    StringBuffer& append(signed int i) {
         return append("%d", i);
     }
 
@@ -107,8 +119,8 @@ public:
         return append("%u", i);
     }
 
-    StringBuffer& append(float i) {
-        return append("%.2f", i);
+    StringBuffer& append(float f) {
+        return append("%.2f", f);
     }
 
     StringBuffer& append(bool b) {
@@ -121,6 +133,15 @@ public:
         return *this;
     }
 
+    template<int L>
+    void toString(StringBuffer<L>& s) const {
+        if(reinterpret_cast<const void*>(this) ==
+           reinterpret_cast<const void*>(&s)) {
+            return;
+        }
+        s.append(data);
+    }
+
     StringBuffer& appendUnicode(unsigned int c) {
         if(c < (1 << 7)) {
             append(static_cast<char>(c & 0x7F));

+ 15 - 9
wrapper/GL.cpp

@@ -1,8 +1,8 @@
 #include <GL/glew.h>
-#include <iostream>
 #include <type_traits>
 
 #include "utils/Array.h"
+#include "utils/Logger.h"
 #include "wrapper/GL.h"
 
 static_assert(std::is_same<GL::Shader, GLuint>::value,
@@ -63,7 +63,8 @@ GL::TextureFormat GL::TextureFormat::color8(int channels) {
         case 3: return TextureFormat(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE);
         case 4: return TextureFormat(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
     }
-    std::cout << channels << " is not a valid amount of channels\n";
+    LOG_ERROR(StringBuffer<50>(channels).append(
+        " is not a valid amount of channels"));
     return unknown();
 }
 
@@ -74,7 +75,8 @@ GL::TextureFormat GL::TextureFormat::float16(int channels) {
         case 3: return TextureFormat(GL_RGB16F, GL_RGB, GL_FLOAT);
         case 4: return TextureFormat(GL_RGBA16F, GL_RGBA, GL_FLOAT);
     }
-    std::cout << channels << " is not a valid amount of channels\n";
+    LOG_ERROR(StringBuffer<50>(channels).append(
+        " is not a valid amount of channels"));
     return unknown();
 }
 
@@ -85,7 +87,8 @@ GL::TextureFormat GL::TextureFormat::float32(int channels) {
         case 3: return TextureFormat(GL_RGB32F, GL_RGB, GL_FLOAT);
         case 4: return TextureFormat(GL_RGBA32F, GL_RGBA, GL_FLOAT);
     }
-    std::cout << channels << " is not a valid amount of channels\n";
+    LOG_ERROR(StringBuffer<50>(channels).append(
+        " is not a valid amount of channels"));
     return unknown();
 }
 
@@ -103,8 +106,9 @@ GL::TextureFormat GL::TextureFormat::unknown() {
 
 bool GL::printError(const char* message) {
     GLenum error = glGetError();
-    if(error != GL_NO_ERROR) {
-        std::cout << message << ": " << gluErrorString(error) << '\n';
+    if(error == GL_NO_ERROR) {
+        LOG_ERROR(StringBuffer<100>(message).append(": ").append(
+            gluErrorString(error)));
         return true;
     }
     return false;
@@ -165,7 +169,8 @@ bool GL::logLinkerError(Program p) {
     if(!linked) {
         Array<char, 1024> log;
         glGetProgramInfoLog(p, log.getLength(), nullptr, log.begin());
-        std::cout << "linker log: " << log.begin() << "\n";
+        LOG_WARNING(StringBuffer<100>("linker log: ")
+                        .append(static_cast<const char*>(log.begin())));
         return true;
     }
     return false;
@@ -194,7 +199,8 @@ bool GL::logCompileError(Shader s) {
     if(!compiled) {
         Array<char, 1024> log;
         glGetShaderInfoLog(s, log.getLength(), nullptr, log.begin());
-        std::cout << "compiler log: " << log.begin() << "\n";
+        LOG_WARNING(StringBuffer<100>("compiler log: ")
+                        .append(static_cast<const char*>(log.begin())));
         return true;
     }
     return false;
@@ -322,7 +328,7 @@ void GL::drawBuffers(int length, ColorAttachment* c) {
 bool GL::printFramebufferError() {
     GLenum error = glCheckFramebufferStatus(GL_FRAMEBUFFER);
     if(error != GL_FRAMEBUFFER_COMPLETE) {
-        std::cout << "framebuffer error: " << error << '\n';
+        LOG_WARNING(StringBuffer<100>("framebuffer error: ").append(error));
         return true;
     }
     return false;