1 // Copyright 2014 Samsung Electronics Co, Ltd. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMMON_LOGGER_H_
6 #define COMMON_LOGGER_H_
10 // Tizen 3.0 uses different debug flag (DLOG_DEBUG_ENABLE) which is always
11 // enabled, following code allows to disable logs with DLOG_DEBUG priority if
12 // TIZEN_DEBUG_ENABLE is not set.
13 // This code should be removed when DLOG_DEBUG_ENABLE flag is no longer set
14 // by default in dlog.h file.
16 #ifdef TIZEN_DEBUG_ENABLE
17 #define LOG_(id, prio, tag, fmt, arg...) \
20 __dlog_print(id, prio, tag, "%s: %s(%d) > " fmt, __MODULE__, __func__, __LINE__, ##arg); \
23 #else // TIZEN_DEBUG_ENABLE
24 #define LOG_(id, prio, tag, fmt, arg...) \
27 if ((int)prio != DLOG_DEBUG) { \
28 __dlog_print(id, prio, tag, "%s: %s(%d) > " fmt, __MODULE__, __func__, __LINE__, ##arg); \
32 #endif // TIZEN_DEBUG_ENABLE
35 #ifdef TIZEN_DEBUG_ENABLE
36 #define SECURE_LOG_(id, prio, tag, fmt, arg...) \
39 __dlog_print(id, prio, tag, "%s: %s(%d) > [SECURE_LOG] " fmt, __MODULE__, __func__, \
43 #else // TIZEN_DEBUG_ENABLE
44 #define SECURE_LOG_(id, prio, tag, fmt, arg...) NOP(fmt, ##arg)
45 #endif // TIZEN_DEBUG_ENABLE
51 #include "common/utils.h"
54 #define LOGGER_TAG "WEBAPI_PLUGINS"
56 #define _LOGGER_LOG(prio, fmt, args...) LOG_(LOG_ID_MAIN, prio, LOGGER_TAG, fmt, ##args)
58 #define _LOGGER_SLOG(prio, fmt, args...) SECURE_LOG_(LOG_ID_MAIN, prio, LOGGER_TAG, fmt, ##args)
60 #define LoggerD(fmt, args...) _LOGGER_LOG(DLOG_DEBUG, fmt, ##args)
61 #define LoggerI(fmt, args...) _LOGGER_LOG(DLOG_INFO, fmt, ##args)
62 #define LoggerW(fmt, args...) _LOGGER_LOG(DLOG_WARN, fmt, ##args)
63 #define LoggerE(fmt, args...) _LOGGER_LOG(DLOG_ERROR, fmt, ##args)
65 #define SLoggerD(fmt, args...) _LOGGER_SLOG(DLOG_DEBUG, fmt, ##args)
66 #define SLoggerI(fmt, args...) _LOGGER_SLOG(DLOG_INFO, fmt, ##args)
67 #define SLoggerW(fmt, args...) _LOGGER_SLOG(DLOG_WARN, fmt, ##args)
68 #define SLoggerE(fmt, args...) _LOGGER_SLOG(DLOG_ERROR, fmt, ##args)
70 // A few definitions of macros that don't generate much code. These are used
71 // by LOGGER() and LOGGER_IF, etc. Since these are used all over our code, it's
72 // better to have compact code for these operations.
73 #ifdef TIZEN_DEBUG_ENABLE
74 #define COMPACT_LOG_DEBUG LogMessage(__MODULE__, __func__, __LINE__, DLOG_DEBUG).stream()
76 #define COMPACT_LOG_DEBUG true ? (void)0 : LogMessageVoidify() & (std::ostringstream())
79 #define COMPACT_LOG_INFO LogMessage(__MODULE__, __func__, __LINE__, DLOG_INFO).stream()
80 #define COMPACT_LOG_WARN LogMessage(__MODULE__, __func__, __LINE__, DLOG_WARN).stream()
81 #define COMPACT_LOG_ERROR LogMessage(__MODULE__, __func__, __LINE__, DLOG_ERROR).stream()
83 #define LOGGER(priority) COMPACT_LOG_##priority
84 #define LOGGER_IF(priority, condition) \
85 !(condition) ? (void)0 : LogMessageVoidify() & (LOGGER(priority))
87 // This class more or less represents a particular log message.
88 // You create an instance of LogMessage and then stream stuff to it.
89 // When you finish streaming to it, ~LogMessage is called and the
90 // full message gets streamed to dlog.
92 // You shouldn't actually use LogMessage's constructor to log things,
93 // though. You should use the LOGGER() macro (and variants thereof) above.
96 LogMessage(const char* file, const char* function, int line, log_priority priority);
99 std::ostream& stream() {
105 const char* function_;
107 log_priority priority_;
109 std::ostringstream stream_;
111 DISALLOW_COPY_AND_ASSIGN(LogMessage);
114 // This class is used to explicitly ignore values in the conditional
115 // logging macros. This avoids compiler warnings like "value computed
116 // is not used" and "statement has no effect".
117 class LogMessageVoidify {
119 LogMessageVoidify() {
122 // This has to be an operator with a precedence lower than << but
124 void operator&(std::ostream&) {
129 #define LogAndReportError_2(error, object) \
131 LoggerE("Reporting error."); \
132 ReportError(error, object); \
135 #define LogAndReportError_3(error, object, log) \
138 ReportError(error, object); \
141 #define LogAndReportError_X(_0, _1, _2, _3, FUNC, ...) FUNC
144 // LogAndCreateResult(const common::PlatformResult& result, picojson::object* obj, [(const char*
146 // LogAndReportError(result, &out);
147 // LogAndReportError(result, &out, ("Failed to open archive."));
148 // LogAndReportError(result, &out, ("Failed to open archive: %d.", 11));
150 #define LogAndReportError(...) \
151 LogAndReportError_X(, ##__VA_ARGS__, LogAndReportError_3(__VA_ARGS__), \
152 LogAndReportError_2(__VA_ARGS__))
155 #define LogAndCreateResult_1(error_code) \
156 (LoggerE("Creating PlatformResult with error"), common::PlatformResult(error_code))
158 #define LogAndCreateResult_2(error_code, msg) \
159 (LoggerE("Creating PlatformResult with error"), common::PlatformResult(error_code, msg))
161 #define LogAndCreateResult_3(error_code, msg, log) \
162 (LoggerE log, common::PlatformResult(error_code, msg))
164 #define LogAndCreateResult_X(_0, _1, _2, _3, FUNC, ...) FUNC
166 // LogAndCreateResult(common::ErrorCode code, [const char* error_message, (const char* log, ...)])
168 // auto pr1 = LogAndCreateResult(ErrorCode::IO_ERR);
169 // PlatformResult pr2 = LogAndCreateResult(ErrorCode::IO_ERR);
170 // return LogAndCreateResult(ErrorCode::IO_ERR);
172 // auto pr3 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.");
173 // PlatformResult pr4 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.");
174 // return LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.");
176 // auto pr5 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory:
177 // reporting IO error"));
178 // PlatformResult pr6 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a
179 // directory: reporting IO error"));
180 // return LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory: reporting IO
183 // auto pr7 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory:
184 // reporting IO error: %d", 33));
185 // PlatformResult pr8 = LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a
186 // directory: reporting IO error: %d", 33));
187 // return LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory.", ("Not a directory: reporting IO
190 #define LogAndCreateResult(...) \
191 LogAndCreateResult_X(, ##__VA_ARGS__, LogAndCreateResult_3(__VA_ARGS__), \
192 LogAndCreateResult_2(__VA_ARGS__), LogAndCreateResult_1(__VA_ARGS__))
195 #define LogAndCreateTizenError_1(error_name) (LoggerE("Creating TizenError"), common::error_name())
197 #define LogAndCreateTizenError_2(error_name, msg) \
198 (LoggerE("Creating TizenError"), common::error_name(msg))
200 #define LogAndCreateTizenError_3(error_name, msg, log) (LoggerE log, common::error_name(msg))
202 #define LogAndCreateTizenError_X(_0, _1, _2, _3, FUNC, ...) FUNC
204 #define LogAndCreateTizenError(...) \
205 LogAndCreateTizenError_X(, ##__VA_ARGS__, LogAndCreateTizenError_3(__VA_ARGS__), \
206 LogAndCreateTizenError_2(__VA_ARGS__), \
207 LogAndCreateTizenError_1(__VA_ARGS__))
210 #define LogAndReturnTizenError_1(error) \
212 LoggerE("Reporting error."); \
216 #define LogAndReturnTizenError_2(error, log) \
222 #define LogAndReturnTizenError_X(_0, _1, _2, FUNC, ...) FUNC
224 #define LogAndReturnTizenError(...) \
225 LogAndReturnTizenError_X(, ##__VA_ARGS__, LogAndReturnTizenError_2(__VA_ARGS__), \
226 LogAndReturnTizenError_1(__VA_ARGS__))
230 // defined here, so LoggerD() depends on TIZEN_DEBUG_ENABLE flag on per-module
234 ScopeLogger(const std::string& f, const std::string& m) : file_(f), method_(m) {
238 LoggerD("%s: %s > Exit", file_.c_str(), method_.c_str());
248 #ifdef TIZEN_DEBUG_ENABLE
249 #define ScopeLogger(EX, args...) \
250 __dlog_print(LOG_ID_MAIN, DLOG_DEBUG, LOGGER_TAG, \
251 "logger.h: ScopeLogger > %s: %s(%d) > Enter " EX, __MODULE__, __func__, __LINE__, \
253 const common::ScopeLogger __sl__{__MODULE__, __func__};
256 #define ScopeLogger(EX, args...)
259 #endif // COMMON_LOGGER_H_