提交 398553c1 编写于 作者: D Daniel Veillard

Add an an internal API for emergency dump of debug buffer

virLogEmergencyDumpAll() allows to dump the content of the
debug buffer from within a signal handler. It saves to all
log file or stderr if none is found
* src/util/logging.h src/util/logging.c: add the new API
  and cleanup the old virLogDump code
* src/libvirt_private.syms: exports it as a private symbol
上级 35708ec1
...@@ -541,6 +541,7 @@ virRegisterStorageDriver; ...@@ -541,6 +541,7 @@ virRegisterStorageDriver;
# logging.h # logging.h
virLogDefineFilter; virLogDefineFilter;
virLogDefineOutput; virLogDefineOutput;
virLogEmergencyDumpAll;
virLogGetDefaultPriority; virLogGetDefaultPriority;
virLogGetFilters; virLogGetFilters;
virLogGetNbFilters; virLogGetNbFilters;
......
...@@ -129,6 +129,9 @@ static virLogPriority virLogDefaultPriority = VIR_LOG_DEFAULT; ...@@ -129,6 +129,9 @@ static virLogPriority virLogDefaultPriority = VIR_LOG_DEFAULT;
static int virLogResetFilters(void); static int virLogResetFilters(void);
static int virLogResetOutputs(void); static int virLogResetOutputs(void);
static int virLogOutputToFd(const char *category, int priority,
const char *funcname, long long linenr,
const char *str, int len, void *data);
/* /*
* Logs accesses must be serialized though a mutex * Logs accesses must be serialized though a mutex
...@@ -277,50 +280,66 @@ static void virLogStr(const char *str, int len) { ...@@ -277,50 +280,66 @@ static void virLogStr(const char *str, int len) {
virLogUnlock(); virLogUnlock();
} }
#if 0 static void virLogDumpAllFD(const char *msg, int len) {
/* int i, found = 0;
* Output the ring buffer
for (i = 0; i < virLogNbOutputs;i++) {
if (virLogOutputs[i].f == virLogOutputToFd) {
int fd = (long) virLogOutputs[i].data;
if (fd >= 0) {
ignore_value (safewrite(fd, msg, len));
found = 1;
}
}
}
if (!found)
ignore_value (safewrite(STDERR_FILENO, msg, len));
}
/**
* virLogEmergencyDumpAll:
* @signum: the signal number
*
* Emergency function called, possibly from a signal handler.
* It need to output the debug ring buffer through the log
* output which are safe to use from a signal handler.
* In case none is found it is emitted to standard error.
*/ */
static int virLogDump(void *data, virLogOutputFunc f) { void
int ret = 0, tmp; virLogEmergencyDumpAll(int signum) {
int ret = 0, len;
char buf[100];
if (virLogLen == 0)
return;
if ((virLogLen == 0) || (f == NULL))
return 0;
virLogLock(); virLogLock();
if (virLogStart + virLogLen < LOG_BUFFER_SIZE) { snprintf(buf, sizeof(buf) - 1,
push_end: "Caught signal %d, dumping internal log buffer:\n", signum);
virLogBuffer[virLogStart + virLogLen] = 0; buf[sizeof(buf) - 1] = 0;
tmp = f(data, &virLogBuffer[virLogStart], virLogLen); virLogDumpAllFD(buf, strlen(buf));
if (tmp < 0) { snprintf(buf, sizeof(buf) - 1, "\n\n ====== start of log =====\n\n");
ret = -1; virLogDumpAllFD(buf, strlen(buf));
goto error; while (virLogLen > 0) {
} if (virLogStart + virLogLen < LOG_BUFFER_SIZE) {
ret += tmp; virLogBuffer[virLogStart + virLogLen] = 0;
virLogStart += tmp; virLogDumpAllFD(&virLogBuffer[virLogStart], virLogLen);
virLogLen -= tmp; ret += virLogLen;
} else { virLogStart += virLogLen;
tmp = LOG_BUFFER_SIZE - virLogStart; virLogLen = 0;
ret = f(data, &virLogBuffer[virLogStart], tmp);
if (ret < 0) {
ret = -1;
goto error;
}
if (ret < tmp) {
virLogStart += ret;
virLogLen -= ret;
} else { } else {
len = LOG_BUFFER_SIZE - virLogStart;
virLogBuffer[LOG_BUFFER_SIZE] = 0;
virLogDumpAllFD(&virLogBuffer[virLogStart], len);
virLogLen -= len;
virLogStart = 0; virLogStart = 0;
virLogLen -= tmp;
/* dump the second part */
if (virLogLen > 0)
goto push_end;
} }
} }
error: snprintf(buf, sizeof(buf) - 1, "\n\n ====== end of log =====\n\n");
virLogDumpAllFD(buf, strlen(buf));
virLogUnlock(); virLogUnlock();
return ret;
} }
#endif
/** /**
* virLogSetDefaultPriority: * virLogSetDefaultPriority:
......
...@@ -133,5 +133,5 @@ extern int virLogParseOutputs(const char *output); ...@@ -133,5 +133,5 @@ extern int virLogParseOutputs(const char *output);
extern void virLogMessage(const char *category, int priority, extern void virLogMessage(const char *category, int priority,
const char *funcname, long long linenr, int flags, const char *funcname, long long linenr, int flags,
const char *fmt, ...) ATTRIBUTE_FMT_PRINTF(6, 7); const char *fmt, ...) ATTRIBUTE_FMT_PRINTF(6, 7);
extern void virLogEmergencyDumpAll(int signum);
#endif #endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册