1 /*
2  * DNS Reply Tool (drool)
3  *
4  * Copyright (c) 2017-2018, OARC, Inc.
5  * Copyright (c) 2017, Comcast Corporation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. Neither the name of the copyright holder nor the names of its
21  *    contributors may be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef __drool_log_h
39 #define __drool_log_h
40 
41 #include <stddef.h>
42 #include <errno.h>
43 
44 #ifndef LOG_DATETIME
45 #define LOG_DATETIME 1
46 #endif
47 #ifndef LOG_FILENAME_LINE
48 #define LOG_FILENAME_LINE 1
49 #endif
50 #ifndef LOG_SHORT_FILENAME
51 #define LOG_SHORT_FILENAME 1
52 #endif
53 #ifndef LOG_THREAD_ID
54 #define LOG_THREAD_ID 0
55 #endif
56 
57 #define LOG_SETTINGS_T_INIT \
58     {                       \
59         0, 0, 0, 1, 1, 1    \
60     }
61 #define LOG_SETTINGS_T_INIT_NONE \
62     {                            \
63         0, 0, 0, 0, 0, 0         \
64     }
65 #define LOG_SETTINGS_T_INIT_ALL \
66     {                           \
67         1, 1, 1, 1, 1, 1        \
68     }
69 typedef struct drool_log_settings drool_log_settings_t;
70 struct drool_log_settings {
71     unsigned short debug : 1;
72     unsigned short info : 1;
73     unsigned short notice : 1;
74     unsigned short warning : 1;
75     unsigned short error : 1;
76     unsigned short critical : 1;
77 };
78 
79 #define LOG_LEVEL_UNKNOWN_STR "unknown"
80 #define LOG_LEVEL_DEBUG_STR "debug"
81 #define LOG_LEVEL_INFO_STR "info"
82 #define LOG_LEVEL_NOTICE_STR "notice"
83 #define LOG_LEVEL_WARNING_STR "warning"
84 #define LOG_LEVEL_ERROR_STR "error"
85 #define LOG_LEVEL_CRITICAL_STR "critical"
86 #define LOG_LEVEL_ALL_STR "all"
87 typedef enum drool_log_level drool_log_level_t;
88 enum drool_log_level {
89     LOG_LEVEL_DEBUG,
90     LOG_LEVEL_INFO,
91     LOG_LEVEL_NOTICE,
92     LOG_LEVEL_WARNING,
93     LOG_LEVEL_ERROR,
94     LOG_LEVEL_CRITICAL,
95 
96     LOG_LEVEL_ALL
97 };
98 #define LDEBUG LOG_LEVEL_DEBUG
99 #define LINFO LOG_LEVEL_INFO
100 #define LNOTICE LOG_LEVEL_NOTICE
101 #define LWARNING LOG_LEVEL_WARNING
102 #define LERROR LOG_LEVEL_ERROR
103 #define LCRITICAL LOG_LEVEL_CRITICAL
104 
105 int log_level_enable(drool_log_settings_t* settings, const drool_log_level_t level);
106 int log_level_disable(drool_log_settings_t* settings, const drool_log_level_t level);
107 const char* log_level_name(const drool_log_level_t level);
108 
109 #define LOG_FACILITY_UNKNOWN_STR "unknown"
110 #define LOG_FACILITY_NONE_STR "none"
111 #define LOG_FACILITY_CORE_STR "core"
112 #define LOG_FACILITY_NETWORK_STR "network"
113 typedef enum drool_log_facility drool_log_facility_t;
114 enum drool_log_facility {
115     LOG_FACILITY_NONE = 0,
116     LOG_FACILITY_CORE,
117     LOG_FACILITY_NETWORK
118 };
119 #define LNONE LOG_FACILITY_NONE
120 #define LCORE LOG_FACILITY_CORE
121 #define LNETWORK LOG_FACILITY_NETWORK
122 
123 const char* log_facility_name(const drool_log_facility_t facility);
124 
125 #define LOG_T_INIT               \
126     {                            \
127         LOG_SETTINGS_T_INIT_ALL, \
128             LOG_SETTINGS_T_INIT, \
129             LOG_SETTINGS_T_INIT, \
130     }
131 typedef struct drool_log drool_log_t;
132 struct drool_log {
133     drool_log_settings_t none;
134     drool_log_settings_t core;
135     drool_log_settings_t network;
136 };
137 
138 int log_is_enabled(const drool_log_t* log, const drool_log_facility_t facility, const drool_log_level_t level);
139 int log_enable(drool_log_t* log, const drool_log_facility_t facility, const drool_log_level_t level);
140 int log_disable(drool_log_t* log, const drool_log_facility_t facility, const drool_log_level_t level);
141 
142 void log_printf_fileline(const drool_log_t* log, const drool_log_facility_t facility, const drool_log_level_t level, const char* file, size_t line, const char* format, ...);
143 #define log_print(log, facility, level, text) \
144     log_printf_fileline(log, facility, level, __FILE__, __LINE__, text)
145 #define log_printf(log, facility, level, format, args...) \
146     log_printf_fileline(log, facility, level, __FILE__, __LINE__, format, args)
147 
148 void log_errnumf_fileline(const drool_log_t* log, const drool_log_facility_t facility, const drool_log_level_t level, const char* file, size_t line, int errnum, const char* format, ...);
149 #define log_errnum(log, facility, level, errnum, text) \
150     log_errnumf_fileline(log, facility, level, __FILE__, __LINE__, errnum, text)
151 #define log_errnumf(log, facility, level, errnum, format, args...) \
152     log_errnumf_fileline(log, facility, level, __FILE__, __LINE__, errnum, format, args)
153 
154 #define log_errno(log, facility, level, text) \
155     log_errnumf_fileline(log, facility, level, __FILE__, __LINE__, errno, text)
156 #define log_errnof(log, facility, level, format, args...) \
157     log_errnumf_fileline(log, facility, level, __FILE__, __LINE__, errno, format, args)
158 
159 #endif /* __drool_log_h */
160