change 'constructor with 1 argument' to explicit constructor
[platform/core/system/libdbuspolicy.git] / src / internal / tslog.hpp
1 /*
2  * Copyright (c) 2015-2019 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  * \file
18  * \ingroup Implementation
19  */
20
21 #ifndef _TSLOG_HPP
22 #define _TSLOG_HPP
23
24 #define LOG_TAG "LIBDBUSPOLICY"
25 #include <dlog.h>
26 #include <iostream>
27 #include <thread>
28 #include <pthread.h>
29 #include <stdlib.h>
30 #include <string>
31
32 namespace tslog
33 {
34         enum class ldp_log_level {
35                 SILENT,
36                 ERROR,
37                 WARNING,
38                 DEFAULT = WARNING,
39                 DEBUG,
40                 VERBOSE
41         };
42         /** Checks value of environmental variable with given name */
43         bool get_log_env(char const *name);
44
45         /** Checks environmental variables and sets global variable defining if logs are enabled */
46         void init(ldp_log_level level = ldp_log_level::SILENT);
47
48         /** Checks if logs are enabled */
49         bool enabled(ldp_log_level level = ldp_log_level::DEBUG);
50
51         void flush(ldp_log_level level = ldp_log_level::ERROR);
52
53         void logError(const std::string &error);
54         void logWarning(const std::string &warning);
55         void logDebug(const std::string &debug);
56         void logVerbose(const std::string &verbose);
57
58         template <typename ...Args>
59         void log_to_stream(std::ostream &stream, const Args &...args) {
60                 std::initializer_list<int>{((void)(stream << args), 0)...};
61         }
62
63         template <typename ...Args>
64         void log_debug(const Args &...args) { if (enabled(ldp_log_level::DEBUG)) log_to_stream(std::cout, args...); }
65
66         template <typename ...Args>
67         void log_error(const Args &...args) { if (enabled(ldp_log_level::ERROR)) log_to_stream(std::cout, args...); }
68
69         template <typename ...Args>
70         void log_warning(const Args &...args) { if (enabled(ldp_log_level::WARNING)) log_to_stream(std::cout, args...); }
71
72         template <typename ...Args>
73         void log_verbose(const Args &...args) { if (enabled(ldp_log_level::VERBOSE)) log_to_stream(std::cout, args...); }
74
75         struct print_errno {
76                 int err;
77                 explicit print_errno(int e) : err(e) {}
78         };
79
80         std::ostream &operator<<(std::ostream &stream, const print_errno &p);
81
82         /* Use this class to ensure that logs from a given function are not interlaced
83          * with logs from another function
84          */
85         class LogLock {
86                 bool locked;
87         public:
88                 explicit LogLock(ldp_log_level level = ldp_log_level::ERROR);
89                 ~LogLock();
90         };
91 }
92
93 #endif