GCC Code Coverage Report
Directory: src/yaplog/ Exec Total Coverage
File: src/yaplog/conf.cpp Lines: 64 64 100.0 %
Date: 2021-11-23 14:33:27 Branches: 34 46 73.9 %

Line Branch Exec Source
1
#include "conf.h"
2
3
#include <iostream>
4
#include <fstream>
5
#include <cstring>
6
7
using namespace logger;
8
9
135
conf::conf(const char *destination,
10
           const char *level,
11
           const char *color,
12
135
           const char *info)
13
135
    : m_output(NULL)
14
135
    , m_err(std::cerr.rdbuf())
15

135
    , m_out(std::cout.rdbuf())
16
{
17
135
    parse_destination(destination);
18
135
    parse_level(level);
19
135
    parse_color(color);
20
135
    parse_info(info);
21
135
}
22
23
135
conf::~conf()
24
{
25
135
}
26
27
28
487
const log_level &conf::getSystemLevel() const
29
{
30
487
    return m_systemlevel;
31
}
32
33
264
std::ostream *conf::getOutput() const
34
{
35
264
    return m_output;
36
}
37
38
79
bool conf::getColor() const
39
{
40
79
    return m_color;
41
}
42
43
76
bool conf::getInfo() const
44
{
45
76
    return m_info;
46
}
47
48
135
void conf::parse_destination(const char *destination)
49
{
50
135
    const char *value = getenv(destination);
51
135
    if (value == NULL) {
52
22
        m_output = &m_err;
53
    } else {
54
113
        if (strcmp(value, "stderr") == 0) {
55
1
            m_output = &m_err;
56
112
        } else if (strcmp(value, "stdout") == 0) {
57
1
            m_output = &m_out;
58
        } else {
59
111
            m_file = std::ofstream(value, std::ofstream::app);
60
111
            if (m_file.is_open()) {
61
110
                m_output = &m_file;
62
            } else {
63
1
                std::cerr << "Fail to open " << value << std::endl;
64
1
                m_output = &m_err;
65
            }
66
        }
67
    }
68
135
}
69
70
135
void conf::parse_level(const char *level)
71
{
72
135
    const char *loglevel = std::getenv(level);
73
135
    if (loglevel == NULL) {
74
13
        m_systemlevel = log_level::none;
75
    } else {
76
        try {
77

124
            int value = std::stoi(loglevel);
78
121
            if (value < 0)
79
1
                m_systemlevel = log_level::none;
80
120
            else if (value > 9)
81
1
                m_systemlevel = log_level::trace;
82
            else
83
119
                m_systemlevel = static_cast<log_level>(value);
84
1
        } catch (std::exception &) {
85
1
            m_systemlevel = log_level::none;
86
1
        }
87
    }
88
135
}
89
90
135
void conf::parse_color(const char *color)
91
{
92
135
    const char *value = std::getenv(color);
93
135
    if (value == NULL) {
94
112
        m_color = false;
95
    } else {
96
        try {
97

25
            int val = std::stoi(value);
98
22
            m_color = (val == 1);
99
1
        } catch (std::exception &) {
100
1
            m_color = false;
101
1
        }
102
    }
103
135
}
104
105
135
void conf::parse_info(const char *info)
106
{
107
135
    const char *value = std::getenv(info);
108
135
    if (value == NULL) {
109
115
        m_info = false;
110
    } else {
111
20
        m_info = (strcmp(value, "debug") == 0);
112
    }
113
135
}