Remove dependency of PSLICE/PSTRING on Logger, move them to separate header and make them always safe.

This commit is contained in:
levlam 2021-05-17 03:24:54 +03:00
parent 29230948c3
commit 17a1015f6b
5 changed files with 69 additions and 56 deletions

View File

@ -66,9 +66,9 @@ Result<std::pair<FileFd, string>> open_temp_file(FileType file_type) {
pmc->set("tmp_file_id", to_string(file_id + 1));
auto temp_dir = get_files_temp_dir(file_type);
auto res = try_create_new_file(PSLICE_SAFE() << temp_dir << file_id);
auto res = try_create_new_file(PSLICE() << temp_dir << file_id);
if (res.is_error()) {
res = try_create_new_file(PSLICE_SAFE() << temp_dir << file_id << "_" << RandSuff{6});
res = try_create_new_file(PSLICE() << temp_dir << file_id << "_" << RandSuff{6});
}
return res;
}
@ -88,20 +88,20 @@ bool for_suggested_file_name(CSlice name, bool use_pmc, bool use_random, F &&cal
auto ext = path_view.extension();
bool active = true;
if (!stem.empty() && !G()->parameters().ignore_file_names) {
active = try_callback(PSLICE_SAFE() << stem << Ext{ext});
active = try_callback(PSLICE() << stem << Ext{ext});
for (int i = 0; active && i < 10; i++) {
active = try_callback(PSLICE_SAFE() << stem << "_(" << i << ")" << Ext{ext});
active = try_callback(PSLICE() << stem << "_(" << i << ")" << Ext{ext});
}
for (int i = 2; active && i < 12 && use_random; i++) {
active = try_callback(PSLICE_SAFE() << stem << "_(" << RandSuff{i} << ")" << Ext{ext});
active = try_callback(PSLICE() << stem << "_(" << RandSuff{i} << ")" << Ext{ext});
}
} else if (use_pmc) {
auto pmc = G()->td_db()->get_binlog_pmc();
int32 file_id = to_integer<int32>(pmc->get("perm_file_id"));
pmc->set("perm_file_id", to_string(file_id + 1));
active = try_callback(PSLICE_SAFE() << "file_" << file_id << Ext{ext});
active = try_callback(PSLICE() << "file_" << file_id << Ext{ext});
if (active) {
active = try_callback(PSLICE_SAFE() << "file_" << file_id << "_" << RandSuff{6} << Ext{ext});
active = try_callback(PSLICE() << "file_" << file_id << "_" << RandSuff{6} << Ext{ext});
}
}
return active;
@ -112,7 +112,7 @@ Result<string> create_from_temp(CSlice temp_path, CSlice dir, CSlice name) {
<< temp_path;
Result<std::pair<FileFd, string>> res = Status::Error(500, "Can't find suitable file name");
for_suggested_file_name(name, true, true, [&](CSlice suggested_name) {
res = try_create_new_file(PSLICE_SAFE() << dir << suggested_name);
res = try_create_new_file(PSLICE() << dir << suggested_name);
return res.is_error();
});
TRY_RESULT(tmp, std::move(res));
@ -125,7 +125,7 @@ Result<string> create_from_temp(CSlice temp_path, CSlice dir, CSlice name) {
Result<string> search_file(CSlice dir, CSlice name, int64 expected_size) {
Result<std::string> res = Status::Error(500, "Can't find suitable file name");
for_suggested_file_name(name, false, false, [&](CSlice suggested_name) {
auto r_pair = try_open_file(PSLICE_SAFE() << dir << suggested_name);
auto r_pair = try_open_file(PSLICE() << dir << suggested_name);
if (r_pair.is_error()) {
return false;
}

View File

@ -235,6 +235,7 @@ set(TDUTILS_SOURCE
td/utils/SharedSlice.h
td/utils/Slice-decl.h
td/utils/Slice.h
td/utils/SliceBuilder.h
td/utils/Span.h
td/utils/SpinLock.h
td/utils/StackAllocator.h

View File

@ -0,0 +1,57 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
//
// 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/Slice.h"
#include "td/utils/StackAllocator.h"
#include "td/utils/StringBuilder.h"
#define PSLICE() ::td::detail::Slicify() & ::td::SliceBuilder().ref()
#define PSTRING() ::td::detail::Stringify() & ::td::SliceBuilder().ref()
namespace td {
class SliceBuilder {
public:
template <class T>
SliceBuilder &operator<<(T &&other) {
sb_ << other;
return *this;
}
MutableCSlice as_cslice() {
return sb_.as_cslice();
}
SliceBuilder &ref() {
return *this;
}
private:
static const size_t DEFAULT_BUFFER_SIZE = 1024;
decltype(StackAllocator::alloc(0)) buffer_ = StackAllocator::alloc(DEFAULT_BUFFER_SIZE);
StringBuilder sb_ = StringBuilder(buffer_.as_slice(), true);
};
namespace detail {
class Slicify {
public:
CSlice operator&(SliceBuilder &slice_builder) {
return slice_builder.as_cslice();
}
};
class Stringify {
public:
string operator&(SliceBuilder &slice_builder) {
return slice_builder.as_cslice().str();
}
};
} // namespace detail
} // namespace td

View File

@ -599,27 +599,4 @@ inline StringBuilder &operator<<(StringBuilder &string_builder, const Status &st
return status.print(string_builder);
}
namespace detail {
class SlicifySafe {
public:
Result<CSlice> operator&(Logger &logger) {
if (logger.is_error()) {
return Status::Error("Buffer overflow");
}
return logger.as_cslice();
}
};
class StringifySafe {
public:
Result<string> operator&(Logger &logger) {
if (logger.is_error()) {
return Status::Error("Buffer overflow");
}
return logger.as_cslice().str();
}
};
} // namespace detail
} // namespace td

View File

@ -26,18 +26,13 @@
#include "td/utils/common.h"
#include "td/utils/port/thread_local.h"
#include "td/utils/Slice.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/StackAllocator.h"
#include "td/utils/StringBuilder.h"
#include <atomic>
#include <type_traits>
#define PSTR_IMPL() ::td::Logger(::td::NullLog().ref(), ::td::LogOptions::plain(), 0)
#define PSLICE() ::td::detail::Slicify() & PSTR_IMPL()
#define PSTRING() ::td::detail::Stringify() & PSTR_IMPL()
#define PSLICE_SAFE() ::td::detail::SlicifySafe() & PSTR_IMPL()
#define PSTRING_SAFE() ::td::detail::StringifySafe() & PSTR_IMPL()
#define VERBOSITY_NAME(x) verbosity_##x
#define GET_VERBOSITY_LEVEL() (::td::get_verbosity_level())
@ -187,9 +182,6 @@ class NullLog : public LogInterface {
public:
void append(CSlice /*slice*/, int /*log_level*/) override {
}
NullLog &ref() {
return *this;
}
};
extern LogInterface *const default_log_interface;
@ -227,7 +219,7 @@ class TsCerr {
class Logger {
public:
static const int BUFFER_SIZE = 128 * 1024;
static const size_t BUFFER_SIZE = 128 * 1024;
Logger(LogInterface &log, const LogOptions &options, int log_level)
: buffer_(StackAllocator::alloc(BUFFER_SIZE))
, log_(log)
@ -274,20 +266,6 @@ class Voidify {
void operator&(const T &) {
}
};
class Slicify {
public:
CSlice operator&(Logger &logger) {
return logger.as_cslice();
}
};
class Stringify {
public:
string operator&(Logger &logger) {
return logger.as_cslice().str();
}
};
} // namespace detail
class TsLog : public LogInterface {