2008-11-06 16:36:07 +00:00
|
|
|
/*
|
2012-12-12 17:59:27 +00:00
|
|
|
* virlog.h: internal logging and debugging
|
2008-11-06 16:36:07 +00:00
|
|
|
*
|
2012-07-13 07:50:04 +00:00
|
|
|
* Copyright (C) 2006-2008, 2011-2012 Red Hat, Inc.
|
2008-11-06 16:36:07 +00:00
|
|
|
*
|
|
|
|
* 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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2008-11-06 16:36:07 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-06-18 16:12:53 +00:00
|
|
|
#pragma once
|
2008-11-06 16:36:07 +00:00
|
|
|
|
2019-06-18 16:12:53 +00:00
|
|
|
#include "internal.h"
|
2008-11-06 16:36:07 +00:00
|
|
|
|
2019-06-18 16:12:53 +00:00
|
|
|
#ifdef PACKAGER_VERSION
|
|
|
|
# ifdef PACKAGER
|
|
|
|
# define VIR_LOG_VERSION_STRING \
|
2020-07-28 18:08:47 +00:00
|
|
|
"libvirt version: " VERSION ", package: " PACKAGER_VERSION " (" PACKAGER ")"
|
2015-02-02 10:28:30 +00:00
|
|
|
# else
|
2017-11-03 12:09:47 +00:00
|
|
|
# define VIR_LOG_VERSION_STRING \
|
2020-07-28 18:08:47 +00:00
|
|
|
"libvirt version: " VERSION ", package: " PACKAGER_VERSION
|
2015-02-02 10:28:30 +00:00
|
|
|
# endif
|
2019-06-18 16:12:53 +00:00
|
|
|
#else
|
|
|
|
# define VIR_LOG_VERSION_STRING \
|
|
|
|
"libvirt version: " VERSION
|
|
|
|
#endif
|
2015-02-02 10:28:30 +00:00
|
|
|
|
2012-10-15 07:14:26 +00:00
|
|
|
/*
|
|
|
|
* To be made public
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
VIR_LOG_DEBUG = 1,
|
|
|
|
VIR_LOG_INFO,
|
|
|
|
VIR_LOG_WARN,
|
|
|
|
VIR_LOG_ERROR,
|
|
|
|
} virLogPriority;
|
|
|
|
|
2019-06-18 16:12:53 +00:00
|
|
|
#define VIR_LOG_DEFAULT VIR_LOG_WARN
|
2012-10-15 07:14:26 +00:00
|
|
|
|
|
|
|
typedef enum {
|
2016-03-15 20:35:17 +00:00
|
|
|
VIR_LOG_TO_STDERR = 0,
|
2012-10-15 07:14:26 +00:00
|
|
|
VIR_LOG_TO_SYSLOG,
|
|
|
|
VIR_LOG_TO_FILE,
|
|
|
|
VIR_LOG_TO_JOURNALD,
|
2016-03-15 20:35:17 +00:00
|
|
|
VIR_LOG_TO_OUTPUT_LAST,
|
2012-10-15 07:14:26 +00:00
|
|
|
} virLogDestination;
|
|
|
|
|
2014-02-27 17:44:53 +00:00
|
|
|
typedef struct _virLogSource virLogSource;
|
|
|
|
typedef virLogSource *virLogSourcePtr;
|
|
|
|
|
|
|
|
struct _virLogSource {
|
|
|
|
const char *name;
|
Switch to filtering based on log source name instead of filename
Currently the log filter strings are used in a string comparison
against the source filename each time log message is emitted.
If no log filters at all are set, there's obviously no string
comparison to be done. If any single log filter is set though,
this imposes a compute burden on every logging call even if logs
from the file in question are disabled. This string comparison
must also be done while the logging mutex is held, which has
implications for concurrency when multiple threads are emitting
log messages.
This changes the log filtering to be done based on the virLogSource
object name. The virLogSource struct is extended to contain
'serial' and 'priority' fields. Any time the global log filter
rules are changed a global serial number is incremented. When a
log message is emitted, the serial in the virLogSource instance
is compared with the global serial number. If out of date, then
the 'priority' field in the virLogSource instance is updated based
on the new filter rules. The 'priority' field is checked to see
whether the log message should be sent to the log outputs.
The comparisons of the 'serial' and 'priority' fields are done
with no locks held. So in the common case each logging call has
an overhead of 2 integer comparisons, with no locks held. Only
if the decision is made to forward the message to the log output,
or if the 'serial' value is out of date do locks need to be
acquired.
Technically the comparisons of the 'serial' and 'priority' fields
should be done with locks held, or using atomic operations. Both
of these options have a notable performance impact, however, and
since all writes a protected by a global mutex, it is believed
that worst case behaviour where the fields are read concurrently
with being written would merely result in an mistaken emission
or dropping of the log message in question. This is an acceptable
tradeoff for the performance benefit of avoiding locking.
As a quick benchmark, a demo program that registers 500 file
descriptors with the event loop (eg equiv of 500 QEMU monitor
commands), creates pending read I/O on every FD, and then runs
virEventRunDefaultImpl() took 4.6 seconds to do 51200 iterations.
After this optimization it only takes 3.3 seconds, with the log
APIs no longer being a relevant factor in the running time.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2014-02-28 13:55:11 +00:00
|
|
|
unsigned int priority;
|
|
|
|
unsigned int serial;
|
2014-02-27 17:44:53 +00:00
|
|
|
};
|
2012-10-15 07:14:26 +00:00
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
/*
|
2019-10-14 12:45:33 +00:00
|
|
|
* G_GNUC_UNUSED is to make gcc keep quiet if all the
|
2014-02-28 12:16:17 +00:00
|
|
|
* log statements in a file are conditionally disabled
|
|
|
|
* at compile time due to configure options.
|
|
|
|
*/
|
2019-06-18 16:12:53 +00:00
|
|
|
#define VIR_LOG_INIT(n) \
|
2019-10-14 12:45:33 +00:00
|
|
|
static G_GNUC_UNUSED virLogSource virLogSelf = { \
|
2017-11-03 12:09:47 +00:00
|
|
|
.name = "" n "", \
|
|
|
|
.priority = VIR_LOG_ERROR, \
|
|
|
|
.serial = 0, \
|
2019-01-20 16:32:42 +00:00
|
|
|
}
|
2012-10-15 07:14:26 +00:00
|
|
|
|
2020-07-24 14:30:06 +00:00
|
|
|
#define VIR_DEBUG_INT(src, filename, linenr, funcname, ...) \
|
Add a metadata parameter to virLog{, V}Message
... and update all users. No change in functionality, the parameter
will be used later.
The metadata representation is as minimal as possible, but requires
the caller to allocate an array on stack explicitly.
The alternative of using varargs in the virLogMessage() callers:
* Would not allow the caller to optionally omit some metadata elements,
except by having two calls to virLogMessage.
* Would not be as type-safe (e.g. using int vs. size_t), and the compiler
wouldn't be able to do type checking
* Depending on parameter order:
a) virLogMessage(..., message format, message params...,
metadata..., NULL)
can not be portably implemented (parse_printf_format() is a glibc
function)
b) virLogMessage(..., metadata..., NULL,
message format, message params...)
would prevent usage of ATTRIBUTE_FMT_PRINTF and the associated
compiler checking.
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2012-10-17 18:17:14 +00:00
|
|
|
virLogMessage(src, VIR_LOG_DEBUG, filename, linenr, funcname, NULL, __VA_ARGS__)
|
2019-06-18 16:12:53 +00:00
|
|
|
#define VIR_INFO_INT(src, filename, linenr, funcname, ...) \
|
Add a metadata parameter to virLog{, V}Message
... and update all users. No change in functionality, the parameter
will be used later.
The metadata representation is as minimal as possible, but requires
the caller to allocate an array on stack explicitly.
The alternative of using varargs in the virLogMessage() callers:
* Would not allow the caller to optionally omit some metadata elements,
except by having two calls to virLogMessage.
* Would not be as type-safe (e.g. using int vs. size_t), and the compiler
wouldn't be able to do type checking
* Depending on parameter order:
a) virLogMessage(..., message format, message params...,
metadata..., NULL)
can not be portably implemented (parse_printf_format() is a glibc
function)
b) virLogMessage(..., metadata..., NULL,
message format, message params...)
would prevent usage of ATTRIBUTE_FMT_PRINTF and the associated
compiler checking.
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2012-10-17 18:17:14 +00:00
|
|
|
virLogMessage(src, VIR_LOG_INFO, filename, linenr, funcname, NULL, __VA_ARGS__)
|
2019-06-18 16:12:53 +00:00
|
|
|
#define VIR_WARN_INT(src, filename, linenr, funcname, ...) \
|
Add a metadata parameter to virLog{, V}Message
... and update all users. No change in functionality, the parameter
will be used later.
The metadata representation is as minimal as possible, but requires
the caller to allocate an array on stack explicitly.
The alternative of using varargs in the virLogMessage() callers:
* Would not allow the caller to optionally omit some metadata elements,
except by having two calls to virLogMessage.
* Would not be as type-safe (e.g. using int vs. size_t), and the compiler
wouldn't be able to do type checking
* Depending on parameter order:
a) virLogMessage(..., message format, message params...,
metadata..., NULL)
can not be portably implemented (parse_printf_format() is a glibc
function)
b) virLogMessage(..., metadata..., NULL,
message format, message params...)
would prevent usage of ATTRIBUTE_FMT_PRINTF and the associated
compiler checking.
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2012-10-17 18:17:14 +00:00
|
|
|
virLogMessage(src, VIR_LOG_WARN, filename, linenr, funcname, NULL, __VA_ARGS__)
|
2019-06-18 16:12:53 +00:00
|
|
|
#define VIR_ERROR_INT(src, filename, linenr, funcname, ...) \
|
Add a metadata parameter to virLog{, V}Message
... and update all users. No change in functionality, the parameter
will be used later.
The metadata representation is as minimal as possible, but requires
the caller to allocate an array on stack explicitly.
The alternative of using varargs in the virLogMessage() callers:
* Would not allow the caller to optionally omit some metadata elements,
except by having two calls to virLogMessage.
* Would not be as type-safe (e.g. using int vs. size_t), and the compiler
wouldn't be able to do type checking
* Depending on parameter order:
a) virLogMessage(..., message format, message params...,
metadata..., NULL)
can not be portably implemented (parse_printf_format() is a glibc
function)
b) virLogMessage(..., metadata..., NULL,
message format, message params...)
would prevent usage of ATTRIBUTE_FMT_PRINTF and the associated
compiler checking.
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2012-10-17 18:17:14 +00:00
|
|
|
virLogMessage(src, VIR_LOG_ERROR, filename, linenr, funcname, NULL, __VA_ARGS__)
|
2008-11-06 16:36:07 +00:00
|
|
|
|
2019-06-18 16:12:53 +00:00
|
|
|
#define VIR_DEBUG(...) \
|
2020-07-28 18:08:47 +00:00
|
|
|
VIR_DEBUG_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
|
2019-06-18 16:12:53 +00:00
|
|
|
#define VIR_INFO(...) \
|
2014-02-27 17:44:53 +00:00
|
|
|
VIR_INFO_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
|
2019-06-18 16:12:53 +00:00
|
|
|
#define VIR_WARN(...) \
|
2014-02-27 17:44:53 +00:00
|
|
|
VIR_WARN_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
|
2019-06-18 16:12:53 +00:00
|
|
|
#define VIR_ERROR(...) \
|
2014-02-27 17:44:53 +00:00
|
|
|
VIR_ERROR_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__)
|
2009-01-06 18:32:03 +00:00
|
|
|
|
Add a metadata parameter to virLog{, V}Message
... and update all users. No change in functionality, the parameter
will be used later.
The metadata representation is as minimal as possible, but requires
the caller to allocate an array on stack explicitly.
The alternative of using varargs in the virLogMessage() callers:
* Would not allow the caller to optionally omit some metadata elements,
except by having two calls to virLogMessage.
* Would not be as type-safe (e.g. using int vs. size_t), and the compiler
wouldn't be able to do type checking
* Depending on parameter order:
a) virLogMessage(..., message format, message params...,
metadata..., NULL)
can not be portably implemented (parse_printf_format() is a glibc
function)
b) virLogMessage(..., metadata..., NULL,
message format, message params...)
would prevent usage of ATTRIBUTE_FMT_PRINTF and the associated
compiler checking.
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2012-10-17 18:17:14 +00:00
|
|
|
|
|
|
|
struct _virLogMetadata {
|
|
|
|
const char *key;
|
|
|
|
const char *s; /* String value, or NULL to use "i" */
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
int iv;
|
Add a metadata parameter to virLog{, V}Message
... and update all users. No change in functionality, the parameter
will be used later.
The metadata representation is as minimal as possible, but requires
the caller to allocate an array on stack explicitly.
The alternative of using varargs in the virLogMessage() callers:
* Would not allow the caller to optionally omit some metadata elements,
except by having two calls to virLogMessage.
* Would not be as type-safe (e.g. using int vs. size_t), and the compiler
wouldn't be able to do type checking
* Depending on parameter order:
a) virLogMessage(..., message format, message params...,
metadata..., NULL)
can not be portably implemented (parse_printf_format() is a glibc
function)
b) virLogMessage(..., metadata..., NULL,
message format, message params...)
would prevent usage of ATTRIBUTE_FMT_PRINTF and the associated
compiler checking.
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2012-10-17 18:17:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _virLogMetadata virLogMetadata;
|
|
|
|
typedef struct _virLogMetadata *virLogMetadataPtr;
|
|
|
|
|
2016-07-04 10:19:38 +00:00
|
|
|
typedef struct _virLogOutput virLogOutput;
|
|
|
|
typedef virLogOutput *virLogOutputPtr;
|
|
|
|
|
2016-07-04 10:16:36 +00:00
|
|
|
typedef struct _virLogFilter virLogFilter;
|
|
|
|
typedef virLogFilter *virLogFilterPtr;
|
|
|
|
|
2008-12-22 10:36:54 +00:00
|
|
|
/**
|
|
|
|
* virLogOutputFunc:
|
2014-02-27 17:44:53 +00:00
|
|
|
* @src: the source of the log message
|
2008-12-22 10:36:54 +00:00
|
|
|
* @priority: the priority for the message
|
2012-09-27 13:44:22 +00:00
|
|
|
* @filename: file where the message was emitted
|
2008-12-22 10:44:10 +00:00
|
|
|
* @linenr: line where the message was emitted
|
2012-09-27 13:44:22 +00:00
|
|
|
* @funcname: the function emitting the message
|
2011-09-28 13:20:07 +00:00
|
|
|
* @timestamp: zero terminated string with timestamp of the message
|
2012-10-17 18:17:15 +00:00
|
|
|
* @metadata: NULL or metadata array, terminated by an item with NULL key
|
2012-09-27 11:45:33 +00:00
|
|
|
* @rawstr: the unformatted message to log, zero terminated
|
2011-09-28 13:20:07 +00:00
|
|
|
* @str: the message to log, preformatted and zero terminated
|
2008-12-22 10:44:10 +00:00
|
|
|
* @data: extra output logging data
|
2008-12-22 10:36:54 +00:00
|
|
|
*
|
|
|
|
* Callback function used to output messages
|
|
|
|
*/
|
2014-02-27 17:44:53 +00:00
|
|
|
typedef void (*virLogOutputFunc) (virLogSourcePtr src,
|
2012-09-27 13:14:01 +00:00
|
|
|
virLogPriority priority,
|
2012-09-27 13:28:44 +00:00
|
|
|
const char *filename,
|
2012-09-27 13:14:01 +00:00
|
|
|
int linenr,
|
2012-09-27 13:28:44 +00:00
|
|
|
const char *funcname,
|
2012-09-20 18:24:00 +00:00
|
|
|
const char *timestamp,
|
2012-10-17 18:17:15 +00:00
|
|
|
virLogMetadataPtr metadata,
|
2012-09-27 13:14:01 +00:00
|
|
|
const char *rawstr,
|
|
|
|
const char *str,
|
2012-09-20 18:24:00 +00:00
|
|
|
void *data);
|
2008-12-22 10:36:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virLogCloseFunc:
|
|
|
|
* @data: extra output logging data
|
|
|
|
*
|
|
|
|
* Callback function used to close a log output
|
|
|
|
*/
|
|
|
|
typedef void (*virLogCloseFunc) (void *data);
|
|
|
|
|
2016-05-10 11:37:32 +00:00
|
|
|
int virLogGetNbFilters(void);
|
|
|
|
int virLogGetNbOutputs(void);
|
|
|
|
char *virLogGetFilters(void);
|
|
|
|
char *virLogGetOutputs(void);
|
|
|
|
virLogPriority virLogGetDefaultPriority(void);
|
|
|
|
int virLogSetDefaultPriority(virLogPriority priority);
|
|
|
|
void virLogSetFromEnv(void);
|
2016-07-04 10:19:38 +00:00
|
|
|
void virLogOutputFree(virLogOutputPtr output);
|
2016-03-30 12:11:12 +00:00
|
|
|
void virLogOutputListFree(virLogOutputPtr *list, int count);
|
2016-07-04 10:16:36 +00:00
|
|
|
void virLogFilterFree(virLogFilterPtr filter);
|
2016-03-29 20:07:23 +00:00
|
|
|
void virLogFilterListFree(virLogFilterPtr *list, int count);
|
2016-11-25 10:50:51 +00:00
|
|
|
int virLogSetOutputs(const char *outputs);
|
2016-03-24 13:34:11 +00:00
|
|
|
int virLogSetFilters(const char *filters);
|
2016-10-31 11:50:24 +00:00
|
|
|
char *virLogGetDefaultOutput(void);
|
2019-10-22 13:26:14 +00:00
|
|
|
void virLogSetDefaultOutput(const char *fname, bool godaemon, bool privileged);
|
2008-12-22 10:36:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal logging API
|
|
|
|
*/
|
2008-11-06 16:36:07 +00:00
|
|
|
|
2016-05-10 11:37:32 +00:00
|
|
|
void virLogLock(void);
|
|
|
|
void virLogUnlock(void);
|
|
|
|
int virLogReset(void);
|
|
|
|
int virLogParseDefaultPriority(const char *priority);
|
|
|
|
int virLogPriorityFromSyslog(int priority);
|
|
|
|
void virLogMessage(virLogSourcePtr source,
|
|
|
|
virLogPriority priority,
|
|
|
|
const char *filename,
|
|
|
|
int linenr,
|
|
|
|
const char *funcname,
|
|
|
|
virLogMetadataPtr metadata,
|
2019-10-15 11:35:07 +00:00
|
|
|
const char *fmt, ...) G_GNUC_PRINTF(7, 8);
|
2013-03-04 20:46:32 +00:00
|
|
|
|
|
|
|
bool virLogProbablyLogMessage(const char *str);
|
2016-07-08 11:46:36 +00:00
|
|
|
virLogOutputPtr virLogOutputNew(virLogOutputFunc f,
|
|
|
|
virLogCloseFunc c,
|
|
|
|
void *data,
|
|
|
|
virLogPriority priority,
|
|
|
|
virLogDestination dest,
|
|
|
|
const char *name) ATTRIBUTE_NONNULL(1);
|
2016-07-08 12:15:43 +00:00
|
|
|
virLogFilterPtr virLogFilterNew(const char *match,
|
2019-10-02 10:51:51 +00:00
|
|
|
virLogPriority priority) ATTRIBUTE_NONNULL(1);
|
2016-03-30 10:22:15 +00:00
|
|
|
int virLogFindOutput(virLogOutputPtr *outputs, size_t noutputs,
|
|
|
|
virLogDestination dest, const void *opaque);
|
2016-03-17 14:18:06 +00:00
|
|
|
int virLogDefineOutputs(virLogOutputPtr *outputs,
|
|
|
|
size_t noutputs) ATTRIBUTE_NONNULL(1);
|
2016-03-29 15:04:36 +00:00
|
|
|
int virLogDefineFilters(virLogFilterPtr *filters, size_t nfilters);
|
2016-08-16 14:27:47 +00:00
|
|
|
virLogOutputPtr virLogParseOutput(const char *src) ATTRIBUTE_NONNULL(1);
|
2016-10-05 12:41:51 +00:00
|
|
|
virLogFilterPtr virLogParseFilter(const char *src) ATTRIBUTE_NONNULL(1);
|
2016-10-05 14:29:15 +00:00
|
|
|
int virLogParseOutputs(const char *src,
|
|
|
|
virLogOutputPtr **outputs) ATTRIBUTE_NONNULL(1);
|
2016-10-05 14:48:47 +00:00
|
|
|
int virLogParseFilters(const char *src,
|
|
|
|
virLogFilterPtr **filters) ATTRIBUTE_NONNULL(1);
|