2 * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
41 static FILE *TL_ErrOut = NULL;
42 static u_int32_t TL_DebugLevel = TL_INFO;
43 static u_int32_t tlflag = TL_OPEN_STDERR | TL_OPEN_SYSLOG;
49 case TL_CRIT : return LOG_CRIT;
50 case TL_ALARM: return LOG_ERR;
51 case TL_WARN : return LOG_WARNING;
52 case TL_INFO : return LOG_INFO;
53 case TL_DEBUG: return LOG_DEBUG;
54 default: return LOG_WARNING;
60 opentlog(u_int32_t flag, u_int32_t level, char *file) {
66 tlflag &= ~TL_OPEN_FILE;
69 if ( (flag & TL_OPEN_FILE) && file ) {
70 if ( (TL_ErrOut=fopen(file,"a")) == NULL )
71 tlog(TL_ALARM,"Can't open log file '%s': %s", file, strerror(errno));
73 u_int32_t oldflag = tlflag;
74 tlflag = TL_OPEN_FILE;
75 tlog(TL_INFO, "Log opened");
76 tlflag = oldflag | TL_OPEN_FILE;
80 tlflag &= ~TL_OPEN_SYSLOG;
81 tlflag |= (flag & TL_OPEN_SYSLOG);
83 tlflag &= ~TL_OPEN_STDERR;
84 tlflag |= (flag & TL_OPEN_STDERR);
90 if ( tlflag & TL_OPEN_FILE ) {
91 tlflag = TL_OPEN_FILE;
92 tlog(TL_INFO,"Log closed");
97 tlflag = TL_OPEN_STDERR | TL_OPEN_SYSLOG;
101 tlog(u_int32_t level,const char *format, ...) {
104 if ( (level & TL_EXIT)==0 )
105 if ( (level & ~TL_EXIT) > TL_DebugLevel )
108 if ( tlflag & (TL_OPEN_STDERR | TL_OPEN_FILE) ) {
113 strftime(buf,64,"%H:%M:%S %d/%m/%Y",localtime(&t));
114 if ( tlflag & TL_OPEN_STDERR ) {
115 fprintf( stderr,"%d %s ", getpid(), buf );
116 va_start(args, format);
117 vfprintf( stderr, format, args);
121 if ( (tlflag & TL_OPEN_FILE) && TL_ErrOut ) {
122 fprintf( TL_ErrOut,"%d %s ", getpid(), buf );
123 va_start(args, format);
124 vfprintf( TL_ErrOut, format, args);
126 fputc('\n', TL_ErrOut);
130 if ( tlflag & TL_OPEN_SYSLOG ) {
131 va_start(args, format);
132 vsyslog( TLtoSYS( (int)(level & ~TL_EXIT) ), format, args );
136 if ( level & TL_EXIT ) {
137 tlog(level & ~TL_EXIT, "Exitting...");