Revert last. It is still wrong.
[gnupg.git] / util / logger.c
blob857436c43396b75be9e85df19720da18f80acca3
1 /* logger.c - log functions
2 * Copyright (C) 1998, 1999 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <errno.h>
29 #include "util.h"
30 #include "i18n.h"
32 static char pidstring[15];
33 static char *pgm_name;
34 static int errorcount;
35 static int strict;
36 static FILE *logfp;
38 /****************
39 * Set the logfile to use (not yet implemneted) or, if logfile is NULL,
40 * the Fd where logoutputs should go.
42 void
43 log_set_logfile( const char *name, int fd )
45 if( name )
46 BUG();
48 if( logfp && logfp != stderr && logfp != stdout )
49 fclose( logfp );
50 if( fd == 1 )
51 logfp = stdout;
52 else if( fd == 2 )
53 logfp = stderr;
54 else
55 logfp = fdopen( fd, "a" );
56 if( !logfp ) {
57 logfp = stderr;
58 log_fatal("can't open fd %d for logging: %s\n", fd, strerror(errno));
62 FILE *
63 log_stream()
65 if( !logfp )
66 logfp = stderr;
67 return logfp;
71 void
72 log_set_name( const char *name )
74 xfree(pgm_name);
75 if( name )
76 pgm_name = xstrdup(name);
77 else
78 pgm_name = NULL;
81 const char *
82 log_get_name(void)
84 return pgm_name? pgm_name : "";
88 void
89 log_set_pid( int pid )
91 if( pid )
92 sprintf(pidstring,"[%u]", (unsigned)pid );
93 else
94 *pidstring = 0;
97 int
98 log_get_errorcount( int clear)
100 int n = errorcount;
101 if( clear )
102 errorcount = 0;
103 return n;
106 void
107 log_inc_errorcount()
109 errorcount++;
113 log_set_strict(int val)
115 int old=strict;
116 strict=val;
117 return old;
120 void
121 g10_log_print_prefix(const char *text)
123 if( !logfp )
124 logfp = stderr;
125 if( pgm_name )
126 fprintf(logfp, "%s%s: %s", pgm_name, pidstring, text );
127 else
128 fprintf(logfp, "?%s: %s", pidstring, text );
129 #ifdef __riscos__
130 fflush( logfp );
131 #endif /* __riscos__ */
135 void
136 g10_log_info( const char *fmt, ... )
138 va_list arg_ptr ;
140 g10_log_print_prefix("");
141 va_start( arg_ptr, fmt ) ;
142 vfprintf(logfp,fmt,arg_ptr) ;
143 va_end(arg_ptr);
144 #ifdef __riscos__
145 fflush( logfp );
146 #endif /* __riscos__ */
150 void
151 g10_log_warning( const char *fmt, ... )
153 va_list arg_ptr ;
155 if(strict)
157 errorcount++;
158 g10_log_print_prefix(_("ERROR: "));
160 else
161 g10_log_print_prefix(_("WARNING: "));
163 va_start( arg_ptr, fmt ) ;
164 vfprintf(logfp,fmt,arg_ptr) ;
165 va_end(arg_ptr);
166 #ifdef __riscos__
167 fflush( logfp );
168 #endif /* __riscos__ */
172 void
173 g10_log_error( const char *fmt, ... )
175 va_list arg_ptr ;
177 g10_log_print_prefix("");
178 va_start( arg_ptr, fmt ) ;
179 vfprintf(logfp,fmt,arg_ptr) ;
180 va_end(arg_ptr);
181 errorcount++;
182 #ifdef __riscos__
183 fflush( logfp );
184 #endif /* __riscos__ */
188 void
189 g10_log_fatal( const char *fmt, ... )
191 va_list arg_ptr ;
193 g10_log_print_prefix("fatal: ");
194 va_start( arg_ptr, fmt ) ;
195 vfprintf(logfp,fmt,arg_ptr) ;
196 va_end(arg_ptr);
197 secmem_dump_stats();
198 #ifdef __riscos__
199 fflush( logfp );
200 #endif /* __riscos__ */
201 exit(2);
204 void
205 g10_log_bug( const char *fmt, ... )
207 va_list arg_ptr ;
209 putc('\n', stderr );
210 g10_log_print_prefix("Ohhhh jeeee: ");
211 va_start( arg_ptr, fmt ) ;
212 vfprintf(stderr,fmt,arg_ptr) ;
213 va_end(arg_ptr);
214 fflush(stderr);
215 secmem_dump_stats();
216 abort();
219 #if defined (__riscos__) \
220 || ( __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ))
221 void
222 g10_log_bug0( const char *file, int line, const char *func )
224 log_bug(_("... this is a bug (%s:%d:%s)\n"), file, line, func );
226 #else
227 void
228 g10_log_bug0( const char *file, int line )
230 log_bug(_("you found a bug ... (%s:%d)\n"), file, line);
232 #endif
234 void
235 g10_log_debug( const char *fmt, ... )
237 va_list arg_ptr ;
239 g10_log_print_prefix("DBG: ");
240 va_start( arg_ptr, fmt ) ;
241 vfprintf(logfp,fmt,arg_ptr) ;
242 va_end(arg_ptr);
243 #ifdef __riscos__
244 fflush( logfp );
245 #endif /* __riscos__ */
250 void
251 g10_log_hexdump( const char *text, const char *buf, size_t len )
253 int i;
255 g10_log_print_prefix(text);
256 for(i=0; i < len; i++ )
257 fprintf(logfp, " %02X", ((const byte*)buf)[i] );
258 fputc('\n', logfp);
259 #ifdef __riscos__
260 fflush( logfp );
261 #endif /* __riscos__ */