Skip to content

Commit

Permalink
Bump spdlog version to v1.14.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed May 1, 2024
1 parent b184a6d commit 3a424b2
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 88 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ else()
message(NOTICE "No data directory found. Set it with CCT_DATA_DIR or if you use Docker mount it at start of the container")
endif()

# Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24:
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif()

# openssl for requests
find_package(OpenSSL REQUIRED)

Expand Down Expand Up @@ -139,8 +144,8 @@ find_package(spdlog CONFIG)
if(NOT spdlog_FOUND)
FetchContent_Declare(
spdlog
URL https://github.com/gabime/spdlog/archive/refs/tags/v1.13.0.tar.gz
URL_HASH SHA256=534f2ee1a4dcbeb22249856edfb2be76a1cf4f708a20b0ac2ed090ee24cfdbc9
URL https://github.com/gabime/spdlog/archive/refs/tags/v1.14.1.tar.gz
URL_HASH SHA256=1586508029a7d0670dfcb2d97575dcdc242d3868a259742b69f100801ab4e16b
)

list(APPEND fetchContentPackagesToMakeAvailable spdlog)
Expand Down
5 changes: 1 addition & 4 deletions src/api/interface/test/exchangeretriever_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ namespace cct {
class ExchangeRetrieverTest : public ::testing::Test {
protected:
LoadConfiguration loadConfiguration{kDefaultDataDir, LoadConfiguration::ExchangeConfigFileType::kTest};
CoincenterInfo coincenterInfo{settings::RunMode::kTestKeys, loadConfiguration};
ExchangeConfigMap exchangeConfigMap{
ComputeExchangeConfigMap(loadConfiguration.exchangeConfigFileName(), LoadExchangeConfigData(loadConfiguration))};

CoincenterInfo coincenterInfo{settings::RunMode::kTestKeys, loadConfiguration};
api::CommonAPI commonAPI{coincenterInfo, Duration::max()};
FiatConverter fiatConverter{coincenterInfo, Duration::max(), Reader()}; // max to avoid real Fiat converter queries

Expand All @@ -45,9 +45,6 @@ class ExchangeRetrieverTest : public ::testing::Test {
api::MockExchangePublic exchangePublic3{"kucoin", fiatConverter, commonAPI, coincenterInfo};
api::APIKey key1{"test1", "user1", "", "", ""};
api::APIKey key2{"test2", "user2", "", "", ""};
api::APIKey key3{"test3", "user3", "", "", ""};
api::APIKey key4{"test4", "user4", "", "", ""};
api::APIKey key5{"test5", "user5", "", "", ""};
Exchange exchange1{coincenterInfo.exchangeConfig(exchangePublic1.name()), exchangePublic1,
std::make_unique<api::MockExchangePrivate>(exchangePublic1, coincenterInfo, key1)};
Exchange exchange2{coincenterInfo.exchangeConfig(exchangePublic2.name()), exchangePublic2,
Expand Down
3 changes: 2 additions & 1 deletion src/engine/src/coincenterinfo_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ MonitoringInfo MonitoringInfo_Create(std::string_view programName, const Coincen
CoincenterInfo CoincenterInfo_Create(std::string_view programName, const CoincenterCmdLineOptions &cmdLineOptions,
settings::RunMode runMode) {
const auto dataDir = cmdLineOptions.getDataDir();
LoggingInfo loggingInfo(LoggingInfo::WithLoggersCreation::kNo, dataDir);

const json generalConfigData = LoadGeneralConfigAndOverrideOptionsFromCLI(cmdLineOptions);

Expand All @@ -64,7 +65,7 @@ CoincenterInfo CoincenterInfo_Create(std::string_view programName, const Coincen
// Create LoggingInfo first as it is a RAII structure re-initializing spdlog loggers.
// It will be held by GeneralConfig and then itself by CoincenterInfo though.
const auto &logConfigJsonPart = static_cast<const json &>(generalConfigData.at("log"));
LoggingInfo loggingInfo(LoggingInfo::WithLoggersCreation::kYes, dataDir, logConfigJsonPart);
loggingInfo = LoggingInfo(LoggingInfo::WithLoggersCreation::kYes, dataDir, logConfigJsonPart);

RequestsConfig requestsConfig(
generalConfigData.at("requests").at("concurrency").at("nbMaxParallelRequests").get<int>());
Expand Down
3 changes: 2 additions & 1 deletion src/objects/include/coincenterinfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "monitoringinfo.hpp"
#include "reader.hpp"
#include "runmodes.hpp"
#include "timedef.hpp"

namespace cct {

Expand All @@ -37,7 +38,7 @@ class CoincenterInfo {
~CoincenterInfo();

/// Sometimes, XBT is used instead of BTC for Bitcoin.
/// Use this function to standardize names
/// Use this method to standardize names
CurrencyCode standardizeCurrencyCode(CurrencyCode currencyCode) const;
CurrencyCode standardizeCurrencyCode(std::string_view currencyCode) const;
CurrencyCode standardizeCurrencyCode(const char *currencyCode) const {
Expand Down
15 changes: 9 additions & 6 deletions src/objects/include/logginginfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ class LoggingInfo {
enum class WithLoggersCreation : int8_t { kNo, kYes };

/// Creates a default logging info, with level 'info' on standard output.
explicit LoggingInfo(WithLoggersCreation withLoggersCreation, std::string_view dataDir = kDefaultDataDir);
explicit LoggingInfo(WithLoggersCreation withLoggersCreation = WithLoggersCreation::kNo,
std::string_view dataDir = kDefaultDataDir);

/// Creates a logging info from general config json file.
LoggingInfo(WithLoggersCreation withLoggersCreation, std::string_view dataDir, const json &generalConfigJsonLogPart);

LoggingInfo(const LoggingInfo &) = delete;
LoggingInfo(LoggingInfo &&loggingInfo) noexcept;
LoggingInfo(LoggingInfo &&rhs) noexcept;
LoggingInfo &operator=(const LoggingInfo &) = delete;
LoggingInfo &operator=(LoggingInfo &&) = delete;
LoggingInfo &operator=(LoggingInfo &&rhs) noexcept;

~LoggingInfo();

Expand All @@ -53,21 +54,23 @@ class LoggingInfo {

bool alsoLogActivityForSimulatedCommands() const { return _alsoLogActivityForSimulatedCommands; }

void swap(LoggingInfo &rhs) noexcept;

private:
void createLoggers();

static void CreateOutputLogger();
void createOutputLogger();

using TrackedCommandTypes = FlatSet<CoincenterCommandType>;

std::string_view _dataDir = kDefaultDataDir;
std::string_view _dataDir;
string _dateFormatStrActivityFiles;
TrackedCommandTypes _trackedCommandTypes;
int64_t _maxFileSizeLogFileInBytes = kDefaultFileSizeInBytes;
int32_t _maxNbLogFiles = kDefaultNbMaxFiles;
int8_t _logLevelConsolePos = PosFromLevel(log::level::info);
int8_t _logLevelFilePos = PosFromLevel(log::level::off);
bool _destroyLoggers = false;
bool _destroyOutputLogger = false;
bool _alsoLogActivityForSimulatedCommands = false;
};

Expand Down
1 change: 1 addition & 0 deletions src/objects/src/exchange-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace cct {

string ConstructAccumulatedExchangeNames(ExchangeNameSpan exchangeNames) {
// TODO: Use C++23 join_with feature
string exchangesStr(exchangeNames.empty() ? "all" : "");
for (const auto &exchangeName : exchangeNames) {
if (!exchangesStr.empty()) {
Expand Down
76 changes: 50 additions & 26 deletions src/objects/src/logginginfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,26 @@ LoggingInfo::LoggingInfo(WithLoggersCreation withLoggersCreation, std::string_vi
_alsoLogActivityForSimulatedCommands = activityTrackingPart["withSimulatedCommands"].get<bool>();
}

LoggingInfo::LoggingInfo(LoggingInfo &&loggingInfo) noexcept
: _dataDir(loggingInfo._dataDir),
_dateFormatStrActivityFiles(std::move(loggingInfo._dateFormatStrActivityFiles)),
_trackedCommandTypes(std::move(loggingInfo._trackedCommandTypes)),
_maxFileSizeLogFileInBytes(loggingInfo._maxFileSizeLogFileInBytes),
_maxNbLogFiles(loggingInfo._maxNbLogFiles),
_logLevelConsolePos(loggingInfo._logLevelConsolePos),
_logLevelFilePos(loggingInfo._logLevelFilePos),
_destroyLoggers(std::exchange(loggingInfo._destroyLoggers, false)),
_alsoLogActivityForSimulatedCommands(loggingInfo._alsoLogActivityForSimulatedCommands) {}
LoggingInfo::LoggingInfo(LoggingInfo &&rhs) noexcept
: _dataDir(std::move(rhs._dataDir)),
_dateFormatStrActivityFiles(std::move(rhs._dateFormatStrActivityFiles)),
_trackedCommandTypes(std::move(rhs._trackedCommandTypes)),
_maxFileSizeLogFileInBytes(rhs._maxFileSizeLogFileInBytes),
_maxNbLogFiles(rhs._maxNbLogFiles),
_logLevelConsolePos(rhs._logLevelConsolePos),
_logLevelFilePos(rhs._logLevelFilePos),
_destroyOutputLogger(std::exchange(rhs._destroyOutputLogger, false)),
_alsoLogActivityForSimulatedCommands(rhs._alsoLogActivityForSimulatedCommands) {}

LoggingInfo &LoggingInfo::operator=(LoggingInfo &&rhs) noexcept {
if (&rhs != this) {
swap(rhs);
}
return *this;
}

LoggingInfo::~LoggingInfo() {
if (_destroyLoggers) {
if (_destroyOutputLogger) {
log::drop(kOutputLoggerName);
}
}
Expand All @@ -87,36 +94,53 @@ void LoggingInfo::createLoggers() {
}

if (_logLevelFilePos != 0) {
auto logFileName = log::filename_t(_dataDir) + "/log/log.txt";
auto &rotatingSink = sinks.emplace_back(
std::make_shared<log::sinks::rotating_file_sink_mt>(logFileName, _maxFileSizeLogFileInBytes, _maxNbLogFiles));
log::filename_t logFileName = log::filename_t(_dataDir) + log::filename_t("/log/log.txt");
auto &rotatingSink = sinks.emplace_back(std::make_shared<log::sinks::rotating_file_sink_mt>(
std::move(logFileName), _maxFileSizeLogFileInBytes, _maxNbLogFiles));

rotatingSink->set_level(LevelFromPos(_logLevelFilePos));
}

constexpr int nbThreads = 1; // only one logger thread is important to keep order between output logger and others
log::init_thread_pool(8192, nbThreads);
auto logger = std::make_shared<log::async_logger>("", sinks.begin(), sinks.end(), log::thread_pool(),
log::async_overflow_policy::block);
if (!sinks.empty()) {
constexpr int nbThreads = 1; // only one logger thread is important to keep order between output logger and others
log::init_thread_pool(8192, nbThreads);
auto logger = std::make_shared<log::async_logger>("", sinks.begin(), sinks.end(), log::thread_pool(),
log::async_overflow_policy::block);

// spdlog level is present in sink context, and also logger context (why?)
// in addition of the levels of each sink, we need to set the main level of the logger based on the max log level of
// both
logger->set_level(LevelFromPos(std::max(_logLevelConsolePos, _logLevelFilePos)));
// spdlog level is present in sink context, and also logger context (why?)
// in addition of the levels of each sink, we need to set the main level of the logger based on the max log level of
// both
logger->set_level(LevelFromPos(std::max(_logLevelConsolePos, _logLevelFilePos)));

log::set_default_logger(logger);
log::set_default_logger(logger);
}

CreateOutputLogger();
createOutputLogger();
}

_destroyLoggers = true;
void LoggingInfo::swap(LoggingInfo &rhs) noexcept {
using std::swap;

_dataDir.swap(rhs._dataDir);
_dateFormatStrActivityFiles.swap(rhs._dateFormatStrActivityFiles);
_trackedCommandTypes.swap(rhs._trackedCommandTypes);
swap(_maxFileSizeLogFileInBytes, rhs._maxFileSizeLogFileInBytes);
swap(_maxNbLogFiles, rhs._maxNbLogFiles);
swap(_logLevelConsolePos, rhs._logLevelConsolePos);
swap(_logLevelFilePos, rhs._logLevelFilePos);
swap(_destroyOutputLogger, rhs._destroyOutputLogger);
swap(_alsoLogActivityForSimulatedCommands, rhs._alsoLogActivityForSimulatedCommands);
}

void LoggingInfo::CreateOutputLogger() {
void LoggingInfo::createOutputLogger() {
auto outputLogger =
std::make_shared<log::async_logger>(kOutputLoggerName, std::make_shared<log::sinks::stdout_color_sink_mt>(),
log::thread_pool(), log::async_overflow_policy::block);
outputLogger->set_level(log::level::level_enum::info);
outputLogger->set_pattern("%v");

log::register_logger(outputLogger);
_destroyOutputLogger = true;
}

} // namespace cct
12 changes: 12 additions & 0 deletions src/objects/test/logginginfo_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,16 @@ TEST(LoggingInfo, MoveConstructor) {

log::info("test2");
}

TEST(LoggingInfo, MoveAssignment) {
LoggingInfo loggingInfo(LoggingInfo::WithLoggersCreation::kYes);

log::info("test1");

LoggingInfo loggingInfo2;

loggingInfo2 = std::move(loggingInfo);

log::info("test2");
}
} // namespace cct
2 changes: 2 additions & 0 deletions src/tech/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,6 @@ add_unit_test(
add_unit_test(
url-encode_test.cpp
test/url-encode_test.cpp
DEFINITIONS
CCT_DISABLE_SPDLOG
)
2 changes: 1 addition & 1 deletion src/tech/include/cct_exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class exception : public std::exception {
public:
static constexpr int kMsgMaxLen = 80;

template <int N, std::enable_if_t<N <= kMsgMaxLen + 1, bool> = true>
template <int N, std::enable_if_t<N <= kMsgMaxLen, bool> = true>
explicit exception(const char (&str)[N]) noexcept {
// Hint: default constructor constructs a variant holding the value-initialized value of the first alternative
// (index() is zero). In our case, it's a std::array, which is what we want here.
Expand Down
48 changes: 15 additions & 33 deletions src/tech/include/fbstring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <type_traits>

#include "cct_config.hpp"
#include "cct_format.hpp"
#include "cct_type_traits.hpp"
#include "unreachable.hpp"

Expand Down Expand Up @@ -2747,39 +2748,7 @@ operator<<(
typename basic_fbstring<E, T, A, S>::value_type,
typename basic_fbstring<E, T, A, S>::traits_type>& os,
const basic_fbstring<E, T, A, S>& str) {
#ifdef _LIBCPP_VERSION
typedef std::basic_ostream<
typename basic_fbstring<E, T, A, S>::value_type,
typename basic_fbstring<E, T, A, S>::traits_type>
_ostream_type;
typename _ostream_type::sentry _s(os);
if (_s) {
typedef std::ostreambuf_iterator<
typename basic_fbstring<E, T, A, S>::value_type,
typename basic_fbstring<E, T, A, S>::traits_type>
_Ip;
size_t __len = str.size();
bool __left =
(os.flags() & _ostream_type::adjustfield) == _ostream_type::left;
if (__pad_and_output(
_Ip(os),
str.data(),
__left ? str.data() + __len : str.data(),
str.data() + __len,
os,
os.fill())
.failed()) {
os.setstate(_ostream_type::badbit | _ostream_type::failbit);
}
}
#elif defined(_MSC_VER)
typedef decltype(os.precision()) streamsize;
// MSVC doesn't define __ostream_insert
os.write(str.data(), static_cast<streamsize>(str.size()));
#else
std::__ostream_insert(os, str.data(), str.size());
#endif
return os;
return os << std::basic_string_view<E, T>(str);
}

template <typename E1, class T, class A, class S>
Expand Down Expand Up @@ -2921,6 +2890,19 @@ FOLLY_POP_WARNING
#undef FOLLY_SANITIZE_ADDRESS
#undef FBSTRING_DISABLE_SSO

#ifndef CCT_DISABLE_SPDLOG

template <class E, class T, class A, class S>
struct fmt::formatter<::folly::basic_fbstring<E, T, A, S>> : fmt::formatter<std::basic_string_view<E, T>> {
template <typename FormatContext>
auto format(const ::folly::basic_fbstring<E, T, A, S> &str, FormatContext &ctx) const -> decltype(ctx.out()) {
using SVType = std::basic_string_view<E, T>;
return fmt::formatter<SVType>::format(SVType(str), ctx);
}
};

#endif

namespace folly {
template <class T>
struct IsSomeString;
Expand Down
Loading

0 comments on commit 3a424b2

Please sign in to comment.