Apply srsLTE formatting rules to srsLog. They were previously formatted with different rules when the project lived in the external rapo.

master
faluco 4 years ago committed by faluco
parent 873f375ecc
commit 1ccb62af3d

@ -124,18 +124,13 @@ struct metric {
static const char* units() { return Units::units(); } static const char* units() { return Units::units(); }
/// Returns the formatting kind of the metric. /// Returns the formatting kind of the metric.
static metric_kind kind() static metric_kind kind() { return metric_kind_selector<metric<Ty, Name, Units> >::kind; }
{
return metric_kind_selector<metric<Ty, Name, Units>>::kind;
}
}; };
/// Template specialization that tags metrics with arithmetic values (integers /// Template specialization that tags metrics with arithmetic values (integers
/// and floating point) as numeric. /// and floating point) as numeric.
template <typename Ty, typename Name, typename Units> template <typename Ty, typename Name, typename Units>
struct metric_kind_selector< struct metric_kind_selector<metric<Ty, Name, Units>, typename std::enable_if<std::is_arithmetic<Ty>::value>::type> {
metric<Ty, Name, Units>,
typename std::enable_if<std::is_arithmetic<Ty>::value>::type> {
static const metric_kind kind = metric_kind::numeric; static const metric_kind kind = metric_kind::numeric;
}; };
@ -168,8 +163,7 @@ namespace detail {
/// eg: using my_metric_t = srslog::build_metric_set_type<m1_t, set1_t, m2_t>; /// eg: using my_metric_t = srslog::build_metric_set_type<m1_t, set1_t, m2_t>;
/// NOTE: Adding duplicated types into the list is not allowed. /// NOTE: Adding duplicated types into the list is not allowed.
template <typename Name, typename... Ts> template <typename Name, typename... Ts>
using build_metric_set_type = using build_metric_set_type = metric_set<Name, typename std::decay<Ts>::type...>;
metric_set<Name, typename std::decay<Ts>::type...>;
} // namespace detail } // namespace detail
@ -200,9 +194,8 @@ using build_context_type = context<typename std::decay<Ts>::type...>;
static const char* name() { return _name_rep; } \ static const char* name() { return _name_rep; } \
}; \ }; \
} \ } \
using _type = srslog::metric<typename std::decay<_value_type>::type, \ using _type = srslog:: \
metric_info::_type##__name_rep, \ metric<typename std::decay<_value_type>::type, metric_info::_type##__name_rep, metric_info::_type##__units>
metric_info::_type##__units>
/// This macro defines a new metric set type using the following attributes: /// This macro defines a new metric set type using the following attributes:
/// a) name: encoded as a string. /// a) name: encoded as a string.
@ -222,8 +215,7 @@ using build_context_type = context<typename std::decay<Ts>::type...>;
static const char* name() { return _name_rep; } \ static const char* name() { return _name_rep; } \
}; \ }; \
} \ } \
using _type = srslog::detail:: \ using _type = srslog::detail::build_metric_set_type<metric_set_info::_type##__name_rep, __VA_ARGS__>
build_metric_set_type<metric_set_info::_type##__name_rep, __VA_ARGS__>
/// This macro defines a list of metric sets of the same type: /// This macro defines a list of metric sets of the same type:
/// a) name: encoded as a string. /// a) name: encoded as a string.
@ -242,8 +234,7 @@ using build_context_type = context<typename std::decay<Ts>::type...>;
static const char* name() { return _name_rep; } \ static const char* name() { return _name_rep; } \
}; \ }; \
} \ } \
using _type = srslog::metric_list<list_info::_type##__name_rep, \ using _type = srslog::metric_list<list_info::_type##__name_rep, typename std::decay<_list_type>::type>
typename std::decay<_list_type>::type>
} // namespace srslog } // namespace srslog

@ -34,8 +34,7 @@ struct flush_backend_cmd {
// memory queue for passing entries. // memory queue for passing entries.
struct log_entry { struct log_entry {
sink* s; sink* s;
std::function<void(log_entry_metadata&& metadata, fmt::memory_buffer& buffer)> std::function<void(log_entry_metadata&& metadata, fmt::memory_buffer& buffer)> format_func;
format_func;
log_entry_metadata metadata; log_entry_metadata metadata;
std::unique_ptr<flush_backend_cmd> flush_cmd; std::unique_ptr<flush_backend_cmd> flush_cmd;
}; };

@ -54,15 +54,12 @@ public:
/// b) is move constructible. /// b) is move constructible.
/// c) is not an specialization of in_place_type_t. /// c) is not an specialization of in_place_type_t.
/// Otherwise the rest of special member functions are considered. /// Otherwise the rest of special member functions are considered.
template < template <typename T,
typename T, typename std::enable_if<!std::is_same<typename std::decay<T>::type, any>{} &&
typename std::enable_if<
!std::is_same<typename std::decay<T>::type, any>{} &&
std::is_move_constructible<typename std::decay<T>::type>{} && std::is_move_constructible<typename std::decay<T>::type>{} &&
!is_in_place_type_t<typename std::decay<T>::type>{}, !is_in_place_type_t<typename std::decay<T>::type>{},
int>::type = 0> int>::type = 0>
explicit any(T&& t) : explicit any(T&& t) : storage(new storage_impl<typename std::decay<T>::type>(std::forward<T>(t)))
storage(new storage_impl<typename std::decay<T>::type>(std::forward<T>(t)))
{} {}
/// Constructs an object of type decayed T directly into the internal storage /// Constructs an object of type decayed T directly into the internal storage
@ -73,12 +70,9 @@ public:
/// Otherwise the rest of special member functions are considered. /// Otherwise the rest of special member functions are considered.
template <typename T, template <typename T,
typename... Args, typename... Args,
typename std::enable_if< typename std::enable_if<std::is_constructible<typename std::decay<T>::type, Args...>{}, int>::type = 0>
std::is_constructible<typename std::decay<T>::type, Args...>{},
int>::type = 0>
explicit any(in_place_type_t<T>, Args&&... args) : explicit any(in_place_type_t<T>, Args&&... args) :
storage(new storage_impl<typename std::decay<T>::type>( storage(new storage_impl<typename std::decay<T>::type>(std::forward<Args>(args)...))
std::forward<Args>(args)...))
{} {}
any(any&& other) : storage(std::move(other.storage)) {} any(any&& other) : storage(std::move(other.storage)) {}
@ -160,8 +154,7 @@ inline T* any_cast(any* operand)
if (!operand || !operand->storage) if (!operand || !operand->storage)
return nullptr; return nullptr;
using U = using U = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
typename std::remove_cv<typename std::remove_reference<T>::type>::type;
if (operand->storage->type() != &any::type_tag<U>::tag) if (operand->storage->type() != &any::type_tag<U>::tag)
return nullptr; return nullptr;
@ -177,8 +170,7 @@ inline const T* any_cast(const any* operand)
if (!operand || !operand->storage) if (!operand || !operand->storage)
return nullptr; return nullptr;
using U = using U = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
typename std::remove_cv<typename std::remove_reference<T>::type>::type;
if (operand->storage->type() != &any::type_tag<U>::tag) if (operand->storage->type() != &any::type_tag<U>::tag)
return nullptr; return nullptr;

@ -27,13 +27,9 @@ class memory_buffer
const size_t length; const size_t length;
public: public:
memory_buffer(const char* buffer, size_t length) : memory_buffer(const char* buffer, size_t length) : buffer(buffer), length(length) {}
buffer(buffer), length(length)
{}
explicit memory_buffer(const std::string& s) : explicit memory_buffer(const std::string& s) : buffer(s.data()), length(s.size()) {}
buffer(s.data()), length(s.size())
{}
/// Returns a pointer to the start of the memory block. /// Returns a pointer to the start of the memory block.
const char* data() const { return buffer; } const char* data() const { return buffer; }

@ -118,10 +118,7 @@ public:
/// Blocks the calling thread on this condition variable up to the specified /// Blocks the calling thread on this condition variable up to the specified
/// timeout. Returns true on timeout expiration, otherwise false. /// timeout. Returns true on timeout expiration, otherwise false.
bool wait(timespec ts) bool wait(timespec ts) { return (::pthread_cond_timedwait(&cond_var, m.raw(), &ts) == ETIMEDOUT); }
{
return (::pthread_cond_timedwait(&cond_var, m.raw(), &ts) == ETIMEDOUT);
}
/// Builds an absolute time timespec structure adding the specified time out /// Builds an absolute time timespec structure adding the specified time out
/// in ms. /// in ms.
@ -152,11 +149,7 @@ class cond_var_scoped_lock
condition_variable& cond_var; condition_variable& cond_var;
public: public:
explicit cond_var_scoped_lock(condition_variable& cond_var) : explicit cond_var_scoped_lock(condition_variable& cond_var) : cond_var(cond_var) { cond_var.lock(); }
cond_var(cond_var)
{
cond_var.lock();
}
cond_var_scoped_lock(const cond_var_scoped_lock&) = delete; cond_var_scoped_lock(const cond_var_scoped_lock&) = delete;
cond_var_scoped_lock& operator=(const cond_var_scoped_lock&) = delete; cond_var_scoped_lock& operator=(const cond_var_scoped_lock&) = delete;

@ -28,8 +28,7 @@ template <std::size_t...>
struct index_sequence {}; struct index_sequence {};
template <std::size_t N, std::size_t... Next> template <std::size_t N, std::size_t... Next>
struct index_sequence_helper struct index_sequence_helper : public index_sequence_helper<N - 1U, N - 1U, Next...> {};
: public index_sequence_helper<N - 1U, N - 1U, Next...> {};
template <std::size_t... Next> template <std::size_t... Next>
struct index_sequence_helper<0U, Next...> { struct index_sequence_helper<0U, Next...> {
@ -53,8 +52,7 @@ struct tuple_index<T, std::tuple<T, Ts...>> {
template <typename T, typename U, typename... Ts> template <typename T, typename U, typename... Ts>
struct tuple_index<T, std::tuple<U, Ts...> > { struct tuple_index<T, std::tuple<U, Ts...> > {
static constexpr std::size_t value = static constexpr std::size_t value = 1 + tuple_index<T, std::tuple<Ts...> >::value;
1 + tuple_index<T, std::tuple<Ts...>>::value;
}; };
template <typename T, typename... Ts> template <typename T, typename... Ts>

@ -39,8 +39,7 @@ log_channel& fetch_log_channel(const std::string& id);
/// Returns an instance of a log_channel with the specified id that writes to /// Returns an instance of a log_channel with the specified id that writes to
/// the specified sink. /// the specified sink.
/// NOTE: Any '#' characters in the id will get removed. /// NOTE: Any '#' characters in the id will get removed.
log_channel& log_channel& fetch_log_channel(const std::string& id, sink& s, log_channel_config config);
fetch_log_channel(const std::string& id, sink& s, log_channel_config config);
/// Creates a new log channel instance with the specified id string and sink, /// Creates a new log channel instance with the specified id string and sink,
/// then registers it in the log channel repository so that it can be later /// then registers it in the log channel repository so that it can be later
@ -80,16 +79,13 @@ inline T* find_logger(const std::string& id)
/// specified id string. All logger channels will write into the default sink. /// specified id string. All logger channels will write into the default sink.
/// The context value of the logger can be printed on each log entry by setting /// The context value of the logger can be printed on each log entry by setting
/// to true the should_print_context argument. /// to true the should_print_context argument.
basic_logger& fetch_basic_logger(const std::string& id, basic_logger& fetch_basic_logger(const std::string& id, bool should_print_context = true);
bool should_print_context = true);
/// Returns an instance of a basic logger (see basic_logger type) with the /// Returns an instance of a basic logger (see basic_logger type) with the
/// specified id string. All logger channels will write into the specified sink. /// specified id string. All logger channels will write into the specified sink.
/// The context value of the logger can be printed on each log entry by setting /// The context value of the logger can be printed on each log entry by setting
/// to true the should_print_context argument. /// to true the should_print_context argument.
basic_logger& fetch_basic_logger(const std::string& id, basic_logger& fetch_basic_logger(const std::string& id, sink& s, bool should_print_context = true);
sink& s,
bool should_print_context = true);
/// Returns a logger instance with the specified id string, type and channel /// Returns a logger instance with the specified id string, type and channel
/// references. /// references.
@ -115,9 +111,7 @@ inline T& fetch_logger(const std::string& id, Args&&... args)
/// the specified sink. Returns a pointer to the newly created logger, otherwise /// the specified sink. Returns a pointer to the newly created logger, otherwise
/// when a logger is already registered with the same id it returns nullptr. /// when a logger is already registered with the same id it returns nullptr.
/// NOTE: Deprecated, use fetch_basic_logger instead. /// NOTE: Deprecated, use fetch_basic_logger instead.
basic_logger* create_basic_logger(const std::string& id, basic_logger* create_basic_logger(const std::string& id, sink& s, bool should_print_context = true);
sink& s,
bool should_print_context = true);
/// Creates a new logger instance with the specified id string, type and channel /// Creates a new logger instance with the specified id string, type and channel
/// references, registering it into the logger repository so that it can be /// references, registering it into the logger repository so that it can be
@ -172,15 +166,13 @@ sink* find_sink(const std::string& id);
/// Returns an instance of a sink that writes to the stdout stream. You may use /// Returns an instance of a sink that writes to the stdout stream. You may use
/// different ids if you need to create multiple stdout sinks with different /// different ids if you need to create multiple stdout sinks with different
/// formatters. /// formatters.
sink& fetch_stdout_sink( sink& fetch_stdout_sink(const std::string& id = "stdout",
const std::string& id = "stdout",
std::unique_ptr<log_formatter> f = get_default_log_formatter()); std::unique_ptr<log_formatter> f = get_default_log_formatter());
/// Returns an instance of a sink that writes to the stderr stream. You may use /// Returns an instance of a sink that writes to the stderr stream. You may use
/// different ids if you need to create multiple stderr sinks with different /// different ids if you need to create multiple stderr sinks with different
/// formatters. /// formatters.
sink& fetch_stderr_sink( sink& fetch_stderr_sink(const std::string& id = "stderr",
const std::string& id = "stderr",
std::unique_ptr<log_formatter> f = get_default_log_formatter()); std::unique_ptr<log_formatter> f = get_default_log_formatter());
/// Returns an instance of a sink that writes into a file in the specified path. /// Returns an instance of a sink that writes into a file in the specified path.
@ -188,8 +180,7 @@ sink& fetch_stderr_sink(
/// new file each time the current file exceeds this value. The units of /// new file each time the current file exceeds this value. The units of
/// max_size are bytes. /// max_size are bytes.
/// NOTE: Any '#' characters in the path will get removed. /// NOTE: Any '#' characters in the path will get removed.
sink& fetch_file_sink( sink& fetch_file_sink(const std::string& path,
const std::string& path,
size_t max_size = 0, size_t max_size = 0,
std::unique_ptr<log_formatter> f = get_default_log_formatter()); std::unique_ptr<log_formatter> f = get_default_log_formatter());

@ -66,8 +66,7 @@ srslog_log_channel* srslog_find_log_channel(const char* id);
/** /**
* Controls whether the specified channel accepts incoming log entries. * Controls whether the specified channel accepts incoming log entries.
*/ */
void srslog_set_log_channel_enabled(srslog_log_channel* channel, void srslog_set_log_channel_enabled(srslog_log_channel* channel, srslog_bool enabled);
srslog_bool enabled);
/** /**
* Returns 1 if the specified channel is accepting incoming log entries, * Returns 1 if the specified channel is accepting incoming log entries,

@ -24,9 +24,7 @@ namespace srslog {
class file_sink : public sink class file_sink : public sink
{ {
public: public:
file_sink(std::string name, file_sink(std::string name, size_t max_size, std::unique_ptr<log_formatter> f) :
size_t max_size,
std::unique_ptr<log_formatter> f) :
sink(std::move(f)), sink(std::move(f)),
max_size((max_size == 0) ? 0 : std::max<size_t>(max_size, 4 * 1024)), max_size((max_size == 0) ? 0 : std::max<size_t>(max_size, 4 * 1024)),
base_filename(std::move(name)) base_filename(std::move(name))
@ -72,8 +70,7 @@ private:
/// Creates a new file and increments the file index counter. /// Creates a new file and increments the file index counter.
detail::error_string create_file() detail::error_string create_file()
{ {
return handler.create( return handler.create(file_utils::build_filename_with_index(base_filename, file_index++));
file_utils::build_filename_with_index(base_filename, file_index++));
} }
/// Handles the file rotation feature when it is activated. /// Handles the file rotation feature when it is activated.

@ -31,8 +31,7 @@ inline std::string format_error(const std::string& error, int error_code)
} }
/// Splits the specified path into a filename and its extension (if present). /// Splits the specified path into a filename and its extension (if present).
inline std::pair<std::string, std::string> inline std::pair<std::string, std::string> split_filename_extension(const std::string& filename)
split_filename_extension(const std::string& filename)
{ {
// Search for the last dot. // Search for the last dot.
auto dot_pos = filename.find_last_of('.'); auto dot_pos = filename.find_last_of('.');
@ -41,8 +40,7 @@ split_filename_extension(const std::string& filename)
// a) No dot found: my_file // a) No dot found: my_file
// b) Dot found at the beginning: .my_file // b) Dot found at the beginning: .my_file
// c) Dot found at the end: my_file. // c) Dot found at the end: my_file.
if (dot_pos == std::string::npos || dot_pos == 0 || if (dot_pos == std::string::npos || dot_pos == 0 || dot_pos == filename.size() - 1) {
dot_pos == filename.size() - 1) {
return {filename, ""}; return {filename, ""};
} }
@ -61,8 +59,7 @@ split_filename_extension(const std::string& filename)
} }
/// Builds a file name formatting the input base name and file index. /// Builds a file name formatting the input base name and file index.
inline std::string build_filename_with_index(const std::string& basename, inline std::string build_filename_with_index(const std::string& basename, size_t index)
size_t index)
{ {
if (index == 0) { if (index == 0) {
return basename; return basename;
@ -101,19 +98,15 @@ public:
return {}; return {};
} }
return format_error( return format_error(fmt::format("Unable to create log file \"{}\"", new_path), errno);
fmt::format("Unable to create log file \"{}\"", new_path), errno);
} }
/// Writes the provided memory buffer into an open file, otherwise does /// Writes the provided memory buffer into an open file, otherwise does
/// nothing. /// nothing.
detail::error_string write(detail::memory_buffer buffer) detail::error_string write(detail::memory_buffer buffer)
{ {
if (handle && if (handle && std::fwrite(buffer.data(), sizeof(char), buffer.size(), handle) != buffer.size()) {
std::fwrite(buffer.data(), sizeof(char), buffer.size(), handle) != auto err_str = format_error(fmt::format("Unable to write log file \"{}\"", path), errno);
buffer.size()) {
auto err_str = format_error(
fmt::format("Unable to write log file \"{}\"", path), errno);
close(); close();
return err_str; return err_str;
} }
@ -125,9 +118,7 @@ public:
detail::error_string flush() detail::error_string flush()
{ {
if (handle && ::fflush(handle) == EOF) { if (handle && ::fflush(handle) == EOF) {
auto err_str = format_error( auto err_str = format_error(fmt::format("Error encountered while flushing log file \"{}\"", path), errno);
fmt::format("Error encountered while flushing log file \"{}\"", path),
errno);
close(); close();
return err_str; return err_str;
} }

@ -25,8 +25,7 @@ class stream_sink : public sink
{ {
public: public:
stream_sink(sink_stream_type s, std::unique_ptr<log_formatter> f) : stream_sink(sink_stream_type s, std::unique_ptr<log_formatter> f) :
sink(std::move(f)), sink(std::move(f)), handle((s == sink_stream_type::stdout) ? stdout : stderr)
handle((s == sink_stream_type::stdout) ? stdout : stderr)
{} {}
stream_sink(const stream_sink& other) = delete; stream_sink(const stream_sink& other) = delete;

@ -56,8 +56,7 @@ srslog_log_channel* srslog_find_log_channel(const char* id)
return c_cast<srslog_log_channel>(find_log_channel(id)); return c_cast<srslog_log_channel>(find_log_channel(id));
} }
void srslog_set_log_channel_enabled(srslog_log_channel* channel, void srslog_set_log_channel_enabled(srslog_log_channel* channel, srslog_bool enabled)
srslog_bool enabled)
{ {
assert(channel && "Expected a valid channel"); assert(channel && "Expected a valid channel");
c_cast<log_channel>(channel)->set_enabled(enabled); c_cast<log_channel>(channel)->set_enabled(enabled);

@ -27,18 +27,16 @@ class srslog_instance
srslog_instance() srslog_instance()
{ {
// stdout and stderr sinks are always present. // stdout and stderr sinks are always present.
auto& stdout_sink = sink_repo.emplace( auto& stdout_sink =
std::piecewise_construct, sink_repo.emplace(std::piecewise_construct,
std::forward_as_tuple("stdout"), std::forward_as_tuple("stdout"),
std::forward_as_tuple(new stream_sink( std::forward_as_tuple(new stream_sink(sink_stream_type::stdout,
sink_stream_type::stdout,
std::unique_ptr<log_formatter>(new text_formatter)))); std::unique_ptr<log_formatter>(new text_formatter))));
default_sink = stdout_sink.get(); default_sink = stdout_sink.get();
sink_repo.emplace(std::piecewise_construct, sink_repo.emplace(std::piecewise_construct,
std::forward_as_tuple("stderr"), std::forward_as_tuple("stderr"),
std::forward_as_tuple(new stream_sink( std::forward_as_tuple(new stream_sink(sink_stream_type::stderr,
sink_stream_type::stderr,
std::unique_ptr<log_formatter>(new text_formatter)))); std::unique_ptr<log_formatter>(new text_formatter))));
// Initialize the default formatter pointer with a text formatter. // Initialize the default formatter pointer with a text formatter.
@ -79,10 +77,7 @@ public:
const detail::log_backend& get_backend() const { return backend; } const detail::log_backend& get_backend() const { return backend; }
/// Installs the specified error handler into the backend. /// Installs the specified error handler into the backend.
void set_error_handler(error_handler callback) void set_error_handler(error_handler callback) { backend.set_error_handler(std::move(callback)); }
{
backend.set_error_handler(std::move(callback));
}
/// Set the specified sink as the default one. /// Set the specified sink as the default one.
void set_default_sink(sink& s) { default_sink = &s; } void set_default_sink(sink& s) { default_sink = &s; }

@ -22,10 +22,7 @@ static constexpr char log_filename[] = "file_sink_test.log";
static bool when_data_is_written_to_file_then_contents_are_valid() static bool when_data_is_written_to_file_then_contents_are_valid()
{ {
file_test_utils::scoped_file_deleter deleter(log_filename); file_test_utils::scoped_file_deleter deleter(log_filename);
file_sink file( file_sink file(log_filename, 0, std::unique_ptr<log_formatter>(new test_dummies::log_formatter_dummy));
log_filename,
0,
std::unique_ptr<log_formatter>(new test_dummies::log_formatter_dummy));
std::vector<std::string> entries; std::vector<std::string> entries;
for (unsigned i = 0; i != 10; ++i) { for (unsigned i = 0; i != 10; ++i) {
@ -37,8 +34,7 @@ static bool when_data_is_written_to_file_then_contents_are_valid()
file.flush(); file.flush();
ASSERT_EQ(file_test_utils::file_exists(log_filename), true); ASSERT_EQ(file_test_utils::file_exists(log_filename), true);
ASSERT_EQ(file_test_utils::compare_file_contents(log_filename, entries), ASSERT_EQ(file_test_utils::compare_file_contents(log_filename, entries), true);
true);
return true; return true;
} }
@ -49,10 +45,7 @@ class file_sink_subclass : public file_sink
{ {
public: public:
file_sink_subclass(std::string name, size_t max_size) : file_sink_subclass(std::string name, size_t max_size) :
file_sink( file_sink(std::move(name), max_size, std::unique_ptr<log_formatter>(new test_dummies::log_formatter_dummy))
std::move(name),
max_size,
std::unique_ptr<log_formatter>(new test_dummies::log_formatter_dummy))
{} {}
uint32_t get_num_of_files() const { return get_file_index(); } uint32_t get_num_of_files() const { return get_file_index(); }
@ -60,14 +53,10 @@ public:
static bool when_data_written_exceeds_size_threshold_then_new_file_is_created() static bool when_data_written_exceeds_size_threshold_then_new_file_is_created()
{ {
std::string filename0 = std::string filename0 = file_utils::build_filename_with_index(log_filename, 0);
file_utils::build_filename_with_index(log_filename, 0); std::string filename1 = file_utils::build_filename_with_index(log_filename, 1);
std::string filename1 = std::string filename2 = file_utils::build_filename_with_index(log_filename, 2);
file_utils::build_filename_with_index(log_filename, 1); file_test_utils::scoped_file_deleter deleter = {filename0, filename1, filename2};
std::string filename2 =
file_utils::build_filename_with_index(log_filename, 2);
file_test_utils::scoped_file_deleter deleter = {
filename0, filename1, filename2};
file_sink_subclass file(log_filename, 5001); file_sink_subclass file(log_filename, 5001);
@ -112,8 +101,7 @@ static bool when_data_written_exceeds_size_threshold_then_new_file_is_created()
int main() int main()
{ {
TEST_FUNCTION(when_data_is_written_to_file_then_contents_are_valid); TEST_FUNCTION(when_data_is_written_to_file_then_contents_are_valid);
TEST_FUNCTION( TEST_FUNCTION(when_data_written_exceeds_size_threshold_then_new_file_is_created);
when_data_written_exceeds_size_threshold_then_new_file_is_created);
return 0; return 0;
} }

@ -47,8 +47,7 @@ inline bool file_exists(const std::string& path)
/// Reads the contents of the file specified in path and returns true if they /// Reads the contents of the file specified in path and returns true if they
/// match with the data in entries, otherwise returns false. /// match with the data in entries, otherwise returns false.
inline bool compare_file_contents(const std::string& path, inline bool compare_file_contents(const std::string& path, const std::vector<std::string>& entries)
const std::vector<std::string>& entries)
{ {
std::ifstream file(path, std::ios::binary); std::ifstream file(path, std::ios::binary);
if (!file.is_open()) { if (!file.is_open()) {

@ -21,9 +21,7 @@ static constexpr char log_filename2[] = "file_utils_test2.log";
static bool filename_extension_split_test() static bool filename_extension_split_test()
{ {
auto checker = [](const std::string& base, auto checker = [](const std::string& base, const std::string& file, const std::string& ext) {
const std::string& file,
const std::string& ext) {
auto result = file_utils::split_filename_extension(base); auto result = file_utils::split_filename_extension(base);
return (result.first == file && result.second == ext); return (result.first == file && result.second == ext);
}; };
@ -48,10 +46,8 @@ static bool filename_with_index_build_test()
ASSERT_EQ(file_utils::build_filename_with_index("filename", 0), "filename"); ASSERT_EQ(file_utils::build_filename_with_index("filename", 0), "filename");
ASSERT_EQ(file_utils::build_filename_with_index("filename", 1), "filename.1"); ASSERT_EQ(file_utils::build_filename_with_index("filename", 1), "filename.1");
ASSERT_EQ(file_utils::build_filename_with_index("filename.txt", 0), ASSERT_EQ(file_utils::build_filename_with_index("filename.txt", 0), "filename.txt");
"filename.txt"); ASSERT_EQ(file_utils::build_filename_with_index("filename.txt", 1), "filename.1.txt");
ASSERT_EQ(file_utils::build_filename_with_index("filename.txt", 1),
"filename.1.txt");
return true; return true;
} }
@ -82,8 +78,7 @@ static bool when_created_method_is_called_then_file_is_created()
return true; return true;
} }
static bool static bool when_created_method_is_called_twice_then_file_is_closed_and_created()
when_created_method_is_called_twice_then_file_is_closed_and_created()
{ {
file_test_utils::scoped_file_deleter deleter = {log_filename, log_filename2}; file_test_utils::scoped_file_deleter deleter = {log_filename, log_filename2};
file_utils::file f; file_utils::file f;
@ -137,8 +132,7 @@ static bool when_data_is_written_to_file_then_contents_are_valid()
err_str = f.flush(); err_str = f.flush();
ASSERT_EQ(err_str.get_error().empty(), true); ASSERT_EQ(err_str.get_error().empty(), true);
ASSERT_EQ(file_test_utils::compare_file_contents(log_filename, {"test\n"}), ASSERT_EQ(file_test_utils::compare_file_contents(log_filename, {"test\n"}), true);
true);
return true; return true;
} }
@ -166,8 +160,7 @@ int main()
TEST_FUNCTION(filename_with_index_build_test); TEST_FUNCTION(filename_with_index_build_test);
TEST_FUNCTION(when_default_constructing_file_then_no_file_is_created); TEST_FUNCTION(when_default_constructing_file_then_no_file_is_created);
TEST_FUNCTION(when_created_method_is_called_then_file_is_created); TEST_FUNCTION(when_created_method_is_called_then_file_is_created);
TEST_FUNCTION( TEST_FUNCTION(when_created_method_is_called_twice_then_file_is_closed_and_created);
when_created_method_is_called_twice_then_file_is_closed_and_created);
TEST_FUNCTION(when_write_method_is_called_with_no_file_then_nothing_happens); TEST_FUNCTION(when_write_method_is_called_with_no_file_then_nothing_happens);
TEST_FUNCTION(when_flush_method_is_called_with_no_file_then_nothing_happens); TEST_FUNCTION(when_flush_method_is_called_with_no_file_then_nothing_happens);
TEST_FUNCTION(when_data_is_written_to_file_then_contents_are_valid); TEST_FUNCTION(when_data_is_written_to_file_then_contents_are_valid);

@ -20,10 +20,8 @@ static constexpr char test_id1[] = "Test1";
static constexpr char test_id2[] = "Test2"; static constexpr char test_id2[] = "Test2";
//: TODO: older compilers may not have defined this C++11 trait. //: TODO: older compilers may not have defined this C++11 trait.
#if (defined(__clang__) && (__clang_major__ >= 5)) || \ #if (defined(__clang__) && (__clang_major__ >= 5)) || (defined(__GNUG__) && (__GNUC__ >= 5))
(defined(__GNUG__) && (__GNUC__ >= 5)) static_assert(std::is_trivially_copyable<detail::memory_buffer>::value, "Expected to be trivially copyable");
static_assert(std::is_trivially_copyable<detail::memory_buffer>::value,
"Expected to be trivially copyable");
#endif #endif
static bool when_srslog_is_created_then_stdout_and_stderr_sinks_exist() static bool when_srslog_is_created_then_stdout_and_stderr_sinks_exist()
@ -82,8 +80,7 @@ static bool when_non_existent_id_is_passed_then_nothing_is_found()
return true; return true;
} }
static bool static bool when_id_with_sharps_is_passed_then_channel_is_fetched_with_clean_id()
when_id_with_sharps_is_passed_then_channel_is_fetched_with_clean_id()
{ {
log_channel& channel1 = fetch_log_channel("a1#a"); log_channel& channel1 = fetch_log_channel("a1#a");
log_channel& channel2 = fetch_log_channel("a2#a", fetch_stdout_sink(), {}); log_channel& channel2 = fetch_log_channel("a2#a", fetch_stdout_sink(), {});
@ -116,8 +113,7 @@ static bool when_fetching_logger_then_logger_instance_is_returned()
log_channel& info = fetch_log_channel("logger.info"); log_channel& info = fetch_log_channel("logger.info");
log_channel& debug = fetch_log_channel("logger.debug"); log_channel& debug = fetch_log_channel("logger.debug");
auto& logger = auto& logger = fetch_logger<basic_logger>(logger_id, error, warning, info, debug);
fetch_logger<basic_logger>(logger_id, error, warning, info, debug);
ASSERT_EQ(logger.id(), logger_id); ASSERT_EQ(logger.id(), logger_id);
@ -127,8 +123,7 @@ static bool when_fetching_logger_then_logger_instance_is_returned()
static bool when_fetching_basic_logger_then_basic_logger_instance_is_returned() static bool when_fetching_basic_logger_then_basic_logger_instance_is_returned()
{ {
basic_logger& logger1 = fetch_basic_logger(basic_logger_id1); basic_logger& logger1 = fetch_basic_logger(basic_logger_id1);
basic_logger& logger2 = basic_logger& logger2 = fetch_basic_logger(basic_logger_id2, fetch_stdout_sink());
fetch_basic_logger(basic_logger_id2, fetch_stdout_sink());
ASSERT_EQ(logger1.id(), basic_logger_id1); ASSERT_EQ(logger1.id(), basic_logger_id1);
ASSERT_EQ(logger2.id(), basic_logger_id2); ASSERT_EQ(logger2.id(), basic_logger_id2);
@ -205,8 +200,7 @@ static bool when_invalid_id_is_passed_then_no_sink_is_found()
return true; return true;
} }
static bool static bool when_setting_stderr_as_default_then_get_default_returns_stderr_sink()
when_setting_stderr_as_default_then_get_default_returns_stderr_sink()
{ {
set_default_sink(fetch_stderr_sink()); set_default_sink(fetch_stderr_sink());
sink& default_sink = get_default_sink(); sink& default_sink = get_default_sink();
@ -216,8 +210,7 @@ when_setting_stderr_as_default_then_get_default_returns_stderr_sink()
return true; return true;
} }
static bool static bool when_creating_non_default_stdout_and_stderr_sinks_then_they_are_created()
when_creating_non_default_stdout_and_stderr_sinks_then_they_are_created()
{ {
auto& out1 = fetch_stdout_sink("stdout1"); auto& out1 = fetch_stdout_sink("stdout1");
auto& err1 = fetch_stderr_sink("stderr1"); auto& err1 = fetch_stderr_sink("stderr1");
@ -260,11 +253,9 @@ static bool when_setting_custom_sink_twice_then_insertion_fails()
return true; return true;
} }
static bool static bool when_default_formatter_is_installed_then_getter_returns_same_formatter()
when_default_formatter_is_installed_then_getter_returns_same_formatter()
{ {
auto f = auto f = std::unique_ptr<log_formatter>(new test_dummies::log_formatter_dummy);
std::unique_ptr<log_formatter>(new test_dummies::log_formatter_dummy);
set_default_log_formatter(std::move(f)); set_default_log_formatter(std::move(f));
// Dummy formatter does not implement the clone method, thus the unique // Dummy formatter does not implement the clone method, thus the unique
@ -278,35 +269,27 @@ when_default_formatter_is_installed_then_getter_returns_same_formatter()
int main() int main()
{ {
TEST_FUNCTION(when_srslog_is_created_then_stdout_and_stderr_sinks_exist); TEST_FUNCTION(when_srslog_is_created_then_stdout_and_stderr_sinks_exist);
TEST_FUNCTION( TEST_FUNCTION(when_no_installed_default_formatter_then_a_formatter_is_returned);
when_no_installed_default_formatter_then_a_formatter_is_returned);
TEST_FUNCTION(when_no_installed_default_sink_then_stdout_sink_is_used); TEST_FUNCTION(when_no_installed_default_sink_then_stdout_sink_is_used);
TEST_FUNCTION(when_fetching_channel_then_channel_instance_is_returned); TEST_FUNCTION(when_fetching_channel_then_channel_instance_is_returned);
TEST_FUNCTION(when_valid_id_is_passed_then_channel_is_found); TEST_FUNCTION(when_valid_id_is_passed_then_channel_is_found);
TEST_FUNCTION(when_non_existent_id_is_passed_then_nothing_is_found); TEST_FUNCTION(when_non_existent_id_is_passed_then_nothing_is_found);
TEST_FUNCTION( TEST_FUNCTION(when_id_with_sharps_is_passed_then_channel_is_fetched_with_clean_id);
when_id_with_sharps_is_passed_then_channel_is_fetched_with_clean_id);
TEST_FUNCTION(when_channel_already_exists_then_fetch_returns_channel); TEST_FUNCTION(when_channel_already_exists_then_fetch_returns_channel);
TEST_FUNCTION(when_fetching_logger_then_logger_instance_is_returned); TEST_FUNCTION(when_fetching_logger_then_logger_instance_is_returned);
TEST_FUNCTION( TEST_FUNCTION(when_fetching_basic_logger_then_basic_logger_instance_is_returned);
when_fetching_basic_logger_then_basic_logger_instance_is_returned);
TEST_FUNCTION(when_valid_id_and_type_is_passed_then_logger_is_found); TEST_FUNCTION(when_valid_id_and_type_is_passed_then_logger_is_found);
TEST_FUNCTION(when_basic_logger_already_exists_then_fetch_returns_logger); TEST_FUNCTION(when_basic_logger_already_exists_then_fetch_returns_logger);
TEST_FUNCTION( TEST_FUNCTION(when_invalid_id_with_valid_type_is_passed_then_no_logger_is_found);
when_invalid_id_with_valid_type_is_passed_then_no_logger_is_found);
TEST_FUNCTION(when_invalid_id_and_type_is_passed_then_no_logger_is_found); TEST_FUNCTION(when_invalid_id_and_type_is_passed_then_no_logger_is_found);
TEST_FUNCTION( TEST_FUNCTION(when_valid_id_with_invalid_type_is_passed_then_no_logger_is_found);
when_valid_id_with_invalid_type_is_passed_then_no_logger_is_found);
TEST_FUNCTION(when_file_sink_is_fetched_then_sink_instance_is_returned); TEST_FUNCTION(when_file_sink_is_fetched_then_sink_instance_is_returned);
TEST_FUNCTION(when_invalid_id_is_passed_then_no_sink_is_found); TEST_FUNCTION(when_invalid_id_is_passed_then_no_sink_is_found);
TEST_FUNCTION( TEST_FUNCTION(when_setting_stderr_as_default_then_get_default_returns_stderr_sink);
when_setting_stderr_as_default_then_get_default_returns_stderr_sink); TEST_FUNCTION(when_creating_non_default_stdout_and_stderr_sinks_then_they_are_created);
TEST_FUNCTION(
when_creating_non_default_stdout_and_stderr_sinks_then_they_are_created);
TEST_FUNCTION(when_setting_custom_sink_first_time_then_insertion_succeeds); TEST_FUNCTION(when_setting_custom_sink_first_time_then_insertion_succeeds);
TEST_FUNCTION(when_setting_custom_sink_twice_then_insertion_fails); TEST_FUNCTION(when_setting_custom_sink_twice_then_insertion_fails);
TEST_FUNCTION( TEST_FUNCTION(when_default_formatter_is_installed_then_getter_returns_same_formatter);
when_default_formatter_is_installed_then_getter_returns_same_formatter);
return 0; return 0;
} }

Loading…
Cancel
Save