log.h revision 1.1.1.2 1 1.1 christos /*
2 1.1 christos * util/log.h - logging service
3 1.1 christos *
4 1.1 christos * Copyright (c) 2007, NLnet Labs. All rights reserved.
5 1.1 christos *
6 1.1 christos * This software is open source.
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos *
12 1.1 christos * Redistributions of source code must retain the above copyright notice,
13 1.1 christos * this list of conditions and the following disclaimer.
14 1.1 christos *
15 1.1 christos * Redistributions in binary form must reproduce the above copyright notice,
16 1.1 christos * this list of conditions and the following disclaimer in the documentation
17 1.1 christos * and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * Neither the name of the NLNET LABS nor the names of its contributors may
20 1.1 christos * be used to endorse or promote products derived from this software without
21 1.1 christos * specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 1.1 christos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 1.1 christos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 1.1 christos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 1.1 christos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 1.1 christos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 1.1 christos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 christos */
35 1.1 christos
36 1.1 christos /**
37 1.1 christos * \file
38 1.1 christos *
39 1.1 christos * This file contains logging functions.
40 1.1 christos */
41 1.1 christos
42 1.1 christos #ifndef UTIL_LOG_H
43 1.1 christos #define UTIL_LOG_H
44 1.1 christos struct sldns_buffer;
45 1.1 christos
46 1.1 christos /**
47 1.1 christos * verbosity value:
48 1.1 christos */
49 1.1 christos enum verbosity_value {
50 1.1 christos /** 0 - no verbose messages */
51 1.1 christos NO_VERBOSE = 0,
52 1.1 christos /** 1 - operational information */
53 1.1 christos VERB_OPS,
54 1.1 christos /** 2 - detailed information */
55 1.1 christos VERB_DETAIL,
56 1.1 christos /** 3 - query level information */
57 1.1 christos VERB_QUERY,
58 1.1 christos /** 4 - algorithm level information */
59 1.1 christos VERB_ALGO,
60 1.1 christos /** 5 - querier client information */
61 1.1 christos VERB_CLIENT
62 1.1 christos };
63 1.1 christos
64 1.1 christos /** The global verbosity setting */
65 1.1 christos extern enum verbosity_value verbosity;
66 1.1 christos
67 1.1 christos /**
68 1.1 christos * log a verbose message, pass the level for this message.
69 1.1 christos * It has printf formatted arguments. No trailing newline is needed.
70 1.1 christos * @param level: verbosity level for this message, compared to global
71 1.1 christos * verbosity setting.
72 1.1 christos * @param format: printf-style format string. Arguments follow.
73 1.1 christos */
74 1.1 christos void verbose(enum verbosity_value level,
75 1.1 christos const char* format, ...) ATTR_FORMAT(printf, 2, 3);
76 1.1 christos
77 1.1 christos /**
78 1.1 christos * call this to initialize logging services.
79 1.1 christos * @param filename: if NULL stderr is used.
80 1.1 christos * @param use_syslog: set to true to ignore filename and use syslog(3).
81 1.1 christos * @param chrootdir: to which directory we have been chrooted, if any.
82 1.1 christos */
83 1.1 christos void log_init(const char* filename, int use_syslog, const char* chrootdir);
84 1.1 christos
85 1.1 christos /**
86 1.1 christos * Set logging to go to the specified file *.
87 1.1 christos * This setting does not affect the use_syslog setting.
88 1.1 christos * @param f: to that file, or pass NULL to disable logging.
89 1.1 christos */
90 1.1 christos void log_file(FILE *f);
91 1.1 christos
92 1.1 christos /**
93 1.1 christos * Init a thread (will print this number for the thread log entries).
94 1.1 christos * Must be called from the thread itself. If not called 0 is printed.
95 1.1 christos * @param num: number to print for this thread. Owned by caller, must
96 1.1 christos * continue to exist.
97 1.1 christos */
98 1.1 christos void log_thread_set(int* num);
99 1.1 christos
100 1.1 christos /**
101 1.1 christos * Get the thread id from logging system. Set after log_init is
102 1.1 christos * initialised, or log_thread_set for newly created threads.
103 1.1 christos * This initialisation happens in unbound as a daemon, in daemon
104 1.1 christos * startup code, when that spawns threads.
105 1.1 christos * @return thread number, from 0 and up. Before initialised, returns 0.
106 1.1 christos */
107 1.1 christos int log_thread_get(void);
108 1.1 christos
109 1.1 christos /**
110 1.1 christos * Set identity to print, default is 'unbound'.
111 1.1 christos * @param id: string to print. Name of executable.
112 1.1 christos */
113 1.1 christos void log_ident_set(const char* id);
114 1.1 christos
115 1.1 christos /**
116 1.1 christos * Set the time value to print in log entries.
117 1.1 christos * @param t: the point is copied and used to find the time.
118 1.1 christos * if NULL, time(2) is used.
119 1.1 christos */
120 1.1 christos void log_set_time(time_t* t);
121 1.1 christos
122 1.1 christos /**
123 1.1 christos * Set if the time value is printed ascii or decimal in log entries.
124 1.1 christos * @param use_asc: if true, ascii is printed, otherwise decimal.
125 1.1 christos * If the conversion fails or you have no time functions,
126 1.1 christos * decimal is printed.
127 1.1 christos */
128 1.1 christos void log_set_time_asc(int use_asc);
129 1.1 christos
130 1.1.1.2 christos /** get log lock */
131 1.1.1.2 christos void* log_get_lock(void);
132 1.1.1.2 christos
133 1.1 christos /**
134 1.1 christos * Log informational message.
135 1.1 christos * Pass printf formatted arguments. No trailing newline is needed.
136 1.1 christos * @param format: printf-style format string. Arguments follow.
137 1.1 christos */
138 1.1 christos void log_info(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
139 1.1 christos
140 1.1 christos /**
141 1.1 christos * Log error message.
142 1.1 christos * Pass printf formatted arguments. No trailing newline is needed.
143 1.1 christos * @param format: printf-style format string. Arguments follow.
144 1.1 christos */
145 1.1 christos void log_err(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
146 1.1 christos
147 1.1 christos /**
148 1.1 christos * Log warning message.
149 1.1 christos * Pass printf formatted arguments. No trailing newline is needed.
150 1.1 christos * @param format: printf-style format string. Arguments follow.
151 1.1 christos */
152 1.1 christos void log_warn(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
153 1.1 christos
154 1.1 christos /**
155 1.1 christos * Log a hex-string to the log. Can be any length.
156 1.1 christos * performs mallocs to do so, slow. But debug useful.
157 1.1 christos * @param msg: string desc to accompany the hexdump.
158 1.1 christos * @param data: data to dump in hex format.
159 1.1 christos * @param length: length of data.
160 1.1 christos */
161 1.1 christos void log_hex(const char* msg, void* data, size_t length);
162 1.1 christos
163 1.1 christos /**
164 1.1 christos * Easy alternative for log_hex, takes a sldns_buffer.
165 1.1 christos * @param level: verbosity level for this message, compared to global
166 1.1 christos * verbosity setting.
167 1.1 christos * @param msg: string desc to print
168 1.1 christos * @param buf: the buffer.
169 1.1 christos */
170 1.1 christos void log_buf(enum verbosity_value level, const char* msg, struct sldns_buffer* buf);
171 1.1 christos
172 1.1 christos /**
173 1.1 christos * Log fatal error message, and exit the current process.
174 1.1 christos * Pass printf formatted arguments. No trailing newline is needed.
175 1.1 christos * @param format: printf-style format string. Arguments follow.
176 1.1 christos */
177 1.1 christos void fatal_exit(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
178 1.1 christos
179 1.1 christos /**
180 1.1 christos * va_list argument version of log_info.
181 1.1 christos * @param pri: priority type, for example 5 (INFO).
182 1.1 christos * @param type: string to designate type of message (info, error).
183 1.1 christos * @param format: the printf style format to print. no newline.
184 1.1 christos * @param args: arguments for format string.
185 1.1 christos */
186 1.1 christos void log_vmsg(int pri, const char* type, const char* format, va_list args);
187 1.1 christos
188 1.1 christos /**
189 1.1 christos * an assertion that is thrown to the logfile.
190 1.1 christos */
191 1.1 christos #ifdef UNBOUND_DEBUG
192 1.1.1.2 christos #ifdef __clang_analyzer__
193 1.1.1.2 christos /* clang analyzer needs to know that log_assert is an assertion, otherwise
194 1.1.1.2 christos * it could complain about the nullptr the assert is guarding against. */
195 1.1.1.2 christos #define log_assert(x) assert(x)
196 1.1.1.2 christos #else
197 1.1 christos # define log_assert(x) \
198 1.1 christos do { if(!(x)) \
199 1.1 christos fatal_exit("%s:%d: %s: assertion %s failed", \
200 1.1 christos __FILE__, __LINE__, __func__, #x); \
201 1.1 christos } while(0);
202 1.1.1.2 christos #endif
203 1.1 christos #else
204 1.1 christos # define log_assert(x) /*nothing*/
205 1.1 christos #endif
206 1.1 christos
207 1.1 christos #ifdef USE_WINSOCK
208 1.1 christos /**
209 1.1 christos * Convert WSA error into string.
210 1.1 christos * @param err: from WSAGetLastError()
211 1.1 christos * @return: string.
212 1.1 christos */
213 1.1 christos char* wsa_strerror(DWORD err);
214 1.1 christos #endif /* USE_WINSOCK */
215 1.1 christos
216 1.1 christos #endif /* UTIL_LOG_H */
217