The Gaudi Framework  v39r3 (979e3109)
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 107 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 125 of file MessageSvcSink.cpp.

125  {
126  const auto& j = json_arg.payload;
127  if ( currentFormat.size() == 0 ) {
128  // dealing with {} format, let's find entry for our type in registry
129  const auto type = j.at( "type" ).get<std::string>();
130  // first looking for the entry, then we drop on ":abc" suffix at a time and try again
131  std::string_view type_key{ type };
132  // look for the full entry
133  auto entry = registry.find( type_key );
134  // we check if we have type separators before entering the loop
135  auto sep = type_key.rfind( ':' );
136  while ( sep != type_key.npos && entry == registry.end() ) {
137  // not found, remove the trailing ":abc" section
138  type_key.remove_suffix( type_key.size() - sep );
139  // check if we have another chunk to strip
140  sep = type_key.rfind( ':' );
141  // see if the shorter key works
142  entry = registry.find( type_key );
143  }
144  // if still not found, we use the basic "counter"
145  if ( entry == registry.end() ) entry = registry.find( "counter" );
146  assert( entry != registry.end() );
147  // print the json string according to format found
148  // This actually will call this formatter again a number of times
149  return fmt::format_to( ctx.out(), fmt::runtime( entry->second ), json_arg );
150  } else {
151  // dealing with a {:name|fmt} format
152  auto actualFormat = "{:" + currentFormat + '}';
153  switch ( currentFormat.back() ) {
154  case 'd':
155  return fmt::format_to( ctx.out(), fmt::runtime( actualFormat ),
156  j.at( currentName ).template get<unsigned int>() );
157  case 'g':
158  return fmt::format_to( ctx.out(), fmt::runtime( actualFormat ), j.at( currentName ).template get<double>() );
159  case 'p':
160  actualFormat[actualFormat.size() - 2] = 'g';
161  return fmt::format_to( ctx.out(), fmt::runtime( actualFormat ),
162  j.at( currentName ).template get<double>() * 100 );
163  default:
164  return fmt::format_to( ctx.out(), "Unknown counter format : {}", currentFormat );
165  }
166  }
167  }

◆ parse()

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

Definition at line 110 of file MessageSvcSink.cpp.

110  {
111  auto fmt_begin = ctx.begin();
112  auto fmt_end = std::find( fmt_begin, ctx.end(), '}' );
113  if ( fmt_begin == fmt_end ) {
114  // we are dealing with {}, only make sure currentFormat is empty
115  currentFormat = "";
116  } else {
117  // non empty format, let's split name from format
118  auto fmt_colon = std::find( fmt_begin, fmt_end, '|' );
119  currentName = std::string( fmt_begin, fmt_colon - fmt_begin );
120  currentFormat = std::string( fmt_colon + 1, fmt_end - fmt_colon - 1 );
121  }
122  return fmt_end;
123  }

Member Data Documentation

◆ currentFormat

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

Definition at line 170 of file MessageSvcSink.cpp.

◆ currentName

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

Definition at line 171 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:170
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:104
fmt::formatter< json_fmt_arg >::currentName
std::string currentName
Definition: MessageSvcSink.cpp:171
gaudirun.type
type
Definition: gaudirun.py:160