30#ifndef NUMCPP_NO_USE_BOOST
41#include "boost/algorithm/string.hpp"
42#include "boost/core/demangle.hpp"
53 template<
typename DataType>
54 using serialize_t =
decltype(std::declval<DataType>().serialize());
59 template<
typename DataType,
typename = std::
void_t<>>
63 static constexpr bool value =
false;
69 template<
typename DataType>
71 std::void_t<std::enable_if_t<std::is_same_v<serialize_t<DataType>, std::string>, int>>>
74 static constexpr bool value =
true;
80 template<
typename DataType>
87 template<
typename DataType>
140 const std::filesystem::path&
filepath() const noexcept
152 if (std::filesystem::is_directory(outputDir))
154 auto dataTypeName = boost::core::demangle(
typeid(DataType).name());
155 boost::algorithm::replace_all(dataTypeName,
"::",
"_");
156 boost::algorithm::replace_all(dataTypeName,
"<",
"_");
157 boost::algorithm::replace_all(dataTypeName,
">",
"_");
158 const auto filename = std::filesystem::path(dataTypeName).replace_extension(
LOG_EXT);
159 filepath_ = std::filesystem::canonical(outputDir) / filename;
161 ofile_ = std::ofstream(filepath_.c_str(), std::ios::out | std::ios::binary);
164 throw std::runtime_error(
"Unable to open the log file:\n\t" + filepath_.string());
169 throw std::runtime_error(
"The provided output log directory is not valid:\n\t" +
227 if (filepath_.empty())
229 throw std::runtime_error(
"The output log directory does not exist");
234 const auto serializedData = dataElement.serialize();
235 ofile_.write(serializedData.data(), serializedData.size());
255 if (filepath_.empty())
257 throw std::runtime_error(
"The output log directory does not exist");
261 dataElements + numElements,
266 std::filesystem::path filepath_{};
267 std::ofstream ofile_;
268 bool enabled_{
true };
303 if (!std::filesystem::is_directory(outputDir))
305 throw std::runtime_error(
"outputDir does not exist");
308 outputDir_ = outputDir;
327 template<
typename DataType>
335 std::filesystem::path outputDir_{
"." };
340 BinaryLogger() =
default;
Binary Logger Singleton.
Definition: BinaryLogger.hpp:276
void setOutputDir(std::string_view outputDir)
Sets the output directory. This should be called BEFORE any type loggers have been created,...
Definition: BinaryLogger.hpp:317
BinaryLogger & operator=(const BinaryLogger &)=delete
BinaryLogger(const BinaryLogger &)=delete
BinaryLogger(BinaryLogger &&)=delete
BinaryLogger & operator=(BinaryLogger &&)=delete
void setOutputDir(const std::filesystem::path &outputDir)
Sets the output directory. This should be called BEFORE any type loggers have been created,...
Definition: BinaryLogger.hpp:301
detail::BinaryDataLogger< DataType > & getTypeLogger()
Gets the logger instance for the specific data type.
Definition: BinaryLogger.hpp:328
static BinaryLogger & getInstance() noexcept
Singleton instance getter.
Definition: BinaryLogger.hpp:289
Binary Logger.
Definition: BinaryLogger.hpp:89
BinaryDataLogger()
Default constructor.
Definition: BinaryLogger.hpp:103
static constexpr char LOG_EXT[]
Definition: BinaryLogger.hpp:96
BinaryDataLogger(std::string_view outputDir)
Constructor.
Definition: BinaryLogger.hpp:118
void log(const_reference dataElement)
Logs the data element.
Definition: BinaryLogger.hpp:220
bool isEnabled() noexcept
Checks whether logger is enabled.
Definition: BinaryLogger.hpp:203
BinaryDataLogger & operator=(BinaryDataLogger &&)=delete
const std::filesystem::path & filepath() const noexcept
The log file path.
Definition: BinaryLogger.hpp:140
const DataType *const const_pointer
Definition: BinaryLogger.hpp:92
void setOutputDir(std::filesystem::path outputDir)
Sets the output log directory.
Definition: BinaryLogger.hpp:150
DataType value_type
Definition: BinaryLogger.hpp:91
void disable() noexcept
Disable the logger.
Definition: BinaryLogger.hpp:195
static constexpr auto DATE_TYPE_HAS_SERIALIZE_METHOD
Definition: BinaryLogger.hpp:98
BinaryDataLogger & operator=(const BinaryDataLogger &)=delete
~BinaryDataLogger()
Destructor.
Definition: BinaryLogger.hpp:126
BinaryDataLogger(const BinaryDataLogger &)=delete
BinaryDataLogger(std::filesystem::path outputDir)
Constructor.
Definition: BinaryLogger.hpp:109
BinaryDataLogger(BinaryDataLogger &&)=delete
const DataType & const_reference
Definition: BinaryLogger.hpp:93
static constexpr auto DATA_ELEMENT_SIZE
Definition: BinaryLogger.hpp:97
void log(const_pointer dataElements, std::size_t numElements)
Logs the data elements.
Definition: BinaryLogger.hpp:248
void flush()
Force a flush of the output stream.
Definition: BinaryLogger.hpp:211
void enable() noexcept
Enable the logger.
Definition: BinaryLogger.hpp:187
void setOutputDir(std::string_view outputDir)
Sets the output log directory.
Definition: BinaryLogger.hpp:179
type trait to check if a type has a serialize method with the correct signature
Definition: BinaryLogger.hpp:61
static constexpr bool value
Definition: BinaryLogger.hpp:63
decltype(std::declval< DataType >().serialize()) serialize_t
type trait to check if a type has a serialize method with the correct signature
Definition: BinaryLogger.hpp:54
constexpr bool has_serialize_v
type trait to check if a type has a serialize method with the correct signature
Definition: BinaryLogger.hpp:81
Definition: BinaryLogger.hpp:45
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225