The Gaudi Framework  v38r1 (3af3a718)
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 76 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 94 of file MessageSvcSink.cpp.

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

◆ parse()

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

Definition at line 79 of file MessageSvcSink.cpp.

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

Member Data Documentation

◆ currentFormat

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

Definition at line 139 of file MessageSvcSink.cpp.

◆ currentName

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

Definition at line 140 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:139
std::find
T find(T... args)
std::string::size
T size(T... args)
fmt::runtime
const T & runtime(const T &v)
Definition: MessageSvcSink.cpp:28
std::string::back
T back(T... args)
GaudiPluginService.cpluginsvc.registry
def registry()
Definition: cpluginsvc.py:83
GaudiPython.Pythonizations.ctx
ctx
Definition: Pythonizations.py:578
ProduceConsume.j
j
Definition: ProduceConsume.py:101
fmt::formatter< json_fmt_arg >::currentName
std::string currentName
Definition: MessageSvcSink.cpp:140
gaudirun.type
type
Definition: gaudirun.py:160