libzypp  17.7.0
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/Hash.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 TIterator>
91  std::ostream & dumpRange( std::ostream & str,
92  TIterator begin, TIterator 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 TIterator>
114  std::ostream & dumpRangeLine( std::ostream & str,
115  TIterator begin, TIterator end )
116  { return dumpRange( str, begin, end, "(", "", ", ", "", ")" ); }
118  template<class TContainer>
119  std::ostream & dumpRangeLine( std::ostream & str, const TContainer & cont )
120  { return dumpRangeLine( str, cont.begin(), cont.end() ); }
121 
122 
124  namespace iomanip
125  {
130  template<class TIterator>
131  struct RangeLine
132  {
133  RangeLine( TIterator begin, TIterator end )
134  : _begin( begin )
135  , _end( end )
136  {}
137  TIterator _begin;
138  TIterator _end;
139  };
140 
142  template<class TIterator>
143  std::ostream & operator<<( std::ostream & str, const RangeLine<TIterator> & obj )
144  { return dumpRangeLine( str, obj._begin, obj._end ); }
145 
146  } // namespce iomanip
148 
156  template<class TIterator>
157  iomanip::RangeLine<TIterator> rangeLine( TIterator begin, TIterator end )
158  { return iomanip::RangeLine<TIterator>( begin, end ); }
160  template<class TContainer>
161  auto rangeLine( const TContainer & cont ) -> decltype( rangeLine( cont.begin(), cont.end() ) )
162  { return rangeLine( cont.begin(), cont.end() ); }
163 
164  template<class Tp>
165  std::ostream & operator<<( std::ostream & str, const std::vector<Tp> & obj )
166  { return dumpRange( str, obj.begin(), obj.end() ); }
167 
168  template<class Tp, class TCmp, class TAlloc>
169  std::ostream & operator<<( std::ostream & str, const std::set<Tp,TCmp,TAlloc> & obj )
170  { return dumpRange( str, obj.begin(), obj.end() ); }
171 
172  template<class Tp>
173  std::ostream & operator<<( std::ostream & str, const std::unordered_set<Tp> & obj )
174  { return dumpRange( str, obj.begin(), obj.end() ); }
175 
176  template<class Tp>
177  std::ostream & operator<<( std::ostream & str, const std::multiset<Tp> & obj )
178  { return dumpRange( str, obj.begin(), obj.end() ); }
179 
180  template<class Tp>
181  std::ostream & operator<<( std::ostream & str, const std::list<Tp> & obj )
182  { return dumpRange( str, obj.begin(), obj.end() ); }
183 
184  template<class Tp>
185  std::ostream & operator<<( std::ostream & str, const Iterable<Tp> & obj )
186  { return dumpRange( str, obj.begin(), obj.end() ); }
187 
189  namespace _logtoolsdetail
190  {
191 
193  // mapEntry
195 
201  template<class TPair>
202  class MapEntry
203  {
204  public:
205  MapEntry( const TPair & pair_r )
206  : _pair( &pair_r )
207  {}
208 
209  const TPair & pair() const
210  { return *_pair; }
211 
212  private:
213  const TPair *const _pair;
214  };
215 
217  template<class TPair>
218  std::ostream & operator<<( std::ostream & str, const MapEntry<TPair> & obj )
219  {
220  return str << '[' << obj.pair().first << "] = " << obj.pair().second;
221  }
222 
224  template<class TPair>
225  MapEntry<TPair> mapEntry( const TPair & pair_r )
226  { return MapEntry<TPair>( pair_r ); }
227 
229  // dumpMap
231 
236  template<class TMap>
237  class DumpMap
238  {
239  public:
240  typedef TMap MapType;
241  typedef typename TMap::value_type PairType;
243 
244  struct Transformer : public std::unary_function<PairType, MapEntryType>
245  {
246  MapEntryType operator()( const PairType & pair_r ) const
247  { return mapEntry( pair_r ); }
248  };
249 
250  typedef transform_iterator<Transformer, typename MapType::const_iterator>
252 
253  public:
254  DumpMap( const TMap & map_r )
255  : _map( &map_r )
256  {}
257 
258  const TMap & map() const
259  { return *_map; }
260 
262  { return make_transform_iterator( map().begin(), Transformer() ); }
263 
265  { return make_transform_iterator( map().end(), Transformer() );}
266 
267  private:
268  const TMap *const _map;
269  };
270 
272  template<class TMap>
273  std::ostream & operator<<( std::ostream & str, const DumpMap<TMap> & obj )
274  { return dumpRange( str, obj.begin(), obj.end() ); }
275 
277  template<class TMap>
278  DumpMap<TMap> dumpMap( const TMap & map_r )
279  { return DumpMap<TMap>( map_r ); }
280 
282  // dumpKeys
284 
292  template<class TMap>
293  class DumpKeys
294  {
295  public:
297 
298  public:
299  DumpKeys( const TMap & map_r )
300  : _map( &map_r )
301  {}
302 
303  const TMap & map() const
304  { return *_map; }
305 
307  { return make_map_key_begin( map() ); }
308 
310  { return make_map_key_end( map() ); }
311 
312  private:
313  const TMap *const _map;
314  };
315 
317  template<class TMap>
318  std::ostream & operator<<( std::ostream & str, const DumpKeys<TMap> & obj )
319  { return dumpRange( str, obj.begin(), obj.end() ); }
320 
322  template<class TMap>
323  DumpKeys<TMap> dumpKeys( const TMap & map_r )
324  { return DumpKeys<TMap>( map_r ); }
325 
327  // dumpValues
329 
337  template<class TMap>
339  {
340  public:
342 
343  public:
344  DumpValues( const TMap & map_r )
345  : _map( &map_r )
346  {}
347 
348  const TMap & map() const
349  { return *_map; }
350 
352  { return make_map_value_begin( map() ); }
353 
355  { return make_map_value_end( map() ); }
356 
357  private:
358  const TMap *const _map;
359  };
360 
362  template<class TMap>
363  std::ostream & operator<<( std::ostream & str, const DumpValues<TMap> & obj )
364  { return dumpRange( str, obj.begin(), obj.end() ); }
365 
367  template<class TMap>
368  DumpValues<TMap> dumpValues( const TMap & map_r )
369  { return DumpValues<TMap>( map_r ); }
370 
372  } // namespace _logtoolsdetail
374 
375  // iomanipulator
376  using _logtoolsdetail::mapEntry; // std::pair as '[key] = value'
377  using _logtoolsdetail::dumpMap; // dumpRange '[key] = value'
378  using _logtoolsdetail::dumpKeys; // dumpRange keys
379  using _logtoolsdetail::dumpValues; // dumpRange values
380 
381  template<class TKey, class Tp>
382  std::ostream & operator<<( std::ostream & str, const std::map<TKey, Tp> & obj )
383  { return str << dumpMap( obj ); }
384 
385  template<class TKey, class Tp>
386  std::ostream & operator<<( std::ostream & str, const std::unordered_map<TKey, Tp> & obj )
387  { return str << dumpMap( obj ); }
388 
389  template<class TKey, class Tp>
390  std::ostream & operator<<( std::ostream & str, const std::multimap<TKey, Tp> & obj )
391  { return str << dumpMap( obj ); }
392 
402  inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj )
403  {
404  std::string ret( "[" );
405  ret += ( obj.good() ? 'g' : '_' );
406  ret += ( obj.eof() ? 'e' : '_' );
407  ret += ( obj.fail() ? 'F' : '_' );
408  ret += ( obj.bad() ? 'B' : '_' );
409  ret += "]";
410  return str << ret;
411  }
412 
414  // iomanipulator: str << dump(val) << ...
415  // calls: std::ostream & dumpOn( std::ostream & str, const Type & obj )
417 
418  namespace detail
419  {
420  template<class Tp>
421  struct Dump
422  {
423  Dump( const Tp & obj_r ) : _obj( obj_r ) {}
424  const Tp & _obj;
425  };
426 
427  template<class Tp>
428  std::ostream & operator<<( std::ostream & str, const Dump<Tp> & obj )
429  { return dumpOn( str, obj._obj ); }
430  }
431 
432  template<class Tp>
433  detail::Dump<Tp> dump( const Tp & obj_r )
434  { return detail::Dump<Tp>(obj_r); }
435 
436 
438 } // namespace zypp
440 #endif // ZYPP_BASE_LOGTOOLS_H
DumpKeys< TMap > dumpKeys(const TMap &map_r)
Definition: LogTools.h:323
MapKVIteratorTraits< TMap >::Value_const_iterator MapValue_const_iterator
Definition: LogTools.h:341
std::ostream & dumpOn(std::ostream &str, const PoolQueryIterator &obj)
Definition: PoolQuery.cc:1839
const TMap & map() const
Definition: LogTools.h:348
const Tp & _obj
Definition: LogTools.h:424
Dump(const Tp &obj_r)
Definition: LogTools.h:423
MapKey_const_iterator end() const
Definition: LogTools.h:309
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator end, const std::string &intro="{", const std::string &pfx="\ ", const std::string &sep="\ ", const std::string &sfx="\, const std::string &extro="}")
Print range defined by iterators (multiline style).
Definition: LogTools.h:91
MapValue_const_iterator end() const
Definition: LogTools.h:354
TMap::value_type PairType
Definition: LogTools.h:241
String related utilities and Regular expression matching.
transform_iterator< GetPairSecond< typename MapType::value_type >, typename MapType::const_iterator > Value_const_iterator
The value iterator type.
Definition: Iterator.h:223
MapKVIteratorTraits< TMap >::Value_const_iterator make_map_value_begin(const TMap &map_r)
Convenience to create the value iterator from container::begin()
Definition: Iterator.h:238
std::map wrapper for stream output.
Definition: LogTools.h:237
MapKVIteratorTraits< TMap >::Key_const_iterator make_map_key_begin(const TMap &map_r)
Convenience to create the key iterator from container::begin()
Definition: Iterator.h:228
MapEntry(const TPair &pair_r)
Definition: LogTools.h:205
MapEntry< TPair > mapEntry(const TPair &pair_r)
Definition: LogTools.h:225
std::pair wrapper for std::map output.
Definition: LogTools.h:202
DumpValues(const TMap &map_r)
Definition: LogTools.h:344
const TMap *const _map
Definition: LogTools.h:268
iomanip::RangeLine< TIterator > rangeLine(TIterator begin, TIterator end)
Iomanip printing dumpRangeLine style.
Definition: LogTools.h:157
Provides API related macros.
const TPair *const _pair
Definition: LogTools.h:213
MapKVIteratorTraits< TMap >::Key_const_iterator make_map_key_end(const TMap &map_r)
Convenience to create the key iterator from container::end()
Definition: Iterator.h:233
std::map wrapper for stream output of keys.
Definition: LogTools.h:293
MapValue_const_iterator begin() const
Definition: LogTools.h:351
DumpMap< TMap > dumpMap(const TMap &map_r)
Definition: LogTools.h:278
MapEntryType operator()(const PairType &pair_r) const
Definition: LogTools.h:246
MapKey_const_iterator begin() const
Definition: LogTools.h:306
MapEntry_const_iterator end() const
Definition: LogTools.h:264
MapKVIteratorTraits< TMap >::Value_const_iterator make_map_value_end(const TMap &map_r)
Convenience to create the value iterator from container::end()
Definition: Iterator.h:243
transform_iterator< Transformer, typename MapType::const_iterator > MapEntry_const_iterator
Definition: LogTools.h:251
MapKVIteratorTraits< TMap >::Key_const_iterator MapKey_const_iterator
Definition: LogTools.h:296
transform_iterator< GetPairFirst< typename MapType::value_type >, typename MapType::const_iterator > Key_const_iterator
The key iterator type.
Definition: Iterator.h:218
DumpValues< TMap > dumpValues(const TMap &map_r)
Definition: LogTools.h:368
DumpKeys(const TMap &map_r)
Definition: LogTools.h:299
const TMap & map() const
Definition: LogTools.h:258
const TPair & pair() const
Definition: LogTools.h:209
const TMap & map() const
Definition: LogTools.h:303
const TMap *const _map
Definition: LogTools.h:313
std::ostream & dumpRangeLine(std::ostream &str, TIterator begin, TIterator end)
Print range defined by iterators (single line style).
Definition: LogTools.h:114
RangeLine(TIterator begin, TIterator end)
Definition: LogTools.h:133
std::map wrapper for stream output of values.
Definition: LogTools.h:338
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
DumpMap(const TMap &map_r)
Definition: LogTools.h:254
detail::Dump< Tp > dump(const Tp &obj_r)
Definition: LogTools.h:433
MapEntry_const_iterator begin() const
Definition: LogTools.h:261
MapEntry< PairType > MapEntryType
Definition: LogTools.h:242