virlog.h 7.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;

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

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

172 173 174 175
typedef enum {
    VIR_LOG_STACK_TRACE = (1 << 0),
} virLogFlags;

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
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);
193
void virLogOutputFree(virLogOutputPtr output);
D
Daniel Veillard 已提交
194 195 196 197

/*
 * Internal logging API
 */
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
void virLogLock(void);
void virLogUnlock(void);
int virLogReset(void);
int virLogParseDefaultPriority(const char *priority);
int virLogParseFilters(const char *filters);
int virLogParseOutputs(const char *output);
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);
221 222 223

bool virLogProbablyLogMessage(const char *str);

224
#endif