1 /* assuan-logging.c - Default logging function.
2 * Copyright (C) 2002, 2003, 2004, 2007 Free Software Foundation, Inc.
4 * This file is part of Assuan.
6 * Assuan is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Assuan is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #ifdef HAVE_W32_SYSTEM
29 #endif /*HAVE_W32_SYSTEM*/
33 #include "assuan-defs.h"
35 static char prefix_buffer
[80];
36 static FILE *_assuan_log
;
37 static int full_logging
;
40 _assuan_set_default_log_stream (FILE *fp
)
45 full_logging
= !!getenv ("ASSUAN_FULL_LOGGING");
50 assuan_set_assuan_log_stream (FILE *fp
)
56 /* Set the per context log stream. Also enable the default log stream
57 if it has not been set. */
59 assuan_set_log_stream (assuan_context_t ctx
, FILE *fp
)
66 _assuan_set_default_log_stream (fp
);
72 assuan_get_assuan_log_stream (void)
74 return _assuan_log
? _assuan_log
: stderr
;
78 /* Set the prefix to be used for logging to TEXT or
79 resets it to the default if TEXT is NULL. */
81 assuan_set_assuan_log_prefix (const char *text
)
85 strncpy (prefix_buffer
, text
, sizeof (prefix_buffer
)-1);
86 prefix_buffer
[sizeof (prefix_buffer
)-1] = 0;
93 assuan_get_assuan_log_prefix (void)
100 _assuan_log_printf (const char *format
, ...)
105 int save_errno
= errno
;
107 fp
= assuan_get_assuan_log_stream ();
108 prf
= assuan_get_assuan_log_prefix ();
110 fprintf (fp
, "%s[%u]: ", prf
, (unsigned int)getpid ());
112 va_start (arg_ptr
, format
);
113 vfprintf (fp
, format
, arg_ptr
);
115 /* If the log stream is a file, the output would be buffered. This
116 is bad for debugging, thus we flush the stream if FORMAT ends
118 if (format
&& *format
&& format
[strlen(format
)-1] == '\n')
124 /* Dump a possibly binary string (used for debugging). Distinguish
125 ascii text from binary and print it accordingly. This function
126 takes FILE pointer arg because logging may be enabled on a per
129 _assuan_log_print_buffer (FILE *fp
, const void *buffer
, size_t length
)
131 const unsigned char *s
;
134 for (n
= length
, s
= buffer
; n
; n
--, s
++)
135 if ((! isascii (*s
) || iscntrl (*s
) || ! isprint (*s
)) && !(*s
>= 0x80))
139 if (! n
&& *s
!= '[')
140 fwrite (buffer
, length
, 1, fp
);
143 #ifdef HAVE_FLOCKFILE
146 putc_unlocked ('[', fp
);
147 if (length
> 16 && ! full_logging
)
149 for (n
= 0; n
< 12; n
++, s
++)
150 fprintf (fp
, " %02x", *s
);
151 fprintf (fp
, " ...(%d bytes skipped)", (int) length
- 12);
155 for (n
= 0; n
< length
; n
++, s
++)
156 fprintf (fp
, " %02x", *s
);
158 putc_unlocked (' ', fp
);
159 putc_unlocked (']', fp
);
160 #ifdef HAVE_FUNLOCKFILE
166 /* Log a user supplied string. Escapes non-printable before
169 _assuan_log_sanitized_string (const char *string
)
171 const unsigned char *s
= (const unsigned char *) string
;
172 FILE *fp
= assuan_get_assuan_log_stream ();
177 #ifdef HAVE_FLOCKFILE
208 if ((isascii (*s
) && isprint (*s
)) || (*s
>= 0x80))
209 putc_unlocked (*s
, fp
);
212 putc_unlocked ('\\', fp
);
213 fprintf (fp
, "x%02x", *s
);
219 putc_unlocked ('\\', fp
);
220 putc_unlocked (c
, fp
);
224 #ifdef HAVE_FUNLOCKFILE
231 #ifdef HAVE_W32_SYSTEM
233 _assuan_w32_strerror (int ec
)
235 static char strerr
[256];
238 ec
= (int)GetLastError ();
239 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, ec
,
240 MAKELANGID (LANG_NEUTRAL
, SUBLANG_DEFAULT
),
241 strerr
, sizeof (strerr
)-1, NULL
);
245 static int (*my_strerror_r
) (unsigned int err
, char *buf
, size_t buflen
);
246 static const char * (*my_strsource
) (unsigned int err
);
249 load_libgpg_error (void)
251 /* This code is not race free but suitable for our purpose. */
252 static volatile int initialized
;
256 return (my_strerror_r
&& my_strsource
)? 0:-1;
257 handle
= LoadLibrary ("libgpg-error-0.dll");
261 foo
= GetProcAddress (handle
, "gpg_strerror_r");
262 bar
= GetProcAddress (handle
, "gpg_strsource");
269 CloseHandle (handle
);
276 _assuan_gpg_strerror_r (unsigned int err
, char *buf
, size_t buflen
)
278 if (load_libgpg_error ())
280 return my_strerror_r (err
, buf
, buflen
);
285 _assuan_gpg_strsource (unsigned int err
)
287 if (load_libgpg_error ())
289 return my_strsource (err
);
291 #endif /*HAVE_W32_SYSTEM*/