diff options
author | Werner Koch <[email protected]> | 2017-11-17 08:37:54 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2017-11-17 08:48:30 +0000 |
commit | 1b653e7f37e2eb2a8f590a43651eb8aecbe8756e (patch) | |
tree | 07981a1260494dc7a94e3e266743fb58070416a2 | |
parent | New configure options and macros for internal use. (diff) | |
download | libgpg-error-1b653e7f37e2eb2a8f590a43651eb8aecbe8756e.tar.gz libgpg-error-1b653e7f37e2eb2a8f590a43651eb8aecbe8756e.zip |
core: Add logging API.
* src/visibility.c (gpgrt_get_errorcount): New API.
(gpgrt_inc_errorcount): New API.
(gpgrt_log_set_sink): New API.
(gpgrt_log_set_socket_dir_cb): New API.
(gpgrt_log_set_pid_suffix_cb): New API.
(gpgrt_log_set_prefix): New API.
(gpgrt_log_get_prefix): New API.
(gpgrt_log_test_fd): New API.
(gpgrt_log_get_fd): New API.
(gpgrt_log_get_stream): New API.
(gpgrt_log): New API.
(gpgrt_logv): New API.
(gpgrt_logv_prefix): New API.
(gpgrt_log_string): New API.
(gpgrt_log_info): New API.
(gpgrt_log_error): New API.
(gpgrt_log_fatal): New API.
(gpgrt_log_bug): New API.
(gpgrt_log_debug): New API.
(gpgrt_log_debug_string): New API.
(gpgrt_log_printf): New API.
(gpgrt_log_flush): New API.
(gpgrt_log_printhex): New API.
(gpgrt_log_clock): New API.
(_gpgrt_log_assert): New function for the ABI.
* src/visibility.h: Add corresponding macros.
* src/gpg-error.def.in: Add new functions.
* src/gpg-error.vers: Add new functions.
* src/gpg-error.h.in (GPGRT_LOG_WITH_PREFIX): New constant.
(GPGRT_LOG_WITH_TIME): New constant.
(GPGRT_LOG_WITH_PID): New constant.
(GPGRT_LOG_RUN_DETACHED): New constant.
(GPGRT_LOG_NO_REGISTRY): New constant.
(GPGRT_LOG_BEGIN): New enum value.
(GPGRT_LOG_CONT): New enum value.
(GPGRT_LOG_INFO): New enum value.
(GPGRT_LOG_WARN): New enum value.
(GPGRT_LOG_ERROR): New enum value.
(GPGRT_LOG_FATAL): New enum value.
(GPGRT_LOG_BUG): New enum value.
(GPGRT_LOG_DEBUG): New enum value.
(log_get_errorcount, log_inc_errorcount, log_set_file, log_set_fd)
(log_set_stream, log_set_socket_dir_cb, log_set_pid_suffix_cb)
(log_set_prefix, log_get_prefix, log_test_fd, log_get_fd)
(log_get_stream, log_log, log_logv, log_logv_prefix, log_string)
(log_bug, log_fatal, log_error, log_info, log_debug, log_debug_string)
(log_printf, log_printhex, log_clock)
(log_flush) [GPGRT_ENABLE_LOG_MACROS]: New wrapper macros.
(gpgrt_assert): New macro.
* src/gpgrt-int.h: Add prototypes for logging.c.
(gpgrt_assert): Redefine for internal use.
* src/logging.h: Remove.
* src/logging.c: Include gpgrt-int.h instead of the stuff from GnuPG.
Use _gpgrt malloc funcsions and the internal versions of the estream
functions. Rename all public fucntions with a _gpgrt_ prefix.
(print_prefix): Return the printed length.
(_gpgrt_logv_internal): Ditto.
(_gpgrt_log_printhex): Change order of args and turn into a printf
like function.
(_gpgrt_logv_printhex): New. Take the core of the former
log_printhex.
(_gpgrt_log_clock): Turn into a printf like functions.
(_gpgrt_logv_clock): New. Take the core of the former log_clock.
--
This logging API is a slightly modified version of what we are using
in GnuPG.
Signed-off-by: Werner Koch <[email protected]>
-rw-r--r-- | src/gpg-error.def.in | 28 | ||||
-rw-r--r-- | src/gpg-error.h.in | 122 | ||||
-rw-r--r-- | src/gpg-error.vers | 28 | ||||
-rw-r--r-- | src/gpgrt-int.h | 67 | ||||
-rw-r--r-- | src/logging.c | 492 | ||||
-rw-r--r-- | src/logging.h | 107 | ||||
-rw-r--r-- | src/visibility.c | 204 | ||||
-rw-r--r-- | src/visibility.h | 53 |
8 files changed, 803 insertions, 298 deletions
diff --git a/src/gpg-error.def.in b/src/gpg-error.def.in index ad65f8c..b718c7a 100644 --- a/src/gpg-error.def.in +++ b/src/gpg-error.def.in @@ -15,6 +15,7 @@ * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see <https://www.gnu.org/licenses/>. + * SPDX-License-Identifier: LGPL-2.1+ * * Note: This file should be updated manually and the ordinals shall * never be changed. Also check gpg-error.vers and visibility.h. @@ -156,4 +157,31 @@ EXPORTS gpgrt_b64dec_proc @114 gpgrt_b64dec_finish @115 + gpgrt_get_errorcount @116 + gpgrt_inc_errorcount @117 + gpgrt_log_set_sink @118 + gpgrt_log_set_socket_dir_cb @119 + gpgrt_log_set_pid_suffix_cb @120 + gpgrt_log_set_prefix @121 + gpgrt_log_get_prefix @122 + gpgrt_log_test_fd @123 + gpgrt_log_get_fd @124 + gpgrt_log_get_stream @125 + gpgrt_log @126 + gpgrt_logv @127 + gpgrt_logv_prefix @128 + gpgrt_log_string @129 + gpgrt_log_bug @130 + gpgrt_log_fatal @131 + gpgrt_log_error @132 + gpgrt_log_info @133 + gpgrt_log_debug @134 + gpgrt_log_debug_string @135 + gpgrt_log_printf @136 + gpgrt_log_printhex @137 + gpgrt_log_clock @138 + gpgrt_log_flush @139 + _gpgrt_log_assert @140 + + ;; end of file with public symbols for Windows. diff --git a/src/gpg-error.h.in b/src/gpg-error.h.in index 988898e..2d515d5 100644 --- a/src/gpg-error.h.in +++ b/src/gpg-error.h.in @@ -15,6 +15,7 @@ * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see <https://www.gnu.org/licenses/>. + * SPDX-License-Identifier: LGPL-2.1+ * * @configure_input@ */ @@ -432,7 +433,9 @@ gpg_error_from_syserror (void) -/* Lock functions. */ +/* + * Lock functions. + */ @include:lock-obj@ @@ -449,14 +452,18 @@ gpg_err_code_t gpgrt_lock_destroy (gpgrt_lock_t *lockhd); -/* Thread functions. */ +/* + * Thread functions. + */ gpg_err_code_t gpgrt_yield (void); -/* Estream */ +/* + * Estream + */ /* The definition of this struct is entirely private. You must not use it for anything. It is only here so some functions can be @@ -855,8 +862,12 @@ int gpgrt_vsnprintf (char *buf,size_t bufsize, # define es_bsprintf gpgrt_bsprintf # define es_vbsprintf gpgrt_vbsprintf #endif /*GPGRT_ENABLE_ES_MACROS*/ + + -/* Base64 decode functions. */ +/* + * Base64 decode functions. + */ struct _gpgrt_b64state; typedef struct _gpgrt_b64state *gpgrt_b64state_t; @@ -866,6 +877,109 @@ gpg_error_t gpgrt_b64dec_proc (gpgrt_b64state_t state, void *buffer, size_t length, size_t *r_nbytes); gpg_error_t gpgrt_b64dec_finish (gpgrt_b64state_t state); + + +/* + * Logging functions + */ + +/* Flag values for gpgrt_log_set_prefix. */ +#define GPGRT_LOG_WITH_PREFIX 1 +#define GPGRT_LOG_WITH_TIME 2 +#define GPGRT_LOG_WITH_PID 4 +#define GPGRT_LOG_RUN_DETACHED 256 +#define GPGRT_LOG_NO_REGISTRY 512 + +/* Log levels as used by gpgrt_log. */ +enum gpgrt_log_levels + { + GPGRT_LOG_BEGIN, + GPGRT_LOG_CONT, + GPGRT_LOG_INFO, + GPGRT_LOG_WARN, + GPGRT_LOG_ERROR, + GPGRT_LOG_FATAL, + GPGRT_LOG_BUG, + GPGRT_LOG_DEBUG + }; + + +/* The next 4 fucntions are not thread-safe - call them early. */ +void gpgrt_log_set_sink (const char *name, estream_t stream, int fd); +void gpgrt_log_set_socket_dir_cb (const char *(*fnc)(void)); +void gpgrt_log_set_pid_suffix_cb (int (*cb)(unsigned long *r_value)); +void gpgrt_log_set_prefix (const char *text, unsigned int flags); + +int gpgrt_get_errorcount (int clear); +void gpgrt_inc_errorcount (void); +const char *gpgrt_log_get_prefix (unsigned int *flags); +int gpgrt_log_test_fd (int fd); +int gpgrt_log_get_fd (void); +estream_t gpgrt_log_get_stream (void); + +void gpgrt_log (int level, const char *fmt, ...) GPGRT_ATTR_PRINTF(2,3); +void gpgrt_logv (int level, const char *fmt, va_list arg_ptr); +void gpgrt_logv_prefix (int level, const char *prefix, + const char *fmt, va_list arg_ptr); +void gpgrt_log_string (int level, const char *string); +void gpgrt_log_bug (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2); +void gpgrt_log_fatal (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2); +void gpgrt_log_error (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); +void gpgrt_log_info (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); +void gpgrt_log_debug (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); +void gpgrt_log_debug_string (const char *string, + const char *fmt, ...) GPGRT_ATTR_PRINTF(2,3); +void gpgrt_log_printf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); +void gpgrt_log_printhex (const void *buffer, size_t length, + const char *fmt, ...) GPGRT_ATTR_PRINTF(3,4); +void gpgrt_log_clock (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); +void gpgrt_log_flush (void); +void _gpgrt_log_assert (const char *expr, const char *file, int line, + const char *func) GPGRT_ATTR_NORETURN; + +#ifdef GPGRT_HAVE_MACRO_FUNCTION +# define gpgrt_assert(expr) \ + ((expr) \ + ? (void) 0 \ + : _gpgrt_log_assert (#expr, __FILE__, __LINE__, __FUNCTION__)) +#else /*!GPGRT_HAVE_MACRO_FUNCTION*/ +void _gpgrt_log_assert (const char *expr, const char *file, int line); +#define gpgrt_assert(expr) \ + ((expr) \ + ? (void) 0 \ + : _gpgrt_log_assert (#expr, __FILE__, __LINE__, NULL)) +#endif /*!GPGRT_HAVE_MACRO_FUNCTION*/ + +#ifdef GPGRT_ENABLE_LOG_MACROS +# define log_get_errorcount gpgrt_get_errorcount +# define log_inc_errorcount gpgrt_inc_errorcount +# define log_set_file(a) gpgrt_log_set_sink ((a), NULL, -1) +# define log_set_fd(a) gpgrt_log_set_sink (NULL, NULL, (a)) +# define log_set_stream(a) gpgrt_log_set_sink (NULL, (a), -1) +# define log_set_socket_dir_cb gpgrt_log_set_socket_dir_cb +# define log_set_pid_suffix_cb gpgrt_log_set_pid_suffix_cb +# define log_set_prefix gpgrt_log_set_prefix +# define log_get_prefix gpgrt_log_get_prefix +# define log_test_fd gpgrt_log_test_fd +# define log_get_fd gpgrt_log_get_fd +# define log_get_stream gpgrt_log_get_stream +# define log_log gpgrt_log +# define log_logv gpgrt_logv +# define log_logv_prefix gpgrt_logv_prefix +# define log_string gpgrt_log_string +# define log_bug gpgrt_log_bug +# define log_fatal gpgrt_log_fatal +# define log_error gpgrt_log_error +# define log_info gpgrt_log_info +# define log_debug gpgrt_log_debug +# define log_debug_string gpgrt_log_debug_string +# define log_printf gpgrt_log_printf +# define log_printhex gpgrt_log_printhex +# define log_clock gpgrt_log_clock +# define log_flush gpgrt_log_flush +#endif /*GPGRT_ENABLE_LOG_MACROS*/ + + #ifdef __cplusplus } #endif diff --git a/src/gpg-error.vers b/src/gpg-error.vers index e44128c..6cce976 100644 --- a/src/gpg-error.vers +++ b/src/gpg-error.vers @@ -15,6 +15,7 @@ # # You should have received a copy of the GNU Lesser General Public # License along with this program; if not, see <https://www.gnu.org/licenses/>. +# SPDX-License-Identifier: LGPL-2.1+ # # NOTE: When adding new functions, please make sure to add them to # visibility.h and gpg-error.def.in as well. @@ -130,6 +131,33 @@ GPG_ERROR_1.0 { gpgrt_b64dec_proc; gpgrt_b64dec_finish; + gpgrt_get_errorcount; + gpgrt_inc_errorcount; + gpgrt_log_set_sink; + gpgrt_log_set_socket_dir_cb; + gpgrt_log_set_pid_suffix_cb; + gpgrt_log_set_prefix; + gpgrt_log_get_prefix; + gpgrt_log_test_fd; + gpgrt_log_get_fd; + gpgrt_log_get_stream; + gpgrt_log; + gpgrt_logv; + gpgrt_logv_prefix; + gpgrt_log_string; + gpgrt_log_bug; + gpgrt_log_fatal; + gpgrt_log_error; + gpgrt_log_info; + gpgrt_log_debug; + gpgrt_log_debug_string; + gpgrt_log_printf; + gpgrt_log_printhex; + gpgrt_log_clock; + gpgrt_log_flush; + _gpgrt_log_assert; + + local: *; }; diff --git a/src/gpgrt-int.h b/src/gpgrt-int.h index 02d54c9..f6f8d0e 100644 --- a/src/gpgrt-int.h +++ b/src/gpgrt-int.h @@ -466,6 +466,73 @@ gpg_error_t _gpgrt_b64dec_finish (gpgrt_b64state_t state); /* + * Local prototypes for logging + */ +int _gpgrt_get_errorcount (int clear); +void _gpgrt_inc_errorcount (void); +void _gpgrt_log_set_sink (const char *name, estream_t stream, int fd); +void _gpgrt_log_set_socket_dir_cb (const char *(*fnc)(void)); +void _gpgrt_log_set_pid_suffix_cb (int (*cb)(unsigned long *r_value)); +void _gpgrt_log_set_prefix (const char *text, unsigned int flags); +const char *_gpgrt_log_get_prefix (unsigned int *flags); +int _gpgrt_log_test_fd (int fd); +int _gpgrt_log_get_fd (void); +estream_t _gpgrt_log_get_stream (void); + +void _gpgrt_log (int level, const char *fmt, ...) GPGRT_ATTR_PRINTF(2,3); +void _gpgrt_logv (int level, const char *fmt, va_list arg_ptr); +void _gpgrt_logv_prefix (int level, const char *prefix, + const char *fmt, va_list arg_ptr); + +void _gpgrt_log_string (int level, const char *string); + +void _gpgrt_log_bug (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2); +void _gpgrt_log_fatal (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2); +void _gpgrt_log_error (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); +void _gpgrt_log_info (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); +void _gpgrt_log_debug (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); +void _gpgrt_log_debug_string (const char *string, const char *fmt, + ...) GPGRT_ATTR_PRINTF(2,3); + +void _gpgrt_log_printf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); + +void _gpgrt_log_flush (void); + +void _gpgrt_logv_printhex (const void *buffer, size_t length, + const char *fmt, va_list arg_ptr); +void _gpgrt_log_printhex (const void *buffer, size_t length, + const char *fmt, ...) GPGRT_ATTR_PRINTF(3,4);; + +void _gpgrt_logv_clock (const char *fmt, va_list arg_ptr); +void _gpgrt_log_clock (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); + +void _gpgrt__log_assert (const char *expr, const char *file, int line, + const char *func) GPGRT_ATTR_NORETURN; + +/* Redefine the assert macro to use our internal function. */ +#undef gpgrt_assert +#ifdef GPGRT_HAVE_MACRO_FUNCTION +#define gpgrt_assert(expr) \ + ((expr) \ + ? (void) 0 \ + : _gpgrt__log_assert (#expr, __FILE__, __LINE__, __FUNCTION__)) +#else /*!GPGRT_HAVE_MACRO_FUNCTION*/ +/* # define BUG() bug_at( __FILE__ , __LINE__ ) */ +#define gpgrt_assert(expr) \ + ((expr) \ + ? (void) 0 \ + : _gpgrt__log_assert (#expr, __FILE__, __LINE__, NULL)) +#endif /*!GPGRT_HAVE_MACRO_FUNCTION*/ + +/* Note: The next function is only to be used by visibility.c. */ +int _gpgrt_logv_internal (int level, int ignore_arg_ptr, + const char *extrastring, + const char *prefmt, const char *fmt, + va_list arg_ptr); + + + +/* * Internal platform abstraction functions (sysutils.c) */ diff --git a/src/logging.c b/src/logging.c index c85d63c..2e675f5 100644 --- a/src/logging.c +++ b/src/logging.c @@ -17,12 +17,13 @@ * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see <https://www.gnu.org/licenses/>. + * SPDX-License-Identifier: LGPL-2.1+ * * This file was originally a part of GnuPG. */ - #include <config.h> + #include <stdlib.h> #include <stdio.h> #include <string.h> @@ -48,12 +49,9 @@ #include <assert.h> /* #include <execinfo.h> */ -#define GNUPG_COMMON_NEED_AFLOCAL 1 -#include "util.h" -#include "i18n.h" -#include "common-defs.h" -#include "logging.h" -#include "sysutils.h" +#define _GPGRT_NEED_AFLOCAL 1 +#include "gpgrt-int.h" + #ifdef HAVE_W32_SYSTEM # ifndef S_IRWXG @@ -109,23 +107,27 @@ static int missing_lf; static int errorcount; +/* Get the error count as maintained by the log fucntions. With CLEAR + * set reset the counter. */ int -log_get_errorcount (int clear) +_gpgrt_get_errorcount (int clear) { - int n = errorcount; - if( clear ) - errorcount = 0; - return n; + int n = errorcount; + if (clear) + errorcount = 0; + return n; } + +/* Increment the error count as maintainer by the log functions. */ void -log_inc_errorcount (void) +_gpgrt_inc_errorcount (void) { errorcount++; } -/* The following 3 functions are used by es_fopencookie to write logs +/* The following 3 functions are used by _gpgrt_fopencookie to write logs to a socket. */ struct fun_cookie_s { @@ -279,7 +281,7 @@ fun_writer (void *cookie_arg, const void *buffer, size_t size) void *addrbuf = NULL; #endif /*HAVE_INET_PTON*/ - addrstr = xtrymalloc (strlen (name) + 1); + addrstr = _gpgrt_malloc (strlen (name) + 1); if (!addrstr) addrlen = 0; /* This indicates an error. */ else if (*name == '[') @@ -289,7 +291,7 @@ fun_writer (void *cookie_arg, const void *buffer, size_t size) p = strchr (addrstr, ']'); if (!p || p[1] != ':' || !parse_portno (p+2, &port)) { - gpg_err_set_errno (EINVAL); + _gpg_err_set_errno (EINVAL); addrlen = 0; } else @@ -307,7 +309,7 @@ fun_writer (void *cookie_arg, const void *buffer, size_t size) srvr_addr = (struct sockaddr *)&srvr_addr_in6; addrlen = sizeof srvr_addr_in6; #else - gpg_err_set_errno (EAFNOSUPPORT); + _gpg_err_set_errno (EAFNOSUPPORT); addrlen = 0; #endif } @@ -319,7 +321,7 @@ fun_writer (void *cookie_arg, const void *buffer, size_t size) p = strchr (addrstr, ':'); if (!p || !parse_portno (p+1, &port)) { - gpg_err_set_errno (EINVAL); + _gpg_err_set_errno (EINVAL); addrlen = 0; } else @@ -352,25 +354,26 @@ fun_writer (void *cookie_arg, const void *buffer, size_t size) #endif /*!HAVE_INET_PTON*/ } - xfree (addrstr); + _gpgrt_free (addrstr); } cookie->fd = addrlen? socket (pf, SOCK_STREAM, 0) : -1; if (cookie->fd == -1) { if (!cookie->quiet && !running_detached - && isatty (es_fileno (es_stderr))) - es_fprintf (es_stderr, "failed to create socket for logging: %s\n", - strerror(errno)); + && isatty (_gpgrt_fileno (es_stderr))) + _gpgrt_fprintf (es_stderr, + "failed to create socket for logging: %s\n", + strerror (errno)); } else { if (connect (cookie->fd, srvr_addr, addrlen) == -1) { if (!cookie->quiet && !running_detached - && isatty (es_fileno (es_stderr))) - es_fprintf (es_stderr, "can't connect to '%s%s': %s\n", - cookie->name, name_for_err, strerror(errno)); + && isatty (_gpgrt_fileno (es_stderr))) + _gpgrt_fprintf (es_stderr, "can't connect to '%s%s': %s\n", + cookie->name, name_for_err, strerror(errno)); sock_close (cookie->fd); cookie->fd = -1; } @@ -416,14 +419,14 @@ fun_writer (void *cookie_arg, const void *buffer, size_t size) } if (!running_detached && cookie->fd != -1 - && isatty (es_fileno (es_stderr))) + && isatty (_gpgrt_fileno (es_stderr))) { if (*cookie->name) - es_fprintf (es_stderr, "error writing to '%s': %s\n", - cookie->name, strerror(errno)); + _gpgrt_fprintf (es_stderr, "error writing to '%s': %s\n", + cookie->name, strerror(errno)); else - es_fprintf (es_stderr, "error writing to file descriptor %d: %s\n", - cookie->fd, strerror(errno)); + _gpgrt_fprintf (es_stderr, "error writing to file descriptor %d: %s\n", + cookie->fd, strerror(errno)); } if (cookie->is_socket && cookie->fd != -1) { @@ -443,7 +446,7 @@ fun_closer (void *cookie_arg) if (cookie->fd != -1 && cookie->fd != 2) sock_close (cookie->fd); - xfree (cookie); + _gpgrt_free (cookie); log_socket = -1; return 0; } @@ -465,7 +468,7 @@ set_file_fd (const char *name, int fd) if (logstream) { if (logstream != es_stderr) - es_fclose (logstream); + _gpgrt_fclose (logstream); logstream = NULL; } @@ -473,7 +476,7 @@ set_file_fd (const char *name, int fd) if (name && !strcmp (name, "-")) { name = NULL; - fd = es_fileno (es_stderr); + fd = _gpgrt_fileno (es_stderr); } want_socket = 0; @@ -503,10 +506,9 @@ set_file_fd (const char *name, int fd) /* Setup a new stream. */ - /* The xmalloc below is justified because we can expect that this - function is called only during initialization and there is no - easy way out of this error condition. */ - cookie = xmalloc (sizeof *cookie + (name? strlen (name):0)); + cookie = _gpgrt_malloc (sizeof *cookie + (name? strlen (name):0)); + if (!cookie) + return; /* oops */ strcpy (cookie->name, name? name:""); cookie->quiet = 0; cookie->is_socket = 0; @@ -532,14 +534,14 @@ set_file_fd (const char *name, int fd) io.func_write = fun_writer; io.func_close = fun_closer; - fp = es_fopencookie (cookie, "w", io); + fp = _gpgrt_fopencookie (cookie, "w", io); } /* On error default to a stderr based estream. */ if (!fp) fp = es_stderr; - es_setvbuf (fp, NULL, _IOLBF, 0); + _gpgrt_setvbuf (fp, NULL, _IOLBF, 0); logstream = fp; @@ -553,46 +555,56 @@ set_file_fd (const char *name, int fd) /* Set the file to write log to. The special names NULL and "-" may - be used to select stderr and names formatted like - "socket:///home/foo/mylogs" may be used to write the logging to the - socket "/home/foo/mylogs". If the connection to the socket fails - or a write error is detected, the function writes to stderr and - tries the next time again to connect the socket. - */ -void -log_set_file (const char *name) -{ - set_file_fd (name? name: "-", -1); -} - + * be used to select stderr and names formatted like + * "socket:///home/foo/mylogs" may be used to write the logging to the + * socket "/home/foo/mylogs". If the connection to the socket fails + * or a write error is detected, the function writes to stderr and + * tries the next time again to connect the socket. + * Warning: This function is not thread-safe. + */ void -log_set_fd (int fd) +_gpgrt_log_set_sink (const char *name, estream_t stream, int fd) { - if (! gnupg_fd_valid (fd)) - log_fatal ("logger-fd is invalid: %s\n", strerror (errno)); - - set_file_fd (NULL, fd); + if (name && !stream && fd == -1) + set_file_fd (name, -1); + else if (!name && !stream) + { + if (!_gpgrt_fd_valid_p (fd)) + _gpgrt_log_fatal ("gpgrt_log_set_sink: fd is invalid: %s\n", + strerror (errno)); + set_file_fd (NULL, fd); + } + else if (!name && stream && fd == -1) + { + _gpgrt_log_fatal ("gpgrt_log_set_sink: stream arg not yet supported\n"); + } + else /* default */ + set_file_fd ("-", -1); } /* Set a function to retrieve the directory name of a socket if - * only "socket://" has been given to log_set_file. */ + * only "socket://" has been given to log_set_file. + * Warning: This function is not thread-safe. */ void -log_set_socket_dir_cb (const char *(*fnc)(void)) +_gpgrt_log_set_socket_dir_cb (const char *(*fnc)(void)) { socket_dir_cb = fnc; } +/* Warning: This function is not thread-safe. */ void -log_set_pid_suffix_cb (int (*cb)(unsigned long *r_value)) +_gpgrt_log_set_pid_suffix_cb (int (*cb)(unsigned long *r_value)) { get_pid_suffix_cb = cb; } +/* Warning: Changing TEXT is not thread-safe. Changing only flags + * might be thread-safe. */ void -log_set_prefix (const char *text, unsigned int flags) +_gpgrt_log_set_prefix (const char *text, unsigned int flags) { if (text) { @@ -611,7 +623,7 @@ log_set_prefix (const char *text, unsigned int flags) const char * -log_get_prefix (unsigned int *flags) +_gpgrt_log_get_prefix (unsigned int *flags) { if (flags) { @@ -633,14 +645,14 @@ log_get_prefix (unsigned int *flags) } /* This function returns true if the file descriptor FD is in use for - logging. This is preferable over a test using log_get_fd in that - it allows the logging code to use more then one file descriptor. */ + * logging. This is preferable over a test using log_get_fd in that + * it allows the logging code to use more then one file descriptor. */ int -log_test_fd (int fd) +_gpgrt_log_test_fd (int fd) { if (logstream) { - int tmp = es_fileno (logstream); + int tmp = _gpgrt_fileno (logstream); if ( tmp != -1 && tmp == fd) return 1; } @@ -650,26 +662,31 @@ log_test_fd (int fd) } int -log_get_fd () +_gpgrt_log_get_fd () { - return logstream? es_fileno(logstream) : -1; + return logstream? _gpgrt_fileno (logstream) : -1; } estream_t -log_get_stream () +_gpgrt_log_get_stream () { if (!logstream) { - log_set_file (NULL); /* Make sure a log stream has been set. */ + /* Make sure a log stream has been set. */ + _gpgrt_log_set_sink (NULL, NULL, -1); assert (logstream); } return logstream; } -static void +/* Note: LOGSTREAM is expected to be locked. */ +static int print_prefix (int level, int leading_backspace) { + int rc; + int length = 0; + if (level != GPGRT_LOG_CONT) { /* Note this does not work for multiple line logging as we would * need to print to a buffer first */ @@ -679,30 +696,46 @@ print_prefix (int level, int leading_backspace) time_t atime = time (NULL); tp = localtime (&atime); - es_fprintf_unlocked (logstream, "%04d-%02d-%02d %02d:%02d:%02d ", + rc = _gpgrt_fprintf_unlocked (logstream, + "%04d-%02d-%02d %02d:%02d:%02d ", 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec ); + if (rc > 0) + length += rc; } if (with_prefix || force_prefixes) - es_fputs_unlocked (prefix_buffer, logstream); + { + _gpgrt_fputs_unlocked (prefix_buffer, logstream); + length += strlen (prefix_buffer); + } if (with_pid || force_prefixes) { unsigned long pidsuf; int pidfmt; if (get_pid_suffix_cb && (pidfmt=get_pid_suffix_cb (&pidsuf))) - es_fprintf_unlocked (logstream, pidfmt == 1? "[%u.%lu]":"[%u.%lx]", - (unsigned int)getpid (), pidsuf); + rc = _gpgrt_fprintf_unlocked (logstream, + pidfmt == 1? "[%u.%lu]":"[%u.%lx]", + (unsigned int)getpid (), pidsuf); else - es_fprintf_unlocked (logstream, "[%u]", (unsigned int)getpid ()); + rc = _gpgrt_fprintf_unlocked (logstream, "[%u]", + (unsigned int)getpid ()); + if (rc > 0) + length += rc; } if ((!with_time && (with_prefix || with_pid)) || force_prefixes) - es_putc_unlocked (':', logstream); + { + _gpgrt_putc_unlocked (':', logstream); + length++; + } /* A leading backspace suppresses the extra space so that we can correctly output, programname, filename and linenumber. */ if (!leading_backspace && (with_time || with_prefix || with_pid || force_prefixes)) - es_putc_unlocked (' ', logstream); + { + _gpgrt_putc_unlocked (' ', logstream); + length++; + } } switch (level) @@ -712,21 +745,40 @@ print_prefix (int level, int leading_backspace) case GPGRT_LOG_INFO: break; case GPGRT_LOG_WARN: break; case GPGRT_LOG_ERROR: break; - case GPGRT_LOG_FATAL: es_fputs_unlocked ("Fatal: ",logstream ); break; - case GPGRT_LOG_BUG: es_fputs_unlocked ("Ohhhh jeeee: ", logstream); break; - case GPGRT_LOG_DEBUG: es_fputs_unlocked ("DBG: ", logstream ); break; + case GPGRT_LOG_FATAL: + _gpgrt_fputs_unlocked ("Fatal: ", logstream); + length += 7; + break; + case GPGRT_LOG_BUG: + _gpgrt_fputs_unlocked ("Ohhhh jeeee: ", logstream); + length += 13; + break; + case GPGRT_LOG_DEBUG: + _gpgrt_fputs_unlocked ("DBG: ", logstream); + length += 5; + break; default: - es_fprintf_unlocked (logstream,"[Unknown log level %d]: ", level); + rc = _gpgrt_fprintf_unlocked (logstream, + "[Unknown log level %d]: ", level); + if (rc > 0) + length += rc; break; } + + return length; } -static void -do_logv (int level, int ignore_arg_ptr, const char *extrastring, - const char *prefmt, const char *fmt, va_list arg_ptr) +/* Internal worker function. Exported so that we can use it in + * visibility.c. Returs the number of characters printed or 0 if the + * line ends in a LF. */ +int +_gpgrt_logv_internal (int level, int ignore_arg_ptr, const char *extrastring, + const char *prefmt, const char *fmt, va_list arg_ptr) { int leading_backspace = (fmt && *fmt == '\b'); + int length; + int rc; if (!logstream) { @@ -738,26 +790,30 @@ do_logv (int level, int ignore_arg_ptr, const char *extrastring, : read_w32_registry_string (NULL, GNUPG_REGISTRY_DIR, "DefaultLogFile")); log_set_file (tmp && *tmp? tmp : NULL); - xfree (tmp); + _gpgrt_free (tmp); #else - log_set_file (NULL); /* Make sure a log stream has been set. */ + /* Make sure a log stream has been set. */ + _gpgrt_log_set_sink (NULL, NULL, -1); #endif assert (logstream); } - es_flockfile (logstream); + _gpgrt_flockfile (logstream); if (missing_lf && level != GPGRT_LOG_CONT) - es_putc_unlocked ('\n', logstream ); + _gpgrt_putc_unlocked ('\n', logstream ); missing_lf = 0; - print_prefix (level, leading_backspace); + length = print_prefix (level, leading_backspace); if (leading_backspace) fmt++; if (fmt) { if (prefmt) - es_fputs_unlocked (prefmt, logstream); + { + _gpgrt_fputs_unlocked (prefmt, logstream); + length += strlen (prefmt); + } if (ignore_arg_ptr) { /* This is used by log_string and comes with the extra @@ -768,15 +824,25 @@ do_logv (int level, int ignore_arg_ptr, const char *extrastring, const char *p, *pend; for (p = fmt; (pend = strchr (p, '\n')); p = pend+1) - es_fprintf_unlocked (logstream, "%*s%.*s", + { + rc = _gpgrt_fprintf_unlocked (logstream, "%*s%.*s", (int)((p != fmt && (with_prefix || force_prefixes)) ?strlen (prefix_buffer)+2:0), "", (int)(pend - p)+1, p); - es_fputs_unlocked (p, logstream); + if (rc > 0) + length += rc; + } + _gpgrt_fputs_unlocked (p, logstream); + length += strlen (p); } else - es_vfprintf_unlocked (logstream, fmt, arg_ptr); + { + rc = _gpgrt_vfprintf_unlocked (logstream, fmt, arg_ptr); + if (rc > 0) + length += rc; + } + if (*fmt && fmt[strlen(fmt)-1] != '\n') missing_lf = 1; } @@ -789,36 +855,50 @@ do_logv (int level, int ignore_arg_ptr, const char *extrastring, if (missing_lf) { - es_putc_unlocked ('\n', logstream); + _gpgrt_putc_unlocked ('\n', logstream); missing_lf = 0; + length = 0; } - print_prefix (level, leading_backspace); - es_fputs_unlocked (">> ", logstream); + length += print_prefix (level, leading_backspace); + _gpgrt_fputs_unlocked (">> ", logstream); + length += 3; missing_lf = 1; while ((c = *extrastring++)) { missing_lf = 1; if (c == '\\') - es_fputs_unlocked ("\\\\", logstream); + { + _gpgrt_fputs_unlocked ("\\\\", logstream); + length += 2; + } else if (c == '\r') - es_fputs_unlocked ("\\r", logstream); + { + _gpgrt_fputs_unlocked ("\\r", logstream); + length += 2; + } else if (c == '\n') { - es_fputs_unlocked ("\\n\n", logstream); + _gpgrt_fputs_unlocked ("\\n\n", logstream); + length = 0; if (*extrastring) { - print_prefix (level, leading_backspace); - es_fputs_unlocked (">> ", logstream); + length += print_prefix (level, leading_backspace); + _gpgrt_fputs_unlocked (">> ", logstream); + length += 3; } else missing_lf = 0; } else - es_putc_unlocked (c, logstream); + { + _gpgrt_putc_unlocked (c, logstream); + length++; + } } if (missing_lf) { - es_putc_unlocked ('\n', logstream); + _gpgrt_putc_unlocked ('\n', logstream); + length = 0; missing_lf = 0; } } @@ -826,15 +906,15 @@ do_logv (int level, int ignore_arg_ptr, const char *extrastring, if (level == GPGRT_LOG_FATAL) { if (missing_lf) - es_putc_unlocked ('\n', logstream); - es_funlockfile (logstream); + _gpgrt_putc_unlocked ('\n', logstream); + _gpgrt_funlockfile (logstream); exit (2); } else if (level == GPGRT_LOG_BUG) { if (missing_lf) - es_putc_unlocked ('\n', logstream ); - es_funlockfile (logstream); + _gpgrt_putc_unlocked ('\n', logstream ); + _gpgrt_funlockfile (logstream); /* Using backtrace requires a configure test and to pass * -rdynamic to gcc. Thus we do not enable it now. */ /* { */ @@ -851,36 +931,46 @@ do_logv (int level, int ignore_arg_ptr, const char *extrastring, abort (); } else - es_funlockfile (logstream); + _gpgrt_funlockfile (logstream); + + /* Bumb the error counter for log_error. */ + if (level == GPGRT_LOG_ERROR) + { + /* Protect against counter overflow. */ + if (errorcount < 30000) + errorcount++; + } + + return length; } void -log_log (int level, const char *fmt, ...) +_gpgrt_log (int level, const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt) ; - do_logv (level, 0, NULL, NULL, fmt, arg_ptr); + _gpgrt_logv_internal (level, 0, NULL, NULL, fmt, arg_ptr); va_end (arg_ptr); } void -log_logv (int level, const char *fmt, va_list arg_ptr) +_gpgrt_logv (int level, const char *fmt, va_list arg_ptr) { - do_logv (level, 0, NULL, NULL, fmt, arg_ptr); + _gpgrt_logv_internal (level, 0, NULL, NULL, fmt, arg_ptr); } /* Same as log_logv but PREFIX is printed immediately before FMT. * Note that PREFIX is an additional string and independent of the - * prefix set by log_set_prefix. */ + * prefix set by gpgrt_log_set_prefix. */ void -log_logv_with_prefix (int level, const char *prefix, - const char *fmt, va_list arg_ptr) +_gpgrt_logv_prefix (int level, const char *prefix, + const char *fmt, va_list arg_ptr) { - do_logv (level, 0, NULL, prefix, fmt, arg_ptr); + _gpgrt_logv_internal (level, 0, NULL, prefix, fmt, arg_ptr); } @@ -889,7 +979,7 @@ do_log_ignore_arg (int level, const char *str, ...) { va_list arg_ptr; va_start (arg_ptr, str); - do_logv (level, 1, NULL, NULL, str, arg_ptr); + _gpgrt_logv_internal (level, 1, NULL, NULL, str, arg_ptr); va_end (arg_ptr); } @@ -897,70 +987,68 @@ do_log_ignore_arg (int level, const char *str, ...) /* Log STRING at LEVEL but indent from the second line on by the * length of the prefix. */ void -log_string (int level, const char *string) +_gpgrt_log_string (int level, const char *string) { /* We need a dummy arg_ptr, but there is no portable way to create - * one. So we call the do_logv function through a variadic wrapper. */ + * one. So we call the _gpgrt_logv_internal function through a + * variadic wrapper. */ do_log_ignore_arg (level, string); } void -log_info (const char *fmt, ...) +_gpgrt_log_info (const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt); - do_logv (GPGRT_LOG_INFO, 0, NULL, NULL, fmt, arg_ptr); + _gpgrt_logv_internal (GPGRT_LOG_INFO, 0, NULL, NULL, fmt, arg_ptr); va_end (arg_ptr); } void -log_error (const char *fmt, ...) +_gpgrt_log_error (const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt); - do_logv (GPGRT_LOG_ERROR, 0, NULL, NULL, fmt, arg_ptr); + _gpgrt_logv_internal (GPGRT_LOG_ERROR, 0, NULL, NULL, fmt, arg_ptr); va_end (arg_ptr); - /* Protect against counter overflow. */ - if (errorcount < 30000) - errorcount++; } void -log_fatal (const char *fmt, ...) +_gpgrt_log_fatal (const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt); - do_logv (GPGRT_LOG_FATAL, 0, NULL, NULL, fmt, arg_ptr); + _gpgrt_logv_internal (GPGRT_LOG_FATAL, 0, NULL, NULL, fmt, arg_ptr); va_end (arg_ptr); abort (); /* Never called; just to make the compiler happy. */ } void -log_bug (const char *fmt, ...) +_gpgrt_log_bug (const char *fmt, ...) { va_list arg_ptr ; va_start (arg_ptr, fmt); - do_logv (GPGRT_LOG_BUG, 0, NULL, NULL, fmt, arg_ptr); + _gpgrt_logv_internal (GPGRT_LOG_BUG, 0, NULL, NULL, fmt, arg_ptr); va_end (arg_ptr); abort (); /* Never called; just to make the compiler happy. */ } void -log_debug (const char *fmt, ...) +_gpgrt_log_debug (const char *fmt, ...) { - va_list arg_ptr ; + va_list arg_ptr; va_start (arg_ptr, fmt); - do_logv (GPGRT_LOG_DEBUG, 0, NULL, NULL, fmt, arg_ptr); + _gpgrt_logv_internal (GPGRT_LOG_DEBUG, 0, NULL, NULL, fmt, arg_ptr); va_end (arg_ptr); } @@ -969,23 +1057,23 @@ log_debug (const char *fmt, ...) * printed with LFs expanded to include the prefix and a final --end-- * marker. */ void -log_debug_with_string (const char *string, const char *fmt, ...) +_gpgrt_log_debug_string (const char *string, const char *fmt, ...) { - va_list arg_ptr ; + va_list arg_ptr; va_start (arg_ptr, fmt); - do_logv (GPGRT_LOG_DEBUG, 0, string, NULL, fmt, arg_ptr); + _gpgrt_logv_internal (GPGRT_LOG_DEBUG, 0, string, NULL, fmt, arg_ptr); va_end (arg_ptr); } void -log_printf (const char *fmt, ...) +_gpgrt_log_printf (const char *fmt, ...) { va_list arg_ptr; va_start (arg_ptr, fmt); - do_logv (fmt ? GPGRT_LOG_CONT : GPGRT_LOG_BEGIN, 0, NULL, NULL, fmt, arg_ptr); + _gpgrt_logv_internal (fmt ? GPGRT_LOG_CONT : GPGRT_LOG_BEGIN, 0, NULL, NULL, fmt, arg_ptr); va_end (arg_ptr); } @@ -993,52 +1081,86 @@ log_printf (const char *fmt, ...) /* Flush the log - this is useful to make sure that the trailing linefeed has been printed. */ void -log_flush (void) +_gpgrt_log_flush (void) { do_log_ignore_arg (GPGRT_LOG_CONT, NULL); } -/* Print a hexdump of BUFFER. With TEXT of NULL print just the raw - dump, with TEXT just an empty string, print a trailing linefeed, - otherwise print an entire debug line. */ +/* Print a hexdump of (BUFFER,LENGTH). With FMT passed as NULL print + * just the raw dump, with FMT being an empty string, print a trailing + * linefeed, otherwise print an entire debug line with the expanded + * FMT followed by a possible wrapped hexdump and a final LF. */ void -log_printhex (const char *text, const void *buffer, size_t length) +_gpgrt_logv_printhex (const void *buffer, size_t length, + const char *fmt, va_list arg_ptr) { - if (text && *text) - log_debug ("%s ", text); + int wrap = 0; + int cnt = 0; + const unsigned char *p; + + /* FIXME: This printing is not yet protected by _gpgrt_flockfile. */ + if (fmt && *fmt) + { + _gpgrt_logv_internal (GPGRT_LOG_DEBUG, 0, NULL, NULL, fmt, arg_ptr); + wrap = 1; + } + if (length) { - const unsigned char *p = buffer; - log_printf ("%02X", *p); - for (length--, p++; length--; p++) - log_printf (" %02X", *p); + if (wrap) + _gpgrt_log_printf (" "); + + for (p = buffer; length--; p++) + { + _gpgrt_log_printf ("%02x", *p); + if (wrap && ++cnt == 32 && length) + { + cnt = 0; + /* (we indicate continuations with a backslash) */ + _gpgrt_log_printf (" \\\n"); + _gpgrt_log_debug (""); + if (fmt && *fmt) + _gpgrt_log_printf (" "); + } + } } - if (text) - log_printf ("\n"); + + if (fmt) + _gpgrt_log_printf ("\n"); } -/* +/* Print a hexdump of (BUFFER,LENGTH). With FMT passed as NULL print + * just the raw dump, with FMT being an empty string, print a trailing + * linefeed, otherwise print an entire debug line with the expanded + * FMT followed by the hexdump and a final LF. */ void -log_printcanon () {} -is found in sexputils.c -*/ +_gpgrt_log_printhex (const void *buffer, size_t length, + const char *fmt, ...) +{ + va_list arg_ptr; + + if (fmt) + { + va_start (arg_ptr, fmt); + _gpgrt_logv_printhex (buffer, length, fmt, arg_ptr); + va_end (arg_ptr); + } + else + _gpgrt_logv_printhex (buffer, length, NULL, NULL); +} -/* -void -log_printsexp () {} -is found in sexputils.c -*/ -/* Print a microsecond timestamp followed by STRING. */ +/* Print a microsecond timestamp followed by FMT. */ void -log_clock (const char *string) +_gpgrt_logv_clock (const char *fmt, va_list arg_ptr) { #if ENABLE_LOG_CLOCK static unsigned long long initial; struct timespec tv; unsigned long long now; + char clockbuf[50]; if (clock_gettime (CLOCK_REALTIME, &tv)) { @@ -1051,45 +1173,41 @@ log_clock (const char *string) if (!initial) initial = now; - log_debug ("[%6llu] %s", (now - initial)/1000, string); + snprintf (clockbuf, sizeof clockbuf, "[%6llu] ", (now - initial)/1000); + _gpgrt_logv_internal (GPGRT_LOG_DEBUG, 0, NULL, clockbuf, fmt, arg_ptr); + #else /*!ENABLE_LOG_CLOCK*/ + /* You may need to link with -ltr to use the above code. */ - log_debug ("[not enabled by configure] %s", string); + + _gpgrt_logv_internal (GPGRT_LOG_DEBUG, 0, NULL, "[no clock] ", fmt, arg_ptr); + #endif /*!ENABLE_LOG_CLOCK*/ } -#ifdef GPGRT_HAVE_MACRO_FUNCTION -void -bug_at( const char *file, int line, const char *func ) -{ - log_log (GPGRT_LOG_BUG, "... this is a bug (%s:%d:%s)\n", file, line, func); - abort (); /* Never called; just to make the compiler happy. */ -} -#else /*!GPGRT_HAVE_MACRO_FUNCTION*/ +/* Print a microsecond timestamp followed by FMT. */ void -bug_at( const char *file, int line ) +_gpgrt_log_clock (const char *fmt, ...) { - log_log (GPGRT_LOG_BUG, "you found a bug ... (%s:%d)\n", file, line); - abort (); /* Never called; just to make the compiler happy. */ + va_list arg_ptr; + + va_start (arg_ptr, fmt); + _gpgrt_logv_clock (fmt, arg_ptr); + va_end (arg_ptr); } -#endif /*!GPGRT_HAVE_MACRO_FUNCTION*/ -#ifdef GPGRT_HAVE_MACRO_FUNCTION void -_log_assert (const char *expr, const char *file, int line, const char *func) +_gpgrt__log_assert (const char *expr, const char *file, + int line, const char *func) { - log_log (GPGRT_LOG_BUG, "Assertion \"%s\" in %s failed (%s:%d)\n", - expr, func, file, line); - abort (); /* Never called; just to make the compiler happy. */ -} +#ifdef GPGRT_HAVE_MACRO_FUNCTION + _gpgrt_log (GPGRT_LOG_BUG, "Assertion \"%s\" in %s failed (%s:%d)\n", + expr, func, file, line); #else /*!GPGRT_HAVE_MACRO_FUNCTION*/ -void -_log_assert (const char *expr, const char *file, int line) -{ - log_log (GPGRT_LOG_BUG, "Assertion \"%s\" failed (%s:%d)\n", + _gpgrt_log (GPGRT_LOG_BUG, "Assertion \"%s\" failed (%s:%d)\n", expr, file, line); +#endif /*!GPGRT_HAVE_MACRO_FUNCTION*/ abort (); /* Never called; just to make the compiler happy. */ } -#endif /*!GPGRT_HAVE_MACRO_FUNCTION*/ diff --git a/src/logging.h b/src/logging.h deleted file mode 100644 index 2fae303..0000000 --- a/src/logging.h +++ /dev/null @@ -1,107 +0,0 @@ -/* logging.h - Definitions for logging.c - * Copyright (C) 1998-2001, 2003-2006, 2009-2010, - * 2017 Free Software Foundation, Inc. - * Copyright (C) 1998-1999, 2001-2006, 2008-2017 Werner Koch - * - * This file is part of Libgpg-error. - * - * Libgpg-error is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * Libgpg-error is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see <https://www.gnu.org/licenses/>. - * - * This file was originally a part of GnuPG. - */ - -#ifndef _GPGRT_LOGGING_H -#define _GPGRT_LOGGING_H - -#include <stdio.h> -#include <stdarg.h> -#include <gpg-error.h> -#include "mischelp.h" -#include "w32help.h" - -int log_get_errorcount (int clear); -void log_inc_errorcount (void); -void log_set_file( const char *name ); -void log_set_fd (int fd); -void log_set_socket_dir_cb (const char *(*fnc)(void)); -void log_set_pid_suffix_cb (int (*cb)(unsigned long *r_value)); -void log_set_prefix (const char *text, unsigned int flags); -const char *log_get_prefix (unsigned int *flags); -int log_test_fd (int fd); -int log_get_fd(void); -estream_t log_get_stream (void); - -#ifdef GPGRT_HAVE_MACRO_FUNCTION - void bug_at (const char *file, int line, const char *func) - GPGRT_ATTR_NORETURN; - void _log_assert (const char *expr, const char *file, int line, - const char *func) GPGRT_ATTR_NORETURN; -# define BUG() bug_at( __FILE__ , __LINE__, __FUNCTION__) -# define log_assert(expr) \ - ((expr) \ - ? (void) 0 \ - : _log_assert (#expr, __FILE__, __LINE__, __FUNCTION__)) -#else /*!GPGRT_HAVE_MACRO_FUNCTION*/ - void bug_at (const char *file, int line); - void _log_assert (const char *expr, const char *file, int line); -# define BUG() bug_at( __FILE__ , __LINE__ ) -# define log_assert(expr) \ - ((expr) \ - ? (void) 0 \ - : _log_assert (#expr, __FILE__, __LINE__)) -#endif /*!GPGRT_HAVE_MACRO_FUNCTION*/ - -/* Flag values for log_set_prefix. */ -#define GPGRT_LOG_WITH_PREFIX 1 -#define GPGRT_LOG_WITH_TIME 2 -#define GPGRT_LOG_WITH_PID 4 -#define GPGRT_LOG_RUN_DETACHED 256 -#define GPGRT_LOG_NO_REGISTRY 512 - -/* Log levels as used by log_log. */ -enum jnlib_log_levels { - GPGRT_LOG_BEGIN, - GPGRT_LOG_CONT, - GPGRT_LOG_INFO, - GPGRT_LOG_WARN, - GPGRT_LOG_ERROR, - GPGRT_LOG_FATAL, - GPGRT_LOG_BUG, - GPGRT_LOG_DEBUG -}; -void log_log (int level, const char *fmt, ...) GPGRT_ATTR_PRINTF(2,3); -void log_logv (int level, const char *fmt, va_list arg_ptr); -void log_logv_with_prefix (int level, const char *prefix, - const char *fmt, va_list arg_ptr); -void log_string (int level, const char *string); -void log_bug (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2); -void log_fatal (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2); -void log_error (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); -void log_info (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); -void log_debug (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); -void log_debug_with_string (const char *string, const char *fmt, - ...) GPGRT_ATTR_PRINTF(2,3); -void log_printf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2); -void log_flush (void); - -/* Print a hexdump of BUFFER. With TEXT passes as NULL print just the - raw dump, with TEXT being an empty string, print a trailing - linefeed, otherwise print an entire debug line with TEXT followed - by the hexdump and a final LF. */ -void log_printhex (const char *text, const void *buffer, size_t length); - -void log_clock (const char *string); - - -#endif /*_GPGRT_LOGGING_H*/ diff --git a/src/visibility.c b/src/visibility.c index b637e7a..569c3e6 100644 --- a/src/visibility.c +++ b/src/visibility.c @@ -15,10 +15,12 @@ * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see <https://www.gnu.org/licenses/>. + * SPDX-License-Identifier: LGPL-2.1+ */ #include <config.h> #include <stdarg.h> +#include <stdlib.h> /* For abort(). */ #define _GPGRT_INCL_BY_VISIBILITY_C 1 #include "gpgrt-int.h" @@ -712,6 +714,8 @@ gpgrt_vsnprintf (char *buf, size_t bufsize, return _gpgrt_estream_vsnprintf (buf, bufsize, format, arg_ptr); } + + gpgrt_b64state_t gpgrt_b64dec_start (const char *title) { @@ -730,3 +734,203 @@ gpgrt_b64dec_finish (gpgrt_b64state_t state) { return _gpgrt_b64dec_finish (state); } + + + +int +gpgrt_get_errorcount (int clear) +{ + return _gpgrt_get_errorcount (clear); +} + +void +gpgrt_inc_errorcount (void) +{ + _gpgrt_inc_errorcount (); +} + +void +gpgrt_log_set_sink (const char *name, estream_t stream, int fd) +{ + _gpgrt_log_set_sink (name, stream, fd); +} + +void +gpgrt_log_set_socket_dir_cb (const char *(*fnc)(void)) +{ + _gpgrt_log_set_socket_dir_cb (fnc); +} + +void +gpgrt_log_set_pid_suffix_cb (int (*cb)(unsigned long *r_value)) +{ + _gpgrt_log_set_pid_suffix_cb (cb); +} + +void +gpgrt_log_set_prefix (const char *text, unsigned int flags) +{ + _gpgrt_log_set_prefix (text, flags); +} + +const char * +gpgrt_log_get_prefix (unsigned int *flags) +{ + return _gpgrt_log_get_prefix (flags); +} + +int +gpgrt_log_test_fd (int fd) +{ + return _gpgrt_log_test_fd (fd); +} + +int +gpgrt_log_get_fd (void) +{ + return _gpgrt_log_get_fd (); +} + +estream_t +gpgrt_log_get_stream (void) +{ + return _gpgrt_log_get_stream (); +} + +void +gpgrt_log (int level, const char *fmt, ...) +{ + va_list arg_ptr ; + + va_start (arg_ptr, fmt) ; + _gpgrt_logv (level, fmt, arg_ptr); + va_end (arg_ptr); +} + +void +gpgrt_logv (int level, const char *fmt, va_list arg_ptr) +{ + _gpgrt_logv (level, fmt, arg_ptr); +} + +void +gpgrt_logv_prefix (int level, const char *prefix, + const char *fmt, va_list arg_ptr) +{ + _gpgrt_logv_prefix (level, prefix, fmt, arg_ptr); +} + +void +gpgrt_log_string (int level, const char *string) +{ + _gpgrt_log_string (level, string); +} + +void +gpgrt_log_info (const char *fmt, ...) +{ + va_list arg_ptr; + + va_start (arg_ptr, fmt); + _gpgrt_logv (GPGRT_LOG_INFO, fmt, arg_ptr); + va_end (arg_ptr); +} + +void +gpgrt_log_error (const char *fmt, ...) +{ + va_list arg_ptr; + + va_start (arg_ptr, fmt); + _gpgrt_logv (GPGRT_LOG_ERROR, fmt, arg_ptr); + va_end (arg_ptr); +} + +void +gpgrt_log_fatal (const char *fmt, ...) +{ + va_list arg_ptr; + + va_start (arg_ptr, fmt); + _gpgrt_logv (GPGRT_LOG_FATAL, fmt, arg_ptr); + va_end (arg_ptr); + abort (); /* Never called; just to make the compiler happy. */ +} + +void +gpgrt_log_bug (const char *fmt, ...) +{ + va_list arg_ptr; + + va_start (arg_ptr, fmt); + _gpgrt_logv (GPGRT_LOG_BUG, fmt, arg_ptr); + va_end (arg_ptr); + abort (); /* Never called; just to make the compiler happy. */ +} + +void +gpgrt_log_debug (const char *fmt, ...) +{ + va_list arg_ptr ; + + va_start (arg_ptr, fmt); + _gpgrt_logv (GPGRT_LOG_DEBUG, fmt, arg_ptr); + va_end (arg_ptr); +} + +void +gpgrt_log_debug_string (const char *string, const char *fmt, ...) +{ + va_list arg_ptr ; + + va_start (arg_ptr, fmt); + _gpgrt_logv_internal (GPGRT_LOG_DEBUG, 0, string, NULL, fmt, arg_ptr); + va_end (arg_ptr); +} + +void +gpgrt_log_printf (const char *fmt, ...) +{ + va_list arg_ptr; + + va_start (arg_ptr, fmt); + _gpgrt_logv (fmt ? GPGRT_LOG_CONT : GPGRT_LOG_BEGIN, fmt, arg_ptr); + va_end (arg_ptr); +} + +void +gpgrt_log_flush (void) +{ + _gpgrt_log_flush (); +} + +void +gpgrt_log_printhex (const void *buffer, size_t length, const char *fmt, ...) +{ + va_list arg_ptr; + + va_start (arg_ptr, fmt); + _gpgrt_logv_printhex (buffer, length, fmt, arg_ptr); + va_end (arg_ptr); +} + +void +gpgrt_log_clock (const char *fmt, ...) +{ + va_list arg_ptr; + + va_start (arg_ptr, fmt); + _gpgrt_logv_clock (fmt, arg_ptr); + va_end (arg_ptr); +} + +void +_gpgrt_log_assert (const char *expr, const char *file, + int line, const char *func) +{ +#ifdef GPGRT_HAVE_MACRO_FUNCTION + _gpgrt__log_assert (expr, file, line, func); +#else + _gpgrt__log_assert (expr, file, line); +#endif +} diff --git a/src/visibility.h b/src/visibility.h index da8e228..dca3019 100644 --- a/src/visibility.h +++ b/src/visibility.h @@ -15,6 +15,7 @@ * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see <https://www.gnu.org/licenses/>. + * SPDX-License-Identifier: LGPL-2.1+ */ #ifndef _GPGRT_VISIBILITY_H @@ -149,6 +150,32 @@ MARK_VISIBLE (gpgrt_b64dec_start) MARK_VISIBLE (gpgrt_b64dec_proc) MARK_VISIBLE (gpgrt_b64dec_finish) +MARK_VISIBLE (gpgrt_get_errorcount) +MARK_VISIBLE (gpgrt_inc_errorcount) +MARK_VISIBLE (gpgrt_log_set_sink) +MARK_VISIBLE (gpgrt_log_set_socket_dir_cb) +MARK_VISIBLE (gpgrt_log_set_pid_suffix_cb) +MARK_VISIBLE (gpgrt_log_set_prefix) +MARK_VISIBLE (gpgrt_log_get_prefix) +MARK_VISIBLE (gpgrt_log_test_fd) +MARK_VISIBLE (gpgrt_log_get_fd) +MARK_VISIBLE (gpgrt_log_get_stream) +MARK_VISIBLE (gpgrt_log) +MARK_VISIBLE (gpgrt_logv) +MARK_VISIBLE (gpgrt_logv_prefix) +MARK_VISIBLE (gpgrt_log_string) +MARK_VISIBLE (gpgrt_log_bug) +MARK_VISIBLE (gpgrt_log_fatal) +MARK_VISIBLE (gpgrt_log_error) +MARK_VISIBLE (gpgrt_log_info) +MARK_VISIBLE (gpgrt_log_debug) +MARK_VISIBLE (gpgrt_log_debug_string) +MARK_VISIBLE (gpgrt_log_printf) +MARK_VISIBLE (gpgrt_log_printhex) +MARK_VISIBLE (gpgrt_log_clock) +MARK_VISIBLE (gpgrt_log_flush) +MARK_VISIBLE (_gpgrt_log_assert) + #undef MARK_VISIBLE #else /*!_GPGRT_INCL_BY_VISIBILITY_C*/ @@ -263,6 +290,32 @@ MARK_VISIBLE (gpgrt_b64dec_finish) #define gpgrt_b64dec_proc _gpgrt_USE_UNDERSCORED_FUNCTION #define gpgrt_b64dec_finish _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_get_errorcount _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_inc_errorcount _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_set_sink _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_set_socket_dir_cb _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_set_pid_suffix_cb _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_set_prefix _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_get_prefix _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_test_fd _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_get_fd _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_get_stream _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_logv _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_logv_prefix _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_string _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_bug _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_fatal _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_error _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_info _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_debug _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_debug_string _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_printf _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_printhex _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_clock _gpgrt_USE_UNDERSCORED_FUNCTION +#define gpgrt_log_flush _gpgrt_USE_UNDERSCORED_FUNCTION +#define _gpgrt_log_assert _gpgrt_USE_UNDERSCORED_FUNCTION + #endif /*!_GPGRT_INCL_BY_VISIBILITY_C*/ #endif /*_GPGRT_VISIBILITY_H*/ |