Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / common / utils / include / ScopeLogger.h
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #ifndef COMMON_UTILS_SCOPELOGGER_H
22 #define COMMON_UTILS_SCOPELOGGER_H
23
24 #include "logger.h"
25
26 #ifdef TB_LOG
27 #include <exception>
28
29 namespace OIC
30 {
31     namespace Service
32     {
33         namespace Logging
34         {
35
36             class ScopeLogger
37             {
38             public:
39                 ScopeLogger(LogLevel level, const char* tag, const char* scopeName) :
40                     m_level{ level },
41                     m_tag{ tag },
42                     m_scopeName{ scopeName }
43                 {
44                     static constexpr char DEFAULT_ENTER_STR[]{ "IN" };
45
46                     OC_LOG_V(m_level, m_tag, "%s %s", m_scopeName, DEFAULT_ENTER_STR);
47                 }
48
49                 ~ScopeLogger()
50                 {
51                     static constexpr char DEFAULT_EXIT_STR[]{ "OUT" };
52
53                     if (std::uncaught_exception())
54                     {
55                         OC_LOG_V(m_level, m_tag, "%s %s by stack unwinding (uncaught exception)",
56                                 m_scopeName, DEFAULT_EXIT_STR);
57                     }
58                     else
59                     {
60                         OC_LOG_V(m_level, m_tag, "%s %s", m_scopeName, DEFAULT_EXIT_STR);
61                     }
62                 }
63
64                 ScopeLogger(const ScopeLogger&) = delete;
65                 ScopeLogger(ScopeLogger&&) = delete;
66
67                 ScopeLogger& operator=(const ScopeLogger&) = delete;
68                 ScopeLogger& operator=(ScopeLogger&&) = delete;
69
70             private:
71                 const LogLevel m_level;
72                 const char* m_tag;
73                 const char* m_scopeName;
74             };
75         }
76
77     }
78 }
79
80 #define SCOPE_LOG(level, tag, scopeName) \
81             Logging::ScopeLogger rcsScopeLogger__((level), (tag), (scopeName))
82
83 #define SCOPE_LOG_F(level, tag) SCOPE_LOG((level), (tag), __func__)
84
85 #else
86 #define SCOPE_LOG_F(level, tag)
87 #define SCOPE_LOG(level, tag, scopeName)
88 #endif
89
90
91 #endif // COMMON_UTILS_SCOPELOGGER_H