GCC Code Coverage Report
Directory: src/yaplog/ Exec Total Coverage
File: src/yaplog/internallog.cpp Lines: 68 72 94.4 %
Date: 2021-11-23 14:33:27 Branches: 30 34 88.2 %

Line Branch Exec Source
1
#include "yaplog.h"
2
3
#include <iostream>
4
#include <cstring>
5
#include <fstream>
6
7
#include "color.h"
8
9
using namespace logger;
10
11
109
InternalLog::InternalLog(log_level level,
12
                         const log_location &loc,
13
109
                         const conf &c)
14
109
    : m_level(level)
15
109
    , m_location(loc)
16
109
    , m_conf(c)
17
{
18

109
    if (m_level <= m_conf.getSystemLevel())
19
73
        print_header();
20
109
}
21
22
220
InternalLog::~InternalLog()
23
{
24
218
    if (m_level <= m_conf.getSystemLevel()) {
25
146
        (*this) << "\n";
26
    }
27
220
}
28
29
73
char InternalLog::char_from_level(enum log_level l)
30
{
31


73
    switch (l) {
32
13
        case fatal:
33
13
            return 'F';
34
11
        case alert:
35
11
            return 'A';
36
10
        case crit:
37
10
            return 'C';
38
9
        case error:
39
9
            return 'E';
40
8
        case warn:
41
8
            return 'W';
42
7
        case notice:
43
7
            return 'N';
44
6
        case info:
45
6
            return 'I';
46
5
        case debug:
47
5
            return 'D';
48
4
        case trace:
49
4
            return 'T';
50
        case none:
51
        default:
52
            return ' ';
53
    }
54
}
55
56
73
void InternalLog::print_header()
57
{
58
73
    bool color = m_conf.getColor();
59
73
    std::ostream *output = m_conf.getOutput();
60
    (*output) << color_start(color)
61
73
              << "[" << char_from_level(m_level) << "]";
62
73
    if (m_conf.getInfo()) {
63
18
        (*output) << " " << m_location.m_file << ":"
64
18
                    << m_location.m_line << "("
65
18
                    << m_location.m_function << ")";
66
    }
67
73
    (*output) << color_end(color) << " ";
68
73
}
69
70
71
72
73
const char *InternalLog::color_start(bool color)
73
{
74
73
    if (!color)
75
55
        return "";
76
77


18
    switch (m_level) {
78
2
        case fatal:
79
2
            return ANSI_COLOR_FATAL;
80
2
        case alert:
81
2
            return ANSI_COLOR_ALERT;
82
2
        case crit:
83
2
            return ANSI_COLOR_CRITIC;
84
2
        case error:
85
2
            return ANSI_COLOR_ERROR;
86
2
        case warn:
87
2
            return ANSI_COLOR_WARNING;
88
2
        case notice:
89
2
            return ANSI_COLOR_NOTICE;
90
2
        case info:
91
2
            return ANSI_COLOR_INFORMATION;
92
2
        case debug:
93
2
            return ANSI_COLOR_DEBUG;
94
2
        case trace:
95
2
            return ANSI_COLOR_TRACE;
96
        case none:
97
        default:
98
            return "";
99
    }
100
}
101
102
73
const char *InternalLog::color_end(bool color)
103
{
104
73
    if (color)
105
18
        return ANSI_COLOR_RESET;
106
    else
107
55
        return "";
108
}