Move FileLog implementation to cpp.

GitOrigin-RevId: 3722bed5a7e480e7336d35489dbacc31a91a9428
This commit is contained in:
levlam 2018-01-28 17:33:14 +03:00
parent d8f4e97747
commit 6d21c7c912
3 changed files with 90 additions and 63 deletions

View File

@ -59,6 +59,7 @@ set(TDUTILS_SOURCE
td/utils/BigNum.cpp
td/utils/buffer.cpp
td/utils/crypto.cpp
td/utils/FileLog.cpp
td/utils/filesystem.cpp
td/utils/find_boundary.cpp
td/utils/Gzip.cpp

View File

@ -0,0 +1,82 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/utils/FileLog.h"
#include "td/utils/common.h"
#include "td/utils/logging.h"
#include "td/utils/port/Fd.h"
#include "td/utils/port/FileFd.h"
#include "td/utils/port/path.h"
#include "td/utils/Slice.h"
#include <limits>
namespace td {
void FileLog::init(string path, int64 rotate_threshold) {
fd_.close();
path_ = std::move(path);
auto r_fd = FileFd::open(path_, FileFd::Create | FileFd::Write | FileFd::Append);
LOG_IF(FATAL, r_fd.is_error()) << "Can't open log: " << r_fd.error();
fd_ = r_fd.move_as_ok();
Fd::duplicate(fd_.get_fd(), Fd::Stderr()).ignore();
size_ = fd_.get_size();
rotate_threshold_ = rotate_threshold;
}
void FileLog::set_rotate_threshold(int64 rotate_threshold) {
rotate_threshold_ = rotate_threshold;
}
void FileLog::append(CSlice cslice, int log_level) {
Slice slice = cslice;
while (!slice.empty()) {
auto r_size = fd_.write(slice);
if (r_size.is_error()) {
process_fatal_error(r_size.error().message());
}
auto written = r_size.ok();
size_ += static_cast<int64>(written);
slice.remove_prefix(written);
}
if (log_level == VERBOSITY_NAME(FATAL)) {
process_fatal_error(cslice);
}
if (size_ > rotate_threshold_) {
auto status = rename(path_, path_ + ".old");
if (status.is_error()) {
process_fatal_error(status.message());
}
do_rotate();
}
}
void FileLog::rotate() {
if (path_.empty()) {
return;
}
do_rotate();
}
void FileLog::do_rotate() {
auto current_verbosity_level = GET_VERBOSITY_LEVEL();
SET_VERBOSITY_LEVEL(std::numeric_limits<int>::min()); // to ensure that nothing will be printed to the closed log
CHECK(!path_.empty());
fd_.close();
auto r_fd = FileFd::open(path_, FileFd::Create | FileFd::Truncate | FileFd::Write);
if (r_fd.is_error()) {
process_fatal_error(r_fd.error().message());
}
fd_ = r_fd.move_as_ok();
Fd::duplicate(fd_.get_fd(), Fd::Stderr()).ignore();
size_ = 0;
SET_VERBOSITY_LEVEL(current_verbosity_level);
}
} // namespace td

View File

@ -8,66 +8,22 @@
#include "td/utils/common.h"
#include "td/utils/logging.h"
#include "td/utils/port/Fd.h"
#include "td/utils/port/FileFd.h"
#include "td/utils/port/path.h"
#include "td/utils/Slice.h"
#include <limits>
namespace td {
// TODO move implementation to cpp
class FileLog : public LogInterface {
static constexpr int DEFAULT_ROTATE_THRESHOLD = 10 * (1 << 20);
static constexpr int64 DEFAULT_ROTATE_THRESHOLD = 10 * (1 << 20);
public:
void append(CSlice cslice, int log_level) override {
Slice slice = cslice;
while (!slice.empty()) {
auto r_size = fd_.write(slice);
if (r_size.is_error()) {
process_fatal_error(r_size.error().message());
}
auto written = r_size.ok();
size_ += static_cast<int64>(written);
slice.remove_prefix(written);
}
if (log_level == VERBOSITY_NAME(FATAL)) {
process_fatal_error(cslice);
}
void init(string path, int64 rotate_threshold = DEFAULT_ROTATE_THRESHOLD);
if (size_ > rotate_threshold_) {
auto status = rename(path_, path_ + ".old");
if (status.is_error()) {
process_fatal_error(status.message());
}
do_rotate();
}
}
void set_rotate_threshold(int64 rotate_threshold);
void rotate() override {
if (path_.empty()) {
return;
}
do_rotate();
}
void append(CSlice cslice, int log_level) override;
void init(string path, int64 rotate_threshold = DEFAULT_ROTATE_THRESHOLD) {
fd_.close();
path_ = std::move(path);
auto r_fd = FileFd::open(path_, FileFd::Create | FileFd::Write | FileFd::Append);
LOG_IF(FATAL, r_fd.is_error()) << "Can't open log: " << r_fd.error();
fd_ = r_fd.move_as_ok();
Fd::duplicate(fd_.get_fd(), Fd::Stderr()).ignore();
size_ = fd_.get_size();
rotate_threshold_ = rotate_threshold;
}
void set_rotate_threshold(int64 rotate_threshold) {
rotate_threshold_ = rotate_threshold;
}
void rotate() override;
private:
FileFd fd_;
@ -75,19 +31,7 @@ class FileLog : public LogInterface {
int64 size_;
int64 rotate_threshold_;
void do_rotate() {
auto current_verbosity_level = GET_VERBOSITY_LEVEL();
SET_VERBOSITY_LEVEL(std::numeric_limits<int>::min()); // to ensure that nothing will be printed to the closed log
CHECK(!path_.empty());
fd_.close();
auto r_fd = FileFd::open(path_, FileFd::Create | FileFd::Truncate | FileFd::Write);
if (r_fd.is_error()) {
process_fatal_error(r_fd.error().message());
}
fd_ = r_fd.move_as_ok();
Fd::duplicate(fd_.get_fd(), Fd::Stderr()).ignore();
size_ = 0;
SET_VERBOSITY_LEVEL(current_verbosity_level);
}
void do_rotate();
};
} // namespace td