The Gaudi Framework  v39r0 (5b8b5eda)
fmt::formatter< json_fmt_arg > Class Reference

fmt formatter function for json class able to handle 2 types of formats : {} : in this case the type entry of json is used to deduce what to print, looking into the registry {:name|fmt} : in this case, the entry 'name' of the json will be printed in given format. More...

Collaboration diagram for fmt::formatter< json_fmt_arg >:

Public Member Functions

template<typename ParseContext >
constexpr auto parse (ParseContext &ctx)
 
template<typename FormatContext >
auto format (const json_fmt_arg &json_arg, FormatContext &ctx) const
 

Private Attributes

std::string currentFormat
 
std::string currentName
 

Detailed Description

fmt formatter function for json class able to handle 2 types of formats : {} : in this case the type entry of json is used to deduce what to print, looking into the registry {:name|fmt} : in this case, the entry 'name' of the json will be printed in given format.

See comment on registry for more details

Definition at line 67 of file MessageSvcSink.cpp.

Member Function Documentation

◆ format()

template<typename FormatContext >
auto fmt::formatter< json_fmt_arg >::format ( const json_fmt_arg &  json_arg,
FormatContext &  ctx 
) const
inline

Definition at line 85 of file MessageSvcSink.cpp.

85  {
86  const auto& j = json_arg.payload;
87  if ( currentFormat.size() == 0 ) {
88  // dealing with {} format, let's find entry for our type in registry
89  const auto type = j.at( "type" ).get<std::string>();
90  // first looking for the entry, then we drop on ":abc" suffix at a time and try again
91  std::string_view type_key{ type };
92  // look for the full entry
93  auto entry = registry.find( type_key );
94  // we check if we have type separators before entering the loop
95  auto sep = type_key.rfind( ':' );
96  while ( sep != type_key.npos && entry == registry.end() ) {
97  // not found, remove the trailing ":abc" section
98  type_key.remove_suffix( type_key.size() - sep );
99  // check if we have another chunk to strip
100  sep = type_key.rfind( ':' );
101  // see if the shorter key works
102  entry = registry.find( type_key );
103  }
104  // if still not found, we use the basic "counter"
105  if ( entry == registry.end() ) entry = registry.find( "counter" );
106  assert( entry != registry.end() );
107  // print the json string according to format found
108  // This actually will call this formatter again a number of times
109  return fmt::format_to( ctx.out(), fmt::runtime( entry->second ), json_arg );
110  } else {
111  // dealing with a {:name|fmt} format
112  auto actualFormat = "{:" + currentFormat + '}';
113  switch ( currentFormat.back() ) {
114  case 'd':
115  return fmt::format_to( ctx.out(), fmt::runtime( actualFormat ),
116  j.at( currentName ).template get<unsigned int>() );
117  case 'g':
118  return fmt::format_to( ctx.out(), fmt::runtime( actualFormat ), j.at( currentName ).template get<double>() );
119  case 'p':
120  actualFormat[actualFormat.size() - 2] = 'g';
121  return fmt::format_to( ctx.out(), fmt::runtime( actualFormat ),
122  j.at( currentName ).template get<double>() * 100 );
123  default:
124  return fmt::format_to( ctx.out(), "Unknown counter format : {}", currentFormat );
125  }
126  }
127  }

◆ parse()

template<typename ParseContext >
constexpr auto fmt::formatter< json_fmt_arg >::parse ( ParseContext &  ctx)
inlineconstexpr

Definition at line 70 of file MessageSvcSink.cpp.

70  {
71  auto fmt_begin = ctx.begin();
72  auto fmt_end = std::find( fmt_begin, ctx.end(), '}' );
73  if ( fmt_begin == fmt_end ) {
74  // we are dealing with {}, only make sure currentFormat is empty
75  currentFormat = "";
76  } else {
77  // non empty format, let's split name from format
78  auto fmt_colon = std::find( fmt_begin, fmt_end, '|' );
79  currentName = std::string( fmt_begin, fmt_colon - fmt_begin );
80  currentFormat = std::string( fmt_colon + 1, fmt_end - fmt_colon - 1 );
81  }
82  return fmt_end;
83  }

Member Data Documentation

◆ currentFormat

std::string fmt::formatter< json_fmt_arg >::currentFormat
private

Definition at line 130 of file MessageSvcSink.cpp.

◆ currentName

std::string fmt::formatter< json_fmt_arg >::currentName
private

Definition at line 131 of file MessageSvcSink.cpp.


The documentation for this class was generated from the following file:
std::string
STL class.
cpluginsvc.registry
def registry()
Definition: cpluginsvc.py:83
fmt::formatter< json_fmt_arg >::currentFormat
std::string currentFormat
Definition: MessageSvcSink.cpp:130
std::find
T find(T... args)
std::string::size
T size(T... args)
std::string::back
T back(T... args)
GaudiPython.Pythonizations.ctx
ctx
Definition: Pythonizations.py:578
ProduceConsume.j
j
Definition: ProduceConsume.py:101
fmt::formatter< json_fmt_arg >::currentName
std::string currentName
Definition: MessageSvcSink.cpp:131
gaudirun.type
type
Definition: gaudirun.py:160