~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to util/logger.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19
 
 */
20
 
 
21
 
#include <config.h>
22
 
#include <stdio.h>
23
 
#include <stdlib.h>
24
 
#include <stdarg.h>
25
 
#include <string.h>
26
 
#include <errno.h>
27
 
 
28
 
#include "util.h"
29
 
#include "i18n.h"
30
 
 
31
 
static char pidstring[15];
32
 
static char *pgm_name;
33
 
static int errorcount;
34
 
static int strict;
35
 
static FILE *logfp;
36
 
 
37
 
/****************
38
 
 * Set the logfile to use (not yet implemneted) or, if logfile is NULL,
39
 
 * the Fd where logoutputs should go.
40
 
 */
41
 
void
42
 
log_set_logfile( const char *name, int fd )
43
 
{
44
 
    if( name )
45
 
        BUG();
46
 
 
47
 
    if( logfp && logfp != stderr && logfp != stdout )
48
 
        fclose( logfp );
49
 
    if( fd == 1 )
50
 
        logfp = stdout;
51
 
    else if( fd == 2 )
52
 
        logfp = stderr;
53
 
    else
54
 
        logfp = fdopen( fd, "a" );
55
 
    if( !logfp ) {
56
 
        logfp = stderr;
57
 
        log_fatal("can't open fd %d for logging: %s\n", fd, strerror(errno));
58
 
    }
59
 
}
60
 
 
61
 
FILE *
62
 
log_stream()
63
 
{
64
 
    if( !logfp )
65
 
        logfp = stderr;
66
 
    return logfp;
67
 
}
68
 
 
69
 
 
70
 
void
71
 
log_set_name( const char *name )
72
 
{
73
 
    m_free(pgm_name);
74
 
    if( name )
75
 
        pgm_name = m_strdup(name);
76
 
    else
77
 
        pgm_name = NULL;
78
 
}
79
 
 
80
 
const char *
81
 
log_get_name(void)
82
 
{
83
 
    return pgm_name? pgm_name : "";
84
 
}
85
 
 
86
 
 
87
 
void
88
 
log_set_pid( int pid )
89
 
{
90
 
    if( pid )
91
 
        sprintf(pidstring,"[%u]", (unsigned)pid );
92
 
    else
93
 
        *pidstring = 0;
94
 
}
95
 
 
96
 
int
97
 
log_get_errorcount( int clear)
98
 
{
99
 
    int n = errorcount;
100
 
    if( clear )
101
 
        errorcount = 0;
102
 
    return n;
103
 
}
104
 
 
105
 
void
106
 
log_inc_errorcount()
107
 
{
108
 
    errorcount++;
109
 
}
110
 
 
111
 
int
112
 
log_set_strict(int val)
113
 
{
114
 
  int old=strict;
115
 
  strict=val;
116
 
  return old;
117
 
}
118
 
 
119
 
void
120
 
g10_log_print_prefix(const char *text)
121
 
{
122
 
    if( !logfp )
123
 
        logfp = stderr;
124
 
    if( pgm_name )
125
 
        fprintf(logfp, "%s%s: %s", pgm_name, pidstring, text );
126
 
    else
127
 
        fprintf(logfp, "?%s: %s", pidstring, text );
128
 
#ifdef __riscos__
129
 
    fflush( logfp );
130
 
#endif /* __riscos__ */
131
 
}
132
 
 
133
 
 
134
 
void
135
 
g10_log_info( const char *fmt, ... )
136
 
{
137
 
    va_list arg_ptr ;
138
 
 
139
 
    g10_log_print_prefix("");
140
 
    va_start( arg_ptr, fmt ) ;
141
 
    vfprintf(logfp,fmt,arg_ptr) ;
142
 
    va_end(arg_ptr);
143
 
#ifdef __riscos__
144
 
    fflush( logfp );
145
 
#endif /* __riscos__ */
146
 
}
147
 
 
148
 
 
149
 
void
150
 
g10_log_warning( const char *fmt, ... )
151
 
{
152
 
    va_list arg_ptr ;
153
 
 
154
 
    if(strict)
155
 
      {
156
 
        errorcount++;
157
 
        g10_log_print_prefix(_("ERROR: "));
158
 
      }
159
 
    else
160
 
      g10_log_print_prefix(_("WARNING: "));
161
 
 
162
 
    va_start( arg_ptr, fmt ) ;
163
 
    vfprintf(logfp,fmt,arg_ptr) ;
164
 
    va_end(arg_ptr);
165
 
#ifdef __riscos__
166
 
    fflush( logfp );
167
 
#endif /* __riscos__ */
168
 
}
169
 
 
170
 
 
171
 
void
172
 
g10_log_error( const char *fmt, ... )
173
 
{
174
 
    va_list arg_ptr ;
175
 
 
176
 
    g10_log_print_prefix("");
177
 
    va_start( arg_ptr, fmt ) ;
178
 
    vfprintf(logfp,fmt,arg_ptr) ;
179
 
    va_end(arg_ptr);
180
 
    errorcount++;
181
 
#ifdef __riscos__
182
 
    fflush( logfp );
183
 
#endif /* __riscos__ */
184
 
}
185
 
 
186
 
 
187
 
void
188
 
g10_log_fatal( const char *fmt, ... )
189
 
{
190
 
    va_list arg_ptr ;
191
 
 
192
 
    g10_log_print_prefix("fatal: ");
193
 
    va_start( arg_ptr, fmt ) ;
194
 
    vfprintf(logfp,fmt,arg_ptr) ;
195
 
    va_end(arg_ptr);
196
 
    secmem_dump_stats();
197
 
#ifdef __riscos__
198
 
    fflush( logfp );
199
 
#endif /* __riscos__ */
200
 
    exit(2);
201
 
}
202
 
 
203
 
void
204
 
g10_log_bug( const char *fmt, ... )
205
 
{
206
 
    va_list arg_ptr ;
207
 
 
208
 
    putc('\n', stderr );
209
 
    g10_log_print_prefix("Ohhhh jeeee: ");
210
 
    va_start( arg_ptr, fmt ) ;
211
 
    vfprintf(stderr,fmt,arg_ptr) ;
212
 
    va_end(arg_ptr);
213
 
    fflush(stderr);
214
 
    secmem_dump_stats();
215
 
    abort();
216
 
}
217
 
 
218
 
#if defined (__riscos__) \
219
 
    || ( __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ))
220
 
void
221
 
g10_log_bug0( const char *file, int line, const char *func )
222
 
{
223
 
    log_bug(_("... this is a bug (%s:%d:%s)\n"), file, line, func );
224
 
}
225
 
#else
226
 
void
227
 
g10_log_bug0( const char *file, int line )
228
 
{
229
 
    log_bug(_("you found a bug ... (%s:%d)\n"), file, line);
230
 
}
231
 
#endif
232
 
 
233
 
void
234
 
g10_log_debug( const char *fmt, ... )
235
 
{
236
 
    va_list arg_ptr ;
237
 
 
238
 
    g10_log_print_prefix("DBG: ");
239
 
    va_start( arg_ptr, fmt ) ;
240
 
    vfprintf(logfp,fmt,arg_ptr) ;
241
 
    va_end(arg_ptr);
242
 
#ifdef __riscos__
243
 
    fflush( logfp );
244
 
#endif /* __riscos__ */
245
 
}
246
 
 
247
 
 
248
 
 
249
 
void
250
 
g10_log_hexdump( const char *text, const char *buf, size_t len )
251
 
{
252
 
    int i;
253
 
 
254
 
    g10_log_print_prefix(text);
255
 
    for(i=0; i < len; i++ )
256
 
        fprintf(logfp, " %02X", ((const byte*)buf)[i] );
257
 
    fputc('\n', logfp);
258
 
#ifdef __riscos__
259
 
    fflush( logfp );
260
 
#endif /* __riscos__ */
261
 
}
262
 
 
263
 
 
264