GCC Code Coverage Report
Directory: src/yaplog/ Exec Total Coverage
File: src/yaplog/internallog.h Lines: 8 8 100.0 %
Date: 2021-11-23 14:33:27 Branches: 4 4 100.0 %

Line Branch Exec Source
1
#ifndef YAPLOG_INTERNALLOG_H_6RMYFK5I
2
#define YAPLOG_INTERNALLOG_H_6RMYFK5I
3
4
#include <yaplog/level.h>
5
#include <yaplog/loglocation.h>
6
#include <yaplog/conf.h>
7
8
#include <ostream>
9
10
#define YAPLOG_LEVEL_DEFAULT "LOGLEVEL"
11
#define YAPLOG_DEST_DEFAULT  "LOGDESTINATION"
12
#define YAPLOG_COLOR_DEFAULT "LOGCOLOR"
13
#define YAPLOG_INFO_DEFAULT  "LOGINFO"
14
15
namespace logger {
16
17
    /**
18
    * @brief Main class that allow logging to a given ostream destination.
19
    */
20
    class InternalLog
21
    {
22
        public:
23
            /**
24
            * @brief Contructor of a simple logger, linked to any standard
25
            *        output stream.
26
            *
27
            * @param level The log level of the given logger.
28
            * @param loc Information of location of log in code base.
29
            * @param c Logger configuration
30
            */
31
            InternalLog(log_level level,
32
                        const log_location &loc,
33
                        const conf &c);
34
35
            /**
36
             * @brief Object destructor
37
             */
38
            virtual ~InternalLog();
39
40
            template<typename T>
41
            friend const InternalLog &operator<<(const InternalLog &, const T &);
42
43
            template<typename T>
44
            friend const InternalLog &operator<<(const InternalLog &, const T *);
45
46
            friend const InternalLog &operator<<(const InternalLog &out,
47
                                                 std::ostream &(*f)(std::ostream &));
48
49
        private:
50
            /**
51
            * @brief Convert a log level to a single character.
52
            *
53
            * @param l Loglevel to convert.
54
            *
55
            * @return Single character symbolizes log level.
56
            */
57
            char char_from_level(log_level l);
58
59
            /**
60
            * @brief Prepare and print log prefix.
61
            *
62
            * Any log use the following prefix:
63
            * - "[X] file:line(function) "
64
            *
65
            * where:
66
            *   - X is one of the following character ('F' for fatal, 'A' for
67
            *     alert, 'C' for critical, 'E' for error, 'W' for warning, 'N'
68
            *     for notice, 'I' for information, 'D' for debug, 'T' for trace
69
            *     or a space for any other level.
70
            *   - file is the name of the file where log append.
71
            *   - line is the line number where log append.
72
            *   - function is the function name where log append.
73
            */
74
            void print_header();
75
            /**
76
            * @brief Depending on color configuration flag, get ANSI escape
77
            *        code of current log level.
78
            *
79
            * @param color Color configuration flag.
80
            *
81
            * @return ANSI escape sequence.
82
            */
83
            const char *color_start(bool color);
84
            /**
85
            * @brief Depending on color configuration flag, get ANSI escape
86
            *        code to reset color.
87
            *
88
            * @param color Color configuration flag.
89
            *
90
            * @return ANSI escape reset sequence.
91
            */
92
            const char *color_end(bool color);
93
94
        protected:
95
            /**
96
            * @brief Log level.
97
            */
98
            log_level m_level;
99
            /**
100
            * @brief Log location.
101
            */
102
            log_location m_location;
103
            /**
104
            * @brief Log configuration.
105
            */
106
            const conf &m_conf;
107
    };
108
109
    /**
110
    * @brief Print any type to an internal logger instance.
111
    *
112
    * @tparam T Typename of data to print
113
    * @param out Internal logger instance where data will be printed
114
    * @param value Data to print
115
    *
116
    * @return Internal logger instance used.
117
    */
118
    template<typename T>
119
216
    inline const InternalLog &operator<<(const InternalLog &out, const T &value)
120
    {
121
216
        if (out.m_level <= out.m_conf.getSystemLevel())
122
128
            (*out.m_conf.getOutput()) << value;
123
216
        return out;
124
    }
125
    /**
126
    * @brief Print any type to an internal logger instance.
127
    *
128
    * @tparam T Typename of data to print
129
    * @param out Internal logger instance where data will be printed
130
    * @param value Pointer to data to print
131
    *
132
    * @return Internal logger instance used.
133
    */
134
    template<typename T>
135
146
    inline const InternalLog &operator<<(const InternalLog &out, const T *value)
136
    {
137
146
        if (out.m_level <= out.m_conf.getSystemLevel())
138
122
            (*out.m_conf.getOutput()) << value;
139
146
        return out;
140
    }
141
142
    /**
143
    * @brief Additional overload to handle ostream specific IO
144
    *         manipulators
145
    *
146
    * @param out Internal logger where IO manipulators must aplly.
147
    * @param f IO function manipulators.
148
    *
149
    * @return Internal logger instance.
150
    */
151
    inline const InternalLog &operator<<(const InternalLog &out,
152
                                         std::ostream &(*f)(std::ostream &))
153
    {
154
        if (out.m_level <= out.m_conf.getSystemLevel())
155
            (*out.m_conf.getOutput()) << f;
156
        return out;
157
    }
158
159
};
160
161
#endif /* end of include guard: YAPLOG_INTERNALLOG_H_6RMYFK5I */