Tizen 2.1 base
[framework/security/gnupg.git] / util / logger.c
1 /* logger.c  -  log functions
2  *      Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include "util.h"
30 #include "i18n.h"
31
32 static char pidstring[15];
33 static char *pgm_name;
34 static int errorcount;
35 static int strict;
36 static FILE *logfp;
37
38 /****************
39  * Set the logfile to use (not yet implemneted) or, if logfile is NULL,
40  * the Fd where logoutputs should go.
41  */
42 void
43 log_set_logfile( const char *name, int fd )
44 {
45     if( name )
46         BUG();
47
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));
59     }
60 }
61
62 FILE *
63 log_stream()
64 {
65     if( !logfp )
66         logfp = stderr;
67     return logfp;
68 }
69
70
71 void
72 log_set_name( const char *name )
73 {
74     xfree(pgm_name);
75     if( name )
76         pgm_name = xstrdup(name);
77     else
78         pgm_name = NULL;
79 }
80
81 const char *
82 log_get_name(void)
83 {
84     return pgm_name? pgm_name : "";
85 }
86
87
88 void
89 log_set_pid( int pid )
90 {
91     if( pid )
92         sprintf(pidstring,"[%u]", (unsigned)pid );
93     else
94         *pidstring = 0;
95 }
96
97 int
98 log_get_errorcount( int clear)
99 {
100     int n = errorcount;
101     if( clear )
102         errorcount = 0;
103     return n;
104 }
105
106 void
107 log_inc_errorcount()
108 {
109     errorcount++;
110 }
111
112 int
113 log_set_strict(int val)
114 {
115   int old=strict;
116   strict=val;
117   return old;
118 }
119
120 void
121 g10_log_print_prefix(const char *text)
122 {
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__ */
132 }
133
134
135 void
136 g10_log_info( const char *fmt, ... )
137 {
138     va_list arg_ptr ;
139
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__ */
147 }
148
149
150 void
151 g10_log_warning( const char *fmt, ... )
152 {
153     va_list arg_ptr ;
154
155     if(strict)
156       {
157         errorcount++;
158         g10_log_print_prefix(_("ERROR: "));
159       }
160     else
161       g10_log_print_prefix(_("WARNING: "));
162
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__ */
169 }
170
171
172 void
173 g10_log_error( const char *fmt, ... )
174 {
175     va_list arg_ptr ;
176
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__ */
185 }
186
187
188 void
189 g10_log_fatal( const char *fmt, ... )
190 {
191     va_list arg_ptr ;
192
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);
202 }
203
204 void
205 g10_log_bug( const char *fmt, ... )
206 {
207     va_list arg_ptr ;
208
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();
217 }
218
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 )
223 {
224     log_bug(_("... this is a bug (%s:%d:%s)\n"), file, line, func );
225 }
226 #else
227 void
228 g10_log_bug0( const char *file, int line )
229 {
230     log_bug(_("you found a bug ... (%s:%d)\n"), file, line);
231 }
232 #endif
233
234 void
235 g10_log_debug( const char *fmt, ... )
236 {
237     va_list arg_ptr ;
238
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__ */
246 }
247
248
249
250 void
251 g10_log_hexdump( const char *text, const char *buf, size_t len )
252 {
253     int i;
254
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__ */
262 }
263
264
265