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