virlog.h 7.2 KB
Newer Older
1
/*
2
 * virlog.h: internal logging and debugging
3
 *
4
 * Copyright (C) 2006-2008, 2011-2012 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22
 *
 */

#ifndef __VIRTLOG_H_
23
# define __VIRTLOG_H_
24

25
# include "internal.h"
26
# include "virbuffer.h"
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/*
 * To be made public
 */
typedef enum {
    VIR_LOG_DEBUG = 1,
    VIR_LOG_INFO,
    VIR_LOG_WARN,
    VIR_LOG_ERROR,
} virLogPriority;

# define VIR_LOG_DEFAULT VIR_LOG_WARN

typedef enum {
    VIR_LOG_TO_STDERR = 1,
    VIR_LOG_TO_SYSLOG,
    VIR_LOG_TO_FILE,
    VIR_LOG_TO_JOURNALD,
} virLogDestination;

47 48 49 50 51
typedef struct _virLogSource virLogSource;
typedef virLogSource *virLogSourcePtr;

struct _virLogSource {
    const char *name;
52 53 54
    unsigned int priority;
    unsigned int serial;
    unsigned int flags;
55
};
56

57 58 59 60 61 62 63 64
/*
 * ATTRIBUTE_UNUSED is to make gcc keep quiet if all the
 * log statements in a file are conditionally disabled
 * at compile time due to configure options.
 */
# define VIR_LOG_INIT(n)                                \
    static ATTRIBUTE_UNUSED virLogSource virLogSelf = { \
        .name = "" n "",                                \
65 66 67
        .priority = VIR_LOG_ERROR,                      \
        .serial = 0,                                    \
        .flags = 0,                                     \
68
    };
69

70 71 72
/*
 * If configured with --enable-debug=yes then library calls
 * are printed to stderr for debugging or to an appropriate channel
73
 * defined at runtime from the libvirt daemon configuration file
74
 */
75
# ifdef ENABLE_DEBUG
76
#  define VIR_DEBUG_INT(src, filename, linenr, funcname, ...)           \
77
    virLogMessage(src, VIR_LOG_DEBUG, filename, linenr, funcname, NULL, __VA_ARGS__)
78
# else
79 80 81 82 83
/**
 * virLogEatParams:
 *
 * Do nothing but eat parameters.
 */
84
static inline void virLogEatParams(virLogSourcePtr unused, ...)
85 86 87 88
{
    /* Silence gcc */
    unused = unused;
}
89 90
#  define VIR_DEBUG_INT(src, filename, linenr, funcname, ...)           \
    virLogEatParams(src, filename, linenr, funcname, __VA_ARGS__)
91
# endif /* !ENABLE_DEBUG */
92

93
# define VIR_INFO_INT(src, filename, linenr, funcname, ...)             \
94
    virLogMessage(src, VIR_LOG_INFO, filename, linenr, funcname, NULL, __VA_ARGS__)
95
# define VIR_WARN_INT(src, filename, linenr, funcname, ...)             \
96
    virLogMessage(src, VIR_LOG_WARN, filename, linenr, funcname, NULL, __VA_ARGS__)
97
# define VIR_ERROR_INT(src, filename, linenr, funcname, ...)            \
98
    virLogMessage(src, VIR_LOG_ERROR, filename, linenr, funcname, NULL, __VA_ARGS__)
99

100
# define VIR_DEBUG(...)                                                 \
101
    VIR_DEBUG_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
102
# define VIR_INFO(...)                                                  \
103
    VIR_INFO_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
104
# define VIR_WARN(...)                                                  \
105
    VIR_WARN_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
106
# define VIR_ERROR(...)                                                 \
107
    VIR_ERROR_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
108

109 110 111 112

struct _virLogMetadata {
    const char *key;
    const char *s;              /* String value, or NULL to use "i" */
113
    int iv;
114 115 116 117 118
};

typedef struct _virLogMetadata virLogMetadata;
typedef struct _virLogMetadata *virLogMetadataPtr;

D
Daniel Veillard 已提交
119 120
/**
 * virLogOutputFunc:
121
 * @src: the source of the log message
D
Daniel Veillard 已提交
122
 * @priority: the priority for the message
123
 * @filename: file where the message was emitted
124
 * @linenr: line where the message was emitted
125
 * @funcname: the function emitting the message
126
 * @timestamp: zero terminated string with timestamp of the message
M
Miloslav Trmač 已提交
127
 * @metadata: NULL or metadata array, terminated by an item with NULL key
128
 * @flags: flags associated with the message
129
 * @rawstr: the unformatted message to log, zero terminated
130
 * @str: the message to log, preformatted and zero terminated
131
 * @data: extra output logging data
D
Daniel Veillard 已提交
132 133 134
 *
 * Callback function used to output messages
 */
135
typedef void (*virLogOutputFunc) (virLogSourcePtr src,
136
                                  virLogPriority priority,
137
                                  const char *filename,
138
                                  int linenr,
139
                                  const char *funcname,
140
                                  const char *timestamp,
M
Miloslav Trmač 已提交
141
                                  virLogMetadataPtr metadata,
142
                                  unsigned int flags,
143 144
                                  const char *rawstr,
                                  const char *str,
145
                                  void *data);
D
Daniel Veillard 已提交
146 147 148 149 150 151 152 153 154

/**
 * virLogCloseFunc:
 * @data: extra output logging data
 *
 * Callback function used to close a log output
 */
typedef void (*virLogCloseFunc) (void *data);

155 156 157 158
typedef enum {
    VIR_LOG_STACK_TRACE = (1 << 0),
} virLogFlags;

159 160
extern int virLogGetNbFilters(void);
extern int virLogGetNbOutputs(void);
161 162
extern char *virLogGetFilters(void);
extern char *virLogGetOutputs(void);
163 164
extern virLogPriority virLogGetDefaultPriority(void);
extern int virLogSetDefaultPriority(virLogPriority priority);
165
extern void virLogSetFromEnv(void);
166 167
extern int virLogDefineFilter(const char *match,
                              virLogPriority priority,
168
                              unsigned int flags);
169 170 171 172 173 174
extern int virLogDefineOutput(virLogOutputFunc f,
                              virLogCloseFunc c,
                              void *data,
                              virLogPriority priority,
                              virLogDestination dest,
                              const char *name,
175
                              unsigned int flags);
D
Daniel Veillard 已提交
176 177 178 179

/*
 * Internal logging API
 */
180

181 182
extern void virLogLock(void);
extern void virLogUnlock(void);
D
Daniel Veillard 已提交
183
extern int virLogReset(void);
184
extern int virLogParseDefaultPriority(const char *priority);
D
Daniel Veillard 已提交
185 186
extern int virLogParseFilters(const char *filters);
extern int virLogParseOutputs(const char *output);
J
Ján Tomko 已提交
187
extern int virLogPriorityFromSyslog(int priority);
188
extern void virLogMessage(virLogSourcePtr source,
189
                          virLogPriority priority,
190
                          const char *filename,
191
                          int linenr,
192
                          const char *funcname,
193 194
                          virLogMetadataPtr metadata,
                          const char *fmt, ...) ATTRIBUTE_FMT_PRINTF(7, 8);
195
extern void virLogVMessage(virLogSourcePtr source,
196
                           virLogPriority priority,
197
                           const char *filename,
198
                           int linenr,
199
                           const char *funcname,
200
                           virLogMetadataPtr metadata,
201
                           const char *fmt,
202
                           va_list vargs) ATTRIBUTE_FMT_PRINTF(7, 0);
203 204 205

bool virLogProbablyLogMessage(const char *str);

206
#endif