aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/debug/Log.cpp
blob: 0def77c003e83a0915c2319a9f34837a84880412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "Log.hpp"
#include "../defines.hpp"
#include "../Compositor.hpp"
#include "RollingLogFollow.hpp"

#include <fstream>
#include <iostream>

void Debug::init(const std::string& IS) {
    logFile = IS + (ISDEBUG ? "/hyprlandd.log" : "/hyprland.log");
    logOfs.open(logFile, std::ios::out | std::ios::app);
}

void Debug::close() {
    logOfs.close();
}

void Debug::log(LogLevel level, std::string str) {
    if (level == TRACE && !trace)
        return;

    if (shuttingDown)
        return;

    std::string coloredStr = str;
    switch (level) {
        case LOG:
            str        = "[LOG] " + str;
            coloredStr = str;
            break;
        case WARN:
            str        = "[WARN] " + str;
            coloredStr = "\033[1;33m" + str + "\033[0m"; // yellow
            break;
        case ERR:
            str        = "[ERR] " + str;
            coloredStr = "\033[1;31m" + str + "\033[0m"; // red
            break;
        case CRIT:
            str        = "[CRITICAL] " + str;
            coloredStr = "\033[1;35m" + str + "\033[0m"; // magenta
            break;
        case INFO:
            str        = "[INFO] " + str;
            coloredStr = "\033[1;32m" + str + "\033[0m"; // green
            break;
        case TRACE:
            str        = "[TRACE] " + str;
            coloredStr = "\033[1;34m" + str + "\033[0m"; // blue
            break;
        default: break;
    }

    rollingLog += str + "\n";
    if (rollingLog.size() > ROLLING_LOG_SIZE)
        rollingLog = rollingLog.substr(rollingLog.size() - ROLLING_LOG_SIZE);

    if (RollingLogFollow::Get().IsRunning())
        RollingLogFollow::Get().AddLog(str);

    if (!disableLogs || !**disableLogs) {
        // log to a file
        logOfs << str << "\n";
        logOfs.flush();
    }

    // log it to the stdout too.
    if (!disableStdout)
        std::cout << ((coloredLogs && !**coloredLogs) ? str : coloredStr) << "\n";
}