Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
logger.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Jan Olszak <j.olszak@samsung.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License
17  */
18 
60 #ifndef LOGGER_LOGGER_HPP
61 #define LOGGER_LOGGER_HPP
62 
63 #include "logger/level.hpp"
64 #include "logger/backend-null.hpp"
65 #ifdef HAVE_SYSTEMD
67 #endif
68 #include "logger/backend-file.hpp"
72 
73 #include <sstream>
74 #include <string>
75 
76 #ifndef PROJECT_SOURCE_DIR
77 #define PROJECT_SOURCE_DIR ""
78 #endif
79 
80 namespace logger {
81 
82 enum class LogType : int {
83  LOG_NULL,
85  LOG_FILE,
87  LOG_SYSLOG,
89 };
90 
99 void setupLogger(const LogType type,
100  const LogLevel level,
101  const std::string &arg = "");
102 
103 class LogBackend;
104 
105 class Logger {
106 public:
107  static void logMessage(LogLevel logLevel,
108  const std::string& message,
109  const std::string& file,
110  const unsigned int line,
111  const std::string& func,
112  const std::string& rootDir);
113 
114  static void setLogLevel(const LogLevel level);
115  static void setLogLevel(const std::string& level);
116  static LogLevel getLogLevel(void);
117  static void setLogBackend(LogBackend* pBackend);
118 };
119 
120 } // namespace logger
121 
123 #define LOG(SEVERITY, MESSAGE) \
125  do { \
126  if (logger::Logger::getLogLevel() <= logger::LogLevel::SEVERITY) { \
127  std::ostringstream messageStream__; \
128  messageStream__ << MESSAGE; \
129  logger::Logger::logMessage(logger::LogLevel::SEVERITY, \
130  messageStream__.str(), \
131  __FILE__, \
132  __LINE__, \
133  __func__, \
134  PROJECT_SOURCE_DIR); \
135  } \
136  } while (0)
137 
138 
140 #define LOGE(MESSAGE) LOG(ERROR, MESSAGE)
141 
143 #define LOGW(MESSAGE) LOG(WARN, MESSAGE)
144 
146 #define LOGI(MESSAGE) LOG(INFO, MESSAGE)
147 
148 #if !defined(NDEBUG)
149 #define LOGD(MESSAGE) LOG(DEBUG, MESSAGE)
151 
153 #define LOGH(MESSAGE) LOG(HELP, MESSAGE)
154 
156 #define LOGT(MESSAGE) LOG(TRACE, MESSAGE)
157 #else
158 #define LOGD(MESSAGE) do {} while (0)
159 #define LOGH(MESSAGE) do {} while (0)
160 #define LOGT(MESSAGE) do {} while (0)
161 #endif
162 
163 #endif // LOGGER_LOGGER_HPP
164 
static LogLevel getLogLevel(void)
Definition: logger.cpp:103
static void logMessage(LogLevel logLevel, const std::string &message, const std::string &file, const unsigned int line, const std::string &func, const std::string &rootDir)
Definition: logger.cpp:81
File backend for logger.
static void setLogBackend(LogBackend *pBackend)
Definition: logger.cpp:108
Persistent file backend for logger.
LogLevel
Available log levels.
Definition: level.hpp:36
void setupLogger(const LogType type, const LogLevel level, const std::string &arg)
A helper function to easily and completely setup a new logger.
Definition: logger.cpp:43
Null backend for logger.
Definition: logger.hpp:105
LogType
Definition: logger.hpp:82
Stderr backend for logger.
Abstract class for logger backends.
Definition: backend.hpp:38
Syslog backend for logger.
static void setLogLevel(const LogLevel level)
Definition: logger.cpp:93
Systemd journal backend for logger.
LogLevel.