From dba0c1aa9525bce88bcc591890436d6f804a1b7c Mon Sep 17 00:00:00 2001 From: Marco Clemencic <marco.clemencic@cern.ch> Date: Tue, 4 Jun 2024 10:12:17 +0200 Subject: [PATCH 1/4] Add test to frame expectation for EventContext formatting --- GaudiKernel/CMakeLists.txt | 1 + .../tests/src/test_EventContextFmt.cpp | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 GaudiKernel/tests/src/test_EventContextFmt.cpp diff --git a/GaudiKernel/CMakeLists.txt b/GaudiKernel/CMakeLists.txt index 3269dc0dc7..d8a827bdef 100644 --- a/GaudiKernel/CMakeLists.txt +++ b/GaudiKernel/CMakeLists.txt @@ -390,6 +390,7 @@ if(BUILD_TESTING) SOURCES tests/src/CounterSerializationTest.cpp tests/src/MonitoringEntityUnitTest.cpp + tests/src/test_EventContextFmt.cpp tests/src/test_PropertyFmt.cpp tests/src/test_finally.cpp LINK diff --git a/GaudiKernel/tests/src/test_EventContextFmt.cpp b/GaudiKernel/tests/src/test_EventContextFmt.cpp new file mode 100644 index 0000000000..5b516974f7 --- /dev/null +++ b/GaudiKernel/tests/src/test_EventContextFmt.cpp @@ -0,0 +1,55 @@ +/***********************************************************************************\ +* (c) Copyright 2024 CERN for the benefit of the LHCb and ATLAS collaborations * +* * +* This software is distributed under the terms of the Apache version 2 licence, * +* copied verbatim in the file "LICENSE". * +* * +* In applying this licence, CERN does not waive the privileges and immunities * +* granted to it by virtue of its status as an Intergovernmental Organization * +* or submit itself to any jurisdiction. * +\***********************************************************************************/ +#include <GaudiKernel/EventContext.h> +#include <fmt/ostream.h> + +#if FMT_VERSION >= 90000 +// make EventContext formattable via fmt +template <> +struct fmt::formatter<EventContext> : ostream_formatter {}; +#endif + +#if __has_include( <catch2/catch.hpp>) +// Catch2 v2 +# include <catch2/catch.hpp> +#else +// Catch2 v3 +# include <catch2/catch_template_test_macros.hpp> +# include <catch2/catch_test_macros.hpp> +#endif + +TEST_CASE( "EventContext formatting" ) { + CHECK( fmt::format( "{}", EventContext{ 1, 2 } ) == "s: 2 e: 1" ); + CHECK( fmt::format( "{}", EventContext{ 1, 2, 3 } ) == "s: 2 e: 1 sub: 3" ); + CHECK( fmt::format( ">{:>20}<", EventContext{ 1, 2, 3 } ) == "> s: 2 e: 1 sub: 3<" ); + + CHECK( fmt::format( "{}", EventContext{} ) == "INVALID" ); + CHECK( fmt::format( "{}", EventContext{ 1 } ) == "INVALID" ); + + CHECK( fmt::format( "***{:^20}***", EventContext{} ) == "*** INVALID ***" ); + + EventContext ctx{ 1, 2, 3 }; + { + std::ostringstream out; + out << ctx; + CHECK( out.str() == "s: 2 e: 1 sub: 3" ); + } + { + std::ostringstream out; + out << &ctx; + CHECK( out.str() == "s: 2 e: 1 sub: 3" ); + } + { + std::ostringstream out; + out << static_cast<EventContext*>( nullptr ); + CHECK( out.str() == "INVALID" ); + } +} -- GitLab From 999a9a7ffcac01395839081be97ac348a1cc77e4 Mon Sep 17 00:00:00 2001 From: Marco Clemencic <marco.clemencic@cern.ch> Date: Tue, 4 Jun 2024 10:14:47 +0200 Subject: [PATCH 2/4] Minimal clean up of EventContext.h --- GaudiKernel/include/GaudiKernel/EventContext.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/GaudiKernel/include/GaudiKernel/EventContext.h b/GaudiKernel/include/GaudiKernel/EventContext.h index 02f77cbbe1..71ebdcb3aa 100644 --- a/GaudiKernel/include/GaudiKernel/EventContext.h +++ b/GaudiKernel/include/GaudiKernel/EventContext.h @@ -1,5 +1,5 @@ /***********************************************************************************\ -* (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations * +* (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations * * * * This software is distributed under the terms of the Apache version 2 licence, * * copied verbatim in the file "LICENSE". * @@ -8,10 +8,9 @@ * granted to it by virtue of its status as an Intergovernmental Organization * * or submit itself to any jurisdiction. * \***********************************************************************************/ -#ifndef GAUDIKERNEL_EVENTCONTEXT_H -#define GAUDIKERNEL_EVENTCONTEXT_H 1 +#pragma once -#include "GaudiKernel/EventIDBase.h" +#include <GaudiKernel/EventIDBase.h> #include <any> #include <cstddef> #include <iostream> @@ -155,5 +154,3 @@ inline std::ostream& operator<<( std::ostream& os, const EventContext* c ) { return os << "INVALID"; } } - -#endif // GAUDIKERNEL_EVENTCONTEXT_H -- GitLab From eb8b27e786943fd7a94886f57b9d2cd6740bf7a8 Mon Sep 17 00:00:00 2001 From: Marco Clemencic <marco.clemencic@cern.ch> Date: Tue, 4 Jun 2024 10:16:21 +0200 Subject: [PATCH 3/4] Make EventContext formattable --- .../include/GaudiKernel/EventContext.h | 33 +++++++++++-------- .../tests/src/test_EventContextFmt.cpp | 7 ---- GaudiUtils/src/component/EventWatchdogAlg.cpp | 7 ---- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/GaudiKernel/include/GaudiKernel/EventContext.h b/GaudiKernel/include/GaudiKernel/EventContext.h index 71ebdcb3aa..9791c7c66c 100644 --- a/GaudiKernel/include/GaudiKernel/EventContext.h +++ b/GaudiKernel/include/GaudiKernel/EventContext.h @@ -13,6 +13,7 @@ #include <GaudiKernel/EventIDBase.h> #include <any> #include <cstddef> +#include <fmt/format.h> #include <iostream> #include <limits> @@ -137,20 +138,26 @@ private: std::any m_extension; }; -inline std::ostream& operator<<( std::ostream& os, const EventContext& ctx ) { - if ( ctx.valid() ) { - os << "s: " << ctx.slot() << " e: " << ctx.evt(); - if ( ctx.usesSubSlot() ) os << " sub: " << ctx.subSlot(); - return os; - } else { - return os << "INVALID"; +template <> +struct fmt::formatter<EventContext> : formatter<string_view> { + auto format( const EventContext& ec, format_context& ctx ) const { + if ( ec.valid() ) { + std::string out; + if ( ec.usesSubSlot() ) { + out = fmt::format( "s: {} e: {} sub: {}", ec.slot(), ec.evt(), ec.subSlot() ); + } else { + out = fmt::format( "s: {} e: {}", ec.slot(), ec.evt() ); + } + return formatter<string_view>::format( out, ctx ); + } else { + return formatter<string_view>::format( "INVALID", ctx ); + } } -} +}; + +inline std::ostream& operator<<( std::ostream& os, const EventContext& ctx ) { return os << fmt::format( "{}", ctx ); } inline std::ostream& operator<<( std::ostream& os, const EventContext* c ) { - if ( c ) { - return os << *c; - } else { - return os << "INVALID"; - } + if ( c ) { return os << fmt::format( "{}", *c ); } + return os << "INVALID"; } diff --git a/GaudiKernel/tests/src/test_EventContextFmt.cpp b/GaudiKernel/tests/src/test_EventContextFmt.cpp index 5b516974f7..260b6ffbe1 100644 --- a/GaudiKernel/tests/src/test_EventContextFmt.cpp +++ b/GaudiKernel/tests/src/test_EventContextFmt.cpp @@ -9,13 +9,6 @@ * or submit itself to any jurisdiction. * \***********************************************************************************/ #include <GaudiKernel/EventContext.h> -#include <fmt/ostream.h> - -#if FMT_VERSION >= 90000 -// make EventContext formattable via fmt -template <> -struct fmt::formatter<EventContext> : ostream_formatter {}; -#endif #if __has_include( <catch2/catch.hpp>) // Catch2 v2 diff --git a/GaudiUtils/src/component/EventWatchdogAlg.cpp b/GaudiUtils/src/component/EventWatchdogAlg.cpp index 31cecc868b..24bc9c3278 100644 --- a/GaudiUtils/src/component/EventWatchdogAlg.cpp +++ b/GaudiUtils/src/component/EventWatchdogAlg.cpp @@ -18,7 +18,6 @@ #include <chrono> #include <csignal> #include <fmt/format.h> -#include <fmt/ostream.h> #include <mutex> #include <range/v3/range/conversion.hpp> #include <range/v3/view/remove.hpp> @@ -41,12 +40,6 @@ namespace { std::mutex s_watchdogReportMutex; } // namespace -#if FMT_VERSION >= 90000 -// make EventContext formattable via fmt -template <> -struct fmt::formatter<EventContext> : ostream_formatter {}; -#endif - using Gaudi::Utils::PeriodicAction; namespace Gaudi { -- GitLab From d36d1a86c239ff2176bdd1a1e3159ade098e1012 Mon Sep 17 00:00:00 2001 From: Marco Clemencic <marco.clemencic@cern.ch> Date: Thu, 6 Jun 2024 09:54:57 +0200 Subject: [PATCH 4/4] Fix compatibility with fmtlib < 8 --- GaudiKernel/include/GaudiKernel/EventContext.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/GaudiKernel/include/GaudiKernel/EventContext.h b/GaudiKernel/include/GaudiKernel/EventContext.h index 9791c7c66c..e5e9588073 100644 --- a/GaudiKernel/include/GaudiKernel/EventContext.h +++ b/GaudiKernel/include/GaudiKernel/EventContext.h @@ -140,7 +140,11 @@ private: template <> struct fmt::formatter<EventContext> : formatter<string_view> { - auto format( const EventContext& ec, format_context& ctx ) const { + auto format( const EventContext& ec, format_context& ctx ) +#if FMT_VERSION >= 80000 + const +#endif + { if ( ec.valid() ) { std::string out; if ( ec.usesSubSlot() ) { -- GitLab