virlog.h 6.9 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 47
/*
 * 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;

typedef enum {
48 49 50 51 52
    VIR_LOG_FROM_FILE,    /* General debugging */
    VIR_LOG_FROM_ERROR,   /* Errors reported */
    VIR_LOG_FROM_AUDIT,   /* Audit operations */
    VIR_LOG_FROM_TRACE,   /* DTrace probe pointers */
    VIR_LOG_FROM_LIBRARY, /* 3rd party libraries */
53 54 55 56

    VIR_LOG_FROM_LAST,
} virLogSource;

57 58 59
/*
 * If configured with --enable-debug=yes then library calls
 * are printed to stderr for debugging or to an appropriate channel
60
 * defined at runtime from the libvirt daemon configuration file
61
 */
62
# ifdef ENABLE_DEBUG
63
#  define VIR_DEBUG_INT(src, filename, linenr, funcname, ...)           \
64
    virLogMessage(src, VIR_LOG_DEBUG, filename, linenr, funcname, NULL, __VA_ARGS__)
65
# else
66 67 68 69 70
/**
 * virLogEatParams:
 *
 * Do nothing but eat parameters.
 */
71
static inline void virLogEatParams(virLogSource unused, ...)
72 73 74 75
{
    /* Silence gcc */
    unused = unused;
}
76 77
#  define VIR_DEBUG_INT(src, filename, linenr, funcname, ...)           \
    virLogEatParams(src, filename, linenr, funcname, __VA_ARGS__)
78
# endif /* !ENABLE_DEBUG */
79

80
# define VIR_INFO_INT(src, filename, linenr, funcname, ...)             \
81
    virLogMessage(src, VIR_LOG_INFO, filename, linenr, funcname, NULL, __VA_ARGS__)
82
# define VIR_WARN_INT(src, filename, linenr, funcname, ...)             \
83
    virLogMessage(src, VIR_LOG_WARN, filename, linenr, funcname, NULL, __VA_ARGS__)
84
# define VIR_ERROR_INT(src, filename, linenr, funcname, ...)            \
85
    virLogMessage(src, VIR_LOG_ERROR, filename, linenr, funcname, NULL, __VA_ARGS__)
86

87
# define VIR_DEBUG(...)                                                 \
88
    VIR_DEBUG_INT(VIR_LOG_FROM_FILE, __FILE__, __LINE__, __func__, __VA_ARGS__)
89
# define VIR_INFO(...)                                                  \
90
    VIR_INFO_INT(VIR_LOG_FROM_FILE, __FILE__, __LINE__, __func__, __VA_ARGS__)
91
# define VIR_WARN(...)                                                  \
92
    VIR_WARN_INT(VIR_LOG_FROM_FILE, __FILE__, __LINE__, __func__, __VA_ARGS__)
93
# define VIR_ERROR(...)                                                 \
94
    VIR_ERROR_INT(VIR_LOG_FROM_FILE, __FILE__, __LINE__, __func__, __VA_ARGS__)
95

96 97 98 99

struct _virLogMetadata {
    const char *key;
    const char *s;              /* String value, or NULL to use "i" */
100
    int iv;
101 102 103 104 105
};

typedef struct _virLogMetadata virLogMetadata;
typedef struct _virLogMetadata *virLogMetadataPtr;

D
Daniel Veillard 已提交
106 107
/**
 * virLogOutputFunc:
108
 * @src: the src for the message
D
Daniel Veillard 已提交
109
 * @priority: the priority for the message
110
 * @filename: file where the message was emitted
111
 * @linenr: line where the message was emitted
112
 * @funcname: the function emitting the message
113
 * @timestamp: zero terminated string with timestamp of the message
M
Miloslav Trmač 已提交
114
 * @metadata: NULL or metadata array, terminated by an item with NULL key
115
 * @flags: flags associated with the message
116
 * @rawstr: the unformatted message to log, zero terminated
117
 * @str: the message to log, preformatted and zero terminated
118
 * @data: extra output logging data
D
Daniel Veillard 已提交
119 120 121
 *
 * Callback function used to output messages
 */
122
typedef void (*virLogOutputFunc) (virLogSource src,
123
                                  virLogPriority priority,
124
                                  const char *filename,
125
                                  int linenr,
126
                                  const char *funcname,
127
                                  const char *timestamp,
M
Miloslav Trmač 已提交
128
                                  virLogMetadataPtr metadata,
129
                                  unsigned int flags,
130 131
                                  const char *rawstr,
                                  const char *str,
132
                                  void *data);
D
Daniel Veillard 已提交
133 134 135 136 137 138 139 140 141

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

142 143 144 145
typedef enum {
    VIR_LOG_STACK_TRACE = (1 << 0),
} virLogFlags;

146 147
extern int virLogGetNbFilters(void);
extern int virLogGetNbOutputs(void);
148 149
extern char *virLogGetFilters(void);
extern char *virLogGetOutputs(void);
150 151
extern virLogPriority virLogGetDefaultPriority(void);
extern int virLogSetDefaultPriority(virLogPriority priority);
152
extern void virLogSetFromEnv(void);
153 154
extern int virLogDefineFilter(const char *match,
                              virLogPriority priority,
155
                              unsigned int flags);
156 157 158 159 160 161
extern int virLogDefineOutput(virLogOutputFunc f,
                              virLogCloseFunc c,
                              void *data,
                              virLogPriority priority,
                              virLogDestination dest,
                              const char *name,
162
                              unsigned int flags);
D
Daniel Veillard 已提交
163 164 165 166

/*
 * Internal logging API
 */
167

168 169
extern void virLogLock(void);
extern void virLogUnlock(void);
D
Daniel Veillard 已提交
170
extern int virLogReset(void);
171
extern int virLogParseDefaultPriority(const char *priority);
D
Daniel Veillard 已提交
172 173
extern int virLogParseFilters(const char *filters);
extern int virLogParseOutputs(const char *output);
J
Ján Tomko 已提交
174
extern int virLogPriorityFromSyslog(int priority);
175
extern void virLogMessage(virLogSource src,
176
                          virLogPriority priority,
177
                          const char *filename,
178
                          int linenr,
179
                          const char *funcname,
180 181
                          virLogMetadataPtr metadata,
                          const char *fmt, ...) ATTRIBUTE_FMT_PRINTF(7, 8);
182
extern void virLogVMessage(virLogSource src,
183
                           virLogPriority priority,
184
                           const char *filename,
185
                           int linenr,
186
                           const char *funcname,
187
                           virLogMetadataPtr metadata,
188
                           const char *fmt,
189
                           va_list vargs) ATTRIBUTE_FMT_PRINTF(7, 0);
190
extern int virLogSetBufferSize(int size);
191
extern void virLogEmergencyDumpAll(int signum);
192 193 194

bool virLogProbablyLogMessage(const char *str);

195
#endif