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;
50 case TL_CRIT : return LOG_CRIT;
51 case TL_ALARM: return LOG_ERR;
52 case TL_WARN : return LOG_WARNING;
53 case TL_INFO : return LOG_INFO;
54 case TL_DEBUG: return LOG_DEBUG;
55 default: return LOG_WARNING;
61 opentlog(u_int32_t flag, u_int32_t level, char *file) {
67 tlflag &= ~TL_OPEN_FILE;
70 if ( (flag & TL_OPEN_FILE) && file ) {
71 if ( (TL_ErrOut=fopen(file,"a")) == NULL )
72 tlog(TL_ALARM,"Can't open log file '%s': %s", file, strerror(errno));
74 u_int32_t oldflag = tlflag;
75 tlflag = TL_OPEN_FILE;
76 tlog(TL_INFO, "Log opened");
77 tlflag = oldflag | TL_OPEN_FILE;
81 tlflag &= ~TL_OPEN_SYSLOG;
82 tlflag |= (flag & TL_OPEN_SYSLOG);
84 tlflag &= ~TL_OPEN_STDERR;
85 tlflag |= (flag & TL_OPEN_STDERR);
91 if ( tlflag & TL_OPEN_FILE ) {
92 tlflag = TL_OPEN_FILE;
93 tlog(TL_INFO,"Log closed");
98 tlflag = TL_OPEN_STDERR | TL_OPEN_SYSLOG;
102 tlog(u_int32_t level,const char *format, ...) {
105 if ( (level & TL_EXIT)==0 )
106 if ( (level & ~TL_EXIT) > TL_DebugLevel )
109 if ( tlflag & (TL_OPEN_STDERR | TL_OPEN_FILE) ) {
114 strftime(buf,64,"%H:%M:%S %d/%m/%Y",localtime(&t));
115 if ( tlflag & TL_OPEN_STDERR ) {
116 fprintf( stderr,"%d %s ", getpid(), buf );
117 va_start(args, format);
118 vfprintf( stderr, format, args);
122 if ( (tlflag & TL_OPEN_FILE) && TL_ErrOut ) {
123 fprintf( TL_ErrOut,"%d %s ", getpid(), buf );
124 va_start(args, format);
125 vfprintf( TL_ErrOut, format, args);
127 fputc('\n', TL_ErrOut);
132 if ( tlflag & TL_OPEN_SYSLOG ) {
133 va_start(args, format);
134 vsyslog( TLtoSYS(level), format, args );
138 if ( level & TL_EXIT ) {
139 tlog(level & ~TL_EXIT, "Exitting...");