The Gaudi Framework  v37r1 (a7f61348)
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)
 

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 75 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 
)
inline

Definition at line 93 of file MessageSvcSink.cpp.

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

◆ parse()

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

Definition at line 78 of file MessageSvcSink.cpp.

78  {
79  auto fmt_begin = ctx.begin();
80  auto fmt_end = std::find( fmt_begin, ctx.end(), '}' );
81  if ( fmt_begin == fmt_end ) {
82  // we are dealing with {}, only make sure currentFormat is empty
83  currentFormat = "";
84  } else {
85  // non empty format, let's split name from format
86  auto fmt_colon = std::find( fmt_begin, fmt_end, '|' );
87  currentName = std::string( fmt_begin, fmt_colon - fmt_begin );
88  currentFormat = std::string( fmt_colon + 1, fmt_end - fmt_colon - 1 );
89  }
90  return fmt_end;
91  }

Member Data Documentation

◆ currentFormat

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

Definition at line 138 of file MessageSvcSink.cpp.

◆ currentName

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

Definition at line 139 of file MessageSvcSink.cpp.


The documentation for this class was generated from the following file:
std::string
STL class.
fmt::formatter< json_fmt_arg >::currentFormat
std::string currentFormat
Definition: MessageSvcSink.cpp:138
std::find
T find(T... args)
std::string::size
T size(T... args)
fmt::runtime
const T & runtime(const T &v)
Definition: MessageSvcSink.cpp:27
std::string::back
T back(T... args)
GaudiPluginService.cpluginsvc.registry
def registry()
Definition: cpluginsvc.py:84
GaudiPython.Pythonizations.ctx
ctx
Definition: Pythonizations.py:588
ProduceConsume.j
j
Definition: ProduceConsume.py:101
fmt::formatter< json_fmt_arg >::currentName
std::string currentName
Definition: MessageSvcSink.cpp:139
gaudirun.type
type
Definition: gaudirun.py:162