virlog.h 8.5 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
# ifdef PACKAGER_VERSION
#  ifdef PACKAGER
#   define VIR_LOG_VERSION_STRING \
     "libvirt version: " VERSION ", package: " PACKAGER_VERSION " (" PACKAGER ")"
#  else
#   define VIR_LOG_VERSION_STRING \
     "libvirt version: " VERSION ", package: " PACKAGER_VERSION
#  endif
# else
#  define VIR_LOG_VERSION_STRING  \
    "libvirt version: " VERSION
# endif

41 42 43 44 45 46 47 48 49 50 51 52 53
/*
 * 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 {
54
    VIR_LOG_TO_STDERR = 0,
55 56 57
    VIR_LOG_TO_SYSLOG,
    VIR_LOG_TO_FILE,
    VIR_LOG_TO_JOURNALD,
58
    VIR_LOG_TO_OUTPUT_LAST,
59 60
} virLogDestination;

61 62 63 64 65
typedef struct _virLogSource virLogSource;
typedef virLogSource *virLogSourcePtr;

struct _virLogSource {
    const char *name;
66 67 68
    unsigned int priority;
    unsigned int serial;
    unsigned int flags;
69
};
70

71 72 73 74 75 76 77 78
/*
 * 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 "",                                \
79 80 81
        .priority = VIR_LOG_ERROR,                      \
        .serial = 0,                                    \
        .flags = 0,                                     \
82
    };
83

84 85 86
/*
 * If configured with --enable-debug=yes then library calls
 * are printed to stderr for debugging or to an appropriate channel
87
 * defined at runtime from the libvirt daemon configuration file
88
 */
89
# ifdef ENABLE_DEBUG
90
#  define VIR_DEBUG_INT(src, filename, linenr, funcname, ...)           \
91
    virLogMessage(src, VIR_LOG_DEBUG, filename, linenr, funcname, NULL, __VA_ARGS__)
92
# else
93 94 95 96 97
/**
 * virLogEatParams:
 *
 * Do nothing but eat parameters.
 */
98
static inline void virLogEatParams(virLogSourcePtr unused, ...)
99 100 101 102
{
    /* Silence gcc */
    unused = unused;
}
103 104
#  define VIR_DEBUG_INT(src, filename, linenr, funcname, ...)           \
    virLogEatParams(src, filename, linenr, funcname, __VA_ARGS__)
105
# endif /* !ENABLE_DEBUG */
106

107
# define VIR_INFO_INT(src, filename, linenr, funcname, ...)             \
108
    virLogMessage(src, VIR_LOG_INFO, filename, linenr, funcname, NULL, __VA_ARGS__)
109
# define VIR_WARN_INT(src, filename, linenr, funcname, ...)             \
110
    virLogMessage(src, VIR_LOG_WARN, filename, linenr, funcname, NULL, __VA_ARGS__)
111
# define VIR_ERROR_INT(src, filename, linenr, funcname, ...)            \
112
    virLogMessage(src, VIR_LOG_ERROR, filename, linenr, funcname, NULL, __VA_ARGS__)
113

114
# define VIR_DEBUG(...)                                                 \
115
    VIR_DEBUG_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
116
# define VIR_INFO(...)                                                  \
117
    VIR_INFO_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
118
# define VIR_WARN(...)                                                  \
119
    VIR_WARN_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
120
# define VIR_ERROR(...)                                                 \
121
    VIR_ERROR_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
122

123 124 125 126

struct _virLogMetadata {
    const char *key;
    const char *s;              /* String value, or NULL to use "i" */
127
    int iv;
128 129 130 131 132
};

typedef struct _virLogMetadata virLogMetadata;
typedef struct _virLogMetadata *virLogMetadataPtr;

133 134 135
typedef struct _virLogOutput virLogOutput;
typedef virLogOutput *virLogOutputPtr;

136 137 138
typedef struct _virLogFilter virLogFilter;
typedef virLogFilter *virLogFilterPtr;

D
Daniel Veillard 已提交
139 140
/**
 * virLogOutputFunc:
141
 * @src: the source of the log message
D
Daniel Veillard 已提交
142
 * @priority: the priority for the message
143
 * @filename: file where the message was emitted
144
 * @linenr: line where the message was emitted
145
 * @funcname: the function emitting the message
146
 * @timestamp: zero terminated string with timestamp of the message
M
Miloslav Trmač 已提交
147
 * @metadata: NULL or metadata array, terminated by an item with NULL key
148
 * @flags: flags associated with the message
149
 * @rawstr: the unformatted message to log, zero terminated
150
 * @str: the message to log, preformatted and zero terminated
151
 * @data: extra output logging data
D
Daniel Veillard 已提交
152 153 154
 *
 * Callback function used to output messages
 */
155
typedef void (*virLogOutputFunc) (virLogSourcePtr src,
156
                                  virLogPriority priority,
157
                                  const char *filename,
158
                                  int linenr,
159
                                  const char *funcname,
160
                                  const char *timestamp,
M
Miloslav Trmač 已提交
161
                                  virLogMetadataPtr metadata,
162
                                  unsigned int flags,
163 164
                                  const char *rawstr,
                                  const char *str,
165
                                  void *data);
D
Daniel Veillard 已提交
166 167 168 169 170 171 172 173 174

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

175 176 177 178
typedef enum {
    VIR_LOG_STACK_TRACE = (1 << 0),
} virLogFlags;

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
int virLogGetNbFilters(void);
int virLogGetNbOutputs(void);
char *virLogGetFilters(void);
char *virLogGetOutputs(void);
virLogPriority virLogGetDefaultPriority(void);
int virLogSetDefaultPriority(virLogPriority priority);
void virLogSetFromEnv(void);
int virLogDefineFilter(const char *match,
                       virLogPriority priority,
                       unsigned int flags);
int virLogDefineOutput(virLogOutputFunc f,
                       virLogCloseFunc c,
                       void *data,
                       virLogPriority priority,
                       virLogDestination dest,
                       const char *name,
                       unsigned int flags);
196
void virLogOutputFree(virLogOutputPtr output);
197
void virLogOutputListFree(virLogOutputPtr *list, int count);
198
void virLogFilterFree(virLogFilterPtr filter);
199
void virLogFilterListFree(virLogFilterPtr *list, int count);
D
Daniel Veillard 已提交
200 201 202 203

/*
 * Internal logging API
 */
204

205 206 207 208
void virLogLock(void);
void virLogUnlock(void);
int virLogReset(void);
int virLogParseDefaultPriority(const char *priority);
209 210
int virLogParseAndDefineFilters(const char *filters);
int virLogParseAndDefineOutputs(const char *output);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
int virLogPriorityFromSyslog(int priority);
void virLogMessage(virLogSourcePtr source,
                   virLogPriority priority,
                   const char *filename,
                   int linenr,
                   const char *funcname,
                   virLogMetadataPtr metadata,
                   const char *fmt, ...) ATTRIBUTE_FMT_PRINTF(7, 8);
void virLogVMessage(virLogSourcePtr source,
                    virLogPriority priority,
                    const char *filename,
                    int linenr,
                    const char *funcname,
                    virLogMetadataPtr metadata,
                    const char *fmt,
                    va_list vargs) ATTRIBUTE_FMT_PRINTF(7, 0);
227 228

bool virLogProbablyLogMessage(const char *str);
E
Erik Skultety 已提交
229 230 231 232 233 234
virLogOutputPtr virLogOutputNew(virLogOutputFunc f,
                                virLogCloseFunc c,
                                void *data,
                                virLogPriority priority,
                                virLogDestination dest,
                                const char *name) ATTRIBUTE_NONNULL(1);
E
Erik Skultety 已提交
235 236 237
virLogFilterPtr virLogFilterNew(const char *match,
                                virLogPriority priority,
                                unsigned int flags) ATTRIBUTE_NONNULL(1);
E
Erik Skultety 已提交
238 239
int virLogFindOutput(virLogOutputPtr *outputs, size_t noutputs,
                     virLogDestination dest, const void *opaque);
240 241
int virLogDefineOutputs(virLogOutputPtr *outputs,
                        size_t noutputs) ATTRIBUTE_NONNULL(1);
242
int virLogDefineFilters(virLogFilterPtr *filters, size_t nfilters);
243

244
#endif