Browse Source

fix some issues with logging

Kolja Strohm 2 months ago
parent
commit
fdc8bd635c
6 changed files with 82 additions and 64 deletions
  1. 10 8
      Console.cpp
  2. 13 6
      Console.h
  3. 48 47
      Logging.cpp
  4. 1 0
      Logging.h
  5. 5 2
      Text.cpp
  6. 5 1
      Text.h

+ 10 - 8
Console.cpp

@@ -256,7 +256,7 @@ int Framework::StickyConsoleContent::print() const
                 std::cout << "\033[0m";
                 if (backgroundColor && (int)backgroundColor[i] != -1)
                 {
-                    if (backgroundColor[i] < Color::LIGHT_Black)
+                    if (backgroundColor[i] < Color::LIGHT_BLACK)
                     {
                         std::cout << Text("\033[1;")
                                          + Text(40 + (int)backgroundColor[i])
@@ -270,7 +270,7 @@ int Framework::StickyConsoleContent::print() const
                     }
                 }
             }
-            else if (color[i] < Color::LIGHT_Black)
+            else if (color[i] < Color::LIGHT_BLACK)
             {
                 std::cout << Text("\033[1;") + Text(30 + (int)color[i]) + "m";
             }
@@ -286,7 +286,7 @@ int Framework::StickyConsoleContent::print() const
                 std::cout << "\033[0m";
                 if (color && (int)color[i] != -1)
                 {
-                    if (color[i] < Color::LIGHT_Black)
+                    if (color[i] < Color::LIGHT_BLACK)
                     {
                         std::cout
                             << Text("\033[1;") + Text(30 + (int)color[i]) + "m";
@@ -298,7 +298,7 @@ int Framework::StickyConsoleContent::print() const
                     }
                 }
             }
-            else if (backgroundColor[i] < Color::LIGHT_Black)
+            else if (backgroundColor[i] < Color::LIGHT_BLACK)
             {
                 std::cout << Text("\033[1;")
                                  + Text(40 + (int)backgroundColor[i]) + "m";
@@ -365,6 +365,11 @@ void Framework::ConsoleProgressBar::setMaxProgress(int maxProgress)
     this->maxProgress = maxProgress;
 }
 
+int Framework::ConsoleProgressBar::getProgress() const
+{
+    return progress;
+}
+
 int Framework::ConsoleProgressBar::print() const
 {
     int maxWidth = zConsoleHandlerRef()->getWidth();
@@ -1257,10 +1262,7 @@ void Framework::ConsoleHandler::print(Text str)
         }
         totalLines += lineCounts[i];
     }
-    if (totalLines > 0)
-    {
-        std::cout << "\33[" << totalLines + 1 << "A";
-    }
+    std::cout << "\33[" << totalLines + 1 << "A";
     std::cout << "\33[0K" // clear current line
               << str.getText();
     if (str.getText()[str.getLength() - 1] != '\n')

+ 13 - 6
Console.h

@@ -12,20 +12,20 @@ namespace Framework
 
     enum class Color
     {
-        Black,
+        BLACK,
         RED,
         GREEN,
         YELLOW,
-        Blue,
-        Magenta,
+        BLUE,
+        MAGENTA,
         CYAN,
         WHITE,
-        LIGHT_Black,
+        LIGHT_BLACK,
         LIGHT_RED,
         LIGHT_GREEN,
         LIGHT_YELLOW,
-        LIGHT_Blue,
-        LIGHT_Magenta,
+        LIGHT_BLUE,
+        LIGHT_MAGENTA,
         LIGHT_CYAN,
         LIGHT_WHITE,
     };
@@ -317,6 +317,13 @@ namespace Framework
          */
         DLLEXPORT void setMaxProgress(int maxProgress);
 
+        /**
+         * the current progress.
+         *
+         * \return the progress
+         */
+        DLLEXPORT int getProgress() const;
+
     protected:
         /**
          * prints the progress bar to the console.

+ 48 - 47
Logging.cpp

@@ -11,62 +11,53 @@ using namespace Framework::Logging;
 
 FlushingOStream Framework::Logging::trace(std::source_location location)
 {
-    DynamicBuffer* buff = 0;
-    return FlushingOStream(
-        buff = new DynamicBuffer([&location, &buff](std::stringbuf& buffer) {
-            zLoggingHandler()->log(
-                LogLevel::Trace, location, buffer.str().c_str());
-            delete buff;
-            return 0;
-        }));
+    DynamicBuffer* buff = new DynamicBuffer([&location](
+                                                std::stringbuf& buffer) {
+        zLoggingHandler()->log(LogLevel::Trace, location, buffer.str().c_str());
+        return 0;
+    });
+    return FlushingOStream(buff, [buff]() { delete buff; });
 }
 
 FlushingOStream Framework::Logging::debug(std::source_location location)
 {
-    DynamicBuffer* buff = 0;
-    return FlushingOStream(
-        buff = new DynamicBuffer([&location, &buff](std::stringbuf& buffer) {
-            zLoggingHandler()->log(
-                LogLevel::Debug, location, buffer.str().c_str());
-            delete buff;
-            return 0;
-        }));
+    DynamicBuffer* buff = new DynamicBuffer([&location](
+                                                std::stringbuf& buffer) {
+        zLoggingHandler()->log(LogLevel::Debug, location, buffer.str().c_str());
+        return 0;
+    });
+    return FlushingOStream(buff, [buff]() { delete buff; });
 }
 
 FlushingOStream Framework::Logging::info(std::source_location location)
 {
-    DynamicBuffer* buff = 0;
-    return FlushingOStream(
-        buff = new DynamicBuffer([&location, &buff](std::stringbuf& buffer) {
-            zLoggingHandler()->log(
-                LogLevel::Info, location, buffer.str().c_str());
-            delete buff;
-            return 0;
-        }));
+    DynamicBuffer* buff = new DynamicBuffer([&location](
+                                                std::stringbuf& buffer) {
+        zLoggingHandler()->log(LogLevel::Info, location, buffer.str().c_str());
+        return 0;
+    });
+    return FlushingOStream(buff, [buff]() { delete buff; });
 }
 
 FlushingOStream Framework::Logging::warning(std::source_location location)
 {
-    DynamicBuffer* buff = 0;
-    return FlushingOStream(
-        buff = new DynamicBuffer([&location, &buff](std::stringbuf& buffer) {
-            zLoggingHandler()->log(
-                LogLevel::Warning, location, buffer.str().c_str());
-            delete buff;
-            return 0;
-        }));
+    DynamicBuffer* buff
+        = new DynamicBuffer([&location](std::stringbuf& buffer) {
+              zLoggingHandler()->log(
+                  LogLevel::Warning, location, buffer.str().c_str());
+              return 0;
+          });
+    return FlushingOStream(buff, [buff]() { delete buff; });
 }
 
 FlushingOStream Framework::Logging::error(std::source_location location)
 {
-    DynamicBuffer* buff = 0;
-    return FlushingOStream(
-        buff = new DynamicBuffer([&location, &buff](std::stringbuf& buffer) {
-            zLoggingHandler()->log(
-                LogLevel::Error, location, buffer.str().c_str());
-            delete buff;
-            return 0;
-        }));
+    DynamicBuffer* buff = new DynamicBuffer([&location](
+                                                std::stringbuf& buffer) {
+        zLoggingHandler()->log(LogLevel::Error, location, buffer.str().c_str());
+        return 0;
+    });
+    return FlushingOStream(buff, [buff]() { delete buff; });
 }
 
 LoggingHandler* Framework::Logging::zLoggingHandler()
@@ -385,7 +376,7 @@ public:
         bool active = enabled(level);
         if (active)
         {
-            if (color < Color::LIGHT_Black)
+            if (color < Color::LIGHT_BLACK)
             {
                 return Text("\033[1;") + Text(30 + (int)color) + "m"
                      + after->formatMessage(msg, level, location) + "\033[0m";
@@ -451,7 +442,7 @@ public:
         bool active = enabled(level);
         if (active)
         {
-            if (color < Color::LIGHT_Black)
+            if (color < Color::LIGHT_BLACK)
             {
                 return Text("\033[1;") + Text(40 + (int)color) + "m"
                      + after->formatMessage(msg, level, location) + "\033[0m";
@@ -571,7 +562,7 @@ Framework::Logging::CoutLoggingChannel::CoutLoggingChannel()
 
 void Framework::Logging::CoutLoggingChannel::writeMessage(const Text& msg) const
 {
-    std::cout << msg.getText() << std::endl;
+    std::cout << msg.getText();
 }
 
 Framework::Logging::ConsoleHandlerLoggingChannel::ConsoleHandlerLoggingChannel(
@@ -593,20 +584,30 @@ void Framework::Logging::ConsoleHandlerLoggingChannel::writeMessage(
 }
 
 Framework::Logging::FileLoggingChannel::FileLoggingChannel(Text filePath)
+    : FileLoggingChannel(new Datei(filePath))
+{}
+
+Framework::Logging::FileLoggingChannel::FileLoggingChannel(Datei* file)
     : LoggingChannel(),
-      file(new Datei(filePath))
+      file(file)
 {
     if (!file->existiert())
     {
         file->erstellen();
     }
-    file->open(
-        Datei::Style::schreiben | Datei::Style::ende | Datei::Style::lesen);
+    if (!file->istOffen())
+    {
+        file->open(
+            Datei::Style::schreiben | Datei::Style::ende | Datei::Style::lesen);
+    }
 }
 
 Framework::Logging::FileLoggingChannel::~FileLoggingChannel()
 {
-    file->close();
+    if (file->istOffen())
+    {
+        file->close();
+    }
     file->release();
 }
 

+ 1 - 0
Logging.h

@@ -335,6 +335,7 @@ namespace Framework
              * the messages will be appended to the file.
              */
             DLLEXPORT FileLoggingChannel(Text filePath);
+            DLLEXPORT FileLoggingChannel(Datei* file);
             DLLEXPORT ~FileLoggingChannel();
 
         protected:

+ 5 - 2
Text.cpp

@@ -20,8 +20,10 @@ int DynamicBuffer::sync()
     return onAppend(*this);
 }
 
-FlushingOStream::FlushingOStream(DynamicBuffer* buffer)
-    : std::ostream(buffer)
+FlushingOStream::FlushingOStream(
+    DynamicBuffer* buffer, std::function<void()> onDestroy)
+    : std::ostream(buffer),
+      onDestroy(onDestroy)
 {}
 
 FlushingOStream::FlushingOStream(const Framework::FlushingOStream& stream)
@@ -31,6 +33,7 @@ FlushingOStream::FlushingOStream(const Framework::FlushingOStream& stream)
 FlushingOStream::~FlushingOStream()
 {
     flush();
+    onDestroy();
 }
 
 // inhalt der Text Klasse aus Text.h

+ 5 - 1
Text.h

@@ -24,8 +24,12 @@ namespace Framework
 
     class FlushingOStream : public std::ostream
     {
+    private:
+        std::function<void()> onDestroy;
+
     public:
-        DLLEXPORT FlushingOStream(DynamicBuffer* buffer);
+        DLLEXPORT FlushingOStream(
+            DynamicBuffer* buffer, std::function<void()> onDestroy = []() {});
         DLLEXPORT FlushingOStream(const Framework::FlushingOStream& stream);
 
         DLLEXPORT ~FlushingOStream();