1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/LogTools.h
12 #ifndef ZYPP_BASE_LOGTOOLS_H
13 #define ZYPP_BASE_LOGTOOLS_H
21 #include <tr1/unordered_set>
22 #include <tr1/unordered_map>
23 #include "zypp/base/Logger.h"
24 #include "zypp/base/Iterator.h"
25 #include "zypp/base/Deprecated.h"
27 ///////////////////////////////////////////////////////////////////
29 { /////////////////////////////////////////////////////////////////
31 /** Print range defined by iterators (multiline style).
33 * intro [ pfx ITEM [ { sep ITEM }+ ] sfx ] extro
36 * The defaults print the range enclosed in \c {}, one item per
37 * line indented by 2 spaces.
43 * {} // on empty range
46 * A comma separated list enclosed in \c () would be:
48 * dumpRange( stream, begin, end, "(", "", ", ", "", ")" );
50 * dumpRangeLine( stream, begin, end );
53 * \note Some special handling is required for printing std::maps.
54 * Therefore iomaipulators \ref dumpMap, \ref dumpKeys and \ref dumpValues
57 * std::map<string,int> m;
62 * dumpRange( DBG, dumpMap(m).begin(), dumpMap(m).end() ) << endl;
68 * dumpRange( DBG, dumpKeys(m).begin(), dumpKeys(m).end() ) << endl;
74 * dumpRange( DBG, dumpValues(m).begin(), dumpValues(m).end() ) << endl;
80 * dumpRangeLine( DBG, dumpMap(m).begin(), dumpMap(m).end() ) << endl;
81 * // ([a] = 1, [b] = 2, [c] = 3)
82 * dumpRangeLine( DBG, dumpKeys(m).begin(), dumpKeys(m).end() ) << endl;
84 * dumpRangeLine( DBG, dumpValues(m).begin(), dumpValues(m).end() ) << endl;
88 template<class _Iterator>
89 std::ostream & dumpRange( std::ostream & str,
90 _Iterator begin, _Iterator end,
91 const std::string & intro = "{",
92 const std::string & pfx = "\n ",
93 const std::string & sep = "\n ",
94 const std::string & sfx = "\n",
95 const std::string & extro = "}" )
100 str << pfx << *begin;
101 for ( ++begin; begin != end; ++begin )
102 str << sep << *begin;
108 /** Print range defined by iterators (single line style).
111 template<class _Iterator>
112 std::ostream & dumpRangeLine( std::ostream & str,
113 _Iterator begin, _Iterator end )
114 { return dumpRange( str, begin, end, "(", "", ", ", "", ")" ); }
118 std::ostream & operator<<( std::ostream & str, const std::vector<_Tp> & obj )
119 { return dumpRange( str, obj.begin(), obj.end() ); }
122 std::ostream & operator<<( std::ostream & str, const std::set<_Tp> & obj )
123 { return dumpRange( str, obj.begin(), obj.end() ); }
126 std::ostream & operator<<( std::ostream & str, const std::tr1::unordered_set<_Tp> & obj )
127 { return dumpRange( str, obj.begin(), obj.end() ); }
130 std::ostream & operator<<( std::ostream & str, const std::list<_Tp> & obj )
131 { return dumpRange( str, obj.begin(), obj.end() ); }
133 ///////////////////////////////////////////////////////////////////
134 namespace _logtoolsdetail
135 { /////////////////////////////////////////////////////////////////
137 ///////////////////////////////////////////////////////////////////
139 ///////////////////////////////////////////////////////////////////
141 /** std::pair wrapper for std::map output.
142 * Just because we want a special output format for std::pair
143 * used in a std::map. The mapped std::pair is printed as
144 * <tt>[key] = value</tt>.
146 template<class _Pair>
150 MapEntry( const _Pair & pair_r )
154 const _Pair & pair() const
158 const _Pair *const _pair;
161 /** \relates MapEntry Stream output. */
162 template<class _Pair>
163 std::ostream & operator<<( std::ostream & str, const MapEntry<_Pair> & obj )
165 return str << '[' << obj.pair().first << "] = " << obj.pair().second;
168 /** \relates MapEntry Convenience function to create MapEntry from std::pair. */
169 template<class _Pair>
170 MapEntry<_Pair> mapEntry( const _Pair & pair_r )
171 { return MapEntry<_Pair>( pair_r ); }
173 ///////////////////////////////////////////////////////////////////
175 ///////////////////////////////////////////////////////////////////
177 /** std::map wrapper for stream output.
178 * Uses a transform_iterator to wrap the std::pair into MapEntry.
185 typedef _Map MapType;
186 typedef typename _Map::value_type PairType;
187 typedef MapEntry<PairType> MapEntryType;
189 struct Transformer : public std::unary_function<PairType, MapEntryType>
191 MapEntryType operator()( const PairType & pair_r ) const
192 { return mapEntry( pair_r ); }
195 typedef transform_iterator<Transformer, typename MapType::const_iterator>
196 MapEntry_const_iterator;
199 DumpMap( const _Map & map_r )
203 const _Map & map() const
206 MapEntry_const_iterator begin() const
207 { return make_transform_iterator( map().begin(), Transformer() ); }
209 MapEntry_const_iterator end() const
210 { return make_transform_iterator( map().end(), Transformer() );}
213 const _Map *const _map;
216 /** \relates DumpMap Stream output. */
218 std::ostream & operator<<( std::ostream & str, const DumpMap<_Map> & obj )
219 { return dumpRange( str, obj.begin(), obj.end() ); }
221 /** \relates DumpMap Convenience function to create DumpMap from std::map. */
223 DumpMap<_Map> dumpMap( const _Map & map_r )
224 { return DumpMap<_Map>( map_r ); }
226 ///////////////////////////////////////////////////////////////////
228 ///////////////////////////////////////////////////////////////////
230 /** std::map wrapper for stream output of keys.
231 * Uses MapKVIterator iterate and write the key values.
233 * std::map<...> mymap;
234 * std::cout << dumpKeys(mymap) << std::endl;
241 typedef typename MapKVIteratorTraits<_Map>::Key_const_iterator MapKey_const_iterator;
244 DumpKeys( const _Map & map_r )
248 const _Map & map() const
251 MapKey_const_iterator begin() const
252 { return make_map_key_begin( map() ); }
254 MapKey_const_iterator end() const
255 { return make_map_key_end( map() ); }
258 const _Map *const _map;
261 /** \relates DumpKeys Stream output. */
263 std::ostream & operator<<( std::ostream & str, const DumpKeys<_Map> & obj )
264 { return dumpRange( str, obj.begin(), obj.end() ); }
266 /** \relates DumpKeys Convenience function to create DumpKeys from std::map. */
268 DumpKeys<_Map> dumpKeys( const _Map & map_r )
269 { return DumpKeys<_Map>( map_r ); }
271 ///////////////////////////////////////////////////////////////////
273 ///////////////////////////////////////////////////////////////////
275 /** std::map wrapper for stream output of values.
276 * Uses MapKVIterator iterate and write the values.
278 * std::map<...> mymap;
279 * std::cout << dumpValues(mymap) << std::endl;
286 typedef typename MapKVIteratorTraits<_Map>::Value_const_iterator MapValue_const_iterator;
289 DumpValues( const _Map & map_r )
293 const _Map & map() const
296 MapValue_const_iterator begin() const
297 { return make_map_value_begin( map() ); }
299 MapValue_const_iterator end() const
300 { return make_map_value_end( map() ); }
303 const _Map *const _map;
306 /** \relates DumpValues Stream output. */
308 std::ostream & operator<<( std::ostream & str, const DumpValues<_Map> & obj )
309 { return dumpRange( str, obj.begin(), obj.end() ); }
311 /** \relates DumpValues Convenience function to create DumpValues from std::map. */
313 DumpValues<_Map> dumpValues( const _Map & map_r )
314 { return DumpValues<_Map>( map_r ); }
316 /////////////////////////////////////////////////////////////////
317 } // namespace _logtoolsdetail
318 ///////////////////////////////////////////////////////////////////
321 using _logtoolsdetail::mapEntry; // std::pair as '[key] = value'
322 using _logtoolsdetail::dumpMap; // dumpRange '[key] = value'
323 using _logtoolsdetail::dumpKeys; // dumpRange keys
324 using _logtoolsdetail::dumpValues; // dumpRange values
326 template<class _Key, class _Tp>
327 std::ostream & operator<<( std::ostream & str, const std::map<_Key, _Tp> & obj )
328 { return str << dumpMap( obj ); }
330 template<class _Key, class _Tp>
331 std::ostream & operator<<( std::ostream & str, const std::tr1::unordered_map<_Key, _Tp> & obj )
332 { return str << dumpMap( obj ); }
334 /** Print stream status bits.
335 * Prints the values of a streams \c good, \c eof, \c failed and \c bad bit.
339 * [_eF_] - eof and fail bit set
340 * [__FB] - fail and bad bit set
343 inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj )
345 std::string ret( "[" );
346 ret += ( obj.good() ? 'g' : '_' );
347 ret += ( obj.eof() ? 'e' : '_' );
348 ret += ( obj.fail() ? 'F' : '_' );
349 ret += ( obj.bad() ? 'B' : '_' );
354 ///////////////////////////////////////////////////////////////////
355 // iomanipulator: str << dump(val) << ...
356 // calls: std::ostream & dumpOn( std::ostream & str, const Type & obj )
357 ///////////////////////////////////////////////////////////////////
364 Dump( const _Tp & obj_r ) : _obj( obj_r ) {}
369 std::ostream & operator<<( std::ostream & str, const Dump<_Tp> & obj )
370 { return dumpOn( str, obj._obj ); }
374 detail::Dump<_Tp> dump( const _Tp & obj_r )
375 { return detail::Dump<_Tp>(obj_r); }
378 /////////////////////////////////////////////////////////////////
380 ///////////////////////////////////////////////////////////////////
381 #endif // ZYPP_BASE_LOGTOOLS_H