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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/*
* Copyright (C) 2015 by Multi-Tech Systems
*
* This file is part of libmts.
*
* libmts is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* libmts is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libmts. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*! \file MTS_Logger.h
\brief A logging utility
\date 15JUN11
\author Sean Godinez
A logging utility capable of writing to stdout, syslog, or file
*/
#ifndef MTS_LOGGER_H_
#define MTS_LOGGER_H_
#include <mts/MTS_NonCopyable.h>
#include <mts/MTS_NonConstructable.h>
#include <mts/MTS_Lock.h>
#include <mts/MTS_Stdint.h>
#include <string>
#ifdef DEBUG
#define printFatal(format, ...) \
if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::FATAL_LEVEL)) \
MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::FATAL_LEVEL, MTS::Logger::PrintLevel::FATAL_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__)
#define printError(format, ...) \
if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::ERROR_LEVEL)) \
MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::ERROR_LEVEL, MTS::Logger::PrintLevel::ERROR_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__)
#define printWarning(format, ...) \
if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::WARNING_LEVEL)) \
MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::WARNING_LEVEL, MTS::Logger::PrintLevel::WARNING_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__)
#define printInfo(format, ...) \
if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::INFO_LEVEL)) \
MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::INFO_LEVEL, MTS::Logger::PrintLevel::INFO_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__)
#define printConfig(format, ...) \
if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::CONFIG_LEVEL)) \
MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::CONFIG_LEVEL, MTS::Logger::PrintLevel::CONFIG_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__)
#define printDebug(format, ...) \
if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::DEBUG_LEVEL)) \
MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::DEBUG_LEVEL, MTS::Logger::PrintLevel::DEBUG_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__)
#define printTrace(format, ...) \
if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::TRACE_LEVEL)) \
MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::TRACE_LEVEL, MTS::Logger::PrintLevel::TRACE_LABEL, "%s:%s:%d| " format, __FILE__, __func__, __LINE__, ##__VA_ARGS__)
#else
#define printFatal(format, ...) \
if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::FATAL_LEVEL)) \
MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::FATAL_LEVEL, MTS::Logger::PrintLevel::FATAL_LABEL, format, ##__VA_ARGS__)
#define printError(format, ...) \
if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::ERROR_LEVEL)) \
MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::ERROR_LEVEL, MTS::Logger::PrintLevel::ERROR_LABEL, format, ##__VA_ARGS__)
#define printWarning(format, ...) \
if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::WARNING_LEVEL)) \
MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::WARNING_LEVEL, MTS::Logger::PrintLevel::WARNING_LABEL, format, ##__VA_ARGS__)
#define printInfo(format, ...) \
if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::INFO_LEVEL)) \
MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::INFO_LEVEL, MTS::Logger::PrintLevel::INFO_LABEL, format, ##__VA_ARGS__)
#define printConfig(format, ...) \
if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::CONFIG_LEVEL)) \
MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::CONFIG_LEVEL, MTS::Logger::PrintLevel::CONFIG_LABEL, format, ##__VA_ARGS__)
#define printDebug(format, ...) \
if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::DEBUG_LEVEL)) \
MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::DEBUG_LEVEL, MTS::Logger::PrintLevel::DEBUG_LABEL, format, ##__VA_ARGS__)
#define printTrace(format, ...) \
if (MTS::Logger::isPrintable(MTS::Logger::PrintLevel::TRACE_LEVEL)) \
MTS::Logger::printfGeneric(MTS::Logger::PrintLevel::TRACE_LEVEL, MTS::Logger::PrintLevel::TRACE_LABEL, format, ##__VA_ARGS__)
#endif
namespace MTS {
class Logger: private NonCopyable {
public:
class PrintLevel: private NonConstructable {
public:
static const int32_t OFF_LEVEL;
static const int32_t MINIMUM_LEVEL;
static const int32_t FATAL_LEVEL;
static const int32_t ERROR_LEVEL;
static const int32_t WARNING_LEVEL;
static const int32_t INFO_LEVEL;
static const int32_t CONFIG_LEVEL;
static const int32_t DEBUG_LEVEL;
static const int32_t TRACE_LEVEL;
static const int32_t MAXIMUM_LEVEL;
static const char* OFF_LABEL;
static const char* FATAL_LABEL;
static const char* ERROR_LABEL;
static const char* WARNING_LABEL;
static const char* INFO_LABEL;
static const char* CONFIG_LABEL;
static const char* DEBUG_LABEL;
static const char* TRACE_LABEL;
static const char* MAXIMUM_LABEL;
};
enum PrintMode {
NO_PRINTING,
STDOUT_ONLY,
FILE_ONLY,
SYSLOG_ONLY,
STDOUT_AND_FILE,
STDOUT_AND_SYSLOG
};
static int getPrintLevel();
static const std::string& getPrintLevelString();
static void setPrintLevel(int32_t level, bool silent = false);
static bool isPrintable(int32_t level);
static void printfFatal(const char* format, ...);
static void printfError(const char* format, ...);
static void printfWarning(const char* format, ...);
static void printfInfo(const char* format, ...);
static void printfConfig(const char* format, ...);
static void printfDebug(const char* format, ...);
static void printfTrace(const char* format, ...);
static void printfGeneric(int level, const char* label, const char* format, ...);
static void printf(int level, const char* format, ...);
static void printf(const char* format, ...);
static bool setup(const PrintMode& mode);
static bool setup(const PrintMode& mode, const std::string& ident, const int& option, const int& facility);
static bool setup(const PrintMode& mode, const std::string& filename);
private:
static volatile int m_iPrintLevel;
static std::string m_sPrintLevel;
static Logger::PrintMode m_eMode;
static Lock m_oPrintLock;
static FILE* m_pFile;
static int m_iLogFacility;
static std::string m_sIdent;
static std::string m_sFileName;
static int32_t syslogPrintLevelConversion(const int32_t& level);
static void printMessage(const int32_t& level, const char* label, const char* format, va_list argptr);
Logger();
};
}
#endif /* MTS_LOG_H_ */
|