aboutsummaryrefslogtreecommitdiffhomepage
path: root/tdutils/td/utils/TsFileLog.cpp
blob: fb0ac7cdbe04f72990a1904f14ba28c167883637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2025
//
// 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/TsFileLog.h"

#include "td/utils/common.h"
#include "td/utils/FileLog.h"
#include "td/utils/logging.h"
#include "td/utils/port/thread_local.h"
#include "td/utils/Slice.h"
#include "td/utils/SliceBuilder.h"

#include <array>
#include <atomic>
#include <limits>
#include <mutex>

namespace td {

namespace detail {
class TsFileLog final : public LogInterface {
 public:
  Status init(string path, int64 rotate_threshold, bool redirect_stderr) {
    path_ = std::move(path);
    rotate_threshold_ = rotate_threshold;
    redirect_stderr_ = redirect_stderr;
    for (size_t i = 0; i < logs_.size(); i++) {
      logs_[i].id = i;
    }
    return init_info(&logs_[0]);
  }

  void rotate() {
    for (auto &info : logs_) {
      if (info.is_inited.load(std::memory_order_acquire)) {
        info.log.lazy_rotate();
      }
    }
  }

 private:
  struct Info {
    FileLog log;
    std::atomic<bool> is_inited{false};
    size_t id;
  };

  static constexpr size_t MAX_THREAD_ID = 128;
  int64 rotate_threshold_ = 0;
  bool redirect_stderr_ = false;
  std::string path_;
  std::array<Info, MAX_THREAD_ID> logs_;
  std::mutex init_mutex_;

  LogInterface *get_current_logger() {
    auto *info = get_current_info();
    if (!info->is_inited.load(std::memory_order_relaxed)) {
      std::unique_lock<std::mutex> lock(init_mutex_);
      if (!info->is_inited.load(std::memory_order_relaxed)) {
        init_info(info).ensure();
      }
    }
    return &info->log;
  }

  Info *get_current_info() {
    return &logs_[get_thread_id()];
  }

  Status init_info(Info *info) {
    TRY_STATUS(info->log.init(get_path(info), std::numeric_limits<int64>::max(), info->id == 0 && redirect_stderr_));
    info->is_inited = true;
    return Status::OK();
  }

  string get_path(const Info *info) const {
    if (info->id == 0) {
      return path_;
    }
    return PSTRING() << path_ << ".thread" << info->id << ".log";
  }

  void do_append(int log_level, CSlice slice) final {
    get_current_logger()->do_append(log_level, slice);
  }

  vector<string> get_file_paths() final {
    vector<string> res;
    for (auto &log : logs_) {
      res.push_back(get_path(&log));
    }
    return res;
  }
};
}  // namespace detail

Result<unique_ptr<LogInterface>> TsFileLog::create(string path, int64 rotate_threshold, bool redirect_stderr) {
  auto res = make_unique<detail::TsFileLog>();
  TRY_STATUS(res->init(std::move(path), rotate_threshold, redirect_stderr));
  return std::move(res);
}

}  // namespace td