Browse Source

Logging tweaks to use events, in prep for new CLI

default_compile_flags
vector-of-bool 3 years ago
parent
commit
81b3a669ad
5 changed files with 43 additions and 4 deletions
  1. +3
    -0
      src/dds.main.cpp
  2. +8
    -2
      src/dds/util/http/pool.cpp
  3. +17
    -1
      src/dds/util/log.cpp
  4. +9
    -1
      src/dds/util/log.hpp
  5. +6
    -0
      src/dds/util/parallel.hpp

+ 3
- 0
src/dds.main.cpp View File

@@ -8,12 +8,15 @@

#include <boost/leaf/handle_exception.hpp>
#include <fmt/ostream.h>
#include <neo/event.hpp>

#include <filesystem>
#include <iostream>

int main_fn(std::string_view program_name, const std::vector<std::string>& argv) {
dds::log::init_logger();
auto log_subscr = neo::subscribe(&dds::log::ev_log::print);

dds::install_signal_handlers();

dds::cli::options opts;

+ 8
- 2
src/dds/util/http/pool.cpp View File

@@ -97,7 +97,7 @@ struct http_client_impl {
.parse_tail = {},
};

dds_log(trace,
dds_log(debug,
" --> HTTP {} {}://{}:{}{}",
params.method,
origin.protocol,
@@ -154,7 +154,7 @@ struct http_client_impl {
disconnect = true;
}
_peer_disconnected = disconnect;
dds_log(trace, " <-- HTTP {} {}", r.status, r.status_message);
dds_log(debug, " <-- HTTP {} {}", r.status, r.status_message);
return r;
}
};
@@ -229,10 +229,16 @@ http_client http_pool::client_for_origin(const network_origin& origin) {
ret._pool = _impl;
if (iter == _impl->_clients.end()) {
// Nothing for this origin yet
dds_log(debug, "Opening new connection to {}://{}:{}", origin.protocol, origin.hostname, origin.port);
auto ptr = std::make_shared<detail::http_client_impl>(origin);
ptr->connect();
ret._impl = ptr;
} else {
dds_log(debug,
"Reusing existing connection to {}://{}:{}",
origin.protocol,
origin.hostname,
origin.port);
ret._impl = iter->second;
_impl->_clients.erase(iter);
}

+ 17
- 1
src/dds/util/log.cpp View File

@@ -1,6 +1,7 @@
#include "./log.hpp"

#include <neo/assert.hpp>
#include <neo/event.hpp>

#include <spdlog/spdlog.h>

@@ -23,6 +24,8 @@ void dds::log::init_logger() noexcept {
spdlog::set_pattern("[%^%-5l%$] %v");
}

void dds::log::ev_log::print() const noexcept { log_print(level, message); }

void dds::log::log_print(dds::log::level l, std::string_view msg) noexcept {
static auto logger_inst = [] {
auto logger = spdlog::default_logger_raw();
@@ -51,5 +54,18 @@ void dds::log::log_print(dds::log::level l, std::string_view msg) noexcept {
neo_assert_always(invariant, false, "Invalid log level", msg, int(l));
}();

logger_inst->log(lvl, "{}", msg);
logger_inst->log(lvl, msg);
}

void dds::log::log_emit(dds::log::ev_log ev) noexcept {
if (!neo::get_event_subscriber<ev_log>()) {
thread_local bool did_warn = false;
if (!did_warn) {
log_print(level::warn,
"The calling thread issued a log message, but there is no subscriber "
"listening for it. The log message will be dropped. This is a bug!");
did_warn = true;
}
}
neo::emit(ev);
}

+ 9
- 1
src/dds/util/log.hpp View File

@@ -18,7 +18,15 @@ enum class level : int {

inline level current_log_level = level::info;

struct ev_log {
log::level level;
std::string_view message;

void print() const noexcept;
};

void log_print(level l, std::string_view s) noexcept;
void log_emit(ev_log) noexcept;

void init_logger() noexcept;

@@ -33,7 +41,7 @@ template <formattable... Args>
void log(level l, std::string_view s, const Args&... args) noexcept {
if (int(l) >= int(current_log_level)) {
auto message = fmt::format(s, args...);
log_print(l, message);
log_emit(ev_log{l, message});
}
}


+ 6
- 0
src/dds/util/parallel.hpp View File

@@ -1,5 +1,9 @@
#pragma once

#include <dds/util/log.hpp>

#include <neo/event.hpp>

#include <algorithm>
#include <iterator>
#include <mutex>
@@ -23,6 +27,8 @@ bool parallel_run(Range&& rng, int n_jobs, Func&& fn) {
std::vector<std::exception_ptr> exceptions;

auto run_one = [&]() mutable {
auto log_subscr = neo::subscribe(&log::ev_log::print);

while (true) {
std::unique_lock lk{mut};
if (!exceptions.empty()) {

Loading…
Cancel
Save