From 4ab5871dc4f93148934a24cf4d0278e5bfc48f08 Mon Sep 17 00:00:00 2001 From: elsid Date: Mon, 13 May 2024 00:40:22 +0200 Subject: [PATCH 1/2] Split Tee logic into different types --- components/debug/debugging.cpp | 140 +++++++++++++++++++++------------ 1 file changed, 88 insertions(+), 52 deletions(-) diff --git a/components/debug/debugging.cpp b/components/debug/debugging.cpp index 67e7ecaaf3..13a72c34dc 100644 --- a/components/debug/debugging.cpp +++ b/components/debug/debugging.cpp @@ -221,51 +221,32 @@ namespace Debug }; #else - class Tee : public DebugOutputBase + namespace { - public: - Tee(std::ostream& stream, std::ostream& stream2) - : out(stream) - , out2(stream2) + Color getColor(Level level) { - // TODO: check which stream is stderr? - mUseColor = useColoredOutput(); - - mColors[Error] = Red; - mColors[Warning] = Yellow; - mColors[Info] = Reset; - mColors[Verbose] = DarkGray; - mColors[Debug] = DarkGray; - mColors[NoLevel] = Reset; - } - - std::streamsize writeImpl(const char* str, std::streamsize size, Level debugLevel) override - { - out.write(str, size); - out.flush(); - - if (mUseColor) + switch (level) { - out2 << "\033[0;" << mColors[debugLevel] << "m"; - out2.write(str, size); - out2 << "\033[0;" << Reset << "m"; + case Error: + return Red; + case Warning: + return Yellow; + case Info: + return Reset; + case Verbose: + return DarkGray; + case Debug: + return DarkGray; + case NoLevel: + return Reset; } - else - { - out2.write(str, size); - } - out2.flush(); - - return size; + return Reset; } - virtual ~Tee() = default; - - private: - static bool useColoredOutput() + bool useColoredOutput() { #if defined(_WIN32) - if (getenv("NO_COLOR")) + if (std::getenv("NO_COLOR") != nullptr) return false; DWORD mode; @@ -273,22 +254,77 @@ namespace Debug return true; // some console emulators may not use the Win32 API, so try the Unixy approach - char* term = getenv("TERM"); - return term && GetFileType(GetStdHandle(STD_ERROR_HANDLE)) == FILE_TYPE_CHAR; + return std::getenv("TERM") != nullptr && GetFileType(GetStdHandle(STD_ERROR_HANDLE)) == FILE_TYPE_CHAR; #else - char* term = getenv("TERM"); - bool useColor = term && !getenv("NO_COLOR") && isatty(fileno(stderr)); - - return useColor; + return std::getenv("TERM") != nullptr && std::getenv("NO_COLOR") == nullptr && isatty(fileno(stderr)); #endif } - std::ostream& out; - std::ostream& out2; - bool mUseColor; + class Identity + { + public: + explicit Identity(std::ostream& stream) + : mStream(stream) + { + } - std::map mColors; - }; + void write(const char* str, std::streamsize size, Level /*level*/) + { + mStream.write(str, size); + mStream.flush(); + } + + private: + std::ostream& mStream; + }; + + class Coloured + { + public: + explicit Coloured(std::ostream& stream) + : mStream(stream) + // TODO: check which stream is stderr? + , mUseColor(useColoredOutput()) + { + } + + void write(const char* str, std::streamsize size, Level level) + { + if (mUseColor) + mStream << "\033[0;" << getColor(level) << 'm'; + mStream.write(str, size); + if (mUseColor) + mStream << "\033[0;" << Reset << 'm'; + mStream.flush(); + } + + private: + std::ostream& mStream; + bool mUseColor; + }; + + template + class Tee : public DebugOutputBase + { + public: + explicit Tee(First first, Second second) + : mFirst(first) + , mSecond(second) + { + } + + std::streamsize writeImpl(const char* str, std::streamsize size, Level debugLevel) override + { + mFirst.write(str, size, debugLevel); + mSecond.write(str, size, debugLevel); + return size; + } + + private: + First mFirst; + Second mSecond; + }; + } #endif } @@ -301,8 +337,8 @@ static std::ofstream logfile; #if defined(_WIN32) && defined(_DEBUG) static boost::iostreams::stream_buffer sb; #else -static boost::iostreams::stream_buffer coutsb; -static boost::iostreams::stream_buffer cerrsb; +static boost::iostreams::stream_buffer> coutsb; +static boost::iostreams::stream_buffer> cerrsb; #endif std::ostream& getRawStdout() @@ -332,8 +368,8 @@ void setupLogging(const std::filesystem::path& logDir, std::string_view appName, const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log"; logfile.open(logDir / logName, mode); - coutsb.open(Debug::Tee(logfile, *rawStdout)); - cerrsb.open(Debug::Tee(logfile, *rawStderr)); + coutsb.open(Debug::Tee(Debug::Identity(logfile), Debug::Coloured(*rawStdout))); + cerrsb.open(Debug::Tee(Debug::Identity(logfile), Debug::Coloured(*rawStderr))); std::cout.rdbuf(&coutsb); std::cerr.rdbuf(&cerrsb); From 38f0533bcfce28c716dc88b7770d9f72648b7510 Mon Sep 17 00:00:00 2001 From: elsid Date: Mon, 13 May 2024 01:08:05 +0200 Subject: [PATCH 2/2] Write to log file records captured before configs are loaded --- components/debug/debugging.cpp | 66 +++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/components/debug/debugging.cpp b/components/debug/debugging.cpp index 13a72c34dc..f828bfae62 100644 --- a/components/debug/debugging.cpp +++ b/components/debug/debugging.cpp @@ -223,6 +223,14 @@ namespace Debug namespace { + struct Record + { + std::string mValue; + Level mLevel; + }; + + std::vector globalBuffer; + Color getColor(Level level) { switch (level) @@ -303,6 +311,23 @@ namespace Debug bool mUseColor; }; + class Buffer + { + public: + explicit Buffer(std::vector& buffer) + : mBuffer(buffer) + { + } + + void write(const char* str, std::streamsize size, Level debugLevel) + { + mBuffer.push_back(Record{ std::string(str, size), debugLevel }); + } + + private: + std::vector& mBuffer; + }; + template class Tee : public DebugOutputBase { @@ -337,8 +362,10 @@ static std::ofstream logfile; #if defined(_WIN32) && defined(_DEBUG) static boost::iostreams::stream_buffer sb; #else -static boost::iostreams::stream_buffer> coutsb; -static boost::iostreams::stream_buffer> cerrsb; +static boost::iostreams::stream_buffer> standardOut; +static boost::iostreams::stream_buffer> standardErr; +static boost::iostreams::stream_buffer> bufferedOut; +static boost::iostreams::stream_buffer> bufferedErr; #endif std::ostream& getRawStdout() @@ -359,20 +386,22 @@ Misc::Locked getLockedRawStderr() // Redirect cout and cerr to the log file void setupLogging(const std::filesystem::path& logDir, std::string_view appName, std::ios_base::openmode mode) { -#if defined(_WIN32) && defined(_DEBUG) - // Redirect cout and cerr to VS debug output when running in debug mode - sb.open(Debug::DebugOutput()); - std::cout.rdbuf(&sb); - std::cerr.rdbuf(&sb); -#else +#if !(defined(_WIN32) && defined(_DEBUG)) const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log"; logfile.open(logDir / logName, mode); - coutsb.open(Debug::Tee(Debug::Identity(logfile), Debug::Coloured(*rawStdout))); - cerrsb.open(Debug::Tee(Debug::Identity(logfile), Debug::Coloured(*rawStderr))); + Debug::Identity log(logfile); - std::cout.rdbuf(&coutsb); - std::cerr.rdbuf(&cerrsb); + for (const Debug::Record& v : Debug::globalBuffer) + log.write(v.mValue.data(), v.mValue.size(), v.mLevel); + + Debug::globalBuffer.clear(); + + standardOut.open(Debug::Tee(log, Debug::Coloured(*rawStdout))); + standardErr.open(Debug::Tee(log, Debug::Coloured(*rawStderr))); + + std::cout.rdbuf(&standardOut); + std::cerr.rdbuf(&standardErr); #endif #ifdef _WIN32 @@ -392,6 +421,19 @@ int wrapApplication(int (*innerApplication)(int argc, char* argv[]), int argc, c rawStderr = std::make_unique(std::cerr.rdbuf()); rawStderrMutex = std::make_unique(); +#if defined(_WIN32) && defined(_DEBUG) + // Redirect cout and cerr to VS debug output when running in debug mode + sb.open(Debug::DebugOutput()); + std::cout.rdbuf(&sb); + std::cerr.rdbuf(&sb); +#else + bufferedOut.open(Debug::Tee(Debug::Buffer(Debug::globalBuffer), Debug::Coloured(*rawStdout))); + bufferedErr.open(Debug::Tee(Debug::Buffer(Debug::globalBuffer), Debug::Coloured(*rawStderr))); + + std::cout.rdbuf(&bufferedOut); + std::cerr.rdbuf(&bufferedErr); +#endif + int ret = 0; try {