Hyprland/src/debug/Log.cpp

81 lines
2.0 KiB
C++
Raw Normal View History

2022-03-16 20:50:55 +01:00
#include "Log.hpp"
#include "../defines.hpp"
2022-06-03 17:48:07 +02:00
#include "../Compositor.hpp"
2022-03-16 20:50:55 +01:00
#include <fstream>
2022-03-17 15:53:45 +01:00
#include <iostream>
2022-03-16 20:50:55 +01:00
void Debug::init(const std::string& IS) {
2022-08-26 18:02:07 +02:00
logFile = "/tmp/hypr/" + IS + (ISDEBUG ? "/hyprlandd.log" : "/hyprland.log");
}
2022-08-22 18:50:38 +02:00
void Debug::wlrLog(wlr_log_importance level, const char* fmt, va_list args) {
char* outputStr = nullptr;
2022-08-22 18:58:29 +02:00
std::ofstream ofs;
ofs.open(logFile, std::ios::out | std::ios::app);
vasprintf(&outputStr, fmt, args);
std::string output = std::string(outputStr);
free(outputStr);
ofs << "[wlr] " << output << "\n";
ofs.close();
2022-08-22 18:50:38 +02:00
}
2022-03-16 20:50:55 +01:00
void Debug::log(LogLevel level, const char* fmt, ...) {
2022-07-01 15:57:56 +02:00
if (disableLogs && *disableLogs)
return;
2022-03-16 20:50:55 +01:00
// log to a file
std::ofstream ofs;
ofs.open(logFile, std::ios::out | std::ios::app);
2022-03-16 20:50:55 +01:00
switch (level) {
case LOG: ofs << "[LOG] "; break;
case WARN: ofs << "[WARN] "; break;
case ERR: ofs << "[ERR] "; break;
case CRIT: ofs << "[CRITICAL] "; break;
case INFO: ofs << "[INFO] "; break;
default: break;
2022-03-16 20:50:55 +01:00
}
2022-07-18 11:46:42 +02:00
// print date and time to the ofs
if (disableTime && !*disableTime) {
auto timet = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
2022-07-18 11:46:42 +02:00
const auto MILLIS = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count() % 1000;
2022-09-25 20:07:48 +02:00
2022-07-18 11:46:42 +02:00
ofs << std::put_time(std::localtime(&timet), "[%H:%M:%S:");
if (MILLIS > 99)
ofs << MILLIS;
else if (MILLIS > 9)
ofs << "0" << MILLIS;
else
ofs << "00" << MILLIS;
ofs << "] ";
}
char* outputStr = nullptr;
2022-05-24 17:20:40 +02:00
va_list args;
va_start(args, fmt);
2022-07-25 22:40:34 +02:00
vasprintf(&outputStr, fmt, args);
2022-05-24 17:20:40 +02:00
va_end(args);
2022-07-25 22:40:34 +02:00
std::string output = std::string(outputStr);
free(outputStr);
2022-05-24 17:20:40 +02:00
2022-07-25 22:40:34 +02:00
ofs << output << "\n";
2022-03-16 20:50:55 +01:00
ofs.close();
2022-03-17 15:53:45 +01:00
// log it to the stdout too.
2023-03-24 14:00:54 +01:00
if (!disableStdout)
std::cout << output << "\n";
2022-03-16 20:50:55 +01:00
}