The Gaudi Framework  v36r9 (fd2bdac3)
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 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 
)
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(), entry->second, json_arg );
110  } else {
111  // dealing with a {:name|fmt} format
112  auto actualFormat = fmt::format( "{{:{}", currentFormat ) + "}";
113  switch ( currentFormat.back() ) {
114  case 'd':
115  return fmt::format_to( ctx.out(), actualFormat, j.at( currentName ).template get<unsigned int>() );
116  case 'g':
117  return fmt::format_to( ctx.out(), actualFormat, j.at( currentName ).template get<double>() );
118  case 'p':
119  actualFormat[actualFormat.size() - 2] = 'g';
120  return fmt::format_to( ctx.out(), actualFormat, j.at( currentName ).template get<double>() * 100 );
121  default:
122  return fmt::format_to( ctx.out(), "Unknown counter format : {}", currentFormat );
123  }
124  }
125  }

◆ 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 128 of file MessageSvcSink.cpp.

◆ currentName

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

Definition at line 129 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:128
std::find
T find(T... args)
std::string::size
T size(T... args)
std::string::back
T back(T... args)
GaudiPluginService.cpluginsvc.registry
def registry()
Definition: cpluginsvc.py:84
GaudiPython.Pythonizations.ctx
ctx
Definition: Pythonizations.py:588
fmt::formatter< json_fmt_arg >::currentName
std::string currentName
Definition: MessageSvcSink.cpp:129
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
gaudirun.type
type
Definition: gaudirun.py:160