Hyprland/src/debug/Log.cpp

75 lines
1.6 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
2022-06-03 17:48:07 +02:00
void Debug::init(std::string IS) {
if (ISDEBUG)
2022-06-03 17:48:07 +02:00
logFile = "/tmp/hypr/" + IS + "/hyprlandd.log";
else
2022-06-03 17:48:07 +02:00
logFile = "/tmp/hypr/" + IS + "/hyprland.log";
}
2022-03-16 20:50:55 +01:00
void Debug::log(LogLevel level, const char* fmt, ...) {
// 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;
2022-06-25 20:50:29 +02:00
case INFO:
ofs << "[INFO] ";
break;
2022-03-16 20:50:55 +01:00
default:
break;
}
char buf[LOGMESSAGESIZE] = "";
2022-05-24 17:20:40 +02:00
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);
2022-03-16 20:50:55 +01:00
2022-05-24 17:20:40 +02:00
if (!outputStr) {
printf("CRITICAL: Cannot alloc size %d for log! (Out of memory?)", logLen + 1);
return;
}
2022-03-16 20:50:55 +01:00
2022-05-24 17:20:40 +02:00
va_start(args, fmt);
vsnprintf(outputStr, logLen + 1U, fmt, args);
va_end(args);
}
ofs << outputStr << "\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.
2022-05-24 17:20:40 +02:00
std::cout << outputStr << "\n";
2022-03-17 15:53:45 +01:00
2022-05-24 17:20:40 +02:00
// free the log
free(outputStr);
2022-03-16 20:50:55 +01:00
}