Skip to content

Commit

Permalink
Support logging of std::error_code
Browse files Browse the repository at this point in the history
  • Loading branch information
erenon committed Apr 8, 2021
1 parent 616b949 commit df83a9c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ add_example(TscClock)
add_inttest(LoggingTimePoint)
add_inttest(LoggingEnums)
add_inttest(LoggingAdaptedStructs)
add_inttest(LoggingErrorCode)
add_inttest(NamedWriters)
add_inttest(SeverityControl)
add_inttest(Categories)
Expand Down
8 changes: 8 additions & 0 deletions doc/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ The pretty printed text format is affected by the `-d` flag when using [bread](#
If `%d` (localtime) appears first in the `-f` (format) parameter, the time point is converted to producer-local timezone
(this is the default). If `%u` (UTC) appears first, the time point is shown in UTC.

### error_code

`std::error_code` objects can be logged after adoption by including a header file:

[catchfile test/integration/LoggingErrorCode.cpp ec]

The error code is serialized in terms of the `message` member, therefore it may result in memory allocations.

### optional

If C++17 is available, the standard optional with a loggable `value_type` can be made loggable:
Expand Down
5 changes: 3 additions & 2 deletions include/binlog/PrettyPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ bool PrettyPrinter::printStruct(detail::OstreamBuffer& out, mserialize::Visitor:
}

if ((sb.name == "std::filesystem::path" && sb.tag == "`str'[c")
|| (sb.name == "std::filesystem::directory_entry" && sb.tag == "`path'{std::filesystem::path`str'[c}"))
{
|| (sb.name == "std::filesystem::directory_entry" && sb.tag == "`path'{std::filesystem::path`str'[c}")
|| (sb.name == "std::error_code" && sb.tag == "`message'[c")
) {
const std::uint32_t size = input.read<std::uint32_t>();
out.write(input.view(size), size);
return true;
Expand Down
10 changes: 10 additions & 0 deletions include/binlog/adapt_stderrorcode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef BINLOG_ADAPT_STDERRORCODE_HPP
#define BINLOG_ADAPT_STDERRORCODE_HPP

// Make std::error_code loggable by including this file

#include <binlog/adapt_struct.hpp>

BINLOG_ADAPT_STRUCT(std::error_code, message)

#endif // BINLOG_ADAPT_STDERRORCODE_HPP
1 change: 1 addition & 0 deletions test/integration/IntegrationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ TEST_CASE("LoggingTuples") { runReadDiff("LoggingTuples", "%m"); }
TEST_CASE("LoggingEnums") { runReadDiff("LoggingEnums", "%m"); }
TEST_CASE("LoggingAdaptedStructs") { runReadDiff("LoggingAdaptedStructs", "%m"); }
TEST_CASE("LoggingTimePoint") { runReadDiff("LoggingTimePoint", "%m"); }
TEST_CASE("LoggingErrorCode") { runReadDiff("LoggingErrorCode", "%m"); }
TEST_CASE("NamedWriters") { runReadDiff("NamedWriters", "%n %m"); }
TEST_CASE("SeverityControl") { runReadDiff("SeverityControl", "%S %m"); }
TEST_CASE("Categories") { runReadDiff("Categories", "%C %n %m"); }
Expand Down
36 changes: 36 additions & 0 deletions test/integration/LoggingErrorCode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//[ec
#include <binlog/adapt_stderrorcode.hpp> // must be included to log std::error_code
//]

#include <binlog/binlog.hpp>

#include <iostream>
#include <string>
#include <system_error>

class error_category : public std::error_category
{
public:
const char* name() const noexcept override { return "error_category"; }
std::string message(int) const override { return "Success"; }
};

int main()
{
//[ec

std::error_code ec;
//]

// Use a custom error category to make sure the message is platform-agnostic
error_category category;
ec.assign(0, category);

//[ec
BINLOG_INFO("ec: {}", ec);
// Outputs: ec: Success
//]

binlog::consume(std::cout);
return 0;
}

0 comments on commit df83a9c

Please sign in to comment.