2 * util/log.h - logging service
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains logging functions.
49 enum verbosity_value {
50 /** 0 - no verbose messages */
52 /** 1 - operational information */
54 /** 2 - detailed information */
56 /** 3 - query level information */
58 /** 4 - algorithm level information */
60 /** 5 - querier client information */
64 /** The global verbosity setting */
65 extern enum verbosity_value verbosity;
68 * log a verbose message, pass the level for this message.
69 * It has printf formatted arguments. No trailing newline is needed.
70 * @param level: verbosity level for this message, compared to global
72 * @param format: printf-style format string. Arguments follow.
74 void verbose(enum verbosity_value level,
75 const char* format, ...) ATTR_FORMAT(printf, 2, 3);
78 * call this to initialize logging services.
79 * @param filename: if NULL stderr is used.
80 * @param use_syslog: set to true to ignore filename and use syslog(3).
81 * @param chrootdir: to which directory we have been chrooted, if any.
83 void log_init(const char* filename, int use_syslog, const char* chrootdir);
86 * Set logging to go to the specified file *.
87 * This setting does not affect the use_syslog setting.
88 * @param f: to that file, or pass NULL to disable logging.
90 void log_file(FILE *f);
93 * Init a thread (will print this number for the thread log entries).
94 * Must be called from the thread itself. If not called 0 is printed.
95 * @param num: number to print for this thread. Owned by caller, must
98 void log_thread_set(int* num);
101 * Get the thread id from logging system. Set after log_init is
102 * initialised, or log_thread_set for newly created threads.
103 * This initialisation happens in unbound as a daemon, in daemon
104 * startup code, when that spawns threads.
105 * @return thread number, from 0 and up. Before initialised, returns 0.
107 int log_thread_get(void);
110 * Set identity to print, default is 'unbound'.
111 * @param id: string to print. Name of executable.
113 void log_ident_set(const char* id);
116 * Set the time value to print in log entries.
117 * @param t: the point is copied and used to find the time.
118 * if NULL, time(2) is used.
120 void log_set_time(time_t* t);
123 * Set if the time value is printed ascii or decimal in log entries.
124 * @param use_asc: if true, ascii is printed, otherwise decimal.
125 * If the conversion fails or you have no time functions,
126 * decimal is printed.
128 void log_set_time_asc(int use_asc);
131 * Log informational message.
132 * Pass printf formatted arguments. No trailing newline is needed.
133 * @param format: printf-style format string. Arguments follow.
135 void log_info(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
139 * Pass printf formatted arguments. No trailing newline is needed.
140 * @param format: printf-style format string. Arguments follow.
142 void log_err(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
145 * Log warning message.
146 * Pass printf formatted arguments. No trailing newline is needed.
147 * @param format: printf-style format string. Arguments follow.
149 void log_warn(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
152 * Log a hex-string to the log. Can be any length.
153 * performs mallocs to do so, slow. But debug useful.
154 * @param msg: string desc to accompany the hexdump.
155 * @param data: data to dump in hex format.
156 * @param length: length of data.
158 void log_hex(const char* msg, void* data, size_t length);
161 * Easy alternative for log_hex, takes a sldns_buffer.
162 * @param level: verbosity level for this message, compared to global
164 * @param msg: string desc to print
165 * @param buf: the buffer.
167 void log_buf(enum verbosity_value level, const char* msg, struct sldns_buffer* buf);
170 * Log fatal error message, and exit the current process.
171 * Pass printf formatted arguments. No trailing newline is needed.
172 * @param format: printf-style format string. Arguments follow.
174 void fatal_exit(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
177 * va_list argument version of log_info.
178 * @param pri: priority type, for example 5 (INFO).
179 * @param type: string to designate type of message (info, error).
180 * @param format: the printf style format to print. no newline.
181 * @param args: arguments for format string.
183 void log_vmsg(int pri, const char* type, const char* format, va_list args);
186 * an assertion that is thrown to the logfile.
189 # define log_assert(x) \
191 fatal_exit("%s:%d: %s: assertion %s failed", \
192 __FILE__, __LINE__, __func__, #x); \
195 # define log_assert(x) /*nothing*/
200 * Convert WSA error into string.
201 * @param err: from WSAGetLastError()
204 char* wsa_strerror(DWORD err);
205 #endif /* USE_WINSOCK */
207 #endif /* UTIL_LOG_H */