Skip to content

Commit

Permalink
Make println utility reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
Neverlord committed Jul 30, 2023
1 parent f338a5f commit 76d491c
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 77 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ set(BROKER_SRC
src/internal/metric_view.cc
src/internal/peering.cc
src/internal/pending_connection.cc
src/internal/println.cc
src/internal/prometheus.cc
src/internal/store_actor.cc
src/internal/web_socket.cc
Expand Down
90 changes: 90 additions & 0 deletions include/broker/internal/println.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#pragma once

#include "broker/convert.hh"

#include <caf/deep_to_string.hpp>
#include <caf/term.hpp>

#include <chrono>
#include <iostream>
#include <mutex>
#include <string>

namespace broker::internal {

std::mutex& println_mtx();

inline void do_print(std::ostream& ostr, const char* x) {
ostr << x;
}

inline void do_print(std::ostream& ostr, std::string_view x) {
ostr << x;
}

inline void do_print(std::ostream& ostr, const caf::term& x) {
ostr << x;
}

template <class T>
void do_print(std::ostream& ostr, const T& x) {
if constexpr (detail::has_convert_v<T, std::string>) {
std::string tmp;
convert(x, tmp);
do_print(ostr, tmp);
} else {
auto tmp = caf::deep_to_string(x);
do_print(ostr, tmp);
}
}

template <class... Ts>
void do_print_all(std::ostream& ostr, const Ts&... xs) {
(do_print(ostr, xs), ...);
}

} // namespace broker::internal

namespace broker::internal::out {

/// Prints a sequence of values to standard output.
template <class... Ts>
void println(Ts&&... xs) {
std::unique_lock guard{println_mtx()};
do_print_all(std::cout, std::forward<Ts>(xs)...);
std::cout << caf::term::reset_endl;
}

} // namespace broker::internal::out

namespace broker::internal::err {

/// Prints a sequence of values to standard error.
template <class... Ts>
void println(Ts&&... xs) {
std::unique_lock guard{println_mtx()};
do_print_all(std::cerr, caf::term::red, std::forward<Ts>(xs)...);
std::cerr << caf::term::reset_endl;
}

} // namespace broker::internal::err

namespace broker::internal::verbose {

bool enabled();

void enabled(bool value);

/// Prints a sequence of values to standard output if verbose logging is
/// enabled.
template <class... Ts>
void println(Ts&&... xs) {
if (!enabled())
return;
std::unique_lock guard{println_mtx()};
do_print_all(std::cout, caf::term::blue, std::chrono::system_clock::now(),
": ", std::forward<Ts>(xs)...);
std::cout << caf::term::reset_endl;
}

} // namespace broker::internal::verbose
82 changes: 5 additions & 77 deletions src/broker-node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "broker/endpoint_id.hh"
#include "broker/internal/configuration_access.hh"
#include "broker/internal/endpoint_access.hh"
#include "broker/internal/println.hh"
#include "broker/internal/type_id.hh"
#include "broker/publisher.hh"
#include "broker/status.hh"
Expand All @@ -47,6 +48,9 @@ using broker::topic;
using broker::internal::native;

namespace atom = broker::internal::atom;
namespace err = broker::internal::err;
namespace out = broker::internal::out;
namespace verbose = broker::internal::verbose;

// -- additional message and atom types ----------------------------------------

Expand Down Expand Up @@ -96,82 +100,6 @@ bool convert(const caf::uri& from, network_info& to) {

} // namespace broker

// -- I/O utility --------------------------------------------------------------

namespace detail {

namespace {

std::mutex ostream_mtx;

} // namespace

int print_impl(std::ostream& ostr, const char* x) {
ostr << x;
return 0;
}

int print_impl(std::ostream& ostr, const string& x) {
ostr << x;
return 0;
}

int print_impl(std::ostream& ostr, const caf::term& x) {
ostr << x;
return 0;
}

template <class T>
int print_impl(std::ostream& ostr, const T& x) {
return print_impl(ostr, caf::deep_to_string(x));
}

template <class... Ts>
void println(std::ostream& ostr, Ts&&... xs) {
std::unique_lock<std::mutex> guard{ostream_mtx};
std::initializer_list<int>{print_impl(ostr, std::forward<Ts>(xs))...};
ostr << caf::term::reset_endl;
}

} // namespace detail

namespace out {

template <class... Ts>
void println(Ts&&... xs) {
::detail::println(std::cout, std::forward<Ts>(xs)...);
}

} // namespace out

namespace err {

template <class... Ts>
void println(Ts&&... xs) {
::detail::println(std::cerr, caf::term::red, node_name, ": ",
std::forward<Ts>(xs)...);
}

} // namespace err

namespace verbose {

namespace {

std::atomic<bool> enabled;

} // namespace

template <class... Ts>
void println(Ts&&... xs) {
if (enabled)
::detail::println(std::clog, caf::term::blue,
std::chrono::system_clock::now(), " ", node_name, ": ",
std::forward<Ts>(xs)...);
}

} // namespace verbose

// -- CAF setup ----------------------------------------------------------------

using namespace caf;
Expand Down Expand Up @@ -460,7 +388,7 @@ int main(int argc, char** argv) try {
// Enable verbose output if demanded by user.
actor verbose_logger;
if (get_or(ep, "verbose", false)) {
verbose::enabled = true;
verbose::enabled(true);
// Launch background worker that prints status and error events when running
// in verbose mode.
ep.subscribe({topic::errors(), topic::statuses()},
Expand Down
31 changes: 31 additions & 0 deletions src/internal/println.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "broker/internal/println.hh"

#include <atomic>

namespace {

std::mutex println_mtx_instance;

std::atomic<bool> verbose_output;

} // namespace

namespace broker::internal {

std::mutex& println_mtx() {
return println_mtx_instance;
}

} // namespace broker::internal

namespace broker::internal::verbose {

bool enabled() {
return verbose_output.load();
}

void enabled(bool value) {
verbose_output = value;
}

} // namespace broker::internal::verbose

0 comments on commit 76d491c

Please sign in to comment.