提交 2a449549 编写于 作者: J Jiri Denemark

logging: Do not log timestamp through syslog

Syslog puts the timestamp to every message anyway so this removes
redundant data.
上级 9b706b27
...@@ -98,7 +98,8 @@ static int virLogResetFilters(void); ...@@ -98,7 +98,8 @@ static int virLogResetFilters(void);
static int virLogResetOutputs(void); static int virLogResetOutputs(void);
static int virLogOutputToFd(const char *category, int priority, static int virLogOutputToFd(const char *category, int priority,
const char *funcname, long long linenr, const char *funcname, long long linenr,
const char *str, int len, void *data); const char *timestamp, const char *str,
void *data);
/* /*
* Logs accesses must be serialized though a mutex * Logs accesses must be serialized though a mutex
...@@ -282,16 +283,16 @@ void virLogShutdown(void) { ...@@ -282,16 +283,16 @@ void virLogShutdown(void) {
/* /*
* Store a string in the ring buffer * Store a string in the ring buffer
*/ */
static void virLogStr(const char *str, int len) { static void virLogStr(const char *str)
{
int tmp; int tmp;
int len;
if ((str == NULL) || (virLogBuffer == NULL) || (virLogSize <= 0)) if ((str == NULL) || (virLogBuffer == NULL) || (virLogSize <= 0))
return; return;
if (len <= 0) len = strlen(str);
len = strlen(str);
if (len >= virLogSize) if (len >= virLogSize)
return; return;
virLogLock();
/* /*
* copy the data and reset the end, we cycle over the end of the buffer * copy the data and reset the end, we cycle over the end of the buffer
...@@ -317,7 +318,6 @@ static void virLogStr(const char *str, int len) { ...@@ -317,7 +318,6 @@ static void virLogStr(const char *str, int len) {
if (virLogStart >= virLogSize) if (virLogStart >= virLogSize)
virLogStart -= virLogSize; virLogStart -= virLogSize;
} }
virLogUnlock();
} }
static void virLogDumpAllFD(const char *msg, int len) { static void virLogDumpAllFD(const char *msg, int len) {
...@@ -618,12 +618,28 @@ cleanup: ...@@ -618,12 +618,28 @@ cleanup:
return ret; return ret;
} }
static char *
virLogFormatTimestamp(void)
{
struct timeval cur_time;
struct tm time_info;
char *str = NULL;
gettimeofday(&cur_time, NULL);
localtime_r(&cur_time.tv_sec, &time_info);
if (virAsprintf(&str, "%02d:%02d:%02d.%03d",
time_info.tm_hour, time_info.tm_min, time_info.tm_sec,
(int) (cur_time.tv_usec / 1000)) < 0)
return NULL;
return str;
}
static int static int
virLogFormatString(char **msg, virLogFormatString(char **msg,
const char *funcname, const char *funcname,
long long linenr, long long linenr,
struct tm *time_info,
struct timeval *cur_time,
int priority, int priority,
const char *str) const char *str)
{ {
...@@ -637,25 +653,19 @@ virLogFormatString(char **msg, ...@@ -637,25 +653,19 @@ virLogFormatString(char **msg,
* to just grep for it to find the right place. * to just grep for it to find the right place.
*/ */
if ((funcname != NULL)) { if ((funcname != NULL)) {
ret = virAsprintf(msg, "%02d:%02d:%02d.%03d: %d: %s : %s:%lld : %s\n", ret = virAsprintf(msg, "%d: %s : %s:%lld : %s\n",
time_info->tm_hour, time_info->tm_min, virThreadSelfID(), virLogPriorityString(priority),
time_info->tm_sec, (int) cur_time->tv_usec / 1000, funcname, linenr, str);
virThreadSelfID(),
virLogPriorityString(priority), funcname, linenr, str);
} else { } else {
ret = virAsprintf(msg, "%02d:%02d:%02d.%03d: %d: %s : %s\n", ret = virAsprintf(msg, "%d: %s : %s\n",
time_info->tm_hour, time_info->tm_min, virThreadSelfID(), virLogPriorityString(priority),
time_info->tm_sec, (int) cur_time->tv_usec / 1000, str);
virThreadSelfID(),
virLogPriorityString(priority), str);
} }
return ret; return ret;
} }
static int static int
virLogVersionString(char **msg, virLogVersionString(char **msg)
struct tm *time_info,
struct timeval *cur_time)
{ {
#ifdef PACKAGER_VERSION #ifdef PACKAGER_VERSION
# ifdef PACKAGER # ifdef PACKAGER
...@@ -670,9 +680,7 @@ virLogVersionString(char **msg, ...@@ -670,9 +680,7 @@ virLogVersionString(char **msg,
"libvirt version: " VERSION "libvirt version: " VERSION
#endif #endif
return virLogFormatString(msg, NULL, 0, return virLogFormatString(msg, NULL, 0, VIR_LOG_INFO, LOG_VERSION_STRING);
time_info, cur_time,
VIR_LOG_INFO, LOG_VERSION_STRING);
} }
/** /**
...@@ -694,9 +702,8 @@ void virLogMessage(const char *category, int priority, const char *funcname, ...@@ -694,9 +702,8 @@ void virLogMessage(const char *category, int priority, const char *funcname,
static bool logVersionStderr = true; static bool logVersionStderr = true;
char *str = NULL; char *str = NULL;
char *msg = NULL; char *msg = NULL;
struct timeval cur_time; char *timestamp = NULL;
struct tm time_info; int fprio, i, ret;
int len, fprio, i, ret;
int saved_errno = errno; int saved_errno = errno;
int emit = 1; int emit = 1;
va_list ap; va_list ap;
...@@ -730,16 +737,15 @@ void virLogMessage(const char *category, int priority, const char *funcname, ...@@ -730,16 +737,15 @@ void virLogMessage(const char *category, int priority, const char *funcname,
goto cleanup; goto cleanup;
} }
va_end(ap); va_end(ap);
gettimeofday(&cur_time, NULL);
localtime_r(&cur_time.tv_sec, &time_info);
ret = virLogFormatString(&msg, funcname, linenr, ret = virLogFormatString(&msg, funcname, linenr, priority, str);
&time_info, &cur_time,
priority, str);
VIR_FREE(str); VIR_FREE(str);
if (ret < 0) if (ret < 0)
goto cleanup; goto cleanup;
if (!(timestamp = virLogFormatTimestamp()))
goto cleanup;
/* /*
* Log based on defaults, first store in the history buffer, * Log based on defaults, first store in the history buffer,
* then if emit push the message on the outputs defined, if none * then if emit push the message on the outputs defined, if none
...@@ -748,42 +754,49 @@ void virLogMessage(const char *category, int priority, const char *funcname, ...@@ -748,42 +754,49 @@ void virLogMessage(const char *category, int priority, const char *funcname,
* threads, but avoid intermixing. Maybe set up locks per output * threads, but avoid intermixing. Maybe set up locks per output
* to improve paralellism. * to improve paralellism.
*/ */
len = strlen(msg); virLogLock();
virLogStr(msg, len); virLogStr(timestamp);
virLogStr(msg);
virLogUnlock();
if (emit == 0) if (emit == 0)
goto cleanup; goto cleanup;
virLogLock(); virLogLock();
for (i = 0; i < virLogNbOutputs;i++) { for (i = 0; i < virLogNbOutputs; i++) {
if (priority >= virLogOutputs[i].priority) { if (priority >= virLogOutputs[i].priority) {
if (virLogOutputs[i].logVersion) { if (virLogOutputs[i].logVersion) {
char *ver = NULL; char *ver = NULL;
if (virLogVersionString(&ver, &time_info, &cur_time) >= 0) if (virLogVersionString(&ver) >= 0)
virLogOutputs[i].f(category, VIR_LOG_INFO, __func__, __LINE__, virLogOutputs[i].f(category, VIR_LOG_INFO,
ver, strlen(ver), __func__, __LINE__,
timestamp, ver,
virLogOutputs[i].data); virLogOutputs[i].data);
VIR_FREE(ver); VIR_FREE(ver);
virLogOutputs[i].logVersion = false; virLogOutputs[i].logVersion = false;
} }
virLogOutputs[i].f(category, priority, funcname, linenr, virLogOutputs[i].f(category, priority, funcname, linenr,
msg, len, virLogOutputs[i].data); timestamp, msg, virLogOutputs[i].data);
} }
} }
if ((virLogNbOutputs == 0) && (flags != 1)) { if ((virLogNbOutputs == 0) && (flags != 1)) {
if (logVersionStderr) { if (logVersionStderr) {
char *ver = NULL; char *ver = NULL;
if (virLogVersionString(&ver, &time_info, &cur_time) >= 0) if (virLogVersionString(&ver) >= 0)
ignore_value (safewrite(STDERR_FILENO, virLogOutputToFd(category, VIR_LOG_INFO,
ver, strlen(ver))); __func__, __LINE__,
timestamp, ver,
(void *) STDERR_FILENO);
VIR_FREE(ver); VIR_FREE(ver);
logVersionStderr = false; logVersionStderr = false;
} }
ignore_value (safewrite(STDERR_FILENO, msg, len)); virLogOutputToFd(category, priority, funcname, linenr,
timestamp, msg, (void *) STDERR_FILENO);
} }
virLogUnlock(); virLogUnlock();
cleanup: cleanup:
VIR_FREE(msg); VIR_FREE(msg);
VIR_FREE(timestamp);
errno = saved_errno; errno = saved_errno;
} }
...@@ -791,13 +804,23 @@ static int virLogOutputToFd(const char *category ATTRIBUTE_UNUSED, ...@@ -791,13 +804,23 @@ static int virLogOutputToFd(const char *category ATTRIBUTE_UNUSED,
int priority ATTRIBUTE_UNUSED, int priority ATTRIBUTE_UNUSED,
const char *funcname ATTRIBUTE_UNUSED, const char *funcname ATTRIBUTE_UNUSED,
long long linenr ATTRIBUTE_UNUSED, long long linenr ATTRIBUTE_UNUSED,
const char *str, int len, void *data) { const char *timestamp,
const char *str,
void *data)
{
int fd = (long) data; int fd = (long) data;
int ret; int ret;
char *msg;
if (fd < 0) if (fd < 0)
return -1; return -1;
ret = safewrite(fd, str, len);
if (virAsprintf(&msg, "%s: %s", timestamp, str) < 0)
return -1;
ret = safewrite(fd, msg, strlen(msg));
VIR_FREE(msg);
return ret; return ret;
} }
...@@ -833,8 +856,10 @@ static int virLogOutputToSyslog(const char *category ATTRIBUTE_UNUSED, ...@@ -833,8 +856,10 @@ static int virLogOutputToSyslog(const char *category ATTRIBUTE_UNUSED,
int priority, int priority,
const char *funcname ATTRIBUTE_UNUSED, const char *funcname ATTRIBUTE_UNUSED,
long long linenr ATTRIBUTE_UNUSED, long long linenr ATTRIBUTE_UNUSED,
const char *str, int len ATTRIBUTE_UNUSED, const char *timestamp ATTRIBUTE_UNUSED,
void *data ATTRIBUTE_UNUSED) { const char *str,
void *data ATTRIBUTE_UNUSED)
{
int prio; int prio;
switch (priority) { switch (priority) {
...@@ -854,7 +879,7 @@ static int virLogOutputToSyslog(const char *category ATTRIBUTE_UNUSED, ...@@ -854,7 +879,7 @@ static int virLogOutputToSyslog(const char *category ATTRIBUTE_UNUSED,
prio = LOG_ERR; prio = LOG_ERR;
} }
syslog(prio, "%s", str); syslog(prio, "%s", str);
return len; return strlen(str);
} }
static char *current_ident = NULL; static char *current_ident = NULL;
......
...@@ -78,8 +78,8 @@ typedef enum { ...@@ -78,8 +78,8 @@ typedef enum {
* @priority: the priority for the message * @priority: the priority for the message
* @funcname: the function emitting the message * @funcname: the function emitting the message
* @linenr: line where the message was emitted * @linenr: line where the message was emitted
* @msg: the message to log, preformatted and zero terminated * @timestamp: zero terminated string with timestamp of the message
* @len: the lenght of the message in bytes without the terminating zero * @str: the message to log, preformatted and zero terminated
* @data: extra output logging data * @data: extra output logging data
* *
* Callback function used to output messages * Callback function used to output messages
...@@ -87,8 +87,9 @@ typedef enum { ...@@ -87,8 +87,9 @@ typedef enum {
* Returns the number of bytes written or -1 in case of error * Returns the number of bytes written or -1 in case of error
*/ */
typedef int (*virLogOutputFunc) (const char *category, int priority, typedef int (*virLogOutputFunc) (const char *category, int priority,
const char *funcname, long long lineno, const char *funcname, long long linenr,
const char *str, int len, void *data); const char *timestamp, const char *str,
void *data);
/** /**
* virLogCloseFunc: * virLogCloseFunc:
......
...@@ -458,11 +458,13 @@ virtTestLogOutput(const char *category ATTRIBUTE_UNUSED, ...@@ -458,11 +458,13 @@ virtTestLogOutput(const char *category ATTRIBUTE_UNUSED,
int priority ATTRIBUTE_UNUSED, int priority ATTRIBUTE_UNUSED,
const char *funcname ATTRIBUTE_UNUSED, const char *funcname ATTRIBUTE_UNUSED,
long long lineno ATTRIBUTE_UNUSED, long long lineno ATTRIBUTE_UNUSED,
const char *str, int len, void *data) const char *timestamp,
const char *str,
void *data)
{ {
struct virtTestLogData *log = data; struct virtTestLogData *log = data;
virBufferAdd(&log->buf, str, len); virBufferAsprintf(&log->buf, "%s: %s", timestamp, str);
return len; return strlen(timestamp) + 2 + strlen(str);
} }
static void static void
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册