2 * Copyright (C) 1999, 2000, 2001, 2004, 2006,
3 * 2010 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute and/or modify this
8 * part of GnuPG under the terms of either
10 * - the GNU Lesser General Public License as published by the Free
11 * Software Foundation; either version 3 of the License, or (at
12 * your option) any later version.
16 * - the GNU General Public License as published by the Free
17 * Software Foundation; either version 2 of the License, or (at
18 * your option) any later version.
20 * or both in parallel, as here.
22 * GnuPG is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 * You should have received a copies of the GNU General Public License
28 * and the GNU Lesser General Public License along with this program;
29 * if not, see <https://www.gnu.org/licenses/>.
32 #ifndef GNUPG_COMMON_LOGGING_H
33 #define GNUPG_COMMON_LOGGING_H
37 #include <gpg-error.h>
41 int log_get_errorcount (int clear);
42 void log_inc_errorcount (void);
43 void log_set_file( const char *name );
44 void log_set_fd (int fd);
45 void log_set_socket_dir_cb (const char *(*fnc)(void));
46 void log_set_pid_suffix_cb (int (*cb)(unsigned long *r_value));
47 void log_set_prefix (const char *text, unsigned int flags);
48 const char *log_get_prefix (unsigned int *flags);
49 int log_test_fd (int fd);
51 estream_t log_get_stream (void);
53 #ifdef GPGRT_HAVE_MACRO_FUNCTION
54 void bug_at (const char *file, int line, const char *func)
56 void _log_assert (const char *expr, const char *file, int line,
57 const char *func) GPGRT_ATTR_NORETURN;
58 # define BUG() bug_at( __FILE__ , __LINE__, __FUNCTION__)
59 # define log_assert(expr) \
62 : _log_assert (#expr, __FILE__, __LINE__, __FUNCTION__))
63 #else /*!GPGRT_HAVE_MACRO_FUNCTION*/
64 void bug_at (const char *file, int line);
65 void _log_assert (const char *expr, const char *file, int line);
66 # define BUG() bug_at( __FILE__ , __LINE__ )
67 # define log_assert(expr) \
70 : _log_assert (#expr, __FILE__, __LINE__))
71 #endif /*!GPGRT_HAVE_MACRO_FUNCTION*/
73 /* Flag values for log_set_prefix. */
74 #define GPGRT_LOG_WITH_PREFIX 1
75 #define GPGRT_LOG_WITH_TIME 2
76 #define GPGRT_LOG_WITH_PID 4
77 #define GPGRT_LOG_RUN_DETACHED 256
78 #define GPGRT_LOG_NO_REGISTRY 512
80 /* Log levels as used by log_log. */
81 enum jnlib_log_levels {
91 void log_log (int level, const char *fmt, ...) GPGRT_ATTR_PRINTF(2,3);
92 void log_logv (int level, const char *fmt, va_list arg_ptr);
93 void log_logv_with_prefix (int level, const char *prefix,
94 const char *fmt, va_list arg_ptr);
95 void log_string (int level, const char *string);
96 void log_bug (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2);
97 void log_fatal (const char *fmt, ...) GPGRT_ATTR_NR_PRINTF(1,2);
98 void log_error (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2);
99 void log_info (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2);
100 void log_debug (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2);
101 void log_debug_with_string (const char *string, const char *fmt,
102 ...) GPGRT_ATTR_PRINTF(2,3);
103 void log_printf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2);
104 void log_flush (void);
106 /* Print a hexdump of BUFFER. With TEXT passes as NULL print just the
107 raw dump, with TEXT being an empty string, print a trailing
108 linefeed, otherwise print an entire debug line with TEXT followed
109 by the hexdump and a final LF. */
110 void log_printhex (const char *text, const void *buffer, size_t length);
112 void log_clock (const char *string);
115 /* Some handy assertion macros which don't abort. */
117 #define return_if_fail(expr) do { \
119 log_debug ("%s:%d: assertion '%s' failed\n", \
120 __FILE__, __LINE__, #expr ); \
123 #define return_null_if_fail(expr) do { \
125 log_debug ("%s:%d: assertion '%s' failed\n", \
126 __FILE__, __LINE__, #expr ); \
129 #define return_val_if_fail(expr,val) do { \
131 log_debug ("%s:%d: assertion '%s' failed\n", \
132 __FILE__, __LINE__, #expr ); \
135 #define never_reached() do { \
136 log_debug ("%s:%d: oops - should never get here\n", \
137 __FILE__, __LINE__ ); \
141 #endif /*GNUPG_COMMON_LOGGING_H*/