1717
1818#ifdef _WIN32
1919#include < Utils/Platform/WindowsHeaders.hpp>
20+ #include < io.h>
21+ #define isatty _isatty
22+ #define fileno _fileno
23+ #else
24+ #include < unistd.h>
2025#endif
2126
2227class ConsoleLogger : public dotnamecpp ::logging::ILogger {
@@ -36,6 +41,9 @@ class ConsoleLogger : public dotnamecpp::logging::ILogger {
3641 dotnamecpp::logging::Level currentLevel_ = dotnamecpp::logging::Level::LOG_INFO;
3742#endif
3843
44+ bool colorEnabled_ = true ; // User can override
45+ bool autoDetectColor_ = true ; // Auto-detect TTY support
46+
3947public:
4048 ConsoleLogger () = default ;
4149 ~ConsoleLogger () {
@@ -49,7 +57,8 @@ class ConsoleLogger : public dotnamecpp::logging::ILogger {
4957 ConsoleLogger &operator =(const ConsoleLogger &) = delete ;
5058 ConsoleLogger (ConsoleLogger &&other) noexcept
5159 : logFile_(std::move(other.logFile_)), addNewLine_(other.addNewLine_),
52- appPrefix_ (std::move(other.appPrefix_)), currentLevel_(other.currentLevel_) {}
60+ appPrefix_ (std::move(other.appPrefix_)), currentLevel_(other.currentLevel_),
61+ colorEnabled_(other.colorEnabled_), autoDetectColor_(other.autoDetectColor_) {}
5362
5463 ConsoleLogger &operator =(ConsoleLogger &&other) noexcept {
5564 if (this != &other) {
@@ -62,6 +71,8 @@ class ConsoleLogger : public dotnamecpp::logging::ILogger {
6271 addNewLine_ = other.addNewLine_ ;
6372 appPrefix_ = std::move (other.appPrefix_ );
6473 currentLevel_ = other.currentLevel_ ;
74+ colorEnabled_ = other.colorEnabled_ ;
75+ autoDetectColor_ = other.autoDetectColor_ ;
6576 }
6677 return *this ;
6778 }
@@ -122,13 +133,17 @@ class ConsoleLogger : public dotnamecpp::logging::ILogger {
122133 header << " " ;
123134
124135 // Log to console
125- setConsoleColor (level);
136+ if (shouldUseColors ()) {
137+ setConsoleColor (level);
138+ }
126139 std::cout << header.str () << message;
127140 if (addNewLine_) {
128141 std::cout << " \n " ;
129142 }
130143
131- resetConsoleColor ();
144+ if (shouldUseColors ()) {
145+ resetConsoleColor ();
146+ }
132147
133148 // Log to file if enabled
134149 if (logFile_.is_open ()) {
@@ -404,6 +419,45 @@ class ConsoleLogger : public dotnamecpp::logging::ILogger {
404419 showHeaderCaller (incCaller);
405420 showHeaderLevel (incLevel);
406421 }
422+
423+ /* *
424+ * @brief Enable or disable colored output
425+ *
426+ * @param enabled Whether to use colored output (overrides auto-detection)
427+ */
428+ void setColorEnabled (bool enabled) {
429+ std::lock_guard<std::mutex> lock (logMutex_);
430+ colorEnabled_ = enabled;
431+ autoDetectColor_ = false ; // Manual override disables auto-detection
432+ }
433+
434+ /* *
435+ * @brief Enable automatic color detection based on TTY
436+ *
437+ * @param autoDetect Whether to auto-detect color support
438+ */
439+ void setAutoDetectColor (bool autoDetect) {
440+ std::lock_guard<std::mutex> lock (logMutex_);
441+ autoDetectColor_ = autoDetect;
442+ }
443+
444+ private:
445+ /* *
446+ * @brief Check if colors should be used for console output
447+ *
448+ * @return true if colors should be used, false otherwise
449+ */
450+ bool shouldUseColors () const {
451+ if (!autoDetectColor_) {
452+ return colorEnabled_;
453+ }
454+ // Auto-detect: check if stdout is a TTY
455+ #ifdef __EMSCRIPTEN__
456+ return false ; // No color support in Emscripten
457+ #else
458+ return colorEnabled_ && (isatty (fileno (stdout)) != 0 );
459+ #endif
460+ }
407461}; // class ConsoleLogger
408462
409463#endif
0 commit comments