The Gaudi Framework  v36r10 (fc05264c)
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 66 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 84 of file MessageSvcSink.cpp.

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

◆ parse()

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

Definition at line 69 of file MessageSvcSink.cpp.

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

Member Data Documentation

◆ currentFormat

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

Definition at line 127 of file MessageSvcSink.cpp.

◆ currentName

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

Definition at line 128 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:127
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:128
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