libzypp: zypp/base/LogTools.h Source File

libzypp 17.25.10

LogTools.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_BASE_LOGTOOLS_H
13 #define ZYPP_BASE_LOGTOOLS_H
14
15 #include <iostream>
16 #include <string>
17 #include <vector>
18 #include <list>
19 #include <set>
20 #include <map>
21
22 #include <zypp/base/Hash.h>
23 #include <zypp/base/Logger.h>
24 #include <zypp/base/Iterator.h>
25 #include <zypp/APIConfig.h>
26
28 namespace zypp
29 {
30
31 using std::endl;
32
90 template<class TIterator>
91 std::ostream & dumpRange( std::ostream & str,
92 TIterator begin, TIterator end,
93 const std::string & intro = "{",
94 const std::string & pfx = "\n ",
95 const std::string & sep = "\n ",
96 const std::string & sfx = "\n",
97 const std::string & extro = "}" )
98 {
99 str << intro;
100 if ( begin != end )
101 {
102 str << pfx << *begin;
103 for ( ++begin; begin != end; ++begin )
104 str << sep << *begin;
105 str << sfx;
106 }
107 return str << extro;
108 }
109
113 template<class TIterator>
114 std::ostream & dumpRangeLine( std::ostream & str,
115 TIterator begin, TIterator end )
116 { return dumpRange( str, begin, end, "(", "", ", ", "", ")" ); }
118 template<class TContainer>
119 std::ostream & dumpRangeLine( std::ostream & str, const TContainer & cont )
120 { return dumpRangeLine( str, cont.begin(), cont.end() ); }
121
122
124 namespace iomanip
125 {
130 template<class TIterator>
131 struct RangeLine
132 {
133 RangeLine( TIterator begin, TIterator end )
134 : _begin( begin )
135 , _end( end )
136 {}
137 TIterator _begin;
138 TIterator _end;
139 };
140
142 template<class TIterator>
143 std::ostream & operator<<( std::ostream & str, const RangeLine<TIterator> & obj )
144 { return dumpRangeLine( str, obj._begin, obj._end ); }
145
146 } // namespce iomanip
148
156 template<class TIterator>
157 iomanip::RangeLine<TIterator> rangeLine( TIterator begin, TIterator end )
158 { return iomanip::RangeLine<TIterator>( begin, end ); }
160 template<class TContainer>
161 auto rangeLine( const TContainer & cont ) -> decltype( rangeLine( cont.begin(), cont.end() ) )
162 { return rangeLine( cont.begin(), cont.end() ); }
163
164 template<class Tp>
165 std::ostream & operator<<( std::ostream & str, const std::vector<Tp> & obj )
166 { return dumpRange( str, obj.begin(), obj.end() ); }
167
168 template<class Tp, class TCmp, class TAlloc>
169 std::ostream & operator<<( std::ostream & str, const std::set<Tp,TCmp,TAlloc> & obj )
170 { return dumpRange( str, obj.begin(), obj.end() ); }
171
172 template<class Tp>
173 std::ostream & operator<<( std::ostream & str, const std::unordered_set<Tp> & obj )
174 { return dumpRange( str, obj.begin(), obj.end() ); }
175
176 template<class Tp>
177 std::ostream & operator<<( std::ostream & str, const std::multiset<Tp> & obj )
178 { return dumpRange( str, obj.begin(), obj.end() ); }
179
180 template<class Tp>
181 std::ostream & operator<<( std::ostream & str, const std::list<Tp> & obj )
182 { return dumpRange( str, obj.begin(), obj.end() ); }
183
184 template<class Tp>
185 std::ostream & operator<<( std::ostream & str, const Iterable<Tp> & obj )
186 { return dumpRange( str, obj.begin(), obj.end() ); }
187
189 namespace _logtoolsdetail
190 {
191
193 // mapEntry
195
201 template<class TPair>
202 class MapEntry
203 {
204 public:
205 MapEntry( const TPair & pair_r )
206 : _pair( &pair_r )
207 {}
208
209 const TPair & pair() const
210 { return *_pair; }
211
212 private:
213 const TPair *const _pair;
214 };
215
217 template<class TPair>
218 std::ostream & operator<<( std::ostream & str, const MapEntry<TPair> & obj )
219 {
220 return str << '[' << obj.pair().first << "] = " << obj.pair().second;
221 }
222
224 template<class TPair>
225 MapEntry<TPair> mapEntry( const TPair & pair_r )
226 { return MapEntry<TPair>( pair_r ); }
227
229 // dumpMap
231
236 template<class TMap>
237 class DumpMap
238 {
239 public:
240 typedef TMap MapType;
241 typedef typename TMap::value_type PairType;
242 typedef MapEntry<PairType> MapEntryType;
243
244 struct Transformer : public std::unary_function<PairType, MapEntryType>
245 {
246 MapEntryType operator()( const PairType & pair_r ) const
247 { return mapEntry( pair_r ); }
248 };
249
250 typedef transform_iterator<Transformer, typename MapType::const_iterator>
251 MapEntry_const_iterator;
252
253 public:
254 DumpMap( const TMap & map_r )
255 : _map( &map_r )
256 {}
257
258 const TMap & map() const
259 { return *_map; }
260
261 MapEntry_const_iterator begin() const
262 { return make_transform_iterator( map().begin(), Transformer() ); }
263
264 MapEntry_const_iterator end() const
265 { return make_transform_iterator( map().end(), Transformer() );}
266
267 private:
268 const TMap *const _map;
269 };
270
272 template<class TMap>
273 std::ostream & operator<<( std::ostream & str, const DumpMap<TMap> & obj )
274 { return dumpRange( str, obj.begin(), obj.end() ); }
275
277 template<class TMap>
278 DumpMap<TMap> dumpMap( const TMap & map_r )
279 { return DumpMap<TMap>( map_r ); }
280
282 // dumpKeys
284
292 template<class TMap>
293 class DumpKeys
294 {
295 public:
296 typedef typename MapKVIteratorTraits<TMap>::Key_const_iterator MapKey_const_iterator;
297
298 public:
299 DumpKeys( const TMap & map_r )
300 : _map( &map_r )
301 {}
302
303 const TMap & map() const
304 { return *_map; }
305
306 MapKey_const_iterator begin() const
307 { return make_map_key_begin( map() ); }
308
309 MapKey_const_iterator end() const
310 { return make_map_key_end( map() ); }
311
312 private:
313 const TMap *const _map;
314 };
315
317 template<class TMap>
318 std::ostream & operator<<( std::ostream & str, const DumpKeys<TMap> & obj )
319 { return dumpRange( str, obj.begin(), obj.end() ); }
320
322 template<class TMap>
323 DumpKeys<TMap> dumpKeys( const TMap & map_r )
324 { return DumpKeys<TMap>( map_r ); }
325
327 // dumpValues
329
337 template<class TMap>
338 class DumpValues
339 {
340 public:
341 typedef typename MapKVIteratorTraits<TMap>::Value_const_iterator MapValue_const_iterator;
342
343 public:
344 DumpValues( const TMap & map_r )
345 : _map( &map_r )
346 {}
347
348 const TMap & map() const
349 { return *_map; }
350
351 MapValue_const_iterator begin() const
352 { return make_map_value_begin( map() ); }
353
354 MapValue_const_iterator end() const
355 { return make_map_value_end( map() ); }
356
357 private:
358 const TMap *const _map;
359 };
360
362 template<class TMap>
363 std::ostream & operator<<( std::ostream & str, const DumpValues<TMap> & obj )
364 { return dumpRange( str, obj.begin(), obj.end() ); }
365
367 template<class TMap>
368 DumpValues<TMap> dumpValues( const TMap & map_r )
369 { return DumpValues<TMap>( map_r ); }
370
372 } // namespace _logtoolsdetail
374
375 // iomanipulator
376 using _logtoolsdetail::mapEntry; // std::pair as '[key] = value'
377 using _logtoolsdetail::dumpMap; // dumpRange '[key] = value'
378 using _logtoolsdetail::dumpKeys; // dumpRange keys
379 using _logtoolsdetail::dumpValues; // dumpRange values
380
381 template<class TKey, class Tp>
382 std::ostream & operator<<( std::ostream & str, const std::map<TKey, Tp> & obj )
383 { return str << dumpMap( obj ); }
384
385 template<class TKey, class Tp>
386 std::ostream & operator<<( std::ostream & str, const std::unordered_map<TKey, Tp> & obj )
387 { return str << dumpMap( obj ); }
388
389 template<class TKey, class Tp>
390 std::ostream & operator<<( std::ostream & str, const std::multimap<TKey, Tp> & obj )
391 { return str << dumpMap( obj ); }
392
402 inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj )
403 {
404 std::string ret( "[" );
405 ret += ( obj.good() ? 'g' : '_' );
406 ret += ( obj.eof() ? 'e' : '_' );
407 ret += ( obj.fail() ? 'F' : '_' );
408 ret += ( obj.bad() ? 'B' : '_' );
409 ret += "]";
410 return str << ret;
411 }
412
414 // iomanipulator: str << dump(val) << ...
415 // calls: std::ostream & dumpOn( std::ostream & str, const Type & obj )
417
418 namespace detail
419 {
420 template<class Tp>
421 struct Dump
422 {
423 Dump( const Tp & obj_r ) : _obj( obj_r ) {}
424 const Tp & _obj;
425 };
426
427 template<class Tp>
428 std::ostream & operator<<( std::ostream & str, const Dump<Tp> & obj )
429 { return dumpOn( str, obj._obj ); }
430 }
431
432 template<class Tp>
433 detail::Dump<Tp> dump( const Tp & obj_r )
434 { return detail::Dump<Tp>(obj_r); }
435
436
438 } // namespace zypp
440 #endif // ZYPP_BASE_LOGTOOLS_H
Hash.h
Iterator.h
Logger.h
RangeLine< TIterator >
Iomanip helper printing dumpRangeLine style.
zypp::Iterable
Definition: Iterable.h:37
zypp::Iterable::begin
iterator_type begin() const
Definition: Iterable.h:63
zypp::Iterable::end
iterator_type end() const
Definition: Iterable.h:66
zypp::_logtoolsdetail::DumpKeys
std::map wrapper for stream output of keys.
Definition: LogTools.h:294
zypp::_logtoolsdetail::DumpKeys::end
MapKey_const_iterator end() const
Definition: LogTools.h:309
zypp::_logtoolsdetail::DumpKeys::dumpKeys
DumpKeys< TMap > dumpKeys(const TMap &map_r)
Convenience function to create DumpKeys from std::map.
Definition: LogTools.h:323
zypp::_logtoolsdetail::DumpKeys::DumpKeys
DumpKeys(const TMap &map_r)
Definition: LogTools.h:299
zypp::_logtoolsdetail::DumpKeys::MapKey_const_iterator
MapKVIteratorTraits< TMap >::Key_const_iterator MapKey_const_iterator
Definition: LogTools.h:296
zypp::_logtoolsdetail::DumpKeys::_map
const TMap *const _map
Definition: LogTools.h:313
zypp::_logtoolsdetail::DumpKeys::begin
MapKey_const_iterator begin() const
Definition: LogTools.h:306
zypp::_logtoolsdetail::DumpKeys::operator<<
std::ostream & operator<<(std::ostream &str, const DumpKeys< TMap > &obj)
Stream output.
Definition: LogTools.h:318
zypp::_logtoolsdetail::DumpKeys::map
const TMap & map() const
Definition: LogTools.h:303
zypp::_logtoolsdetail::DumpMap
std::map wrapper for stream output.
Definition: LogTools.h:238
zypp::_logtoolsdetail::DumpMap::operator<<
std::ostream & operator<<(std::ostream &str, const DumpMap< TMap > &obj)
Stream output.
Definition: LogTools.h:273
zypp::_logtoolsdetail::DumpMap::MapEntryType
MapEntry< PairType > MapEntryType
Definition: LogTools.h:242
zypp::_logtoolsdetail::DumpMap::MapType
TMap MapType
Definition: LogTools.h:240
zypp::_logtoolsdetail::DumpMap::begin
MapEntry_const_iterator begin() const
Definition: LogTools.h:261
zypp::_logtoolsdetail::DumpMap::PairType
TMap::value_type PairType
Definition: LogTools.h:241
zypp::_logtoolsdetail::DumpMap::map
const TMap & map() const
Definition: LogTools.h:258
zypp::_logtoolsdetail::DumpMap::MapEntry_const_iterator
transform_iterator< Transformer, typename MapType::const_iterator > MapEntry_const_iterator
Definition: LogTools.h:251
zypp::_logtoolsdetail::DumpMap::end
MapEntry_const_iterator end() const
Definition: LogTools.h:264
zypp::_logtoolsdetail::DumpMap::_map
const TMap *const _map
Definition: LogTools.h:268
zypp::_logtoolsdetail::DumpMap::dumpMap
DumpMap< TMap > dumpMap(const TMap &map_r)
Convenience function to create DumpMap from std::map.
Definition: LogTools.h:278
zypp::_logtoolsdetail::DumpMap::DumpMap
DumpMap(const TMap &map_r)
Definition: LogTools.h:254
zypp::_logtoolsdetail::DumpValues
std::map wrapper for stream output of values.
Definition: LogTools.h:339
zypp::_logtoolsdetail::DumpValues::operator<<
std::ostream & operator<<(std::ostream &str, const DumpValues< TMap > &obj)
Stream output.
Definition: LogTools.h:363
zypp::_logtoolsdetail::DumpValues::dumpValues
DumpValues< TMap > dumpValues(const TMap &map_r)
Convenience function to create DumpValues from std::map.
Definition: LogTools.h:368
zypp::_logtoolsdetail::DumpValues::_map
const TMap *const _map
Definition: LogTools.h:358
zypp::_logtoolsdetail::DumpValues::DumpValues
DumpValues(const TMap &map_r)
Definition: LogTools.h:344
zypp::_logtoolsdetail::DumpValues::end
MapValue_const_iterator end() const
Definition: LogTools.h:354
zypp::_logtoolsdetail::DumpValues::begin
MapValue_const_iterator begin() const
Definition: LogTools.h:351
zypp::_logtoolsdetail::DumpValues::MapValue_const_iterator
MapKVIteratorTraits< TMap >::Value_const_iterator MapValue_const_iterator
Definition: LogTools.h:341
zypp::_logtoolsdetail::DumpValues::map
const TMap & map() const
Definition: LogTools.h:348
zypp::_logtoolsdetail::MapEntry
std::pair wrapper for std::map output.
Definition: LogTools.h:203
zypp::_logtoolsdetail::MapEntry::pair
const TPair & pair() const
Definition: LogTools.h:209
zypp::_logtoolsdetail::MapEntry::MapEntry
MapEntry(const TPair &pair_r)
Definition: LogTools.h:205
zypp::_logtoolsdetail::MapEntry::operator<<
std::ostream & operator<<(std::ostream &str, const MapEntry< TPair > &obj)
Stream output.
Definition: LogTools.h:218
zypp::_logtoolsdetail::MapEntry::mapEntry
MapEntry< TPair > mapEntry(const TPair &pair_r)
Convenience function to create MapEntry from std::pair.
Definition: LogTools.h:225
zypp::_logtoolsdetail::MapEntry::_pair
const TPair *const _pair
Definition: LogTools.h:213
str
String related utilities and Regular expression matching.
zypp::detail::dumpOn
std::ostream & dumpOn(std::ostream &str, const PoolQueryIterator &obj)
Definition: PoolQuery.cc:1809
zypp::detail::operator<<
std::ostream & operator<<(std::ostream &str, const Dump< Tp > &obj)
Definition: LogTools.h:428
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
zypp::make_map_key_end
MapKVIteratorTraits< TMap >::Key_const_iterator make_map_key_end(const TMap &map_r)
Convenience to create the key iterator from container::end()
Definition: Iterator.h:233
zypp::dumpRangeLine
std::ostream & dumpRangeLine(std::ostream &str, TIterator begin, TIterator end)
Print range defined by iterators (single line style).
Definition: LogTools.h:114
zypp::dump
detail::Dump< Tp > dump(const Tp &obj_r)
Definition: LogTools.h:433
zypp::make_map_key_begin
MapKVIteratorTraits< TMap >::Key_const_iterator make_map_key_begin(const TMap &map_r)
Convenience to create the key iterator from container::begin()
Definition: Iterator.h:228
zypp::make_map_value_end
MapKVIteratorTraits< TMap >::Value_const_iterator make_map_value_end(const TMap &map_r)
Convenience to create the value iterator from container::end()
Definition: Iterator.h:243
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:147
zypp::make_map_value_begin
MapKVIteratorTraits< TMap >::Value_const_iterator make_map_value_begin(const TMap &map_r)
Convenience to create the value iterator from container::begin()
Definition: Iterator.h:238
zypp::rangeLine
iomanip::RangeLine< TIterator > rangeLine(TIterator begin, TIterator end)
Iomanip printing dumpRangeLine style.
Definition: LogTools.h:157
zypp::dumpRange
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator end, const std::string &intro="{", const std::string &pfx="\n ", const std::string &sep="\n ", const std::string &sfx="\n", const std::string &extro="}")
Print range defined by iterators (multiline style).
Definition: LogTools.h:91
zypp::MapKVIteratorTraits::Key_const_iterator
transform_iterator< GetPairFirst< typename MapType::value_type >, typename MapType::const_iterator > Key_const_iterator
The key iterator type.
Definition: Iterator.h:218
zypp::MapKVIteratorTraits::Value_const_iterator
transform_iterator< GetPairSecond< typename MapType::value_type >, typename MapType::const_iterator > Value_const_iterator
The value iterator type.
Definition: Iterator.h:223
zypp::_logtoolsdetail::DumpMap::Transformer
Definition: LogTools.h:245
zypp::_logtoolsdetail::DumpMap::Transformer::operator()
MapEntryType operator()(const PairType &pair_r) const
Definition: LogTools.h:246
zypp::detail::Dump
Definition: LogTools.h:422
zypp::detail::Dump::_obj
const Tp & _obj
Definition: LogTools.h:424
zypp::detail::Dump::Dump
Dump(const Tp &obj_r)
Definition: LogTools.h:423
zypp::iomanip::RangeLine
Definition: LogTools.h:132
zypp::iomanip::RangeLine::operator<<
std::ostream & operator<<(std::ostream &str, const RangeLine< TIterator > &obj)
<TIterator>
Definition: LogTools.h:143
zypp::iomanip::RangeLine::_end
TIterator _end
Definition: LogTools.h:138
zypp::iomanip::RangeLine::RangeLine
RangeLine(TIterator begin, TIterator end)
Definition: LogTools.h:133
zypp::iomanip::RangeLine::_begin
TIterator _begin
Definition: LogTools.h:137