diff options
Diffstat (limited to 'src/debug')
-rw-r--r-- | src/debug/Log.cpp | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/src/debug/Log.cpp b/src/debug/Log.cpp index 0acecb04..3a008758 100644 --- a/src/debug/Log.cpp +++ b/src/debug/Log.cpp @@ -5,8 +5,6 @@ #include <iostream> void Debug::log(LogLevel level, const char* fmt, ...) { - va_list args; - va_start(args, fmt); // log to a file const std::string DEBUGPATH = ISDEBUG ? "/tmp/hypr/hyprlandd.log" : "/tmp/hypr/hyprland.log"; @@ -31,15 +29,36 @@ void Debug::log(LogLevel level, const char* fmt, ...) { } char buf[LOGMESSAGESIZE] = ""; + char* outputStr; + int logLen; + + va_list args; + va_start(args, fmt); + logLen = vsnprintf(buf, sizeof buf, fmt, args); + va_end(args); + + if ((long unsigned int)logLen < sizeof buf) { + outputStr = strdup(buf); + } else { + outputStr = (char*)malloc(logLen + 1); - vsprintf(buf, fmt, args); + if (!outputStr) { + printf("CRITICAL: Cannot alloc size %d for log! (Out of memory?)", logLen + 1); + return; + } - ofs << buf << "\n"; + va_start(args, fmt); + vsnprintf(outputStr, logLen + 1U, fmt, args); + va_end(args); + } + + ofs << outputStr << "\n"; ofs.close(); // log it to the stdout too. - std::cout << buf << "\n"; + std::cout << outputStr << "\n"; - va_end(args); + // free the log + free(outputStr); } |