1238106Sdes/*
2238106Sdes * util/log.h - logging service
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24238106Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25238106Sdes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26238106Sdes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27238106Sdes * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28238106Sdes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29238106Sdes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30238106Sdes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31238106Sdes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32238106Sdes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33238106Sdes * POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * This file contains logging functions.
40238106Sdes */
41238106Sdes
42238106Sdes#ifndef UTIL_LOG_H
43238106Sdes#define UTIL_LOG_H
44238106Sdes#include <ldns/buffer.h>
45238106Sdes
46238106Sdes/**
47238106Sdes * verbosity value:
48238106Sdes */
49238106Sdesenum verbosity_value {
50238106Sdes /** 0 - no verbose messages */
51238106Sdes	NO_VERBOSE = 0,
52238106Sdes /** 1 - operational information */
53238106Sdes 	VERB_OPS,
54238106Sdes /** 2 - detailed information */
55238106Sdes 	VERB_DETAIL,
56238106Sdes /** 3 - query level information */
57238106Sdes 	VERB_QUERY,
58238106Sdes /** 4 - algorithm level information */
59238106Sdes 	VERB_ALGO,
60238106Sdes /** 5 - querier client information */
61238106Sdes	VERB_CLIENT
62238106Sdes};
63238106Sdes
64238106Sdes/** The global verbosity setting */
65238106Sdesextern enum verbosity_value verbosity;
66238106Sdes
67238106Sdes/**
68238106Sdes * log a verbose message, pass the level for this message.
69238106Sdes * It has printf formatted arguments. No trailing newline is needed.
70238106Sdes * @param level: verbosity level for this message, compared to global
71238106Sdes *	verbosity setting.
72238106Sdes * @param format: printf-style format string. Arguments follow.
73238106Sdes */
74238106Sdesvoid verbose(enum verbosity_value level,
75238106Sdes	const char* format, ...) ATTR_FORMAT(printf, 2, 3);
76238106Sdes
77238106Sdes/**
78238106Sdes * call this to initialize logging services.
79238106Sdes * @param filename: if NULL stderr is used.
80238106Sdes * @param use_syslog: set to true to ignore filename and use syslog(3).
81238106Sdes * @param chrootdir: to which directory we have been chrooted, if any.
82238106Sdes */
83238106Sdesvoid log_init(const char* filename, int use_syslog, const char* chrootdir);
84238106Sdes
85238106Sdes/**
86238106Sdes * Set logging to go to the specified file *.
87238106Sdes * This setting does not affect the use_syslog setting.
88238106Sdes * @param f: to that file, or pass NULL to disable logging.
89238106Sdes */
90238106Sdesvoid log_file(FILE *f);
91238106Sdes
92238106Sdes/**
93238106Sdes * Init a thread (will print this number for the thread log entries).
94238106Sdes * Must be called from the thread itself. If not called 0 is printed.
95238106Sdes * @param num: number to print for this thread. Owned by caller, must
96238106Sdes *	continue to exist.
97238106Sdes */
98238106Sdesvoid log_thread_set(int* num);
99238106Sdes
100238106Sdes/**
101238106Sdes * Set identity to print, default is 'unbound'.
102238106Sdes * @param id: string to print. Name of executable.
103238106Sdes */
104238106Sdesvoid log_ident_set(const char* id);
105238106Sdes
106238106Sdes/**
107238106Sdes * Set the time value to print in log entries.
108238106Sdes * @param t: the point is copied and used to find the time.
109238106Sdes * 	if NULL, time(2) is used.
110238106Sdes */
111238106Sdesvoid log_set_time(uint32_t* t);
112238106Sdes
113238106Sdes/**
114238106Sdes * Set if the time value is printed ascii or decimal in log entries.
115238106Sdes * @param use_asc: if true, ascii is printed, otherwise decimal.
116238106Sdes *	If the conversion fails or you have no time functions,
117238106Sdes *	decimal is printed.
118238106Sdes */
119238106Sdesvoid log_set_time_asc(int use_asc);
120238106Sdes
121238106Sdes/**
122238106Sdes * Log informational message.
123238106Sdes * Pass printf formatted arguments. No trailing newline is needed.
124238106Sdes * @param format: printf-style format string. Arguments follow.
125238106Sdes */
126238106Sdesvoid log_info(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
127238106Sdes
128238106Sdes/**
129238106Sdes * Log error message.
130238106Sdes * Pass printf formatted arguments. No trailing newline is needed.
131238106Sdes * @param format: printf-style format string. Arguments follow.
132238106Sdes */
133238106Sdesvoid log_err(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
134238106Sdes
135238106Sdes/**
136238106Sdes * Log warning message.
137238106Sdes * Pass printf formatted arguments. No trailing newline is needed.
138238106Sdes * @param format: printf-style format string. Arguments follow.
139238106Sdes */
140238106Sdesvoid log_warn(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
141238106Sdes
142238106Sdes/**
143238106Sdes * Log a hex-string to the log. Can be any length.
144238106Sdes * performs mallocs to do so, slow. But debug useful.
145238106Sdes * @param msg: string desc to accompany the hexdump.
146238106Sdes * @param data: data to dump in hex format.
147238106Sdes * @param length: length of data.
148238106Sdes */
149238106Sdesvoid log_hex(const char* msg, void* data, size_t length);
150238106Sdes
151238106Sdes/**
152238106Sdes * Easy alternative for log_hex, takes a ldns_buffer.
153238106Sdes * @param level: verbosity level for this message, compared to global
154238106Sdes *	verbosity setting.
155238106Sdes * @param msg: string desc to print
156238106Sdes * @param buf: the buffer.
157238106Sdes */
158238106Sdesvoid log_buf(enum verbosity_value level, const char* msg, ldns_buffer* buf);
159238106Sdes
160238106Sdes/**
161238106Sdes * Log fatal error message, and exit the current process.
162238106Sdes * Pass printf formatted arguments. No trailing newline is needed.
163238106Sdes * @param format: printf-style format string. Arguments follow.
164238106Sdes */
165238106Sdesvoid fatal_exit(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
166238106Sdes
167238106Sdes/**
168238106Sdes * va_list argument version of log_info.
169238106Sdes * @param pri: priority type, for example 5 (INFO).
170238106Sdes * @param type: string to designate type of message (info, error).
171238106Sdes * @param format: the printf style format to print. no newline.
172238106Sdes * @param args: arguments for format string.
173238106Sdes */
174238106Sdesvoid log_vmsg(int pri, const char* type, const char* format, va_list args);
175238106Sdes
176238106Sdes/**
177238106Sdes * an assertion that is thrown to the logfile.
178238106Sdes */
179238106Sdes#ifdef UNBOUND_DEBUG
180238106Sdes#  define log_assert(x) \
181238106Sdes	do { if(!(x)) \
182238106Sdes		fatal_exit("%s:%d: %s: assertion %s failed", \
183238106Sdes			__FILE__, __LINE__, __func__, #x); \
184238106Sdes	} while(0);
185238106Sdes#else
186238106Sdes#  define log_assert(x) /*nothing*/
187238106Sdes#endif
188238106Sdes
189238106Sdes#ifdef USE_WINSOCK
190238106Sdes/**
191238106Sdes * Convert WSA error into string.
192238106Sdes * @param err: from WSAGetLastError()
193238106Sdes * @return: string.
194238106Sdes */
195238106Sdeschar* wsa_strerror(DWORD err);
196238106Sdes#endif /* USE_WINSOCK */
197238106Sdes
198238106Sdes#endif /* UTIL_LOG_H */
199