1c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott/* $OpenBSD: err.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */ 2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott/* 4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * log.c 5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * 6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * Based on err.c, which was adapted from OpenBSD libc *err* *warn* code. 7c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * 8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * Copyright (c) 2005 Nick Mathewson <nickm@freehaven.net> 9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * 10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * Copyright (c) 2000 Dug Song <dugsong@monkey.org> 11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * 12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * Copyright (c) 1993 13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * The Regents of the University of California. All rights reserved. 14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * 15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * Redistribution and use in source and binary forms, with or without 16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * modification, are permitted provided that the following conditions 17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * are met: 18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * 1. Redistributions of source code must retain the above copyright 19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * notice, this list of conditions and the following disclaimer. 20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * 2. Redistributions in binary form must reproduce the above copyright 21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * notice, this list of conditions and the following disclaimer in the 22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * documentation and/or other materials provided with the distribution. 23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * 3. Neither the name of the University nor the names of its contributors 24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * may be used to endorse or promote products derived from this software 25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * without specific prior written permission. 26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * 27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott * SUCH DAMAGE. 38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott */ 39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifdef HAVE_CONFIG_H 41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "config.h" 42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifdef WIN32 45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define WIN32_LEAN_AND_MEAN 46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <windows.h> 47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#undef WIN32_LEAN_AND_MEAN 48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <sys/types.h> 50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifdef HAVE_SYS_TIME_H 51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <sys/time.h> 52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#else 53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <sys/_libevent_time.h> 54c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif 55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <stdio.h> 56c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <stdlib.h> 57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <stdarg.h> 58c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <string.h> 59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <errno.h> 60c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "event.h" 61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "log.h" 63c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "evutil.h" 64c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 65c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstatic void _warn_helper(int severity, int log_errno, const char *fmt, 66c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_list ap); 67c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstatic void event_log(int severity, const char *msg); 68c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 69c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid 70c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottevent_err(int eval, const char *fmt, ...) 71c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott{ 72c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_list ap; 73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_start(ap, fmt); 75c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott _warn_helper(_EVENT_LOG_ERR, errno, fmt, ap); 76c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_end(ap); 77c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott exit(eval); 78c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 79c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 80c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid 81c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottevent_warn(const char *fmt, ...) 82c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott{ 83c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_list ap; 84c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 85c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_start(ap, fmt); 86c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott _warn_helper(_EVENT_LOG_WARN, errno, fmt, ap); 87c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_end(ap); 88c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 89c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 90c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid 91c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottevent_errx(int eval, const char *fmt, ...) 92c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott{ 93c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_list ap; 94c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 95c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_start(ap, fmt); 96c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott _warn_helper(_EVENT_LOG_ERR, -1, fmt, ap); 97c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_end(ap); 98c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott exit(eval); 99c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 100c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 101c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid 102c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottevent_warnx(const char *fmt, ...) 103c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott{ 104c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_list ap; 105c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 106c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_start(ap, fmt); 107c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott _warn_helper(_EVENT_LOG_WARN, -1, fmt, ap); 108c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_end(ap); 109c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 110c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 111c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid 112c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottevent_msgx(const char *fmt, ...) 113c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott{ 114c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_list ap; 115c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 116c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_start(ap, fmt); 117c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott _warn_helper(_EVENT_LOG_MSG, -1, fmt, ap); 118c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_end(ap); 119c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 120c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 121c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid 122c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott_event_debugx(const char *fmt, ...) 123c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott{ 124c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_list ap; 125c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 126c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_start(ap, fmt); 127c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott _warn_helper(_EVENT_LOG_DEBUG, -1, fmt, ap); 128c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott va_end(ap); 129c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 130c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 131c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstatic void 132c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott_warn_helper(int severity, int log_errno, const char *fmt, va_list ap) 133c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott{ 134c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott char buf[1024]; 135c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott size_t len; 136c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 137c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (fmt != NULL) 138c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott evutil_vsnprintf(buf, sizeof(buf), fmt, ap); 139c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott else 140c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott buf[0] = '\0'; 141c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 142c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (log_errno >= 0) { 143c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott len = strlen(buf); 144c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (len < sizeof(buf) - 3) { 145c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott evutil_snprintf(buf + len, sizeof(buf) - len, ": %s", 146c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott strerror(log_errno)); 147c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 148c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 149c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 150c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott event_log(severity, buf); 151c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 152c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 153c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstatic event_log_cb log_fn = NULL; 154c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 155c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottvoid 156c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottevent_set_log_callback(event_log_cb cb) 157c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott{ 158c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott log_fn = cb; 159c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 160c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 161c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottstatic void 162c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottevent_log(int severity, const char *msg) 163c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott{ 164c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott if (log_fn) 165c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott log_fn(severity, msg); 166c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott else { 167c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott const char *severity_str; 168c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott switch (severity) { 169c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott case _EVENT_LOG_DEBUG: 170c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott severity_str = "debug"; 171c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott break; 172c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott case _EVENT_LOG_MSG: 173c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott severity_str = "msg"; 174c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott break; 175c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott case _EVENT_LOG_WARN: 176c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott severity_str = "warn"; 177c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott break; 178c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott case _EVENT_LOG_ERR: 179c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott severity_str = "err"; 180c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott break; 181c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott default: 182c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott severity_str = "???"; 183c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott break; 184c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 185c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott (void)fprintf(stderr, "[%s] %s\n", severity_str, msg); 186c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott } 187c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 188