summaryrefslogtreecommitdiffstats
path: root/common/log.h
blob: 38a798862b3abb990f5d8a28d760ab2333138ab8 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
   Copyright (C) 2012 Red Hat, Inc.

   This library 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.

   This library 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 library; if not, see <http://www.gnu.org/licenses/>.
*/

#ifndef H_SPICE_LOG
#define H_SPICE_LOG

#include <glib.h>

#include <spice/macros.h>
#include <stdarg.h>
#include "macros.h"

SPICE_BEGIN_DECLS

#ifndef SPICE_LOG_DOMAIN
#define SPICE_LOG_DOMAIN "Spice"
#endif

#define SPICE_STRLOC  __FILE__ ":" G_STRINGIFY (__LINE__)

typedef enum {
    SPICE_LOG_LEVEL_ERROR,
    SPICE_LOG_LEVEL_CRITICAL,
    SPICE_LOG_LEVEL_WARNING,
    SPICE_LOG_LEVEL_INFO,
    SPICE_LOG_LEVEL_DEBUG,
} SpiceLogLevel;

void spice_logv(const char *log_domain,
                SpiceLogLevel log_level,
                const char *strloc,
                const char *function,
                const char *format,
                va_list args) SPICE_ATTR_PRINTF(5, 0);

void spice_log(const char *log_domain,
               SpiceLogLevel log_level,
               const char *strloc,
               const char *function,
               const char *format,
               ...) SPICE_ATTR_PRINTF(5, 6);

#define spice_return_if_fail(x) G_STMT_START {                          \
    if G_LIKELY(x) { } else {                                           \
        spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_CRITICAL, SPICE_STRLOC, G_STRFUNC, "condition `%s' failed", #x); \
        return;                                                         \
    }                                                                   \
} G_STMT_END

#define spice_return_val_if_fail(x, val) G_STMT_START {                 \
    if G_LIKELY(x) { } else {                                           \
        spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_CRITICAL, SPICE_STRLOC, __FUNCTION__, "condition `%s' failed", #x); \
        return (val);                                                   \
    }                                                                   \
} G_STMT_END

#define spice_warn_if_reached() G_STMT_START {                          \
    spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_WARNING, SPICE_STRLOC, __FUNCTION__, "should not be reached"); \
} G_STMT_END

#define spice_printerr(format, ...) G_STMT_START {                      \
    fprintf(stderr, "%s: " format "\n", __FUNCTION__, ## __VA_ARGS__);  \
} G_STMT_END

#define spice_info(format, ...) G_STMT_START {                         \
    spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_INFO, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
} G_STMT_END

#define spice_debug(format, ...) G_STMT_START {                         \
    spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_DEBUG, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
} G_STMT_END

#define spice_warning(format, ...) G_STMT_START {                       \
    spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_WARNING, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
} G_STMT_END

#define spice_critical(format, ...) G_STMT_START {                          \
    spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_CRITICAL, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
} G_STMT_END

#define spice_error(format, ...) G_STMT_START {                         \
    spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_ERROR, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
} G_STMT_END

#define spice_warn_if_fail(x) G_STMT_START {            \
    if G_LIKELY(x) { } else {                           \
        spice_warning("condition `%s' failed", #x);     \
    }                                                   \
} G_STMT_END

#define spice_warn_if(x) G_STMT_START {                 \
    if G_UNLIKELY(x) {                                  \
        spice_warning("condition `%s' reached", #x);    \
    }                                                   \
} G_STMT_END

#define spice_assert(x) G_STMT_START {                  \
    if G_LIKELY(x) { } else {                           \
        spice_error("assertion `%s' failed", #x);       \
    }                                                   \
} G_STMT_END

/* FIXME: improve that some day.. */
#define spice_static_assert(x) SPICE_STMT_START {       \
    spice_assert(x);                                    \
} SPICE_STMT_END

SPICE_END_DECLS

#endif /* H_SPICE_LOG */