2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/port/FileFd.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2018-10-24 17:42:40 +02:00
|
|
|
#include "td/utils/Status.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-06-26 01:24:13 +02:00
|
|
|
#include <atomic>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileLog final : public LogInterface {
|
2018-01-28 15:33:14 +01:00
|
|
|
static constexpr int64 DEFAULT_ROTATE_THRESHOLD = 10 * (1 << 20);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
2020-06-26 01:24:13 +02:00
|
|
|
static Result<unique_ptr<LogInterface>> create(string path, int64 rotate_threshold = DEFAULT_ROTATE_THRESHOLD,
|
|
|
|
bool redirect_stderr = true);
|
2019-08-12 13:45:57 +02:00
|
|
|
Status init(string path, int64 rotate_threshold = DEFAULT_ROTATE_THRESHOLD, bool redirect_stderr = true);
|
2018-10-24 17:42:40 +02:00
|
|
|
|
|
|
|
Slice get_path() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-05-17 15:18:19 +02:00
|
|
|
vector<string> get_file_paths() final;
|
2019-01-17 23:17:20 +01:00
|
|
|
|
2018-01-28 15:33:14 +01:00
|
|
|
void set_rotate_threshold(int64 rotate_threshold);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-10-24 17:42:40 +02:00
|
|
|
int64 get_rotate_threshold() const;
|
|
|
|
|
2020-09-27 13:37:35 +02:00
|
|
|
bool get_redirect_stderr() const;
|
|
|
|
|
2021-05-17 16:58:33 +02:00
|
|
|
void after_rotation() final;
|
2018-01-09 10:40:56 +01:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
void lazy_rotate();
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
FileFd fd_;
|
|
|
|
string path_;
|
2018-10-26 16:11:20 +02:00
|
|
|
int64 size_ = 0;
|
|
|
|
int64 rotate_threshold_ = 0;
|
2019-08-13 22:52:54 +02:00
|
|
|
bool redirect_stderr_ = false;
|
2020-06-26 01:24:13 +02:00
|
|
|
std::atomic<bool> want_rotate_{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-05-17 15:18:19 +02:00
|
|
|
void do_append(int log_level, CSlice slice) final;
|
|
|
|
|
2021-05-17 16:58:33 +02:00
|
|
|
void do_after_rotation();
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-01-28 15:33:14 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|