fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.4 KiB
94 lines
2.4 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
8 years ago
|
// This source code is licensed under both the GPLv2 (found in the
|
||
|
// COPYING file in the root directory) and Apache 2.0 License
|
||
|
// (found in the LICENSE.Apache file in the root directory).
|
||
11 years ago
|
|
||
6 years ago
|
#include "logging/log_buffer.h"
|
||
11 years ago
|
|
||
10 years ago
|
#include "port/sys_time.h"
|
||
|
#include "port/port.h"
|
||
11 years ago
|
|
||
|
namespace rocksdb {
|
||
|
|
||
|
LogBuffer::LogBuffer(const InfoLogLevel log_level,
|
||
|
Logger*info_log)
|
||
|
: log_level_(log_level), info_log_(info_log) {}
|
||
|
|
||
10 years ago
|
void LogBuffer::AddLogToBuffer(size_t max_log_size, const char* format,
|
||
|
va_list ap) {
|
||
11 years ago
|
if (!info_log_ || log_level_ < info_log_->GetInfoLogLevel()) {
|
||
|
// Skip the level because of its level.
|
||
|
return;
|
||
|
}
|
||
|
|
||
10 years ago
|
char* alloc_mem = arena_.AllocateAligned(max_log_size);
|
||
11 years ago
|
BufferedLog* buffered_log = new (alloc_mem) BufferedLog();
|
||
|
char* p = buffered_log->message;
|
||
10 years ago
|
char* limit = alloc_mem + max_log_size - 1;
|
||
11 years ago
|
|
||
|
// store the time
|
||
|
gettimeofday(&(buffered_log->now_tv), nullptr);
|
||
|
|
||
|
// Print the message
|
||
|
if (p < limit) {
|
||
|
va_list backup_ap;
|
||
|
va_copy(backup_ap, ap);
|
||
11 years ago
|
auto n = vsnprintf(p, limit - p, format, backup_ap);
|
||
10 years ago
|
#ifndef OS_WIN
|
||
|
// MS reports -1 when the buffer is too short
|
||
11 years ago
|
assert(n >= 0);
|
||
10 years ago
|
#endif
|
||
|
if (n > 0) {
|
||
|
p += n;
|
||
|
} else {
|
||
|
p = limit;
|
||
|
}
|
||
11 years ago
|
va_end(backup_ap);
|
||
|
}
|
||
|
|
||
11 years ago
|
if (p > limit) {
|
||
|
p = limit;
|
||
|
}
|
||
|
|
||
11 years ago
|
// Add '\0' to the end
|
||
|
*p = '\0';
|
||
|
|
||
|
logs_.push_back(buffered_log);
|
||
|
}
|
||
|
|
||
|
void LogBuffer::FlushBufferToLog() {
|
||
|
for (BufferedLog* log : logs_) {
|
||
|
const time_t seconds = log->now_tv.tv_sec;
|
||
|
struct tm t;
|
||
8 years ago
|
if (localtime_r(&seconds, &t) != nullptr) {
|
||
|
Log(log_level_, info_log_,
|
||
|
"(Original Log Time %04d/%02d/%02d-%02d:%02d:%02d.%06d) %s",
|
||
|
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min,
|
||
|
t.tm_sec, static_cast<int>(log->now_tv.tv_usec), log->message);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
logs_.clear();
|
||
|
}
|
||
|
|
||
10 years ago
|
void LogToBuffer(LogBuffer* log_buffer, size_t max_log_size, const char* format,
|
||
|
...) {
|
||
|
if (log_buffer != nullptr) {
|
||
|
va_list ap;
|
||
|
va_start(ap, format);
|
||
|
log_buffer->AddLogToBuffer(max_log_size, format, ap);
|
||
|
va_end(ap);
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
void LogToBuffer(LogBuffer* log_buffer, const char* format, ...) {
|
||
10 years ago
|
const size_t kDefaultMaxLogSize = 512;
|
||
11 years ago
|
if (log_buffer != nullptr) {
|
||
|
va_list ap;
|
||
|
va_start(ap, format);
|
||
10 years ago
|
log_buffer->AddLogToBuffer(kDefaultMaxLogSize, format, ap);
|
||
11 years ago
|
va_end(ap);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
} // namespace rocksdb
|