diff --git a/tdutils/CMakeLists.txt b/tdutils/CMakeLists.txt index f72aa40b..ce58be57 100644 --- a/tdutils/CMakeLists.txt +++ b/tdutils/CMakeLists.txt @@ -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 diff --git a/tdutils/td/utils/FileLog.cpp b/tdutils/td/utils/FileLog.cpp new file mode 100644 index 00000000..7732b150 --- /dev/null +++ b/tdutils/td/utils/FileLog.cpp @@ -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 + +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(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::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 diff --git a/tdutils/td/utils/FileLog.h b/tdutils/td/utils/FileLog.h index 7ca4249d..2114448d 100644 --- a/tdutils/td/utils/FileLog.h +++ b/tdutils/td/utils/FileLog.h @@ -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 - 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(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::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