Skip to content

Commit

Permalink
Minor. Logging: make create_printer public
Browse files Browse the repository at this point in the history
  • Loading branch information
BerengerBerthoul committed Oct 19, 2023
1 parent 9ad2954 commit 0e46452
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
11 changes: 0 additions & 11 deletions std_e/logging/build_printer_from_strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,4 @@ build_printer(const std::string& printer_name, const std::vector<std::string>& p
}


auto
add_printer(const std::string& logger_name, const std::string& printer_type, const std::vector<std::string>& printer_args) -> void {
const std::string error_msg_context = "in add_printer";

auto p = build_printer(printer_type, printer_args, error_msg_context);

logger& l = get_logger(logger_name);
l.printers.emplace_back(std::move(p));
}


} // std_e
3 changes: 0 additions & 3 deletions std_e/logging/build_printer_from_strings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,5 @@ _build_printer_impl_1_string(
auto
build_printer(const std::string& printer_name, const std::vector<std::string>& printer_args, const std::string& error_msg_context) -> std::unique_ptr<printer>;

auto
add_printer(const std::string& logger_name, const std::string& printer_type, const std::vector<std::string>& printer_args) -> void;


} // std_e
13 changes: 12 additions & 1 deletion std_e/logging/create_loggers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace std_e {


auto
create_printer(const std::string& printer_desc, const std::string& error_msg_context) {
create_printer(const std::string& printer_desc, const std::string& error_msg_context) -> std::unique_ptr<printer> {
auto n_open_paren = std::count(begin(printer_desc),end(printer_desc),'(');
auto n_close_paren = std::count(begin(printer_desc),end(printer_desc),')');
STD_E_ASSERT(n_close_paren == n_open_paren); // precondition of the function
Expand Down Expand Up @@ -94,4 +94,15 @@ create_loggers(const std::string& conf, const std::string& error_msg_context) ->
}


auto
add_printer(const std::string& logger_name, const std::string& printer_desc) -> void {
const std::string error_msg_context = "in add_printer";

auto p = create_printer(printer_desc, error_msg_context);

logger& l = get_logger(logger_name);
l.printers.emplace_back(std::move(p));
}


} // std_e
2 changes: 2 additions & 0 deletions std_e/logging/create_loggers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
namespace std_e {


auto create_printer(const std::string& printer_desc, const std::string& error_msg_context) -> std::unique_ptr<printer>;
auto create_loggers(const std::string& conf, const std::string& error_msg_context) -> std::vector<logger>;

auto add_printer(const std::string& logger_name, const std::string& printer_desc) -> void;

} // std_e
13 changes: 9 additions & 4 deletions std_e/logging/printer_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,21 @@ class file_printer : public printer {
file_printer& operator=(file_printer&&) = default;

file_printer(const std::string& file_name)
: file(std::ofstream(file_name.c_str(), std::fstream::out))
: _file_name(file_name)
, _file(std::ofstream(file_name.c_str(), std::fstream::out))
{}
~file_printer() {
file << std::flush;
_file << std::flush;
}
auto file_name() const {
return _file_name;
}
auto log(const std::string& msg) -> void override {
file << msg;
_file << msg;
}
private:
std::ofstream file;
std::string _file_name;
std::ofstream _file;
};

class recording_printer : public printer {
Expand Down
12 changes: 12 additions & 0 deletions std_e/logging/test/create_loggers.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
#include "std_e/logging/printer_common.hpp"


TEST_CASE("create_printer") {
std::string printer_0_desc = "stdout_printer";
auto printer_0 = std_e::create_printer(printer_0_desc, "<context>");
CHECK_NOTHROW( dynamic_cast<std_e::stdout_printer&>(*printer_0) );

std::string printer_1_desc = "file_printer('my_file.log')";
auto printer_1 = std_e::create_printer(printer_1_desc, "<context>");
CHECK_NOTHROW( dynamic_cast<std_e::file_printer&>(*printer_1) );
auto& fp = dynamic_cast<std_e::file_printer&>(*printer_1);
CHECK(fp.file_name() == "my_file.log");
}

TEST_CASE("create_loggers") {
std::string conf =
"my_logger : stdout_printer\n"
Expand Down

0 comments on commit 0e46452

Please sign in to comment.