/* * Copyright (c) 2004 Teodor Sigaev * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include "tlog.h" static FILE *TL_ErrOut = NULL; static u_int32_t TL_DebugLevel = TL_INFO; static u_int32_t tlflag = TL_OPEN_STDERR | TL_OPEN_SYSLOG; static int TLtoSYS(int level) { level &= ~TL_EXIT; switch(level) { case TL_CRIT : return LOG_CRIT; case TL_ALARM: return LOG_ERR; case TL_WARN : return LOG_WARNING; case TL_INFO : return LOG_INFO; case TL_DEBUG: return LOG_DEBUG; default: return LOG_WARNING; } return LOG_ERR; } void opentlog(u_int32_t flag, u_int32_t level, char *file) { if (flag==0) flag=tlflag; TL_DebugLevel=level; tlflag &= ~TL_OPEN_FILE; if ( TL_ErrOut ) fclose(TL_ErrOut); if ( (flag & TL_OPEN_FILE) && file ) { if ( (TL_ErrOut=fopen(file,"a")) == NULL ) tlog(TL_ALARM,"Can't open log file '%s': %s", file, strerror(errno)); else { u_int32_t oldflag = tlflag; tlflag = TL_OPEN_FILE; tlog(TL_INFO, "Log opened"); tlflag = oldflag | TL_OPEN_FILE; } } tlflag &= ~TL_OPEN_SYSLOG; tlflag |= (flag & TL_OPEN_SYSLOG); tlflag &= ~TL_OPEN_STDERR; tlflag |= (flag & TL_OPEN_STDERR); } void closetlog() { if ( TL_ErrOut ) { if ( tlflag & TL_OPEN_FILE ) { tlflag = TL_OPEN_FILE; tlog(TL_INFO,"Log closed"); } fclose(TL_ErrOut); } TL_ErrOut=NULL; tlflag = TL_OPEN_STDERR | TL_OPEN_SYSLOG; } void tlog(u_int32_t level,const char *format, ...) { va_list args; if ( (level & TL_EXIT)==0 ) if ( (level & ~TL_EXIT) > TL_DebugLevel ) return; if ( tlflag & (TL_OPEN_STDERR | TL_OPEN_FILE) ) { time_t t; char buf[64]; t = time(NULL); strftime(buf,64,"%H:%M:%S %d/%m/%Y",localtime(&t)); if ( tlflag & TL_OPEN_STDERR ) { fprintf( stderr,"%d %s ", getpid(), buf ); va_start(args, format); vfprintf( stderr, format, args); va_end(args); fputc('\n', stderr); } if ( (tlflag & TL_OPEN_FILE) && TL_ErrOut ) { fprintf( TL_ErrOut,"%d %s ", getpid(), buf ); va_start(args, format); vfprintf( TL_ErrOut, format, args); va_end(args); fputc('\n', TL_ErrOut); fflush(TL_ErrOut); } } if ( tlflag & TL_OPEN_SYSLOG ) { va_start(args, format); vsyslog( TLtoSYS(level), format, args ); va_end(args); } if ( level & TL_EXIT ) { tlog(level & ~TL_EXIT, "Exitting..."); closetlog(); exit(1); } }