The Gaudi Framework  v40r0 (475e45c1)
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 102 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 120 of file MessageSvcSink.cpp.

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

◆ parse()

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

Definition at line 105 of file MessageSvcSink.cpp.

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

Member Data Documentation

◆ currentFormat

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

Definition at line 165 of file MessageSvcSink.cpp.

◆ currentName

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

Definition at line 166 of file MessageSvcSink.cpp.


The documentation for this class was generated from the following file:
cpluginsvc.registry
def registry()
Definition: cpluginsvc.py:83
fmt::formatter< json_fmt_arg >::currentFormat
std::string currentFormat
Definition: MessageSvcSink.cpp:165
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:166
gaudirun.type
type
Definition: gaudirun.py:160