libzypp  13.10.6
LogTools.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
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 
22 #include "zypp/base/Tr1hash.h"
23 #include "zypp/base/Logger.h"
24 #include "zypp/base/Iterator.h"
25 #include "zypp/APIConfig.h"
26 
28 namespace zypp
29 {
30 
31  using std::endl;
32 
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 = "}" )
98  {
99  str << intro;
100  if ( begin != end )
101  {
102  str << pfx << *begin;
103  for ( ++begin; begin != end; ++begin )
104  str << sep << *begin;
105  str << sfx;
106  }
107  return str << extro;
108  }
109 
113  template<class _Iterator>
114  std::ostream & dumpRangeLine( std::ostream & str,
115  _Iterator begin, _Iterator end )
116  { return dumpRange( str, begin, end, "(", "", ", ", "", ")" ); }
117 
118 
119  template<class _Tp>
120  std::ostream & operator<<( std::ostream & str, const std::vector<_Tp> & obj )
121  { return dumpRange( str, obj.begin(), obj.end() ); }
122 
123  template<class _Tp>
124  std::ostream & operator<<( std::ostream & str, const std::set<_Tp> & obj )
125  { return dumpRange( str, obj.begin(), obj.end() ); }
126 
127  template<class _Tp>
128  std::ostream & operator<<( std::ostream & str, const std::tr1::unordered_set<_Tp> & obj )
129  { return dumpRange( str, obj.begin(), obj.end() ); }
130 
131  template<class _Tp>
132  std::ostream & operator<<( std::ostream & str, const std::multiset<_Tp> & obj )
133  { return dumpRange( str, obj.begin(), obj.end() ); }
134 
135  template<class _Tp>
136  std::ostream & operator<<( std::ostream & str, const std::list<_Tp> & obj )
137  { return dumpRange( str, obj.begin(), obj.end() ); }
138 
140  namespace _logtoolsdetail
141  {
142 
144  // mapEntry
146 
152  template<class _Pair>
153  class MapEntry
154  {
155  public:
156  MapEntry( const _Pair & pair_r )
157  : _pair( &pair_r )
158  {}
159 
160  const _Pair & pair() const
161  { return *_pair; }
162 
163  private:
164  const _Pair *const _pair;
165  };
166 
168  template<class _Pair>
169  std::ostream & operator<<( std::ostream & str, const MapEntry<_Pair> & obj )
170  {
171  return str << '[' << obj.pair().first << "] = " << obj.pair().second;
172  }
173 
175  template<class _Pair>
176  MapEntry<_Pair> mapEntry( const _Pair & pair_r )
177  { return MapEntry<_Pair>( pair_r ); }
178 
180  // dumpMap
182 
187  template<class _Map>
188  class DumpMap
189  {
190  public:
191  typedef _Map MapType;
192  typedef typename _Map::value_type PairType;
194 
195  struct Transformer : public std::unary_function<PairType, MapEntryType>
196  {
197  MapEntryType operator()( const PairType & pair_r ) const
198  { return mapEntry( pair_r ); }
199  };
200 
201  typedef transform_iterator<Transformer, typename MapType::const_iterator>
203 
204  public:
205  DumpMap( const _Map & map_r )
206  : _map( &map_r )
207  {}
208 
209  const _Map & map() const
210  { return *_map; }
211 
213  { return make_transform_iterator( map().begin(), Transformer() ); }
214 
216  { return make_transform_iterator( map().end(), Transformer() );}
217 
218  private:
219  const _Map *const _map;
220  };
221 
223  template<class _Map>
224  std::ostream & operator<<( std::ostream & str, const DumpMap<_Map> & obj )
225  { return dumpRange( str, obj.begin(), obj.end() ); }
226 
228  template<class _Map>
229  DumpMap<_Map> dumpMap( const _Map & map_r )
230  { return DumpMap<_Map>( map_r ); }
231 
233  // dumpKeys
235 
243  template<class _Map>
244  class DumpKeys
245  {
246  public:
248 
249  public:
250  DumpKeys( const _Map & map_r )
251  : _map( &map_r )
252  {}
253 
254  const _Map & map() const
255  { return *_map; }
256 
258  { return make_map_key_begin( map() ); }
259 
261  { return make_map_key_end( map() ); }
262 
263  private:
264  const _Map *const _map;
265  };
266 
268  template<class _Map>
269  std::ostream & operator<<( std::ostream & str, const DumpKeys<_Map> & obj )
270  { return dumpRange( str, obj.begin(), obj.end() ); }
271 
273  template<class _Map>
274  DumpKeys<_Map> dumpKeys( const _Map & map_r )
275  { return DumpKeys<_Map>( map_r ); }
276 
278  // dumpValues
280 
288  template<class _Map>
290  {
291  public:
293 
294  public:
295  DumpValues( const _Map & map_r )
296  : _map( &map_r )
297  {}
298 
299  const _Map & map() const
300  { return *_map; }
301 
303  { return make_map_value_begin( map() ); }
304 
306  { return make_map_value_end( map() ); }
307 
308  private:
309  const _Map *const _map;
310  };
311 
313  template<class _Map>
314  std::ostream & operator<<( std::ostream & str, const DumpValues<_Map> & obj )
315  { return dumpRange( str, obj.begin(), obj.end() ); }
316 
318  template<class _Map>
319  DumpValues<_Map> dumpValues( const _Map & map_r )
320  { return DumpValues<_Map>( map_r ); }
321 
323  } // namespace _logtoolsdetail
325 
326  // iomanipulator
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
331 
332  template<class _Key, class _Tp>
333  std::ostream & operator<<( std::ostream & str, const std::map<_Key, _Tp> & obj )
334  { return str << dumpMap( obj ); }
335 
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 ); }
339 
340  template<class _Key, class _Tp>
341  std::ostream & operator<<( std::ostream & str, const std::multimap<_Key, _Tp> & obj )
342  { return str << dumpMap( obj ); }
343 
353  inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj )
354  {
355  std::string ret( "[" );
356  ret += ( obj.good() ? 'g' : '_' );
357  ret += ( obj.eof() ? 'e' : '_' );
358  ret += ( obj.fail() ? 'F' : '_' );
359  ret += ( obj.bad() ? 'B' : '_' );
360  ret += "]";
361  return str << ret;
362  }
363 
365  // iomanipulator: str << dump(val) << ...
366  // calls: std::ostream & dumpOn( std::ostream & str, const Type & obj )
368 
369  namespace detail
370  {
371  template<class _Tp>
372  struct Dump
373  {
374  Dump( const _Tp & obj_r ) : _obj( obj_r ) {}
375  const _Tp & _obj;
376  };
377 
378  template<class _Tp>
379  std::ostream & operator<<( std::ostream & str, const Dump<_Tp> & obj )
380  { return dumpOn( str, obj._obj ); }
381  }
382 
383  template<class _Tp>
384  detail::Dump<_Tp> dump( const _Tp & obj_r )
385  { return detail::Dump<_Tp>(obj_r); }
386 
387 
389 } // namespace zypp
391 #endif // ZYPP_BASE_LOGTOOLS_H
DumpKeys< _Map > dumpKeys(const _Map &map_r)
Definition: LogTools.h:274
_Map::value_type PairType
Definition: LogTools.h:192
MapEntry< PairType > MapEntryType
Definition: LogTools.h:193
MapEntry_const_iterator begin() const
Definition: LogTools.h:212
const _Tp & _obj
Definition: LogTools.h:375
MapValue_const_iterator end() const
Definition: LogTools.h:305
MapKVIteratorTraits< _Map >::Value_const_iterator make_map_value_end(const _Map &map_r)
Convenience to create the value iterator from container::end()
Definition: Iterator.h:241
std::ostream & dumpOn(std::ostream &str, const PoolQueryIterator &obj)
Definition: PoolQuery.cc:1718
DumpMap(const _Map &map_r)
Definition: LogTools.h:205
MapKVIteratorTraits< _Map >::Key_const_iterator MapKey_const_iterator
Definition: LogTools.h:247
const _Pair *const _pair
Definition: LogTools.h:164
transform_iterator< GetPairFirst< typename MapType::value_type >, typename MapType::const_iterator > Key_const_iterator
The key iterator type.
Definition: Iterator.h:216
std::map wrapper for stream output.
Definition: LogTools.h:188
MapEntryType operator()(const PairType &pair_r) const
Definition: LogTools.h:197
DumpKeys(const _Map &map_r)
Definition: LogTools.h:250
transform_iterator< GetPairSecond< typename MapType::value_type >, typename MapType::const_iterator > Value_const_iterator
The value iterator type.
Definition: Iterator.h:221
MapKVIteratorTraits< _Map >::Value_const_iterator MapValue_const_iterator
Definition: LogTools.h:292
const _Map & map() const
Definition: LogTools.h:254
MapKey_const_iterator begin() const
Definition: LogTools.h:257
MapKVIteratorTraits< _Map >::Value_const_iterator make_map_value_begin(const _Map &map_r)
Convenience to create the value iterator from container::begin()
Definition: Iterator.h:236
std::ostream & dumpRange(std::ostream &str, _Iterator begin, _Iterator end, const std::string &intro="{", const std::string &pfx="\n ", const std::string &sep="\n ", const std::string &sfx="\n", const std::string &extro="}")
Print range defined by iterators (multiline style).
Definition: LogTools.h:91
std::pair wrapper for std::map output.
Definition: LogTools.h:153
DumpValues< _Map > dumpValues(const _Map &map_r)
Definition: LogTools.h:319
DumpMap< _Map > dumpMap(const _Map &map_r)
Definition: LogTools.h:229
Provides API related macros.
const _Map & map() const
Definition: LogTools.h:299
MapEntry< _Pair > mapEntry(const _Pair &pair_r)
Definition: LogTools.h:176
std::map wrapper for stream output of keys.
Definition: LogTools.h:244
transform_iterator< Transformer, typename MapType::const_iterator > MapEntry_const_iterator
Definition: LogTools.h:202
std::ostream & dumpRangeLine(std::ostream &str, _Iterator begin, _Iterator end)
Print range defined by iterators (single line style).
Definition: LogTools.h:114
DumpValues(const _Map &map_r)
Definition: LogTools.h:295
MapKVIteratorTraits< _Map >::Key_const_iterator make_map_key_begin(const _Map &map_r)
Convenience to create the key iterator from container::begin()
Definition: Iterator.h:226
Dump(const _Tp &obj_r)
Definition: LogTools.h:374
MapEntry_const_iterator end() const
Definition: LogTools.h:215
detail::Dump< _Tp > dump(const _Tp &obj_r)
Definition: LogTools.h:384
MapKey_const_iterator end() const
Definition: LogTools.h:260
const _Map *const _map
Definition: LogTools.h:264
std::map wrapper for stream output of values.
Definition: LogTools.h:289
MapKVIteratorTraits< _Map >::Key_const_iterator make_map_key_end(const _Map &map_r)
Convenience to create the key iterator from container::end()
Definition: Iterator.h:231
const _Pair & pair() const
Definition: LogTools.h:160
MapEntry(const _Pair &pair_r)
Definition: LogTools.h:156
const _Map *const _map
Definition: LogTools.h:219
const _Map & map() const
Definition: LogTools.h:209
MapValue_const_iterator begin() const
Definition: LogTools.h:302