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/base/Deprecated.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::list<_Tp> & obj )
133 { return dumpRange( str, obj.begin(), obj.end() ); }
135 ///////////////////////////////////////////////////////////////////
136 namespace _logtoolsdetail
137 { /////////////////////////////////////////////////////////////////
139 ///////////////////////////////////////////////////////////////////
141 ///////////////////////////////////////////////////////////////////
143 /** std::pair wrapper for std::map output.
144 * Just because we want a special output format for std::pair
145 * used in a std::map. The mapped std::pair is printed as
146 * <tt>[key] = value</tt>.
148 template<class _Pair>
152 MapEntry( const _Pair & pair_r )
156 const _Pair & pair() const
160 const _Pair *const _pair;
163 /** \relates MapEntry Stream output. */
164 template<class _Pair>
165 std::ostream & operator<<( std::ostream & str, const MapEntry<_Pair> & obj )
167 return str << '[' << obj.pair().first << "] = " << obj.pair().second;
170 /** \relates MapEntry Convenience function to create MapEntry from std::pair. */
171 template<class _Pair>
172 MapEntry<_Pair> mapEntry( const _Pair & pair_r )
173 { return MapEntry<_Pair>( pair_r ); }
175 ///////////////////////////////////////////////////////////////////
177 ///////////////////////////////////////////////////////////////////
179 /** std::map wrapper for stream output.
180 * Uses a transform_iterator to wrap the std::pair into MapEntry.
187 typedef _Map MapType;
188 typedef typename _Map::value_type PairType;
189 typedef MapEntry<PairType> MapEntryType;
191 struct Transformer : public std::unary_function<PairType, MapEntryType>
193 MapEntryType operator()( const PairType & pair_r ) const
194 { return mapEntry( pair_r ); }
197 typedef transform_iterator<Transformer, typename MapType::const_iterator>
198 MapEntry_const_iterator;
201 DumpMap( const _Map & map_r )
205 const _Map & map() const
208 MapEntry_const_iterator begin() const
209 { return make_transform_iterator( map().begin(), Transformer() ); }
211 MapEntry_const_iterator end() const
212 { return make_transform_iterator( map().end(), Transformer() );}
215 const _Map *const _map;
218 /** \relates DumpMap Stream output. */
220 std::ostream & operator<<( std::ostream & str, const DumpMap<_Map> & obj )
221 { return dumpRange( str, obj.begin(), obj.end() ); }
223 /** \relates DumpMap Convenience function to create DumpMap from std::map. */
225 DumpMap<_Map> dumpMap( const _Map & map_r )
226 { return DumpMap<_Map>( map_r ); }
228 ///////////////////////////////////////////////////////////////////
230 ///////////////////////////////////////////////////////////////////
232 /** std::map wrapper for stream output of keys.
233 * Uses MapKVIterator iterate and write the key values.
235 * std::map<...> mymap;
236 * std::cout << dumpKeys(mymap) << std::endl;
243 typedef typename MapKVIteratorTraits<_Map>::Key_const_iterator MapKey_const_iterator;
246 DumpKeys( const _Map & map_r )
250 const _Map & map() const
253 MapKey_const_iterator begin() const
254 { return make_map_key_begin( map() ); }
256 MapKey_const_iterator end() const
257 { return make_map_key_end( map() ); }
260 const _Map *const _map;
263 /** \relates DumpKeys Stream output. */
265 std::ostream & operator<<( std::ostream & str, const DumpKeys<_Map> & obj )
266 { return dumpRange( str, obj.begin(), obj.end() ); }
268 /** \relates DumpKeys Convenience function to create DumpKeys from std::map. */
270 DumpKeys<_Map> dumpKeys( const _Map & map_r )
271 { return DumpKeys<_Map>( map_r ); }
273 ///////////////////////////////////////////////////////////////////
275 ///////////////////////////////////////////////////////////////////
277 /** std::map wrapper for stream output of values.
278 * Uses MapKVIterator iterate and write the values.
280 * std::map<...> mymap;
281 * std::cout << dumpValues(mymap) << std::endl;
288 typedef typename MapKVIteratorTraits<_Map>::Value_const_iterator MapValue_const_iterator;
291 DumpValues( const _Map & map_r )
295 const _Map & map() const
298 MapValue_const_iterator begin() const
299 { return make_map_value_begin( map() ); }
301 MapValue_const_iterator end() const
302 { return make_map_value_end( map() ); }
305 const _Map *const _map;
308 /** \relates DumpValues Stream output. */
310 std::ostream & operator<<( std::ostream & str, const DumpValues<_Map> & obj )
311 { return dumpRange( str, obj.begin(), obj.end() ); }
313 /** \relates DumpValues Convenience function to create DumpValues from std::map. */
315 DumpValues<_Map> dumpValues( const _Map & map_r )
316 { return DumpValues<_Map>( map_r ); }
318 /////////////////////////////////////////////////////////////////
319 } // namespace _logtoolsdetail
320 ///////////////////////////////////////////////////////////////////
323 using _logtoolsdetail::mapEntry; // std::pair as '[key] = value'
324 using _logtoolsdetail::dumpMap; // dumpRange '[key] = value'
325 using _logtoolsdetail::dumpKeys; // dumpRange keys
326 using _logtoolsdetail::dumpValues; // dumpRange values
328 template<class _Key, class _Tp>
329 std::ostream & operator<<( std::ostream & str, const std::map<_Key, _Tp> & obj )
330 { return str << dumpMap( obj ); }
332 template<class _Key, class _Tp>
333 std::ostream & operator<<( std::ostream & str, const std::tr1::unordered_map<_Key, _Tp> & obj )
334 { return str << dumpMap( obj ); }
336 /** Print stream status bits.
337 * Prints the values of a streams \c good, \c eof, \c failed and \c bad bit.
341 * [_eF_] - eof and fail bit set
342 * [__FB] - fail and bad bit set
345 inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj )
347 std::string ret( "[" );
348 ret += ( obj.good() ? 'g' : '_' );
349 ret += ( obj.eof() ? 'e' : '_' );
350 ret += ( obj.fail() ? 'F' : '_' );
351 ret += ( obj.bad() ? 'B' : '_' );
356 ///////////////////////////////////////////////////////////////////
357 // iomanipulator: str << dump(val) << ...
358 // calls: std::ostream & dumpOn( std::ostream & str, const Type & obj )
359 ///////////////////////////////////////////////////////////////////
366 Dump( const _Tp & obj_r ) : _obj( obj_r ) {}
371 std::ostream & operator<<( std::ostream & str, const Dump<_Tp> & obj )
372 { return dumpOn( str, obj._obj ); }
376 detail::Dump<_Tp> dump( const _Tp & obj_r )
377 { return detail::Dump<_Tp>(obj_r); }
380 /////////////////////////////////////////////////////////////////
382 ///////////////////////////////////////////////////////////////////
383 #endif // ZYPP_BASE_LOGTOOLS_H