Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/topic/neverlord/println'
Browse files Browse the repository at this point in the history
* origin/topic/neverlord/println:
  Bump dockerfile images
  Fix endless recursion in do_print
  Make println utility reusable
  • Loading branch information
timwoj committed Aug 24, 2023
2 parents f9af5f0 + 487895f commit db01d8f
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 84 deletions.
8 changes: 8 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
2.7.0-dev.25 | 2023-08-24 15:32:31 -0700

* Bump dockerfile images (Dominik Charousset, Corelight)

* Fix endless recursion in do_print (Dominik Charousset, Corelight)

* Make println utility reusable (Dominik Charousset, Corelight)

2.7.0-dev.21 | 2023-08-15 17:39:57 +0100

* Build broker bindings for older versions of Python (Johanna Amann, Corelight)
Expand Down
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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.0-dev.21
2.7.0-dev.25
2 changes: 1 addition & 1 deletion ci/debian-10/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ENV DEBIAN_FRONTEND="noninteractive" TZ="America/Los_Angeles"

# A version field to invalide Cirrus's build cache when needed, as suggested in
# https://github.com/cirruslabs/cirrus-ci-docs/issues/544#issuecomment-566066822
ENV DOCKERFILE_VERSION 20220519
ENV DOCKERFILE_VERSION 20230813

ENV CMAKE_DIR "/opt/cmake"
ENV CMAKE_VERSION "3.19.1"
Expand Down
2 changes: 1 addition & 1 deletion ci/debian-11/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ENV DEBIAN_FRONTEND="noninteractive" TZ="America/Los_Angeles"

# A version field to invalide Cirrus's build cache when needed, as suggested in
# https://github.com/cirruslabs/cirrus-ci-docs/issues/544#issuecomment-566066822
ENV DOCKERFILE_VERSION 20230728
ENV DOCKERFILE_VERSION 20230813

RUN apt-get update && apt-get -y install \
cmake \
Expand Down
2 changes: 1 addition & 1 deletion ci/fedora-37/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM fedora:37

# A version field to invalide Cirrus's build cache when needed, as suggested in
# https://github.com/cirruslabs/cirrus-ci-docs/issues/544#issuecomment-566066822
ENV DOCKERFILE_VERSION 20221127
ENV DOCKERFILE_VERSION 20230813

RUN dnf -y install \
cmake \
Expand Down
2 changes: 1 addition & 1 deletion ci/fedora-38/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM fedora:38

# A version field to invalide Cirrus's build cache when needed, as suggested in
# https://github.com/cirruslabs/cirrus-ci-docs/issues/544#issuecomment-566066822
ENV DOCKERFILE_VERSION 20230612
ENV DOCKERFILE_VERSION 20230813

RUN dnf -y install \
cmake \
Expand Down
2 changes: 1 addition & 1 deletion ci/ubuntu-22.04/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ENV DEBIAN_FRONTEND="noninteractive" TZ="America/Los_Angeles"

# A version field to invalide Cirrus's build cache when needed, as suggested in
# https://github.com/cirruslabs/cirrus-ci-docs/issues/544#issuecomment-566066822
ENV DOCKERFILE_VERSION 20220615
ENV DOCKERFILE_VERSION 20230813

RUN apt-get update && apt-get -y install \
cmake \
Expand Down
2 changes: 1 addition & 1 deletion ci/ubuntu-22.10/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ENV DEBIAN_FRONTEND="noninteractive" TZ="America/Los_Angeles"

# A version field to invalide Cirrus's build cache when needed, as suggested in
# https://github.com/cirruslabs/cirrus-ci-docs/issues/544#issuecomment-566066822
ENV DOCKERFILE_VERSION 20230728
ENV DOCKERFILE_VERSION 20230813

RUN apt-get update && apt-get -y install \
cmake \
Expand Down
92 changes: 92 additions & 0 deletions include/broker/internal/println.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#pragma once

#include "broker/convert.hh"

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

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

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>
std::enable_if_t<!std::is_convertible_v<T, std::string_view>>
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 db01d8f

Please sign in to comment.