2018-01-28 15:33:14 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-01-28 15:33:14 +01:00
|
|
|
//
|
|
|
|
// 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/FileFd.h"
|
|
|
|
#include "td/utils/port/path.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/port/StdStreams.h"
|
2018-01-28 15:33:14 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2018-10-24 17:42:40 +02:00
|
|
|
Status FileLog::init(string path, int64 rotate_threshold) {
|
2018-01-28 15:48:11 +01:00
|
|
|
if (path == path_) {
|
|
|
|
set_rotate_threshold(rotate_threshold);
|
2018-10-24 17:42:40 +02:00
|
|
|
return Status::OK();
|
2018-01-28 15:48:11 +01:00
|
|
|
}
|
2019-01-17 23:17:20 +01:00
|
|
|
if (path.empty()) {
|
|
|
|
return Status::Error("Log file path can't be empty");
|
|
|
|
}
|
2018-01-28 15:48:11 +01:00
|
|
|
|
2018-10-24 17:42:40 +02:00
|
|
|
TRY_RESULT(fd, FileFd::open(path, FileFd::Create | FileFd::Write | FileFd::Append));
|
2018-01-28 15:33:14 +01:00
|
|
|
|
2018-01-28 15:48:11 +01:00
|
|
|
fd_.close();
|
2018-10-24 17:42:40 +02:00
|
|
|
fd_ = std::move(fd);
|
2018-09-13 13:26:22 +02:00
|
|
|
if (!Stderr().empty()) {
|
|
|
|
fd_.get_native_fd().duplicate(Stderr().get_native_fd()).ignore();
|
|
|
|
}
|
2018-01-28 15:48:11 +01:00
|
|
|
|
2019-01-17 23:17:20 +01:00
|
|
|
auto r_path = realpath(path, true);
|
|
|
|
if (r_path.is_error()) {
|
|
|
|
path_ = std::move(path);
|
|
|
|
} else {
|
|
|
|
path_ = r_path.move_as_ok();
|
|
|
|
}
|
2018-01-28 15:33:14 +01:00
|
|
|
size_ = fd_.get_size();
|
|
|
|
rotate_threshold_ = rotate_threshold;
|
2018-10-24 17:42:40 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice FileLog::get_path() const {
|
|
|
|
return path_;
|
2018-01-28 15:33:14 +01:00
|
|
|
}
|
|
|
|
|
2019-01-17 23:17:20 +01:00
|
|
|
vector<string> FileLog::get_file_paths() {
|
|
|
|
vector<string> result;
|
|
|
|
if (!path_.empty()) {
|
|
|
|
result.push_back(path_);
|
|
|
|
result.push_back(PSTRING() << path_ << ".old");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-28 15:33:14 +01:00
|
|
|
void FileLog::set_rotate_threshold(int64 rotate_threshold) {
|
|
|
|
rotate_threshold_ = rotate_threshold;
|
|
|
|
}
|
|
|
|
|
2018-10-24 17:42:40 +02:00
|
|
|
int64 FileLog::get_rotate_threshold() const {
|
|
|
|
return rotate_threshold_;
|
|
|
|
}
|
|
|
|
|
2018-01-28 15:33:14 +01:00
|
|
|
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()) {
|
2019-03-25 23:48:50 +01:00
|
|
|
process_fatal_error(PSLICE() << r_size.error() << " in " << __FILE__ << " at " << __LINE__);
|
2018-01-28 15:33:14 +01:00
|
|
|
}
|
|
|
|
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_) {
|
2018-07-08 01:47:46 +02:00
|
|
|
auto status = rename(path_, PSLICE() << path_ << ".old");
|
2018-01-28 15:33:14 +01:00
|
|
|
if (status.is_error()) {
|
2019-03-25 23:48:50 +01:00
|
|
|
process_fatal_error(PSLICE() << status.error() << " in " << __FILE__ << " at " << __LINE__);
|
2018-01-28 15:33:14 +01:00
|
|
|
}
|
|
|
|
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()) {
|
2019-03-25 23:48:50 +01:00
|
|
|
process_fatal_error(PSLICE() << r_fd.error() << " in " << __FILE__ << " at " << __LINE__);
|
2018-01-28 15:33:14 +01:00
|
|
|
}
|
|
|
|
fd_ = r_fd.move_as_ok();
|
2018-09-13 13:26:22 +02:00
|
|
|
if (!Stderr().empty()) {
|
|
|
|
fd_.get_native_fd().duplicate(Stderr().get_native_fd()).ignore();
|
|
|
|
}
|
2018-01-28 15:33:14 +01:00
|
|
|
size_ = 0;
|
|
|
|
SET_VERBOSITY_LEVEL(current_verbosity_level);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|