1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/LogTools.h
12 #ifndef ZYPP_BASE_LOGTOOLS_H
13 #define ZYPP_BASE_LOGTOOLS_H
21 #include "zypp/base/Logger.h"
22 #include "zypp/base/Iterator.h"
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
28 /** Print range defined by iterators.
30 * intro [ pfx ITEM [ { sep ITEM }+ ] sfx ] extro
32 * The defaults print the range enclosed in \c {}, one item per
33 * line indented by 2 spaces.
39 * {} // on empty rande
41 * A comma separated list enclosed in \c () would be
43 * dumpRange( stream, begin, end, "(", "", ", ", "", ")" );
46 template<class _Iterator>
47 std::ostream & dumpRange( std::ostream & str,
48 _Iterator begin, _Iterator end,
49 const std::string & intro = "{",
50 const std::string & pfx = "\n ",
51 const std::string & sep = "\n ",
52 const std::string & sfx = "\n",
53 const std::string & extro = "}" )
59 for ( ++begin; begin != end; ++begin )
66 template<class _Iterator>
67 std::ostream & dumpRangeLine( std::ostream & str,
68 _Iterator begin, _Iterator end )
69 { return dumpRange( str, begin, end, "(", "", ", ", "", ")" ); }
72 std::ostream & operator<<( std::ostream & str, const std::vector<_Tp> & obj )
73 { return dumpRange( str, obj.begin(), obj.end() ); }
76 std::ostream & operator<<( std::ostream & str, const std::set<_Tp> & obj )
77 { return dumpRange( str, obj.begin(), obj.end() ); }
80 std::ostream & operator<<( std::ostream & str, const std::list<_Tp> & obj )
81 { return dumpRange( str, obj.begin(), obj.end() ); }
83 ///////////////////////////////////////////////////////////////////
84 namespace _logtoolsdetail
85 { /////////////////////////////////////////////////////////////////
87 ///////////////////////////////////////////////////////////////////
89 ///////////////////////////////////////////////////////////////////
91 /** std::pair wrapper for std::map output.
92 * Just because we want a special output format for std::pair
99 MapEntry( const _Pair & pair_r )
103 const _Pair & pair() const
107 const _Pair *const _pair;
110 /** \relates MapEntry Stream output. */
111 template<class _Pair>
112 std::ostream & operator<<( std::ostream & str, const MapEntry<_Pair> & obj )
114 return str << '[' << obj.pair().first << "] = " << obj.pair().second;
117 /** Functor transforming std::pair into MapEntry. */
118 template<class _Pair>
119 struct GetMapEntry : public std::unary_function<_Pair, typename zypp::_logtoolsdetail::MapEntry<_Pair> >
121 typename zypp::_logtoolsdetail::MapEntry<_Pair> operator()( const _Pair & pair_r ) const
122 { return MapEntry<_Pair>( pair_r ); }
125 /** std::map wrapper for stream output.
126 * Provides the transform_iterator used to write std::pair formated as
133 typedef _Map MapType;
134 typedef typename _Map::value_type PairType;
136 typedef transform_iterator<GetMapEntry<PairType>, typename MapType::const_iterator>
137 MapEntry_const_iterator;
139 DumpMap( const _Map & map_r )
143 const _Map & map() const
146 MapEntry_const_iterator map_begin() const
147 { return make_transform_iterator( map().begin(), GetMapEntry<PairType>() ); }
149 MapEntry_const_iterator map_end() const
150 { return make_transform_iterator( map().end(), GetMapEntry<PairType>() );}
153 const _Map *const _map;
156 /** \relates DumpMap Stream output. */
158 std::ostream & operator<<( std::ostream & str, const DumpMap<_Map> & obj )
160 return dumpRange( str, obj.map_begin(), obj.map_end() );
163 /** \relates DumpMap Convenience function to create DumpMap from std::map. */
165 DumpMap<_Map> dumpMap( const _Map & map_r )
166 { return DumpMap<_Map>( map_r ); }
168 ///////////////////////////////////////////////////////////////////
170 ///////////////////////////////////////////////////////////////////
172 /** std::map wrapper for stream output of keys.
173 * Uses MapKVIterator iterate and write the key values.
175 * std::map<...> mymap;
176 * std::cout << dumpKeys(mymap) << std::endl;
183 DumpKeys( const _Map & map_r )
187 const _Map & map() const
191 const _Map *const _map;
194 /** \relates DumpKeys Stream output. */
196 std::ostream & operator<<( std::ostream & str, const DumpKeys<_Map> & obj )
197 { return dumpRange( str, make_map_key_begin(obj.map()), make_map_key_end(obj.map()) ); }
199 /** \relates DumpKeys Convenience function to create DumpKeys from std::map. */
201 DumpKeys<_Map> dumpKeys( const _Map & map_r )
202 { return DumpKeys<_Map>( map_r ); }
204 ///////////////////////////////////////////////////////////////////
206 ///////////////////////////////////////////////////////////////////
208 /** std::map wrapper for stream output of values.
209 * Uses MapKVIterator iterate and write the values.
211 * std::map<...> mymap;
212 * std::cout << dumpValues(mymap) << std::endl;
219 DumpValues( const _Map & map_r )
223 const _Map & map() const
227 const _Map *const _map;
230 /** \relates DumpValues Stream output. */
232 std::ostream & operator<<( std::ostream & str, const DumpValues<_Map> & obj )
233 { return dumpRange( str, make_map_value_begin(obj.map()), make_map_value_end(obj.map()) ); }
235 /** \relates DumpValues Convenience function to create DumpValues from std::map. */
237 DumpValues<_Map> dumpValues( const _Map & map_r )
238 { return DumpValues<_Map>( map_r ); }
240 /////////////////////////////////////////////////////////////////
241 } // namespace _logtoolsdetail
242 ///////////////////////////////////////////////////////////////////
244 using _logtoolsdetail::dumpMap; // dumpRange '[key] = value'
245 using _logtoolsdetail::dumpKeys; // dumpRange keys
246 using _logtoolsdetail::dumpValues; // dumpRange values
248 template<class _Key, class _Tp>
249 std::ostream & operator<<( std::ostream & str, const std::map<_Key, _Tp> & obj )
250 { return str << dumpMap( obj ); }
252 /** Print stream status bits.
253 * Prints the values of a streams \c good, \c eof, \c failed and \c bad bit.
257 * [_eF_] - eof and fail bit set
258 * [__FB] - fail and bad bit set
261 inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj )
263 std::string ret( "[" );
264 ret += ( obj.good() ? 'g' : '_' );
265 ret += ( obj.eof() ? 'e' : '_' );
266 ret += ( obj.fail() ? 'F' : '_' );
267 ret += ( obj.bad() ? 'B' : '_' );
272 /////////////////////////////////////////////////////////////////
274 ///////////////////////////////////////////////////////////////////
275 #endif // ZYPP_BASE_LOGTOOLS_H