Belle II Software light-2406-ragdoll
KeyValuePrinter.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10
11#include <iomanip>
12#include <sstream>
13#include <string>
14#include <vector>
15#include <map>
16
17namespace Belle2 {
47 public:
53 explicit KeyValuePrinter(bool use_json, unsigned key_max_length = 10):
54 m_json(use_json),
55 m_maxpad(key_max_length + 2),
56 m_delim("")
57 { }
59
61 std::string string() const
62 {
63 if (m_json)
64 return "{\n" + m_stream.str() + "\n}\n";
65 else
66 return m_stream.str();
67 }
69 template <class T> void put(const std::string& key, const T& value)
70 {
71 if (m_json) {
72 m_stream << m_delim << "\t" << escape(key) << ": " << escape(value) << "";
73 } else {
74 m_stream << std::left << std::setw(m_maxpad) << key + ": " << escape(value) << "\n";
75 }
76 m_delim = ",\n";
77 }
79 template <class T> void put(const std::string& key, const std::map<std::string, T>& value)
80 {
81 if (m_json) {
82 m_stream << m_delim << "\t" << escape(key) << ": " << escape(value) << "";
83 } else {
84 m_stream << "\n" << key << "\n";
85 for (char unused : key) {
86 (void)unused;
87 m_stream << '-';
88 }
89 m_stream << "\n" << escape(value) << "\n";
90 }
91 m_delim = ",\n";
92 }
93 private:
94 bool m_json;
95 unsigned m_maxpad;
96 std::string m_delim;
97 std::stringstream m_stream;
100 std::string escape(const std::string& value) const;
102 std::string escape(const char* value) const { return escape(std::string(value)); }
104 template <class T> std::string escape(const T& value) const { return std::to_string(value); }
106 template <class T> std::string escape(const std::vector<T>& value) const
107 {
108 std::string delim = "";
109 std::string s = "[";
110 for (auto el : value) {
111 s += delim + escape(el);
112 delim = ", ";
113 }
114 s += "]";
115 return s;
116 }
118 template <class T> std::string escape(const std::map<std::string, T>& value) const
119 {
120 //well, it's valid json, but not pretty
121 KeyValuePrinter printer(m_json, m_maxpad - 2);
122 for (auto pair : value)
123 printer.put(pair.first, pair.second);
124 return printer.string();
125 }
126 };
128}
create human-readable or JSON output for key value pairs.
std::string escape(const std::map< std::string, T > &value) const
escape map<string, T>.
void put(const std::string &key, const std::map< std::string, T > &value)
Specialization for map<>
void put(const std::string &key, const T &value)
Add one key-value pair.
std::string escape(const char *value) const
escape string literals.
unsigned m_maxpad
for human-readable output: how much padding after key?
std::stringstream m_stream
output being built.
std::string escape(const std::string &value) const
escape string.
std::string escape(const T &value) const
escape numeric value.
bool m_json
create JSON output?
std::string m_delim
for JSON: comma to print after prev entry.
std::string string() const
Return completed string.
KeyValuePrinter(bool use_json, unsigned key_max_length=10)
Ctor.
std::string escape(const std::vector< T > &value) const
escape vector<T>.
Abstract base class for different kinds of events.
Definition: ClusterUtils.h:24