Ton

Форк
0
/
errorlog.cpp 
74 строки · 2.1 Кб
1
/*
2
    This file is part of TON Blockchain Library.
3

4
    TON Blockchain Library is free software: you can redistribute it and/or modify
5
    it under the terms of the GNU Lesser General Public License as published by
6
    the Free Software Foundation, either version 2 of the License, or
7
    (at your option) any later version.
8

9
    TON Blockchain Library is distributed in the hope that it will be useful,
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    GNU Lesser General Public License for more details.
13

14
    You should have received a copy of the GNU Lesser General Public License
15
    along with TON Blockchain Library.  If not, see <http://www.gnu.org/licenses/>.
16

17
    Copyright 2017-2020 Telegram Systems LLP
18
*/
19
#include "errorlog.h"
20
#include "checksum.h"
21

22
#include "td/utils/port/FileFd.h"
23
#include "td/utils/filesystem.h"
24
#include "td/utils/port/path.h"
25
#include "td/utils/Time.h"
26

27
#include <mutex>
28

29
namespace ton {
30

31
namespace errorlog {
32

33
td::FileFd fd;
34
std::mutex init_mutex_;
35
std::string files_path_;
36

37
void ErrorLog::create(std::string db_root) {
38
  init_mutex_.lock();
39
  if (!fd.empty()) {
40
    init_mutex_.unlock();
41
    return;
42
  }
43
  auto path = db_root + "/error";
44
  td::mkdir(path).ensure();
45
  files_path_ = path + "/files";
46
  td::mkdir(files_path_).ensure();
47
  auto R = td::FileFd::open(path + "/log.txt",
48
                            td::FileFd::Flags::Write | td::FileFd::Flags::Append | td::FileFd::Flags::Create);
49
  R.ensure();
50
  fd = R.move_as_ok();
51
  init_mutex_.unlock();
52
}
53

54
void ErrorLog::log(std::string error) {
55
  error = PSTRING() << "[" << td::Clocks::system() << "] " << error << "\n";
56
  CHECK(!fd.empty());
57
  auto s = td::Slice{error};
58
  while (s.size() > 0) {
59
    auto R = fd.write(s);
60
    R.ensure();
61
    s.remove_prefix(R.move_as_ok());
62
  }
63
}
64

65
void ErrorLog::log_file(td::BufferSlice data) {
66
  auto filename = sha256_bits256(data.as_slice());
67
  auto path = files_path_ + "/" + filename.to_hex();
68

69
  td::write_file(path, data.as_slice()).ensure();
70
}
71

72
}  // namespace errorlog
73

74
}  // namespace ton
75

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.