Skip to content

Commit

Permalink
Merge pull request #898 from krasznaa/LoggingTweaks-main-20250227
Browse files Browse the repository at this point in the history
Logging Tweaks, main branch (2025.02.27.)
  • Loading branch information
krasznaa authored Feb 27, 2025
1 parent bc90eb3 commit 5f93fe3
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 67 deletions.
4 changes: 3 additions & 1 deletion core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# TRACCC library, part of the ACTS project (R&D line)
#
# (c) 2021-2024 CERN for the benefit of the ACTS project
# (c) 2021-2025 CERN for the benefit of the ACTS project
#
# Mozilla Public License Version 2.0

Expand Down Expand Up @@ -44,6 +44,8 @@ traccc_add_library( traccc_core core TYPE SHARED
"include/traccc/utils/memory_resource.hpp"
"include/traccc/utils/seed_generator.hpp"
"include/traccc/utils/subspace.hpp"
"include/traccc/utils/logging.hpp"
"src/utils/logging.cpp"
# Clusterization algorithmic code.
"include/traccc/clusterization/details/sparse_ccl.hpp"
"include/traccc/clusterization/impl/sparse_ccl.ipp"
Expand Down
31 changes: 22 additions & 9 deletions core/include/traccc/utils/logging.hpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2021-2022 CERN for the benefit of the ACTS project
* (c) 2025 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

#pragma once

// Acts include(s).
#include <Acts/Utilities/Logger.hpp>

namespace traccc {

/// Use the @c Acts::Logging namespace.
namespace Logging = ::Acts::Logging;

/// Use the @c Acts::Logger type.
using Logger = ::Acts::Logger;

inline std::unique_ptr<const Logger> getDefaultLogger(
const std::string& name, const Logging::Level& lvl,
std::ostream* log_stream = &std::cout) {
return ::Acts::getDefaultLogger(name, lvl, log_stream);
}
/// Construct a logger with default settings for this project
///
/// @param name the name of the log writer
/// @param lvl the log level
/// @param log_stream the stream to write the log to
///
/// @return a unique pointer to the logger
///
std::unique_ptr<const Logger> getDefaultLogger(
const std::string& name, const Logging::Level& lvl = Logging::INFO,
std::ostream* log_stream = &std::cout);

/// Construct a dummy logger that does nothing
///
/// @return a reference to the dummy logger
///
const Logger& getDummyLogger();

inline const Logger& getDummyLogger() {
return ::Acts::getDummyLogger();
}
} // namespace traccc

#define TRACCC_LOCAL_LOGGER(x) ACTS_LOCAL_LOGGER(x)
Expand Down
66 changes: 66 additions & 0 deletions core/src/utils/logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2025 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

// Local include(s).
#include "traccc/utils/logging.hpp"

// System include(s).
#include <functional>
#include <iostream>
#include <sstream>
#include <stdexcept>

namespace {

/// Decorator to split the output into separate lines
class split_output_decorator final : public traccc::Logging::OutputDecorator {

public:
explicit split_output_decorator(
std::unique_ptr<traccc::Logging::OutputPrintPolicy> print_policy)
: traccc::Logging::OutputDecorator(std::move(print_policy)) {}

void flush(const traccc::Logging::Level& lvl,
const std::string& input) override {

// Split the message into separate lines.
std::istringstream iss(input);
for (std::string line; std::getline(iss, line);) {
m_wrappee->flush(lvl, line);
}
}

std::unique_ptr<OutputPrintPolicy> clone(
const std::string& name) const override {
return std::make_unique<split_output_decorator>(m_wrappee->clone(name));
}
};

} // namespace

namespace traccc {

std::unique_ptr<const Logger> getDefaultLogger(const std::string& name,
const Logging::Level& lvl,
std::ostream* log_stream) {

return std::make_unique<const Logger>(
std::make_unique<::split_output_decorator>(
std::make_unique<Logging::LevelOutputDecorator>(
std::make_unique<Logging::NamedOutputDecorator>(
std::make_unique<Logging::TimedOutputDecorator>(
std::make_unique<Logging::DefaultPrintPolicy>(
log_stream)),
name, 30))),
std::make_unique<Logging::DefaultFilterPolicy>(lvl));
}

const Logger& getDummyLogger() {
return ::Acts::getDummyLogger();
}

} // namespace traccc
8 changes: 3 additions & 5 deletions examples/options/src/program_options.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2024 CERN for the benefit of the ACTS project
* (c) 2024-2025 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand Down Expand Up @@ -57,17 +57,15 @@ program_options::program_options(
}

// Tell the user what's happening.
TRACCC_INFO("Running " << description);
TRACCC_INFO("\nRunning " << description);

configuration_list cl;

for (const auto& opt : options) {
cl.add_child(opt.get().as_printable());
}

cl.print();

std::cout << std::endl;
TRACCC_INFO("\n" << cl.print() << "\n");
}

} // namespace traccc::opts
55 changes: 30 additions & 25 deletions examples/utils/include/traccc/examples/utils/printable.hpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2023-2024 CERN for the benefit of the ACTS project
* (c) 2023-2025 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

#pragma once

#include <iostream>
// System include(s).
#include <iosfwd>
#include <memory>
#include <string>
#include <string_view>
#include <vector>

namespace traccc {
class configuration_printable {
public:
virtual void print() const;
virtual std::string print() const;

virtual void print_impl(std::string self_prefix, std::string child_prefix,
std::size_t prefix_len,
std::size_t max_key_width) const = 0;
virtual void print_impl(const std::string& self_prefix,
const std::string& child_prefix,
std::size_t prefix_len, std::size_t max_key_width,
std::ostream& out) const = 0;

virtual std::size_t get_max_key_width_impl() const = 0;

Expand All @@ -28,17 +31,18 @@ class configuration_printable {

class configuration_category final : public configuration_printable {
public:
explicit configuration_category(std::string n);
explicit configuration_category(std::string_view n);

void add_child(std::unique_ptr<configuration_printable>&& elem);
void add_child(std::unique_ptr<configuration_printable> elem);

void print_impl(std::string self_prefix, std::string child_prefix,
std::size_t prefix_len,
std::size_t max_key_width) const final;
void print_impl(const std::string& self_prefix,
const std::string& child_prefix, std::size_t prefix_len,
std::size_t max_key_width,
std::ostream& out) const override;

std::size_t get_max_key_width_impl() const final;
std::size_t get_max_key_width_impl() const override;

~configuration_category() final;
~configuration_category() override;

private:
std::string name;
Expand All @@ -49,31 +53,32 @@ class configuration_list final : public configuration_printable {
public:
configuration_list();

void add_child(std::unique_ptr<configuration_printable>&& elem);
void add_child(std::unique_ptr<configuration_printable> elem);

void print_impl(std::string self_prefix, std::string child_prefix,
std::size_t prefix_len,
std::size_t max_key_width) const final;
void print_impl(const std::string& self_prefix,
const std::string& child_prefix, std::size_t prefix_len,
std::size_t max_key_width,
std::ostream& out) const override;

std::size_t get_max_key_width_impl() const final;
std::size_t get_max_key_width_impl() const override;

~configuration_list() final;
~configuration_list() override;

private:
std::vector<std::unique_ptr<configuration_printable>> elements;
};

class configuration_kv_pair final : public configuration_printable {
public:
configuration_kv_pair(std::string k, std::string v);
configuration_kv_pair(std::string_view k, std::string_view v);

void print_impl(std::string self_prefix, std::string,
std::size_t prefix_len,
std::size_t max_key_width) const final;
void print_impl(const std::string& self_prefix, const std::string&,
std::size_t prefix_len, std::size_t max_key_width,
std::ostream& out) const override;

std::size_t get_max_key_width_impl() const final;
std::size_t get_max_key_width_impl() const override;

~configuration_kv_pair() final;
~configuration_kv_pair() override;

private:
std::string key;
Expand Down
59 changes: 32 additions & 27 deletions examples/utils/src/printable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,43 @@
* Mozilla Public License Version 2.0
*/

// Local include(s).
#include "traccc/examples/utils/printable.hpp"

#include <iostream>
#include <memory>
#include <string>
#include <vector>
// System include(s).
#include <sstream>

namespace traccc {
void configuration_printable::print() const {
std::string configuration_printable::print() const {
std::size_t mkw = get_max_key_width_impl();
print_impl("", "", 0, mkw);
std::ostringstream oss;
print_impl("", "", 0, mkw, oss);
return oss.str();
};

configuration_printable::~configuration_printable() = default;

configuration_category::configuration_category(std::string n) : name(n) {}
configuration_category::configuration_category(std::string_view n) : name(n) {}

void configuration_category::add_child(
std::unique_ptr<configuration_printable>&& elem) {
std::unique_ptr<configuration_printable> elem) {
elements.push_back(std::move(elem));
}

void configuration_category::print_impl(std::string self_prefix,
std::string child_prefix,
void configuration_category::print_impl(const std::string& self_prefix,
const std::string& child_prefix,
std::size_t prefix_len,
std::size_t max_key_width) const {
std::cout << (self_prefix + name + ":") << std::endl;
std::size_t max_key_width,
std::ostream& out) const {
out << self_prefix << name << ":\n";

for (std::size_t i = 0; i < elements.size(); i++) {
if (i == elements.size() - 1) {
elements[i]->print_impl(child_prefix + "", child_prefix + " ",
prefix_len + 2, max_key_width);
prefix_len + 2, max_key_width, out);
} else {
elements[i]->print_impl(child_prefix + "", child_prefix + "",
prefix_len + 2, max_key_width);
prefix_len + 2, max_key_width, out);
}
}
}
Expand All @@ -59,16 +61,18 @@ configuration_category::~configuration_category() = default;
configuration_list::configuration_list() = default;

void configuration_list::add_child(
std::unique_ptr<configuration_printable>&& elem) {
std::unique_ptr<configuration_printable> elem) {
elements.push_back(std::move(elem));
}

void configuration_list::print_impl(std::string self_prefix,
std::string child_prefix,
void configuration_list::print_impl(const std::string& self_prefix,
const std::string& child_prefix,
std::size_t prefix_len,
std::size_t max_key_width) const {
std::size_t max_key_width,
std::ostream& out) const {
for (auto& i : elements) {
i->print_impl(self_prefix, child_prefix, prefix_len, max_key_width);
i->print_impl(self_prefix, child_prefix, prefix_len, max_key_width,
out);
}
}

Expand All @@ -84,17 +88,18 @@ std::size_t configuration_list::get_max_key_width_impl() const {

configuration_list::~configuration_list() = default;

configuration_kv_pair::configuration_kv_pair(std::string k, std::string v)
configuration_kv_pair::configuration_kv_pair(std::string_view k,
std::string_view v)
: key(k), value(v) {}

void configuration_kv_pair::print_impl(std::string self_prefix, std::string,
void configuration_kv_pair::print_impl(const std::string& self_prefix,
const std::string&,
std::size_t prefix_len,
std::size_t max_key_width) const {
std::cout << (self_prefix + key + ": " +
std::string((max_key_width - (prefix_len + key.length())),
' ') +
value)
<< std::endl;
std::size_t max_key_width,
std::ostream& out) const {
out << self_prefix << key << ": "
<< std::string((max_key_width - (prefix_len + key.length())), ' ')
<< value << "\n";
}

std::size_t configuration_kv_pair::get_max_key_width_impl() const {
Expand Down

0 comments on commit 5f93fe3

Please sign in to comment.