comment typo fixed
[platform/upstream/libzypp.git] / zypp / base / LogTools.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/LogTools.h
10  *
11 */
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 #include "zypp/base/Logger.h"
22 #include "zypp/base/Iterator.h"
23
24 ///////////////////////////////////////////////////////////////////
25 namespace zypp
26 { /////////////////////////////////////////////////////////////////
27
28   /** Print range defined by iterators.
29    * \code
30    * intro [ pfx ITEM [ { sep ITEM }+ ] sfx ] extro
31    * \endcode
32    * The defaults print the range enclosed in \c {}, one item per
33    * line indented by 2 spaces.
34    * \code
35    * {
36    *   item1
37    *   item2
38    * }
39    * {} // on empty range
40    * \endcode
41    * A comma separated list enclosed in \c () would be
42    * \code
43    * dumpRange( stream, begin, end, "(", "", ", ", "", ")" );
44    * \endcode
45   */
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 = "}" )
54     {
55       str << intro;
56       if ( begin != end )
57         {
58           str << pfx << *begin;
59           for (  ++begin; begin != end; ++begin )
60             str << sep << *begin;
61           str << sfx;
62         }
63       return str << extro;
64     }
65
66   template<class _Iterator>
67     std::ostream & dumpRangeLine( std::ostream & str,
68                                   _Iterator begin, _Iterator end )
69     { return dumpRange( str, begin, end, "(", "", ", ", "", ")" ); }
70
71   template<class _Tp>
72     std::ostream & operator<<( std::ostream & str, const std::vector<_Tp> & obj )
73     { return dumpRange( str, obj.begin(), obj.end() ); }
74
75   template<class _Tp>
76     std::ostream & operator<<( std::ostream & str, const std::set<_Tp> & obj )
77     { return dumpRange( str, obj.begin(), obj.end() ); }
78
79   template<class _Tp>
80     std::ostream & operator<<( std::ostream & str, const std::list<_Tp> & obj )
81     { return dumpRange( str, obj.begin(), obj.end() ); }
82
83   ///////////////////////////////////////////////////////////////////
84   namespace _logtoolsdetail
85   { /////////////////////////////////////////////////////////////////
86
87     ///////////////////////////////////////////////////////////////////
88     // mapEntry
89     ///////////////////////////////////////////////////////////////////
90
91     /** std::pair wrapper for std::map output.
92      * Just because we want a special output format for std::pair
93      * used in a std::map.
94     */
95     template<class _Pair>
96       class MapEntry
97       {
98       public:
99         MapEntry( const _Pair & pair_r )
100         : _pair( &pair_r )
101         {}
102
103         const _Pair & pair() const
104         { return *_pair; }
105
106       private:
107         const _Pair *const _pair;
108       };
109
110     /** \relates MapEntry Stream output. */
111     template<class _Pair>
112       std::ostream & operator<<( std::ostream & str, const MapEntry<_Pair> & obj )
113       {
114         return str << '[' << obj.pair().first << "] = " << obj.pair().second;
115       }
116
117     /** \relates MapEntry Convenience function to create MapEntry from std::pair. */
118     template<class _Pair>
119       MapEntry<_Pair> mapEntry( const _Pair & pair_r )
120       { return MapEntry<_Pair>( pair_r ); }
121
122     ///////////////////////////////////////////////////////////////////
123     // dumpMap
124     ///////////////////////////////////////////////////////////////////
125
126     /** std::map wrapper for stream output.
127      * Provides the transform_iterator used to write std::pair formated as
128      * MapEntry.
129      */
130     template<class _Map>
131       class DumpMap
132       {
133       private:
134         typedef _Map                        MapType;
135         typedef typename _Map::value_type   PairType;
136         typedef MapEntry<PairType>          MapEntryType;
137
138         struct Transformer : public std::unary_function<PairType, MapEntryType>
139         {
140           MapEntryType operator()( const PairType & pair_r ) const
141           { return mapEntry( pair_r ); }
142         };
143
144         typedef transform_iterator<Transformer, typename MapType::const_iterator>
145                 MapEntry_const_iterator;
146
147       public:
148         DumpMap( const _Map & map_r )
149         : _map( &map_r )
150         {}
151
152         const _Map & map() const
153         { return *_map; }
154
155         MapEntry_const_iterator map_begin() const
156         { return make_transform_iterator( map().begin(), Transformer() ); }
157
158         MapEntry_const_iterator map_end() const
159         { return make_transform_iterator( map().end(), Transformer() );}
160
161       private:
162         const _Map *const _map;
163       };
164
165     /** \relates DumpMap Stream output. */
166     template<class _Map>
167       std::ostream & operator<<( std::ostream & str, const DumpMap<_Map> & obj )
168       { return dumpRange( str, obj.map_begin(), obj.map_end() ); }
169
170     /** \relates DumpMap Convenience function to create DumpMap from std::map. */
171     template<class _Map>
172       DumpMap<_Map> dumpMap( const _Map & map_r )
173       { return DumpMap<_Map>( map_r ); }
174
175     ///////////////////////////////////////////////////////////////////
176     // dumpKeys
177     ///////////////////////////////////////////////////////////////////
178
179     /** std::map wrapper for stream output of keys.
180      * Uses MapKVIterator iterate and write the key values.
181      * \code
182      * std::map<...> mymap;
183      * std::cout << dumpKeys(mymap) << std::endl;
184      * \endcode
185      */
186     template<class _Map>
187       class DumpKeys
188       {
189       public:
190         DumpKeys( const _Map & map_r )
191         : _map( &map_r )
192         {}
193
194         const _Map & map() const
195         { return *_map; }
196
197       private:
198         const _Map *const _map;
199       };
200
201     /** \relates DumpKeys Stream output. */
202     template<class _Map>
203       std::ostream & operator<<( std::ostream & str, const DumpKeys<_Map> & obj )
204       { return dumpRange( str, make_map_key_begin(obj.map()), make_map_key_end(obj.map()) ); }
205
206     /** \relates DumpKeys Convenience function to create DumpKeys from std::map. */
207     template<class _Map>
208       DumpKeys<_Map> dumpKeys( const _Map & map_r )
209       { return DumpKeys<_Map>( map_r ); }
210
211     ///////////////////////////////////////////////////////////////////
212     // dumpValues
213     ///////////////////////////////////////////////////////////////////
214
215     /** std::map wrapper for stream output of values.
216      * Uses MapKVIterator iterate and write the values.
217      * \code
218      * std::map<...> mymap;
219      * std::cout << dumpValues(mymap) << std::endl;
220      * \endcode
221      */
222     template<class _Map>
223       class DumpValues
224       {
225       public:
226         DumpValues( const _Map & map_r )
227         : _map( &map_r )
228         {}
229
230         const _Map & map() const
231         { return *_map; }
232
233       private:
234         const _Map *const _map;
235       };
236
237     /** \relates DumpValues Stream output. */
238     template<class _Map>
239       std::ostream & operator<<( std::ostream & str, const DumpValues<_Map> & obj )
240       { return dumpRange( str, make_map_value_begin(obj.map()), make_map_value_end(obj.map()) ); }
241
242     /** \relates DumpValues Convenience function to create DumpValues from std::map. */
243     template<class _Map>
244       DumpValues<_Map> dumpValues( const _Map & map_r )
245       { return DumpValues<_Map>( map_r ); }
246
247     /////////////////////////////////////////////////////////////////
248   } // namespace _logtoolsdetail
249   ///////////////////////////////////////////////////////////////////
250
251   // iomanipulator
252   using _logtoolsdetail::mapEntry;   // std::pair as '[key] = value'
253   using _logtoolsdetail::dumpMap;    // dumpRange '[key] = value'
254   using _logtoolsdetail::dumpKeys;   // dumpRange keys
255   using _logtoolsdetail::dumpValues; // dumpRange values
256
257   template<class _Key, class _Tp>
258     std::ostream & operator<<( std::ostream & str, const std::map<_Key, _Tp> & obj )
259     { return str << dumpMap( obj ); }
260
261   /** Print stream status bits.
262    * Prints the values of a streams \c good, \c eof, \c failed and \c bad bit.
263    *
264    * \code
265    *  [g___] - good
266    *  [_eF_] - eof and fail bit set
267    *  [__FB] - fail and bad bit set
268    * \endcode
269   */
270   inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj )
271   {
272     std::string ret( "[" );
273     ret += ( obj.good() ? 'g' : '_' );
274     ret += ( obj.eof()  ? 'e' : '_' );
275     ret += ( obj.fail() ? 'F' : '_' );
276     ret += ( obj.bad()  ? 'B' : '_' );
277     ret += "]";
278     return str << ret;
279   }
280
281   /////////////////////////////////////////////////////////////////
282 } // namespace zypp
283 ///////////////////////////////////////////////////////////////////
284 #endif // ZYPP_BASE_LOGTOOLS_H