122347Spst/*- 222347Spst * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru> 322347Spst * All rights reserved. 429964Sache * 592906Smarkm * Redistribution and use in source and binary forms, with or without 622347Spst * modification, are permitted provided that the following conditions 722347Spst * are met: 822347Spst * 1. Redistributions of source code must retain the above copyright 922347Spst * notice, this list of conditions and the following disclaimer. 1022347Spst * 2. Redistributions in binary form must reproduce the above copyright 1122347Spst * notice, this list of conditions and the following disclaimer in the 1222347Spst * documentation and/or other materials provided with the distribution. 1322347Spst * 1422347Spst * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1522347Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1622347Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1722347Spst * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1829964Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1922347Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2022347Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2122347Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2222347Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2322347Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2422347Spst * SUCH DAMAGE. 2522347Spst * 2622347Spst */ 2722347Spst 2822347Spst#include <sys/cdefs.h> 2922347Spst__FBSDID("$FreeBSD: releng/10.3/usr.sbin/nscd/log.c 194093 2009-06-13 00:43:56Z des $"); 3022347Spst 3122347Spst#include <assert.h> 3222347Spst#include <stdarg.h> 3322347Spst#include <stdio.h> 3429964Sache#include <stdlib.h> 3529964Sache#include <syslog.h> 3629964Sache 3722347Spst#include "log.h" 3822347Spst 3922347Spstvoid 4022347Spst__log_msg(int level, const char *sender, const char *message, ...) 4122347Spst{ 4222347Spst va_list ap; 4322347Spst char *fmessage; 4422347Spst 4522347Spst fmessage = NULL; 4622347Spst va_start(ap, message); 4722347Spst vasprintf(&fmessage, message, ap); 4822347Spst va_end(ap); 4922347Spst assert(fmessage != NULL); 5022347Spst 5192906Smarkm printf("M%d from %s: %s\n", level, sender, fmessage); 5222347Spst#ifndef NO_SYSLOG 5322347Spst if (level == 0) 5422347Spst syslog(LOG_INFO, "nscd message (from %s): %s", sender, 5522347Spst fmessage); 5622347Spst#endif 5722347Spst free(fmessage); 5822347Spst} 5922347Spst 6022347Spstvoid 6122347Spst__log_err(int level, const char *sender, const char *error, ...) 6222347Spst{ 6322347Spst va_list ap; 6422347Spst char *ferror; 6522347Spst 6622347Spst ferror = NULL; 6722347Spst va_start(ap, error); 6822347Spst vasprintf(&ferror, error, ap); 6922347Spst va_end(ap); 7022347Spst assert(ferror != NULL); 7122347Spst 7222347Spst printf("E%d from %s: %s\n", level, sender, ferror); 7322347Spst 7422347Spst#ifndef NO_SYSLOG 7522347Spst if (level == 0) 7622347Spst syslog(LOG_ERR, "nscd error (from %s): %s", sender, ferror); 7722347Spst#endif 7822347Spst free(ferror); 7922347Spst} 8022347Spst