summaryrefslogtreecommitdiff
path: root/Log.cc
blob: a0ee7582cb6e8ba75249a417481f2961fa0a4454 (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
#include <iostream>
#include <string>

#include "Color.hh"
#include "Log.hh"

namespace pcc {
void Log(LogLevel level, const char* format, ...) {
    char buf[1024];
    va_list va;
    va_start(va, format);
    if (GetLogLevel() <= level) {
        std::cerr << GetExecutableName() << ": ";
        switch (level) {
        case PCC_ERROR:
            std::cerr << Color::RED << "error: ";
            break;
        case PCC_WARNING:
            std::cerr << Color::MAGENTA << "warning: ";
            break;
        case PCC_INFO:
            std::cerr << Color::CYAN << "info: ";
            break;
        case PCC_DEBUG:
            std::cerr << Color::GREEN << "remark: ";
            break;
        }
        std::vsnprintf(buf, sizeof(buf), format, va);
        std::cerr << Color::WHITE << buf << Color::DEFAULT << std::endl;
    }
    va_end(va);
}

static LogLevel CurrentLogLevel = PCC_ERROR;

void SetLogLevel(LogLevel level) { CurrentLogLevel = level; }
LogLevel GetLogLevel() { return CurrentLogLevel; }

static std::string Executable("pcc");

void SetExecutableName(const char* name) { Executable = name; }
const char* GetExecutableName() { return Executable.c_str(); }
} // namespace pcc