MINIFI-34 Establishing CMake build system to provide build functionality equivalent...
[nifi-minifi-cpp.git] / libminifi / include / Logger.h
1 /**
2 * @file Logger.h
3 * Logger class declaration
4 * This is a C++ wrapper for spdlog, a lightweight C++ logging library
5 *
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements. See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21 #ifndef __LOGGER_H__
22 #define __LOGGER_H__
23
24 #include "spdlog/spdlog.h"
25
26 using spdlog::stdout_logger_mt;
27 using spdlog::rotating_logger_mt;
28 using spdlog::logger;
29
30 #define LOG_BUFFER_SIZE 1024
31 #define FILL_BUFFER char buffer[LOG_BUFFER_SIZE]; \
32 va_list args; \
33 va_start(args, format); \
34 vsnprintf(buffer, LOG_BUFFER_SIZE,format, args); \
35 va_end(args);
36
37 //! 5M default log file size
38 #define DEFAULT_LOG_FILE_SIZE (5*1024*1024)
39 //! 3 log files rotation
40 #define DEFAULT_LOG_FILE_NUMBER 3
41 #define LOG_NAME "minifi log"
42 #define LOG_FILE_NAME "minifi-app.log"
43
44 typedef enum
45 {
46 trace = 0,
47 debug = 1,
48 info = 2,
49 notice = 3,
50 warn = 4,
51 err = 5,
52 critical = 6,
53 alert = 7,
54 emerg = 8,
55 off = 9
56 } LOG_LEVEL_E;
57
58 //! Logger Class
59 class Logger {
60
61 public:
62
63 //! Get the singleton logger instance
64 static Logger * getLogger() {
65 if (!_logger)
66 _logger = new Logger();
67 return _logger;
68 }
69 void setLogLevel(LOG_LEVEL_E level) {
70 if (_spdlog == NULL)
71 return;
72 _spdlog->set_level((spdlog::level::level_enum) level);
73 }
74 //! Destructor
75 ~Logger() {}
76 /**
77 * @brief Log error message
78 * @param format format string ('man printf' for syntax)
79 * @warning does not check @p log or @p format for null. Caller must ensure parameters and format string lengths match
80 */
81 void log_error(const char *const format, ...) {
82 if(_spdlog == NULL)
83 return;
84 FILL_BUFFER
85 _spdlog->error(buffer);
86 }
87 /**
88 * @brief Log warn message
89 * @param format format string ('man printf' for syntax)
90 * @warning does not check @p log or @p format for null. Caller must ensure parameters and format string lengths match
91 */
92 void log_warn(const char *const format, ...) {
93 if(_spdlog == NULL)
94 return;
95 FILL_BUFFER
96 _spdlog->warn(buffer);
97 }
98 /**
99 * @brief Log info message
100 * @param format format string ('man printf' for syntax)
101 * @warning does not check @p log or @p format for null. Caller must ensure parameters and format string lengths match
102 */
103 void log_info(const char *const format, ...) {
104 if(_spdlog == NULL)
105 return;
106 FILL_BUFFER
107 _spdlog->info(buffer);
108 }
109 /**
110 * @brief Log debug message
111 * @param format format string ('man printf' for syntax)
112 * @warning does not check @p log or @p format for null. Caller must ensure parameters and format string lengths match
113 */
114 void log_debug(const char *const format, ...) {
115 if(_spdlog == NULL)
116 return;
117 FILL_BUFFER
118 _spdlog->debug(buffer);
119 }
120 /**
121 * @brief Log trace message
122 * @param format format string ('man printf' for syntax)
123 * @warning does not check @p log or @p format for null. Caller must ensure parameters and format string lengths match
124 */
125 void log_trace(const char *const format, ...) {
126 if(_spdlog == NULL)
127 return;
128 FILL_BUFFER
129 _spdlog->trace(buffer);
130 }
131
132 protected:
133
134 private:
135 // Prevent default copy constructor and assignment operation
136 // Only support pass by reference or pointer
137 Logger(const Logger &parent);
138 Logger &operator=(const Logger &parent);
139 //! Constructor
140 /*!
141 * Create a logger
142 * */
143 Logger(const std::string logger_name = LOG_NAME, const std::string filename = LOG_FILE_NAME, size_t max_file_size = DEFAULT_LOG_FILE_SIZE, size_t max_files = DEFAULT_LOG_FILE_NUMBER, bool force_flush = true) {
144 _spdlog = rotating_logger_mt(logger_name, filename, max_file_size, max_files, force_flush);
145 _spdlog->set_level((spdlog::level::level_enum) debug);
146 }
147 //! spdlog
148 std::shared_ptr<logger> _spdlog;
149
150 //! Singleton logger instance
151 static Logger *_logger;
152 };
153
154 #endif