Cleanup spec files
[platform/core/security/device-certificate-manager-backend.git] / src / log.h
1 /******************************************************************
2  *
3  * Copyright 2020 Samsung Electronics All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  ******************************************************************/
18 #ifndef SHARED_LOG_H_
19 #define SHARED_LOG_H_
20
21 #include <stdlib.h>
22 #include <syslog.h>
23 #include <sstream>
24
25 #define UNUSED __attribute__((unused))
26
27 extern int __log_level;
28 extern void dcm_print(int priority, char const *fmt, ...);
29
30 namespace {
31         template <typename ...Args>
32         void UNUSED __LOG_FUN(int level, const std::stringstream &format, Args&&... args) {
33                 dcm_print(level, format.str().c_str(), std::forward<Args>(args)...);
34         }
35
36         template <>
37         void UNUSED __LOG_FUN(int level, const std::stringstream &format) {
38                 dcm_print(level, "%s", format.str().c_str());
39         }
40
41         template <typename ...Args>
42         void UNUSED __LOG_FUN(int level, const char *format, Args&&... args) {
43                 dcm_print(level, format, std::forward<Args>(args)...);
44         }
45
46         template <>
47         void UNUSED __LOG_FUN(int level, const char *format) {
48                 dcm_print(level, "%s", format);
49         }
50
51 } // namespace anonymous
52
53 #define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
54
55 #define __LOG(LEVEL, FORMAT, ...) \
56         do { \
57                 if (LEVEL <= __log_level) { \
58                         std::stringstream __LOG_FORMAT; \
59                         __LOG_FORMAT << __FILENAME__ << ": " << __func__ << "(" << __LINE__ << ") > " << FORMAT; \
60                         __LOG_FUN(LEVEL, __LOG_FORMAT, ##__VA_ARGS__); \
61                 } \
62         } while (0)
63
64 #define LOGM(...)  __LOG(LOG_EMERG, __VA_ARGS__)   /* system is unusable */
65 #define LOGE(...)  __LOG(LOG_ERR, __VA_ARGS__)     /* error conditions */
66 #define LOGW(...)  __LOG(LOG_WARNING, __VA_ARGS__) /* warning conditions */
67 #define LOGI(...)  __LOG(LOG_INFO, __VA_ARGS__)    /* informational */
68 #define LOGD(...)  __LOG(LOG_DEBUG, __VA_ARGS__)   /* debug-level messages */
69
70 #endif /* SHARED_LOG_H_ */