virlog.h 7.1 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 52
typedef struct _virLogSource virLogSource;
typedef virLogSource *virLogSourcePtr;

struct _virLogSource {
    const char *name;
};
53

54 55 56 57 58 59 60 61 62
/*
 * 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 "",                                \
    };
63

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

87
# define VIR_INFO_INT(src, filename, linenr, funcname, ...)             \
88
    virLogMessage(src, VIR_LOG_INFO, filename, linenr, funcname, NULL, __VA_ARGS__)
89
# define VIR_WARN_INT(src, filename, linenr, funcname, ...)             \
90
    virLogMessage(src, VIR_LOG_WARN, filename, linenr, funcname, NULL, __VA_ARGS__)
91
# define VIR_ERROR_INT(src, filename, linenr, funcname, ...)            \
92
    virLogMessage(src, VIR_LOG_ERROR, filename, linenr, funcname, NULL, __VA_ARGS__)
93

94
# define VIR_DEBUG(...)                                                 \
95
    VIR_DEBUG_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
96
# define VIR_INFO(...)                                                  \
97
    VIR_INFO_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
98
# define VIR_WARN(...)                                                  \
99
    VIR_WARN_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
100
# define VIR_ERROR(...)                                                 \
101
    VIR_ERROR_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
102

103 104 105 106

struct _virLogMetadata {
    const char *key;
    const char *s;              /* String value, or NULL to use "i" */
107
    int iv;
108 109 110 111 112
};

typedef struct _virLogMetadata virLogMetadata;
typedef struct _virLogMetadata *virLogMetadataPtr;

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

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

149 150 151 152
typedef enum {
    VIR_LOG_STACK_TRACE = (1 << 0),
} virLogFlags;

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

/*
 * Internal logging API
 */
174

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

bool virLogProbablyLogMessage(const char *str);

202
#endif