1
/*---------------------------------------------------------------------\
3
| |__ / \ / / . \ . \ |
8
\---------------------------------------------------------------------*/
9
/** \file zypp/base/LogTools.h
12
#ifndef ZYPP_BASE_LOGTOOLS_H
13
#define ZYPP_BASE_LOGTOOLS_H
22
#include "zypp/base/Tr1hash.h"
23
#include "zypp/base/Logger.h"
24
#include "zypp/base/Iterator.h"
25
#include "zypp/APIConfig.h"
27
///////////////////////////////////////////////////////////////////
29
{ /////////////////////////////////////////////////////////////////
33
/** Print range defined by iterators (multiline style).
35
* intro [ pfx ITEM [ { sep ITEM }+ ] sfx ] extro
38
* The defaults print the range enclosed in \c {}, one item per
39
* line indented by 2 spaces.
45
* {} // on empty range
48
* A comma separated list enclosed in \c () would be:
50
* dumpRange( stream, begin, end, "(", "", ", ", "", ")" );
52
* dumpRangeLine( stream, begin, end );
55
* \note Some special handling is required for printing std::maps.
56
* Therefore iomaipulators \ref dumpMap, \ref dumpKeys and \ref dumpValues
59
* std::map<string,int> m;
64
* dumpRange( DBG, dumpMap(m).begin(), dumpMap(m).end() ) << endl;
70
* dumpRange( DBG, dumpKeys(m).begin(), dumpKeys(m).end() ) << endl;
76
* dumpRange( DBG, dumpValues(m).begin(), dumpValues(m).end() ) << endl;
82
* dumpRangeLine( DBG, dumpMap(m).begin(), dumpMap(m).end() ) << endl;
83
* // ([a] = 1, [b] = 2, [c] = 3)
84
* dumpRangeLine( DBG, dumpKeys(m).begin(), dumpKeys(m).end() ) << endl;
86
* dumpRangeLine( DBG, dumpValues(m).begin(), dumpValues(m).end() ) << endl;
90
template<class _Iterator>
91
std::ostream & dumpRange( std::ostream & str,
92
_Iterator begin, _Iterator 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 = "}" )
102
str << pfx << *begin;
103
for ( ++begin; begin != end; ++begin )
104
str << sep << *begin;
110
/** Print range defined by iterators (single line style).
113
template<class _Iterator>
114
std::ostream & dumpRangeLine( std::ostream & str,
115
_Iterator begin, _Iterator end )
116
{ return dumpRange( str, begin, end, "(", "", ", ", "", ")" ); }
120
std::ostream & operator<<( std::ostream & str, const std::vector<_Tp> & obj )
121
{ return dumpRange( str, obj.begin(), obj.end() ); }
124
std::ostream & operator<<( std::ostream & str, const std::set<_Tp> & obj )
125
{ return dumpRange( str, obj.begin(), obj.end() ); }
128
std::ostream & operator<<( std::ostream & str, const std::tr1::unordered_set<_Tp> & obj )
129
{ return dumpRange( str, obj.begin(), obj.end() ); }
132
std::ostream & operator<<( std::ostream & str, const std::multiset<_Tp> & obj )
133
{ return dumpRange( str, obj.begin(), obj.end() ); }
136
std::ostream & operator<<( std::ostream & str, const std::list<_Tp> & obj )
137
{ return dumpRange( str, obj.begin(), obj.end() ); }
139
///////////////////////////////////////////////////////////////////
140
namespace _logtoolsdetail
141
{ /////////////////////////////////////////////////////////////////
143
///////////////////////////////////////////////////////////////////
145
///////////////////////////////////////////////////////////////////
147
/** std::pair wrapper for std::map output.
148
* Just because we want a special output format for std::pair
149
* used in a std::map. The mapped std::pair is printed as
150
* <tt>[key] = value</tt>.
152
template<class _Pair>
156
MapEntry( const _Pair & pair_r )
160
const _Pair & pair() const
164
const _Pair *const _pair;
167
/** \relates MapEntry Stream output. */
168
template<class _Pair>
169
std::ostream & operator<<( std::ostream & str, const MapEntry<_Pair> & obj )
171
return str << '[' << obj.pair().first << "] = " << obj.pair().second;
174
/** \relates MapEntry Convenience function to create MapEntry from std::pair. */
175
template<class _Pair>
176
MapEntry<_Pair> mapEntry( const _Pair & pair_r )
177
{ return MapEntry<_Pair>( pair_r ); }
179
///////////////////////////////////////////////////////////////////
181
///////////////////////////////////////////////////////////////////
183
/** std::map wrapper for stream output.
184
* Uses a transform_iterator to wrap the std::pair into MapEntry.
191
typedef _Map MapType;
192
typedef typename _Map::value_type PairType;
193
typedef MapEntry<PairType> MapEntryType;
195
struct Transformer : public std::unary_function<PairType, MapEntryType>
197
MapEntryType operator()( const PairType & pair_r ) const
198
{ return mapEntry( pair_r ); }
201
typedef transform_iterator<Transformer, typename MapType::const_iterator>
202
MapEntry_const_iterator;
205
DumpMap( const _Map & map_r )
209
const _Map & map() const
212
MapEntry_const_iterator begin() const
213
{ return make_transform_iterator( map().begin(), Transformer() ); }
215
MapEntry_const_iterator end() const
216
{ return make_transform_iterator( map().end(), Transformer() );}
219
const _Map *const _map;
222
/** \relates DumpMap Stream output. */
224
std::ostream & operator<<( std::ostream & str, const DumpMap<_Map> & obj )
225
{ return dumpRange( str, obj.begin(), obj.end() ); }
227
/** \relates DumpMap Convenience function to create DumpMap from std::map. */
229
DumpMap<_Map> dumpMap( const _Map & map_r )
230
{ return DumpMap<_Map>( map_r ); }
232
///////////////////////////////////////////////////////////////////
234
///////////////////////////////////////////////////////////////////
236
/** std::map wrapper for stream output of keys.
237
* Uses MapKVIterator iterate and write the key values.
239
* std::map<...> mymap;
240
* std::cout << dumpKeys(mymap) << std::endl;
247
typedef typename MapKVIteratorTraits<_Map>::Key_const_iterator MapKey_const_iterator;
250
DumpKeys( const _Map & map_r )
254
const _Map & map() const
257
MapKey_const_iterator begin() const
258
{ return make_map_key_begin( map() ); }
260
MapKey_const_iterator end() const
261
{ return make_map_key_end( map() ); }
264
const _Map *const _map;
267
/** \relates DumpKeys Stream output. */
269
std::ostream & operator<<( std::ostream & str, const DumpKeys<_Map> & obj )
270
{ return dumpRange( str, obj.begin(), obj.end() ); }
272
/** \relates DumpKeys Convenience function to create DumpKeys from std::map. */
274
DumpKeys<_Map> dumpKeys( const _Map & map_r )
275
{ return DumpKeys<_Map>( map_r ); }
277
///////////////////////////////////////////////////////////////////
279
///////////////////////////////////////////////////////////////////
281
/** std::map wrapper for stream output of values.
282
* Uses MapKVIterator iterate and write the values.
284
* std::map<...> mymap;
285
* std::cout << dumpValues(mymap) << std::endl;
292
typedef typename MapKVIteratorTraits<_Map>::Value_const_iterator MapValue_const_iterator;
295
DumpValues( const _Map & map_r )
299
const _Map & map() const
302
MapValue_const_iterator begin() const
303
{ return make_map_value_begin( map() ); }
305
MapValue_const_iterator end() const
306
{ return make_map_value_end( map() ); }
309
const _Map *const _map;
312
/** \relates DumpValues Stream output. */
314
std::ostream & operator<<( std::ostream & str, const DumpValues<_Map> & obj )
315
{ return dumpRange( str, obj.begin(), obj.end() ); }
317
/** \relates DumpValues Convenience function to create DumpValues from std::map. */
319
DumpValues<_Map> dumpValues( const _Map & map_r )
320
{ return DumpValues<_Map>( map_r ); }
322
/////////////////////////////////////////////////////////////////
323
} // namespace _logtoolsdetail
324
///////////////////////////////////////////////////////////////////
327
using _logtoolsdetail::mapEntry; // std::pair as '[key] = value'
328
using _logtoolsdetail::dumpMap; // dumpRange '[key] = value'
329
using _logtoolsdetail::dumpKeys; // dumpRange keys
330
using _logtoolsdetail::dumpValues; // dumpRange values
332
template<class _Key, class _Tp>
333
std::ostream & operator<<( std::ostream & str, const std::map<_Key, _Tp> & obj )
334
{ return str << dumpMap( obj ); }
336
template<class _Key, class _Tp>
337
std::ostream & operator<<( std::ostream & str, const std::tr1::unordered_map<_Key, _Tp> & obj )
338
{ return str << dumpMap( obj ); }
340
template<class _Key, class _Tp>
341
std::ostream & operator<<( std::ostream & str, const std::multimap<_Key, _Tp> & obj )
342
{ return str << dumpMap( obj ); }
344
/** Print stream status bits.
345
* Prints the values of a streams \c good, \c eof, \c failed and \c bad bit.
349
* [_eF_] - eof and fail bit set
350
* [__FB] - fail and bad bit set
353
inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj )
355
std::string ret( "[" );
356
ret += ( obj.good() ? 'g' : '_' );
357
ret += ( obj.eof() ? 'e' : '_' );
358
ret += ( obj.fail() ? 'F' : '_' );
359
ret += ( obj.bad() ? 'B' : '_' );
364
///////////////////////////////////////////////////////////////////
365
// iomanipulator: str << dump(val) << ...
366
// calls: std::ostream & dumpOn( std::ostream & str, const Type & obj )
367
///////////////////////////////////////////////////////////////////
374
Dump( const _Tp & obj_r ) : _obj( obj_r ) {}
379
std::ostream & operator<<( std::ostream & str, const Dump<_Tp> & obj )
380
{ return dumpOn( str, obj._obj ); }
384
detail::Dump<_Tp> dump( const _Tp & obj_r )
385
{ return detail::Dump<_Tp>(obj_r); }
388
/////////////////////////////////////////////////////////////////
390
///////////////////////////////////////////////////////////////////
391
#endif // ZYPP_BASE_LOGTOOLS_H