xref: /openbsd-src/usr.sbin/unbound/util/log.h (revision a3167c07bc9d407e1aa954f9a074ef7e6cc5f9e1)
1933707f3Ssthen /*
2933707f3Ssthen  * util/log.h - logging service
3933707f3Ssthen  *
4933707f3Ssthen  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5933707f3Ssthen  *
6933707f3Ssthen  * This software is open source.
7933707f3Ssthen  *
8933707f3Ssthen  * Redistribution and use in source and binary forms, with or without
9933707f3Ssthen  * modification, are permitted provided that the following conditions
10933707f3Ssthen  * are met:
11933707f3Ssthen  *
12933707f3Ssthen  * Redistributions of source code must retain the above copyright notice,
13933707f3Ssthen  * this list of conditions and the following disclaimer.
14933707f3Ssthen  *
15933707f3Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16933707f3Ssthen  * this list of conditions and the following disclaimer in the documentation
17933707f3Ssthen  * and/or other materials provided with the distribution.
18933707f3Ssthen  *
19933707f3Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20933707f3Ssthen  * be used to endorse or promote products derived from this software without
21933707f3Ssthen  * specific prior written permission.
22933707f3Ssthen  *
23933707f3Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
245d76a658Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
255d76a658Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
265d76a658Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
275d76a658Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
285d76a658Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
295d76a658Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
305d76a658Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
315d76a658Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
325d76a658Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
335d76a658Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34933707f3Ssthen  */
35933707f3Ssthen 
36933707f3Ssthen /**
37933707f3Ssthen  * \file
38933707f3Ssthen  *
39933707f3Ssthen  * This file contains logging functions.
40933707f3Ssthen  */
41933707f3Ssthen 
42933707f3Ssthen #ifndef UTIL_LOG_H
43933707f3Ssthen #define UTIL_LOG_H
445d76a658Ssthen struct sldns_buffer;
45933707f3Ssthen 
46933707f3Ssthen /**
47933707f3Ssthen  * verbosity value:
48933707f3Ssthen  */
49933707f3Ssthen enum verbosity_value {
50933707f3Ssthen  /** 0 - no verbose messages */
51933707f3Ssthen 	NO_VERBOSE = 0,
52933707f3Ssthen  /** 1 - operational information */
53933707f3Ssthen  	VERB_OPS,
54933707f3Ssthen  /** 2 - detailed information */
55933707f3Ssthen  	VERB_DETAIL,
56933707f3Ssthen  /** 3 - query level information */
57933707f3Ssthen  	VERB_QUERY,
58933707f3Ssthen  /** 4 - algorithm level information */
59933707f3Ssthen  	VERB_ALGO,
60933707f3Ssthen  /** 5 - querier client information */
61933707f3Ssthen 	VERB_CLIENT
62933707f3Ssthen };
63933707f3Ssthen 
64933707f3Ssthen /** The global verbosity setting */
65933707f3Ssthen extern enum verbosity_value verbosity;
66933707f3Ssthen 
67933707f3Ssthen /**
68933707f3Ssthen  * log a verbose message, pass the level for this message.
69933707f3Ssthen  * It has printf formatted arguments. No trailing newline is needed.
70933707f3Ssthen  * @param level: verbosity level for this message, compared to global
71933707f3Ssthen  *	verbosity setting.
72933707f3Ssthen  * @param format: printf-style format string. Arguments follow.
73933707f3Ssthen  */
74933707f3Ssthen void verbose(enum verbosity_value level,
75933707f3Ssthen 	const char* format, ...) ATTR_FORMAT(printf, 2, 3);
76933707f3Ssthen 
77933707f3Ssthen /**
78933707f3Ssthen  * call this to initialize logging services.
79933707f3Ssthen  * @param filename: if NULL stderr is used.
80933707f3Ssthen  * @param use_syslog: set to true to ignore filename and use syslog(3).
81933707f3Ssthen  * @param chrootdir: to which directory we have been chrooted, if any.
82933707f3Ssthen  */
83933707f3Ssthen void log_init(const char* filename, int use_syslog, const char* chrootdir);
84933707f3Ssthen 
85933707f3Ssthen /**
86933707f3Ssthen  * Set logging to go to the specified file *.
87933707f3Ssthen  * This setting does not affect the use_syslog setting.
88933707f3Ssthen  * @param f: to that file, or pass NULL to disable logging.
89933707f3Ssthen  */
90933707f3Ssthen void log_file(FILE *f);
91933707f3Ssthen 
92933707f3Ssthen /**
93933707f3Ssthen  * Init a thread (will print this number for the thread log entries).
94933707f3Ssthen  * Must be called from the thread itself. If not called 0 is printed.
95933707f3Ssthen  * @param num: number to print for this thread. Owned by caller, must
96933707f3Ssthen  *	continue to exist.
97933707f3Ssthen  */
98933707f3Ssthen void log_thread_set(int* num);
99933707f3Ssthen 
100933707f3Ssthen /**
101fdfb4ba6Ssthen  * Get the thread id from logging system.  Set after log_init is
102fdfb4ba6Ssthen  * initialised, or log_thread_set for newly created threads.
103fdfb4ba6Ssthen  * This initialisation happens in unbound as a daemon, in daemon
104fdfb4ba6Ssthen  * startup code, when that spawns threads.
105fdfb4ba6Ssthen  * @return thread number, from 0 and up.  Before initialised, returns 0.
106fdfb4ba6Ssthen  */
107fdfb4ba6Ssthen int log_thread_get(void);
108fdfb4ba6Ssthen 
109fdfb4ba6Ssthen /**
110933707f3Ssthen  * Set identity to print, default is 'unbound'.
111933707f3Ssthen  * @param id: string to print. Name of executable.
112933707f3Ssthen  */
113933707f3Ssthen void log_ident_set(const char* id);
114eaf2578eSsthen 
115eaf2578eSsthen /**
116eaf2578eSsthen  * Set default identity to print, default is 'unbound'.
117eaf2578eSsthen  * @param id: string to print. Name of executable.
118eaf2578eSsthen  */
119eaf2578eSsthen void log_ident_set_default(const char* id);
120eaf2578eSsthen 
121eaf2578eSsthen /**
122eaf2578eSsthen  * Revert identity to print, back to the recorded default value.
123eaf2578eSsthen  */
124*a3167c07Ssthen void log_ident_revert_to_default(void);
125eaf2578eSsthen 
126eaf2578eSsthen /**
127eaf2578eSsthen  * Set identity to print if there is an identity, otherwise
128eaf2578eSsthen  * set the default.
129eaf2578eSsthen  * @param identity: the identity to set.
130eaf2578eSsthen  */
131eaf2578eSsthen void log_ident_set_or_default(const char* identity);
132933707f3Ssthen 
133933707f3Ssthen /**
134933707f3Ssthen  * Set if the time value is printed ascii or decimal in log entries.
135933707f3Ssthen  * @param use_asc: if true, ascii is printed, otherwise decimal.
136933707f3Ssthen  *	If the conversion fails or you have no time functions,
137933707f3Ssthen  *	decimal is printed.
138933707f3Ssthen  */
139933707f3Ssthen void log_set_time_asc(int use_asc);
140933707f3Ssthen 
141938a3a5eSflorian /** get log lock */
142938a3a5eSflorian void* log_get_lock(void);
143938a3a5eSflorian 
144933707f3Ssthen /**
145933707f3Ssthen  * Log informational message.
146933707f3Ssthen  * Pass printf formatted arguments. No trailing newline is needed.
147933707f3Ssthen  * @param format: printf-style format string. Arguments follow.
148933707f3Ssthen  */
149933707f3Ssthen void log_info(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
150933707f3Ssthen 
151933707f3Ssthen /**
152933707f3Ssthen  * Log error message.
153933707f3Ssthen  * Pass printf formatted arguments. No trailing newline is needed.
154933707f3Ssthen  * @param format: printf-style format string. Arguments follow.
155933707f3Ssthen  */
156933707f3Ssthen void log_err(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
157933707f3Ssthen 
158933707f3Ssthen /**
159933707f3Ssthen  * Log warning message.
160933707f3Ssthen  * Pass printf formatted arguments. No trailing newline is needed.
161933707f3Ssthen  * @param format: printf-style format string. Arguments follow.
162933707f3Ssthen  */
163933707f3Ssthen void log_warn(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
164933707f3Ssthen 
165933707f3Ssthen /**
166933707f3Ssthen  * Log a hex-string to the log. Can be any length.
167933707f3Ssthen  * performs mallocs to do so, slow. But debug useful.
168933707f3Ssthen  * @param msg: string desc to accompany the hexdump.
169933707f3Ssthen  * @param data: data to dump in hex format.
170933707f3Ssthen  * @param length: length of data.
171933707f3Ssthen  */
172933707f3Ssthen void log_hex(const char* msg, void* data, size_t length);
173933707f3Ssthen 
174933707f3Ssthen /**
175f6b99bafSsthen  * Log query.
176f6b99bafSsthen  * Pass printf formatted arguments. No trailing newline is needed.
177f6b99bafSsthen  * @param format: printf-style format string. Arguments follow.
178f6b99bafSsthen  */
179f6b99bafSsthen void log_query(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
180f6b99bafSsthen 
181f6b99bafSsthen /**
182f6b99bafSsthen  * Log reply.
183f6b99bafSsthen  * Pass printf formatted arguments. No trailing newline is needed.
184f6b99bafSsthen  * @param format: printf-style format string. Arguments follow.
185f6b99bafSsthen  */
186f6b99bafSsthen void log_reply(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
187f6b99bafSsthen 
188f6b99bafSsthen /**
1895d76a658Ssthen  * Easy alternative for log_hex, takes a sldns_buffer.
190933707f3Ssthen  * @param level: verbosity level for this message, compared to global
191933707f3Ssthen  *	verbosity setting.
192933707f3Ssthen  * @param msg: string desc to print
193933707f3Ssthen  * @param buf: the buffer.
194933707f3Ssthen  */
1955d76a658Ssthen void log_buf(enum verbosity_value level, const char* msg, struct sldns_buffer* buf);
196933707f3Ssthen 
197933707f3Ssthen /**
198933707f3Ssthen  * Log fatal error message, and exit the current process.
199933707f3Ssthen  * Pass printf formatted arguments. No trailing newline is needed.
200933707f3Ssthen  * @param format: printf-style format string. Arguments follow.
201933707f3Ssthen  */
202452a1548Ssthen void fatal_exit(const char* format, ...) ATTR_FORMAT(printf, 1, 2) ATTR_NORETURN;
203933707f3Ssthen 
204933707f3Ssthen /**
205933707f3Ssthen  * va_list argument version of log_info.
206933707f3Ssthen  * @param pri: priority type, for example 5 (INFO).
207933707f3Ssthen  * @param type: string to designate type of message (info, error).
208933707f3Ssthen  * @param format: the printf style format to print. no newline.
209933707f3Ssthen  * @param args: arguments for format string.
210933707f3Ssthen  */
211933707f3Ssthen void log_vmsg(int pri, const char* type, const char* format, va_list args);
212933707f3Ssthen 
213933707f3Ssthen /**
214933707f3Ssthen  * an assertion that is thrown to the logfile.
215933707f3Ssthen  */
216933707f3Ssthen #ifdef UNBOUND_DEBUG
217938a3a5eSflorian #ifdef __clang_analyzer__
218938a3a5eSflorian /* clang analyzer needs to know that log_assert is an assertion, otherwise
219938a3a5eSflorian  * it could complain about the nullptr the assert is guarding against. */
220938a3a5eSflorian #define log_assert(x) assert(x)
221938a3a5eSflorian #else
222933707f3Ssthen #  define log_assert(x) \
223933707f3Ssthen 	do { if(!(x)) \
224933707f3Ssthen 		fatal_exit("%s:%d: %s: assertion %s failed", \
225933707f3Ssthen 			__FILE__, __LINE__, __func__, #x); \
226933707f3Ssthen 	} while(0);
227938a3a5eSflorian #endif
228933707f3Ssthen #else
229933707f3Ssthen #  define log_assert(x) /*nothing*/
230933707f3Ssthen #endif
231933707f3Ssthen 
232933707f3Ssthen #ifdef USE_WINSOCK
233933707f3Ssthen /**
234933707f3Ssthen  * Convert WSA error into string.
235933707f3Ssthen  * @param err: from WSAGetLastError()
236933707f3Ssthen  * @return: string.
237933707f3Ssthen  */
238933707f3Ssthen char* wsa_strerror(DWORD err);
239933707f3Ssthen #endif /* USE_WINSOCK */
240933707f3Ssthen 
241933707f3Ssthen #endif /* UTIL_LOG_H */
242