diff options
author | vaxerski <[email protected]> | 2022-05-24 17:20:40 +0200 |
---|---|---|
committer | vaxerski <[email protected]> | 2022-05-24 17:20:40 +0200 |
commit | d8d8a29fc2a6e134c15dac64a919991b535acca4 (patch) | |
tree | e1b3dca93cc8bbac0c1897684d09fa8c4711fa37 | |
parent | f522c597efb3ef3efc01fd6fba9a2b4ef4cadbcc (diff) | |
download | Hyprland-d8d8a29fc2a6e134c15dac64a919991b535acca4.tar.gz Hyprland-d8d8a29fc2a6e134c15dac64a919991b535acca4.zip |
fix memory issues with logs
-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); } |