1 /* logging.c - useful logging functions
2 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 /* This file should replace logger.c in the future - for now it is not
23 * used by GnuPG but by GPA.
24 * It is a quite simple implemenation but sufficient for most purposes.
34 #include <sys/types.h>
40 #define JNLIB_NEED_LOG_LOGV 1
41 #include "libjnlib-config.h"
45 static FILE *logstream
;
46 static char prefix_buffer
[80];
48 static int with_prefix
;
51 static int missing_lf
;
52 static int errorcount
;
56 write2stderr( const char *s
)
58 write( 2, s
, strlen(s
) );
63 do_die(int rc
, const char *text
)
65 write2stderr("\nFatal error: ");
73 log_get_errorcount (int clear
)
82 log_set_file( const char *name
)
84 FILE *fp
= (name
&& strcmp(name
,"-"))? fopen(name
, "a") : stderr
;
86 fprintf(stderr
, "failed to open log file `%s': %s\n",
87 name
, strerror(errno
));
90 setvbuf( fp
, NULL
, _IOLBF
, 0 );
92 if( logstream
&& logstream
!= stderr
)
100 log_set_prefix (const char *text
, unsigned int flags
)
104 strncpy (prefix_buffer
, text
, sizeof (prefix_buffer
)-1);
105 prefix_buffer
[sizeof (prefix_buffer
)-1] = 0;
108 with_prefix
= (flags
& 1);
109 with_time
= (flags
& 2);
110 with_pid
= (flags
& 4);
116 return fileno(logstream
?logstream
:stderr
);
122 return logstream
?logstream
:stderr
;
127 do_logv( int level
, const char *fmt
, va_list arg_ptr
)
132 if (missing_lf
&& level
!= JNLIB_LOG_CONT
)
133 putc('\n', logstream
);
136 if (level
!= JNLIB_LOG_CONT
)
137 { /* Note this does not work for multiple line logging as we would
138 * need to print to a buffer first */
142 time_t atime
= time (NULL
);
144 tp
= localtime (&atime
);
145 fprintf (logstream
, "%04d-%02d-%02d %02d:%02d:%02d ",
146 1900+tp
->tm_year
, tp
->tm_mon
+1, tp
->tm_mday
,
147 tp
->tm_hour
, tp
->tm_min
, tp
->tm_sec
);
150 fputs (prefix_buffer
, logstream
);
152 fprintf (logstream
, "[%u]", (unsigned int)getpid ());
154 putc (':', logstream
);
155 putc (' ', logstream
);
160 case JNLIB_LOG_BEGIN
: break;
161 case JNLIB_LOG_CONT
: break;
162 case JNLIB_LOG_INFO
: break;
163 case JNLIB_LOG_WARN
: break;
164 case JNLIB_LOG_ERROR
: break;
165 case JNLIB_LOG_FATAL
: fputs("Fatal: ",logstream
); break;
166 case JNLIB_LOG_BUG
: fputs("Ohhhh jeeee: ", logstream
); break;
167 case JNLIB_LOG_DEBUG
: fputs("DBG: ", logstream
); break;
168 default: fprintf(logstream
,"[Unknown log level %d]: ", level
); break;
173 vfprintf(logstream
,fmt
,arg_ptr
) ;
174 if (*fmt
&& fmt
[strlen(fmt
)-1] != '\n')
178 if (level
== JNLIB_LOG_FATAL
)
180 if (level
== JNLIB_LOG_BUG
)
185 do_log( int level
, const char *fmt
, ... )
189 va_start( arg_ptr
, fmt
) ;
190 do_logv( level
, fmt
, arg_ptr
);
196 log_logv (int level
, const char *fmt
, va_list arg_ptr
)
198 do_logv (level
, fmt
, arg_ptr
);
202 log_info( const char *fmt
, ... )
206 va_start( arg_ptr
, fmt
) ;
207 do_logv( JNLIB_LOG_INFO
, fmt
, arg_ptr
);
212 log_error( const char *fmt
, ... )
216 va_start( arg_ptr
, fmt
) ;
217 do_logv( JNLIB_LOG_ERROR
, fmt
, arg_ptr
);
219 /* protect against counter overflow */
220 if( errorcount
< 30000 )
226 log_fatal( const char *fmt
, ... )
230 va_start( arg_ptr
, fmt
) ;
231 do_logv( JNLIB_LOG_FATAL
, fmt
, arg_ptr
);
233 abort(); /* never called, bugs it makes the compiler happy */
237 log_bug( const char *fmt
, ... )
241 va_start( arg_ptr
, fmt
) ;
242 do_logv( JNLIB_LOG_BUG
, fmt
, arg_ptr
);
244 abort(); /* never called, but it makes the compiler happy */
248 log_debug( const char *fmt
, ... )
252 va_start( arg_ptr
, fmt
) ;
253 do_logv( JNLIB_LOG_DEBUG
, fmt
, arg_ptr
);
259 log_printf (const char *fmt
, ...)
263 va_start (arg_ptr
, fmt
);
264 do_logv (fmt
? JNLIB_LOG_CONT
: JNLIB_LOG_BEGIN
, fmt
, arg_ptr
);
268 /* Print a hexdump of BUFFER. With TEXT of NULL print just the raw
269 dump, with TEXT just an empty string, print a trailing linefeed,
270 otherwise print an entire debug line. */
272 log_printhex (const char *text
, const void *buffer
, size_t length
)
275 log_debug ("%s ", text
);
278 const unsigned char *p
= buffer
;
279 log_printf ("%02X", *p
);
280 for (length
--, p
++; length
--; p
++)
281 log_printf (" %02X", *p
);
288 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 )
290 bug_at( const char *file
, int line
, const char *func
)
292 do_log( JNLIB_LOG_BUG
,
293 ("... this is a bug (%s:%d:%s)\n"), file
, line
, func
);
294 abort(); /* never called, but it makes the compiler happy */
298 bug_at( const char *file
, int line
)
300 do_log( JNLIB_LOG_BUG
,
301 _("you found a bug ... (%s:%d)\n"), file
, line
);
302 abort(); /* never called, but it makes the compiler happy */