32#ifndef VARIABLE_FORMATTER_H_
33#define VARIABLE_FORMATTER_H_
41#include <drain/Convert.h>
42#include <drain/RegExp.h>
43#include <drain/StringTools.h>
45#include <drain/SmartMapTools.h>
70template <
class T=std::
string>
80 typedef std::map<std::string,T> map_t;
91 bool handle(
const std::string & key,
const map_t & variables, std::ostream & ostr)
const final {
98 return handle(k, format, variables, ostr);
110 bool handle(
const std::string & key,
const std::string & format,
const map_t & variables, std::ostream & ostr)
const {
112 const typename std::map<std::string,T>::const_iterator it = variables.find(key);
113 if (it == variables.end()) {
118 const T & value = it->second;
120 iosFormat.copyTo(ostr);
144 bool formatVariable(
const std::string & key,
const T & value,
const std::string & format, std::ostream & ostr)
const {
152 bool formatVariable(
const std::string & key,
const T & value,
const std::string & format, std::string & str)
const final {
153 std::stringstream sstr;
176 bool formatValue(
const T & value,
const std::string & format, std::ostream & ostr) {
185 const char firstChar = format.at(0);
186 const char lastChar = format.at(format.size()-1);
188 if (firstChar ==
':'){
190 mout.
attention<LOG_DEBUG>(
"substring extraction: ", format);
196 std::vector<size_t> v;
199 size_t count = s.size();
207 if (pos >= s.size()){
208 mout.
warn(
"index ", pos,
" greater than size (", s.size(),
") of string value '", s,
"' of '", value,
"'");
211 count = std::min(count, s.size()-pos);
212 ostr << s.substr(v[1], count);
215 mout.
warn(
"unsupported formatting '", format,
"' for variable '", value,
"'");
216 mout.advice(
"use :startpos or :startpos:count for substring extraction");
221 else if (firstChar ==
'%'){
223 mout.
attention<LOG_DEBUG>(
"string formatting: ", format);
228 const size_t BUFFER_SIZE = 256;
229 char buffer[BUFFER_SIZE];
239 n = std::sprintf(buffer, format.c_str(), s.c_str());
247 n = std::sprintf(buffer, format.c_str(), s.at(0));
267 n = std::sprintf(buffer, format.c_str(), d);
283 n = std::sprintf(buffer, format.c_str(), i);
287 mout.
warn(
"formatting '", format,
"' requested for '", value,
"' : unsupported type key: '", lastChar,
"'");
292 if (n > BUFFER_SIZE){
293 mout.
fail(
"formatting with '", format,
"' exceeded buffer size (", BUFFER_SIZE,
')');
static void convert(const T1 &src, T2 &dst)
General case: dispatch to Converter<T1> traits.
Definition Convert.h:131
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:326
Logger & warn(const TT &... args)
Possible error, but execution can continue.
Definition Log.h:440
Logger & fail(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition Log.h:463
Logger & attention(const TT &... args)
Possible error, but execution can continue. Special type of Logger::warn().
Definition Log.h:486
Logger & unimplemented(const TT &... args)
Feature to be done. Special type of Logger::note().
Definition Log.h:521