50f0eab7431b6d0c4761032e1ce48415e74bd5d2
[platform/framework/web/crosswalk-tizen.git] / src / common / logger.h
1 // Copyright 2015 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.
4
5 #ifndef WRT_COMMON_LOGGER_H_
6 #define WRT_COMMON_LOGGER_H_
7
8 #include <dlog.h>
9 #include <sstream>
10
11 #undef LOGGER_TAG
12 #define LOGGER_TAG "WRT"
13
14 #define _LOGGER_LOG(prio, fmt, args...) \
15   LOG_(LOG_ID_MAIN, prio, LOGGER_TAG, fmt, ##args)
16
17 #define _LOGGER_SLOG(prio, fmt, args...) \
18   SECURE_LOG_(LOG_ID_MAIN, prio, LOGGER_TAG, fmt, ##args)
19
20 #define LoggerD(fmt, args...) _LOGGER_LOG(DLOG_DEBUG, fmt, ##args)
21 #define LoggerI(fmt, args...) _LOGGER_LOG(DLOG_INFO, fmt, ##args)
22 #define LoggerW(fmt, args...) _LOGGER_LOG(DLOG_WARN, fmt, ##args)
23 #define LoggerE(fmt, args...) _LOGGER_LOG(DLOG_ERROR, fmt, ##args)
24
25 #define SLoggerD(fmt, args...) _LOGGER_SLOG(DLOG_DEBUG, fmt, ##args)
26 #define SLoggerI(fmt, args...) _LOGGER_SLOG(DLOG_INFO, fmt, ##args)
27 #define SLoggerW(fmt, args...) _LOGGER_SLOG(DLOG_WARN, fmt, ##args)
28 #define SLoggerE(fmt, args...) _LOGGER_SLOG(DLOG_ERROR, fmt, ##args)
29
30 namespace wrt {
31 namespace utils {
32
33 class LogMessageVodify {
34  public:
35   LogMessageVodify() {}
36   void operator&(const std::ostream&) const {}
37 };
38
39 class LogMessage {
40  public:
41   LogMessage(int severity, const char* file, const char* func, const int line)
42       : severity_(severity), file_(file), func_(func), line_(line) {}
43   ~LogMessage() {
44     __dlog_print(LOG_ID_MAIN, severity_, LOGGER_TAG,
45                  "%s: %s(%d) > %s", file_, func_, line_, stream_.str().c_str());
46   }
47   std::ostream& stream() { return stream_; }
48  private:
49   const int severity_;
50   const char* file_;
51   const char* func_;
52   const int line_;
53   std::ostringstream stream_;
54 };
55
56 }  // namespace utils
57 }  // namespace wrt
58
59 #ifndef __MODULE__
60 #define __MODULE__                                                            \
61     (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
62 #endif
63
64 #define LOGGER(severity)                                                      \
65   wrt::utils::LogMessageVodify() &                                            \
66     wrt::utils::LogMessage(DLOG_ ## severity,                                 \
67                            __MODULE__, __FUNCTION__, __LINE__).stream()
68
69
70 #endif  // WRT_COMMON_LOGGER_H_