raw
mpi-genesis~            1 /* logger.c  -	log functions
mpi-genesis~ 2 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
mpi-genesis~ 3 *
mpi-genesis~ 4 * This file is part of GnuPG.
mpi-genesis~ 5 *
mpi-genesis~ 6 * GnuPG is free software; you can redistribute it and/or modify
mpi-genesis~ 7 * it under the terms of the GNU General Public License as published by
mpi-genesis~ 8 * the Free Software Foundation; either version 3 of the License, or
mpi-genesis~ 9 * (at your option) any later version.
mpi-genesis~ 10 *
mpi-genesis~ 11 * GnuPG is distributed in the hope that it will be useful,
mpi-genesis~ 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis~ 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis~ 14 * GNU General Public License for more details.
mpi-genesis~ 15 *
mpi-genesis~ 16 * You should have received a copy of the GNU General Public License
mpi-genesis~ 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
mpi-genesis~ 18 */
mpi-genesis~ 19
mpi-genesis~ 20 #include <config.h>
mpi-genesis~ 21 #include <stdio.h>
mpi-genesis~ 22 #include <stdlib.h>
mpi-genesis~ 23 #include <stdarg.h>
mpi-genesis~ 24 #include <string.h>
mpi-genesis~ 25 #include <errno.h>
mpi-genesis~ 26
mpi-genesis~ 27 #include "util.h"
mpi-genesis~ 28
mpi-genesis~ 29 static char pidstring[15];
mpi-genesis~ 30 static char *pgm_name;
mpi-genesis~ 31 static int errorcount;
mpi-genesis~ 32 static int strict;
mpi-genesis~ 33 static FILE *logfp;
mpi-genesis~ 34
mpi-genesis~ 35 /****************
mpi-genesis~ 36 * Set the logfile to use (not yet implemneted) or, if logfile is NULL,
mpi-genesis~ 37 * the Fd where logoutputs should go.
mpi-genesis~ 38 */
mpi-genesis~ 39 void
mpi-genesis~ 40 log_set_logfile( const char *name, int fd )
mpi-genesis~ 41 {
mpi-genesis~ 42 if( name )
mpi-genesis~ 43 BUG();
mpi-genesis~ 44
mpi-genesis~ 45 if( logfp && logfp != stderr && logfp != stdout )
mpi-genesis~ 46 fclose( logfp );
mpi-genesis~ 47 if( fd == 1 )
mpi-genesis~ 48 logfp = stdout;
mpi-genesis~ 49 else if( fd == 2 )
mpi-genesis~ 50 logfp = stderr;
mpi-genesis~ 51 else
mpi-genesis~ 52 logfp = fdopen( fd, "a" );
mpi-genesis~ 53 if( !logfp ) {
mpi-genesis~ 54 logfp = stderr;
mpi-genesis~ 55 log_fatal("can't open fd %d for logging: %s\n", fd, strerror(errno));
mpi-genesis~ 56 }
mpi-genesis~ 57 }
mpi-genesis~ 58
mpi-genesis~ 59 FILE *
mpi-genesis~ 60 log_stream()
mpi-genesis~ 61 {
mpi-genesis~ 62 if( !logfp )
mpi-genesis~ 63 logfp = stderr;
mpi-genesis~ 64 return logfp;
mpi-genesis~ 65 }
mpi-genesis~ 66
mpi-genesis~ 67
mpi-genesis~ 68 void
mpi-genesis~ 69 log_set_name( const char *name )
mpi-genesis~ 70 {
mpi-genesis~ 71 xfree(pgm_name);
mpi-genesis~ 72 if( name )
mpi-genesis~ 73 pgm_name = xstrdup(name);
mpi-genesis~ 74 else
mpi-genesis~ 75 pgm_name = NULL;
mpi-genesis~ 76 }
mpi-genesis~ 77
mpi-genesis~ 78 const char *
mpi-genesis~ 79 log_get_name(void)
mpi-genesis~ 80 {
mpi-genesis~ 81 return pgm_name? pgm_name : "";
mpi-genesis~ 82 }
mpi-genesis~ 83
mpi-genesis~ 84
mpi-genesis~ 85 void
mpi-genesis~ 86 log_set_pid( int pid )
mpi-genesis~ 87 {
mpi-genesis~ 88 if( pid )
mpi-genesis~ 89 sprintf(pidstring,"[%u]", (unsigned)pid );
mpi-genesis~ 90 else
mpi-genesis~ 91 *pidstring = 0;
mpi-genesis~ 92 }
mpi-genesis~ 93
mpi-genesis~ 94 int
mpi-genesis~ 95 log_get_errorcount( int clear)
mpi-genesis~ 96 {
mpi-genesis~ 97 int n = errorcount;
mpi-genesis~ 98 if( clear )
mpi-genesis~ 99 errorcount = 0;
mpi-genesis~ 100 return n;
mpi-genesis~ 101 }
mpi-genesis~ 102
mpi-genesis~ 103 void
mpi-genesis~ 104 log_inc_errorcount()
mpi-genesis~ 105 {
mpi-genesis~ 106 errorcount++;
mpi-genesis~ 107 }
mpi-genesis~ 108
mpi-genesis~ 109 int
mpi-genesis~ 110 log_set_strict(int val)
mpi-genesis~ 111 {
mpi-genesis~ 112 int old=strict;
mpi-genesis~ 113 strict=val;
mpi-genesis~ 114 return old;
mpi-genesis~ 115 }
mpi-genesis~ 116
mpi-genesis~ 117 void
mpi-genesis~ 118 g10_log_print_prefix(const char *text)
mpi-genesis~ 119 {
mpi-genesis~ 120 if( !logfp )
mpi-genesis~ 121 logfp = stderr;
mpi-genesis~ 122 if( pgm_name )
mpi-genesis~ 123 fprintf(logfp, "%s%s: %s", pgm_name, pidstring, text );
mpi-genesis~ 124 else
mpi-genesis~ 125 fprintf(logfp, "?%s: %s", pidstring, text );
mpi-genesis~ 126 #ifdef __riscos__
mpi-genesis~ 127 fflush( logfp );
mpi-genesis~ 128 #endif /* __riscos__ */
mpi-genesis~ 129 }
mpi-genesis~ 130
mpi-genesis~ 131
mpi-genesis~ 132 void
mpi-genesis~ 133 g10_log_info( const char *fmt, ... )
mpi-genesis~ 134 {
mpi-genesis~ 135 va_list arg_ptr ;
mpi-genesis~ 136
mpi-genesis~ 137 g10_log_print_prefix("");
mpi-genesis~ 138 va_start( arg_ptr, fmt ) ;
mpi-genesis~ 139 vfprintf(logfp,fmt,arg_ptr) ;
mpi-genesis~ 140 va_end(arg_ptr);
mpi-genesis~ 141 #ifdef __riscos__
mpi-genesis~ 142 fflush( logfp );
mpi-genesis~ 143 #endif /* __riscos__ */
mpi-genesis~ 144 }
mpi-genesis~ 145
mpi-genesis~ 146
mpi-genesis~ 147 void
mpi-genesis~ 148 g10_log_warning( const char *fmt, ... )
mpi-genesis~ 149 {
mpi-genesis~ 150 va_list arg_ptr ;
mpi-genesis~ 151
mpi-genesis~ 152 if(strict)
mpi-genesis~ 153 {
mpi-genesis~ 154 errorcount++;
mpi-genesis~ 155 g10_log_print_prefix(_("ERROR: "));
mpi-genesis~ 156 }
mpi-genesis~ 157 else
mpi-genesis~ 158 g10_log_print_prefix(_("WARNING: "));
mpi-genesis~ 159
mpi-genesis~ 160 va_start( arg_ptr, fmt ) ;
mpi-genesis~ 161 vfprintf(logfp,fmt,arg_ptr) ;
mpi-genesis~ 162 va_end(arg_ptr);
mpi-genesis~ 163 #ifdef __riscos__
mpi-genesis~ 164 fflush( logfp );
mpi-genesis~ 165 #endif /* __riscos__ */
mpi-genesis~ 166 }
mpi-genesis~ 167
mpi-genesis~ 168
mpi-genesis~ 169 void
mpi-genesis~ 170 g10_log_error( const char *fmt, ... )
mpi-genesis~ 171 {
mpi-genesis~ 172 va_list arg_ptr ;
mpi-genesis~ 173
mpi-genesis~ 174 g10_log_print_prefix("");
mpi-genesis~ 175 va_start( arg_ptr, fmt ) ;
mpi-genesis~ 176 vfprintf(logfp,fmt,arg_ptr) ;
mpi-genesis~ 177 va_end(arg_ptr);
mpi-genesis~ 178 errorcount++;
mpi-genesis~ 179 #ifdef __riscos__
mpi-genesis~ 180 fflush( logfp );
mpi-genesis~ 181 #endif /* __riscos__ */
mpi-genesis~ 182 }
mpi-genesis~ 183
mpi-genesis~ 184
mpi-genesis~ 185 void
mpi-genesis~ 186 g10_log_fatal( const char *fmt, ... )
mpi-genesis~ 187 {
mpi-genesis~ 188 va_list arg_ptr ;
mpi-genesis~ 189
mpi-genesis~ 190 g10_log_print_prefix("fatal: ");
mpi-genesis~ 191 va_start( arg_ptr, fmt ) ;
mpi-genesis~ 192 vfprintf(logfp,fmt,arg_ptr) ;
mpi-genesis~ 193 va_end(arg_ptr);
mpi-genesis~ 194 secmem_dump_stats();
mpi-genesis~ 195 #ifdef __riscos__
mpi-genesis~ 196 fflush( logfp );
mpi-genesis~ 197 #endif /* __riscos__ */
mpi-genesis~ 198 exit(2);
mpi-genesis~ 199 }
mpi-genesis~ 200
mpi-genesis~ 201 void
mpi-genesis~ 202 g10_log_bug( const char *fmt, ... )
mpi-genesis~ 203 {
mpi-genesis~ 204 va_list arg_ptr ;
mpi-genesis~ 205
mpi-genesis~ 206 putc('\n', stderr );
mpi-genesis~ 207 g10_log_print_prefix("Ohhhh jeeee: ");
mpi-genesis~ 208 va_start( arg_ptr, fmt ) ;
mpi-genesis~ 209 vfprintf(stderr,fmt,arg_ptr) ;
mpi-genesis~ 210 va_end(arg_ptr);
mpi-genesis~ 211 fflush(stderr);
mpi-genesis~ 212 secmem_dump_stats();
mpi-genesis~ 213 abort();
mpi-genesis~ 214 }
mpi-genesis~ 215
mpi-genesis~ 216 #if defined (__riscos__) \
mpi-genesis~ 217 || ( __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ))
mpi-genesis~ 218 void
mpi-genesis~ 219 g10_log_bug0( const char *file, int line, const char *func )
mpi-genesis~ 220 {
mpi-genesis~ 221 log_bug(_("... this is a bug (%s:%d:%s)\n"), file, line, func );
mpi-genesis~ 222 }
mpi-genesis~ 223 #else
mpi-genesis~ 224 void
mpi-genesis~ 225 g10_log_bug0( const char *file, int line )
mpi-genesis~ 226 {
mpi-genesis~ 227 log_bug(_("you found a bug ... (%s:%d)\n"), file, line);
mpi-genesis~ 228 }
mpi-genesis~ 229 #endif
mpi-genesis~ 230
mpi-genesis~ 231 void
mpi-genesis~ 232 g10_log_debug( const char *fmt, ... )
mpi-genesis~ 233 {
mpi-genesis~ 234 va_list arg_ptr ;
mpi-genesis~ 235
mpi-genesis~ 236 g10_log_print_prefix("DBG: ");
mpi-genesis~ 237 va_start( arg_ptr, fmt ) ;
mpi-genesis~ 238 vfprintf(logfp,fmt,arg_ptr) ;
mpi-genesis~ 239 va_end(arg_ptr);
mpi-genesis~ 240 #ifdef __riscos__
mpi-genesis~ 241 fflush( logfp );
mpi-genesis~ 242 #endif /* __riscos__ */
mpi-genesis~ 243 }
mpi-genesis~ 244
mpi-genesis~ 245
mpi-genesis~ 246
mpi-genesis~ 247 void
mpi-genesis~ 248 g10_log_hexdump( const char *text, const char *buf, size_t len )
mpi-genesis~ 249 {
mpi-genesis~ 250 int i;
mpi-genesis~ 251
mpi-genesis~ 252 g10_log_print_prefix(text);
mpi-genesis~ 253 for(i=0; i < len; i++ )
mpi-genesis~ 254 fprintf(logfp, " %02X", ((const byte*)buf)[i] );
mpi-genesis~ 255 fputc('\n', logfp);
mpi-genesis~ 256 #ifdef __riscos__
mpi-genesis~ 257 fflush( logfp );
mpi-genesis~ 258 #endif /* __riscos__ */
mpi-genesis~ 259 }
mpi-genesis~ 260
mpi-genesis~ 261
mpi-genesis~ 262