Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new logger API #414

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libbroker/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set(BROKER_SRC
broker/entity_id.cc
broker/envelope.cc
broker/error.cc
broker/event_observer.cc
broker/filter_type.cc
broker/format/bin.cc
broker/format/json.cc
Expand All @@ -64,6 +65,7 @@ set(BROKER_SRC
broker/internal/web_socket.cc
broker/internal/wire_format.cc
broker/internal_command.cc
broker/logger.cc
broker/mailbox.cc
broker/network_info.cc
broker/overflow_policy.cc
Expand Down
9 changes: 9 additions & 0 deletions libbroker/broker/backend.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
#include <string>

namespace broker {

Expand All @@ -25,4 +26,12 @@ bool inspect(Inspector& f, backend& x) {
return f.apply(get, set);
}

/// @relates backend
inline void convert(backend x, std::string& str) {
if (x == backend::memory)
str = "memory";
else if (x == backend::sqlite)
str = "sqlite";
}

} // namespace broker
8 changes: 8 additions & 0 deletions libbroker/broker/command_envelope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,12 @@ expected<envelope_ptr> command_envelope::deserialize(
return {std::move(result)};
}

void convert(const command_envelope_ptr& cmd, std::string& str) {
if (!cmd) {
str = "null";
return;
}
str = cmd->stringify();
}

} // namespace broker
3 changes: 3 additions & 0 deletions libbroker/broker/command_envelope.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ public:
/// @relates command_envelope
using command_envelope_ptr = intrusive_ptr<const command_envelope>;

/// @relates command_envelope
void convert(const command_envelope_ptr& cmd, std::string& str);

} // namespace broker
5 changes: 3 additions & 2 deletions libbroker/broker/convert.hh
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ template <class T>
std::enable_if_t<
detail::has_convert_v<T, std::string> // must have a convert function
&& !std::is_arithmetic_v<T> // avoid ambiguitiy with default overloads
&& !std::is_convertible_v<T, std::string>, // avoid ambiguity with
// conversion-to-string
&& !std::is_convertible_v<T, std::string> // avoid ambiguity with
// conversion-to-string
&& !std::is_same_v<T, std::string_view>,
std::ostream&>
operator<<(std::ostream& os, const T& x) {
std::string str;
Expand Down
18 changes: 9 additions & 9 deletions libbroker/broker/data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ data data::from_type(data::type t) {
}
}

void data::convert_to(std::string& str) const {
txt_v1::encode(*this, std::back_inserter(str));
}

bool data::convert_to(endpoint_id& node) const {
return is<std::string>(*this) && convert(get<std::string>(*this), node);
}

namespace {

vector empty_vector;
Expand Down Expand Up @@ -354,7 +362,7 @@ struct data_converter {
}

void operator()(timespan ts) {
convert(ts.count(), str);
convert(data{ts.count()}, str);
str += "ns";
}

Expand Down Expand Up @@ -391,14 +399,6 @@ void convert(const table& x, std::string& str) {
txt_v1::encode(x, std::back_inserter(str));
}

void convert(const data& x, std::string& str) {
txt_v1::encode(x, std::back_inserter(str));
}

bool convert(const data& x, endpoint_id& node) {
return is<std::string>(x) && convert(get<std::string>(x), node);
}

bool convert(const endpoint_id& node, data& d) {
if (node)
d = to_string(node);
Expand Down
16 changes: 14 additions & 2 deletions libbroker/broker/data.hh
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ public:

// -- conversions ------------------------------------------------------------

void convert_to(std::string& str) const;

bool convert_to(endpoint_id& node) const;

/// Retrieves the @c boolean value or returns @p fallback if this object does
/// not contain a @c boolean.
bool to_boolean(bool fallback = false) const noexcept {
Expand Down Expand Up @@ -476,10 +480,18 @@ bool inspect(Inspector& f, broker::table& tbl) {
}

/// @relates data
void convert(const data& x, std::string& str);
template <class Data>
std::enable_if_t<std::is_same_v<Data, data>> convert(const Data& x,
std::string& str) {
x.convert_to(str);
}

/// @relates data
bool convert(const data& x, endpoint_id& node);
template <class Data>
std::enable_if_t<std::is_same_v<Data, data>, bool> convert(const Data& x,
endpoint_id& node) {
return x.convert_to(node);
}

/// @relates data
bool convert(const endpoint_id& node, data& x);
Expand Down
36 changes: 20 additions & 16 deletions libbroker/broker/detail/filesystem.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <vector>

#include "broker/config.hh"
#include "broker/internal/logger.hh"
#include "broker/logger.hh"

#ifdef BROKER_HAS_STD_FILESYSTEM

Expand All @@ -31,11 +31,13 @@ inline bool is_file(const path& p) {

inline bool mkdirs(const path& p) {
std::error_code ec;
auto rval = std::filesystem::create_directories(p, ec);
if (!rval)
BROKER_ERROR("failed to make directories: " << p.native() << ":"
<< ec.message());
return rval;
if (!std::filesystem::create_directories(p, ec)) {
log::core::error("cannot-create-directory",
"failed to create directory {}: {}", p.string(),
ec.message());
return false;
}
return true;
}

inline path dirname(path p) {
Expand All @@ -45,20 +47,22 @@ inline path dirname(path p) {

inline bool remove(const path& p) {
std::error_code ec;
auto rval = std::filesystem::remove(p, ec);
if (!rval)
BROKER_ERROR("failed to remove path: " << p.native() << ":"
<< ec.message());
return rval;
if (!std::filesystem::remove(p, ec)) {
log::core::error("cannot-remove-path", "failed to remove {}: {}",
p.string(), ec.message());
return false;
}
return true;
}

inline bool remove_all(const path& p) {
std::error_code ec;
auto rval = std::filesystem::remove_all(p, ec);
if (!rval)
BROKER_ERROR("failed to recursively remove path: " << p.native() << ":"
<< ec.message());
return rval;
if (!std::filesystem::remove_all(p, ec)) {
log::core::error("cannot-remove-path", "failed to remove {}: {}",
p.string(), ec.message());
return false;
}
return true;
}

} // namespace broker::detail
Expand Down
20 changes: 11 additions & 9 deletions libbroker/broker/detail/flare.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "broker/config.hh"
#include "broker/detail/assert.hh"
#include "broker/internal/logger.hh"
#include "broker/logger.hh"

#ifdef BROKER_WINDOWS

Expand Down Expand Up @@ -79,18 +79,22 @@ struct stack_buffer {
flare::flare() {
auto maybe_fds = caf::net::make_pipe();
if (!maybe_fds) {
BROKER_ERROR("failed to create pipe: " << maybe_fds.error());
log::core::critical("cannot-create-pipe", "failed to create pipe: {}",
maybe_fds.error());
abort();
}
auto [first, second] = *maybe_fds;
fds_[0] = first.id;
fds_[1] = second.id;
if (auto err = caf::net::child_process_inherit(first, false))
BROKER_ERROR("failed to set flare fd 0 CLOEXEC: " << err);
log::core::error("cannot-set-cloexec",
"failed to set flare fd 0 CLOEXEC: {}", err);
if (auto err = caf::net::child_process_inherit(second, false))
BROKER_ERROR("failed to set flare fd 1 CLOEXEC: " << err);
log::core::error("cannot-set-cloexec",
"failed to set flare fd 1 CLOEXEC: {}", err);
if (auto err = caf::net::nonblocking(first, true)) {
BROKER_ERROR("failed to set flare fd 0 NONBLOCK: " << err);
log::core::critical("cannot-set-nonblock",
"failed to set flare fd 0 NONBLOCK: {}", err);
std::terminate();
}
// Do not set the write handle to nonblock, because we want the producer to
Expand All @@ -114,7 +118,8 @@ void flare::fire(size_t num) {
int len = static_cast<int>(std::min(remaining, stack_buffer_size));
auto n = PIPE_WRITE(fds_[1], tmp.data, len);
if (n <= 0) {
BROKER_ERROR("unable to write flare pipe!");
log::core::error("cannot-write-flare-pipe",
"failed to write to flare pipe: {}", n);
std::terminate();
}
remaining -= static_cast<size_t>(n);
Expand Down Expand Up @@ -145,10 +150,8 @@ bool flare::extinguish_one() {
}

void flare::await_one() {
BROKER_TRACE("");
pollfd p = {fds_[0], POLLIN, 0};
for (;;) {
BROKER_DEBUG("polling");
auto n = ::poll(&p, 1, -1);
if (n < 0 && !try_again_later())
std::terminate();
Expand All @@ -160,7 +163,6 @@ void flare::await_one() {
}

bool flare::await_one_impl(int ms_timeout) {
BROKER_TRACE("");
pollfd p = {fds_[0], POLLIN, 0};
auto n = ::poll(&p, 1, ms_timeout);
if (n < 0 && !try_again_later())
Expand Down
Loading
Loading