Initial service app implementation
[platform/framework/web/crosswalk-tizen.git] / tizen / common / logger.h
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #ifndef XWALK_COMMON_LOGGER_H_
18 #define XWALK_COMMON_LOGGER_H_
19
20 #include <dlog.h>
21 #include <sstream>
22
23 #undef LOGGER_TAG
24 #define LOGGER_TAG "WRT"
25
26 #define _LOGGER_LOG(prio, fmt, args...) \
27   LOG_(LOG_ID_MAIN, prio, LOGGER_TAG, fmt, ##args)
28
29 #define _LOGGER_SLOG(prio, fmt, args...) \
30   SECURE_LOG_(LOG_ID_MAIN, prio, LOGGER_TAG, fmt, ##args)
31
32 #define LoggerD(fmt, args...) _LOGGER_LOG(DLOG_DEBUG, fmt, ##args)
33 #define LoggerI(fmt, args...) _LOGGER_LOG(DLOG_INFO, fmt, ##args)
34 #define LoggerW(fmt, args...) _LOGGER_LOG(DLOG_WARN, fmt, ##args)
35 #define LoggerE(fmt, args...) _LOGGER_LOG(DLOG_ERROR, fmt, ##args)
36
37 #define SLoggerD(fmt, args...) _LOGGER_SLOG(DLOG_DEBUG, fmt, ##args)
38 #define SLoggerI(fmt, args...) _LOGGER_SLOG(DLOG_INFO, fmt, ##args)
39 #define SLoggerW(fmt, args...) _LOGGER_SLOG(DLOG_WARN, fmt, ##args)
40 #define SLoggerE(fmt, args...) _LOGGER_SLOG(DLOG_ERROR, fmt, ##args)
41
42 namespace common {
43 namespace utils {
44
45 class LogMessageVodify {
46  public:
47   LogMessageVodify() {}
48   void operator&(const std::ostream&)const {}
49 };
50
51 class LogMessage {
52  public:
53   LogMessage(int severity,
54              const char* tag,
55              const char* file,
56              const char* func,
57              const int line)
58       : severity_(severity), tag_(tag), file_(file), func_(func), line_(line) {}
59   LogMessage(int severity, const char* tag)
60       : severity_(severity), tag_(tag), file_(NULL), func_(NULL), line_(0) {}
61   ~LogMessage() {
62     if (file_) {
63       __dlog_print(LOG_ID_MAIN, severity_, tag_, "%s: %s(%d) > %s", file_,
64                    func_, line_, stream_.str().c_str());
65     } else {
66       __dlog_print(LOG_ID_MAIN, severity_, tag_, "%s", stream_.str().c_str());
67     }
68   }
69   std::ostream& stream() { return stream_; }
70
71  private:
72   const int severity_;
73   const char* tag_;
74   const char* file_;
75   const char* func_;
76   const int line_;
77   std::ostringstream stream_;
78 };
79
80 }  // namespace utils
81 }  // namespace common
82
83 #ifndef __MODULE__
84 #define __MODULE__ \
85   (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
86 #endif
87
88 #define LOGGER(severity)                                                 \
89   common::utils::LogMessageVodify() &                                    \
90       common::utils::LogMessage(DLOG_##severity, LOGGER_TAG, __MODULE__, \
91                                 __FUNCTION__, __LINE__)                  \
92           .stream()
93
94 #define LOGGER_RAW(level, tag)        \
95   common::utils::LogMessageVodify() & \
96       common::utils::LogMessage(level, tag).stream()
97
98 #endif  // XWALK_COMMON_LOGGER_H_