summaryrefslogtreecommitdiffstats
path: root/common/logging.h
blob: a5800cb4c9ab55c10b2a882aa120e2ebe252e170 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* logging.h
 * Copyright (C) 1999, 2000, 2001, 2004, 2006,
 *               2010 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute and/or modify this
 * part of GnuPG under the terms of either
 *
 *   - the GNU Lesser General Public License as published by the Free
 *     Software Foundation; either version 3 of the License, or (at
 *     your option) any later version.
 *
 * or
 *
 *   - the GNU General Public License as published by the Free
 *     Software Foundation; either version 2 of the License, or (at
 *     your option) any later version.
 *
 * or both in parallel, as here.
 *
 * GnuPG 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
 * General Public License for more details.
 *
 * You should have received a copies of the GNU General Public License
 * and the GNU Lesser General Public License along with this program;
 * if not, see <https://www.gnu.org/licenses/>.
 */

#ifndef GNUPG_COMMON_LOGGING_H
#define GNUPG_COMMON_LOGGING_H

#include <stdio.h>
#include <stdarg.h>
#include <gpg-error.h>
#include "mischelp.h"
#include "w32help.h"

/* We use the libgpg-error provided log functions.  but we need one
 * more function:  */
#ifdef GPGRT_HAVE_MACRO_FUNCTION
#  define BUG() bug_at ( __FILE__, __LINE__, __FUNCTION__)
static inline void bug_at (const char *file, int line, const char *func)
                           GPGRT_ATTR_NORETURN;
static inline void
bug_at (const char *file, int line, const char *func)
{
  gpgrt_log (GPGRT_LOGLVL_BUG, "there is a bug at %s:%d:%s\n",
             file, line, func);
  abort ();
}
#else
#  define BUG() bug_at ( __FILE__, __LINE__)
static inline void bug_at (const char *file, int line)
                           GPGRT_ATTR_NORETURN;
static inline void
bug_at (const char *file, int line)
{
  gpgrt_log (GPGRT_LOGLVL_BUG, "there is a bug at %s:%d\n", file, line);
  abort ();
}
#endif /*!GPGRT_HAVE_MACRO_FUNCTION*/



/* Some handy assertion macros which don't abort.  */

#define return_if_fail(expr) do {                        \
    if (!(expr)) {                                       \
        log_debug ("%s:%d: assertion '%s' failed\n",     \
                   __FILE__, __LINE__, #expr );          \
        return;	                                         \
    } } while (0)
#define return_null_if_fail(expr) do {                   \
    if (!(expr)) {                                       \
        log_debug ("%s:%d: assertion '%s' failed\n",     \
                   __FILE__, __LINE__, #expr );          \
        return NULL;	                                 \
    } } while (0)
#define return_val_if_fail(expr,val) do {                \
    if (!(expr)) {                                       \
        log_debug ("%s:%d: assertion '%s' failed\n",     \
                   __FILE__, __LINE__, #expr );          \
        return (val);	                                 \
    } } while (0)
#define never_reached() do {                             \
    log_debug ("%s:%d: oops - should never get here\n",  \
               __FILE__, __LINE__ );                     \
    } while (0)


#endif /*GNUPG_COMMON_LOGGING_H*/