From fce887fee104bc1015e7341af32ff18681e68be0 Mon Sep 17 00:00:00 2001
From: Christophe Guillon <christophe.guillon@inria.fr>
Date: Wed, 10 Jul 2024 15:10:35 +0200
Subject: [PATCH] [Log] Add possibility to disable colorization of log outputs

Add setConsoleColor(bool) method to Log module in order to control
colorization on log outputs. Default should be assumed true.
This is useful generally when redirecting outputs or using a
non colorized terminal in order to avoid annoying ANSI escape
sequences.
Add set_console_color(bool) python binding.
Add AIDGE_LOG_COLOR=[off|OFF|0] env var to disable colors at startup.
---
 include/aidge/utils/Log.hpp         |  9 +++++++++
 python_binding/utils/pybind_Log.cpp |  8 ++++++++
 src/utils/Log.cpp                   | 13 ++++++++++++-
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/include/aidge/utils/Log.hpp b/include/aidge/utils/Log.hpp
index a400f8046..f198e83fb 100644
--- a/include/aidge/utils/Log.hpp
+++ b/include/aidge/utils/Log.hpp
@@ -145,6 +145,14 @@ public:
         mConsoleLevel = level;
     }
 
+    /**
+     * Set or disable colors on console.
+     * Initial value should be assumed true.
+    */
+    static void setConsoleColor(bool enabled) {
+        mConsoleColor = enabled;
+    }
+
     /**
      * Set the minimum log level saved in the log file.
     */
@@ -173,6 +181,7 @@ private:
     static void initFile(const std::string& fileName);
 
     static Level mConsoleLevel;
+    static bool mConsoleColor;
     static Level mFileLevel;
     static std::string mFileName;
     static std::unique_ptr<FILE, decltype(&std::fclose)> mFile;
diff --git a/python_binding/utils/pybind_Log.cpp b/python_binding/utils/pybind_Log.cpp
index 7b5e7548b..f70a4bfab 100644
--- a/python_binding/utils/pybind_Log.cpp
+++ b/python_binding/utils/pybind_Log.cpp
@@ -82,6 +82,14 @@ void init_Log(py::module& m){
           :param level: Log level.
           :type level: Level
           )mydelimiter")
+    .def_static("set_console_color", &Log::setConsoleColor, py::arg("enabled"),
+          R"mydelimiter(
+          Enables or disable color output on comsole.
+          Initial value should be assumed True.
+
+          :param enabled: Activate or deactivate colors on console.
+          :type enabled: bool
+          )mydelimiter")
     .def_static("set_file_level", &Log::setFileLevel, py::arg("level"),
           R"mydelimiter(
           Set the minimum log level saved in the log file.
diff --git a/src/utils/Log.cpp b/src/utils/Log.cpp
index 54af888ca..ae8816e78 100644
--- a/src/utils/Log.cpp
+++ b/src/utils/Log.cpp
@@ -28,6 +28,16 @@ Aidge::Log::Level Aidge::Log::mConsoleLevel = []() {
     }
     return Info;
 }();
+bool Aidge::Log::mConsoleColor = []() {
+    const char* logColor = std::getenv("AIDGE_LOG_COLOR");
+    if (logColor == nullptr)
+        return true;
+    auto logColorStr = std::string(logColor);
+    if (logColorStr == "off" || logColorStr == "OFF" ||
+        logColorStr == "0")
+        return false;
+    return true;
+}();
 Aidge::Log::Level Aidge::Log::mFileLevel = []() {
     const char* logLevel = std::getenv("AIDGE_LOGLEVEL_FILE");
     if (logLevel != nullptr) {
@@ -55,7 +65,8 @@ void Aidge::Log::log(Level level, const std::string& msg) {
         // Styles that were already applied to msg with fmt are kept also in 
         // the log file.
         const auto modifier
-            = (level == Debug) ? fmt::fg(fmt::color::gray)
+            = !mConsoleColor ? fmt::text_style()
+            : (level == Debug) ? fmt::fg(fmt::color::gray)
             : (level == Notice) ? fmt::fg(fmt::color::medium_purple)
             : (level == Warn) ? fmt::fg(fmt::color::orange)
             : (level == Error) ? fmt::fg(fmt::color::red)
-- 
GitLab