logging: move to an internal rolling log buffer

disables logging to the logfile by default
This commit is contained in:
Vaxry 2023-11-14 20:06:04 +00:00
parent e8469f8b1b
commit e195e51c1b
7 changed files with 47 additions and 23 deletions

View file

@ -396,6 +396,8 @@ int main(int argc, char** argv) {
request(fullRequest); request(fullRequest);
else if (fullRequest.contains("/globalshortcuts")) else if (fullRequest.contains("/globalshortcuts"))
request(fullRequest); request(fullRequest);
else if (fullRequest.contains("/rollinglog"))
request(fullRequest);
else if (fullRequest.contains("/instances")) else if (fullRequest.contains("/instances"))
instancesRequest(json); instancesRequest(json);
else if (fullRequest.contains("/switchxkblayout")) else if (fullRequest.contains("/switchxkblayout"))

View file

@ -145,7 +145,7 @@ void CConfigManager::setDefaultVars() {
configValues["debug:log_damage"].intValue = 0; configValues["debug:log_damage"].intValue = 0;
configValues["debug:overlay"].intValue = 0; configValues["debug:overlay"].intValue = 0;
configValues["debug:damage_blink"].intValue = 0; configValues["debug:damage_blink"].intValue = 0;
configValues["debug:disable_logs"].intValue = 0; configValues["debug:disable_logs"].intValue = 1;
configValues["debug:disable_time"].intValue = 1; configValues["debug:disable_time"].intValue = 1;
configValues["debug:enable_stdout_logs"].intValue = 0; configValues["debug:enable_stdout_logs"].intValue = 0;
configValues["debug:damage_tracking"].intValue = DAMAGE_TRACKING_FULL; configValues["debug:damage_tracking"].intValue = DAMAGE_TRACKING_FULL;

View file

@ -119,7 +119,7 @@ void CrashReporter::createAndSaveCrash(int sig) {
finalCrashReport += "\n\nLog tail:\n"; finalCrashReport += "\n\nLog tail:\n";
finalCrashReport += execAndGet(("cat \"" + Debug::logFile + "\" | tail -n 50").c_str()); finalCrashReport += Debug::rollingLog;
const auto HOME = getenv("HOME"); const auto HOME = getenv("HOME");
const auto CACHE_HOME = getenv("XDG_CACHE_HOME"); const auto CACHE_HOME = getenv("XDG_CACHE_HOME");

View file

@ -625,6 +625,20 @@ std::string animationsRequest(HyprCtl::eHyprCtlOutputFormat format) {
return ret; return ret;
} }
std::string rollinglogRequest(HyprCtl::eHyprCtlOutputFormat format) {
std::string result = "";
if (format == HyprCtl::FORMAT_JSON) {
result += "[\n\"log\":\"";
result += escapeJSONStrings(Debug::rollingLog);
result += "\"]";
} else {
result = Debug::rollingLog;
}
return result;
}
std::string globalShortcutsRequest(HyprCtl::eHyprCtlOutputFormat format) { std::string globalShortcutsRequest(HyprCtl::eHyprCtlOutputFormat format) {
std::string ret = ""; std::string ret = "";
const auto SHORTCUTS = g_pProtocolManager->m_pGlobalShortcutsProtocolManager->getAllShortcuts(); const auto SHORTCUTS = g_pProtocolManager->m_pGlobalShortcutsProtocolManager->getAllShortcuts();
@ -1351,6 +1365,8 @@ std::string getReply(std::string request) {
return globalShortcutsRequest(format); return globalShortcutsRequest(format);
else if (request == "animations") else if (request == "animations")
return animationsRequest(format); return animationsRequest(format);
else if (request == "rollinglog")
return rollinglogRequest(format);
else if (request.starts_with("plugin")) else if (request.starts_with("plugin"))
return dispatchPlugin(request); return dispatchPlugin(request);
else if (request.starts_with("notify")) else if (request.starts_with("notify"))

View file

@ -19,7 +19,8 @@ namespace HyprCtl {
inline int iSocketFD = -1; inline int iSocketFD = -1;
enum eHyprCtlOutputFormat { enum eHyprCtlOutputFormat
{
FORMAT_NORMAL = 0, FORMAT_NORMAL = 0,
FORMAT_JSON FORMAT_JSON
}; };

View file

@ -10,25 +10,24 @@ void Debug::init(const std::string& IS) {
} }
void Debug::wlrLog(wlr_log_importance level, const char* fmt, va_list args) { void Debug::wlrLog(wlr_log_importance level, const char* fmt, va_list args) {
if (disableLogs && *disableLogs)
return;
if (level > wlr_log_get_verbosity()) if (level > wlr_log_get_verbosity())
return; return;
char* outputStr = nullptr; char* outputStr = nullptr;
std::ofstream ofs;
ofs.open(logFile, std::ios::out | std::ios::app);
vasprintf(&outputStr, fmt, args); vasprintf(&outputStr, fmt, args);
std::string output = std::string(outputStr); std::string output = std::string(outputStr);
free(outputStr); free(outputStr);
ofs << "[wlr] " << output << "\n"; rollingLog += output + "\n";
ofs.close(); if (!disableLogs || !*disableLogs) {
std::ofstream ofs;
ofs.open(logFile, std::ios::out | std::ios::app);
ofs << "[wlr] " << output << "\n";
ofs.close();
}
if (!disableStdout) if (!disableStdout)
std::cout << output << "\n"; std::cout << output << "\n";

View file

@ -7,9 +7,11 @@
#include "../includes.hpp" #include "../includes.hpp"
#include "../helpers/MiscFunctions.hpp" #include "../helpers/MiscFunctions.hpp"
#define LOGMESSAGESIZE 1024 #define LOGMESSAGESIZE 1024
#define ROLLING_LOG_SIZE 4096
enum LogLevel { enum LogLevel
{
NONE = -1, NONE = -1,
LOG = 0, LOG = 0,
WARN, WARN,
@ -26,12 +28,11 @@ namespace Debug {
inline bool disableStdout = false; inline bool disableStdout = false;
inline bool trace = false; inline bool trace = false;
inline std::string rollingLog = ""; // rolling log contains the ROLLING_LOG_SIZE tail of the log
void init(const std::string& IS); void init(const std::string& IS);
template <typename... Args> template <typename... Args>
void log(LogLevel level, std::format_string<Args...> fmt, Args&&... args) { void log(LogLevel level, std::format_string<Args...> fmt, Args&&... args) {
if (disableLogs && *disableLogs)
return;
if (level == TRACE && !trace) if (level == TRACE && !trace)
return; return;
@ -47,10 +48,6 @@ namespace Debug {
default: break; default: break;
} }
// log to a file
std::ofstream ofs;
ofs.open(logFile, std::ios::out | std::ios::app);
// print date and time to the ofs // print date and time to the ofs
if (disableTime && !*disableTime) { if (disableTime && !*disableTime) {
#ifndef _LIBCPP_VERSION #ifndef _LIBCPP_VERSION
@ -69,9 +66,18 @@ namespace Debug {
// 3. this is actually what std::format in stdlib does // 3. this is actually what std::format in stdlib does
logMsg += std::vformat(fmt.get(), std::make_format_args(args...)); logMsg += std::vformat(fmt.get(), std::make_format_args(args...));
ofs << logMsg << "\n"; rollingLog += logMsg + "\n";
if (rollingLog.size() > ROLLING_LOG_SIZE)
rollingLog = rollingLog.substr(rollingLog.size() - ROLLING_LOG_SIZE);
ofs.close(); if (!disableLogs || !*disableLogs) {
// log to a file
std::ofstream ofs;
ofs.open(logFile, std::ios::out | std::ios::app);
ofs << logMsg << "\n";
ofs.close();
}
// log it to the stdout too. // log it to the stdout too.
if (!disableStdout) if (!disableStdout)