libzypp 17.31.31
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-core/base/Hash.h>
23#include <zypp-core/base/Logger.h>
24#include <zypp-core/base/String.h>
25#include <zypp-core/base/Iterator.h>
26#include <zypp-core/Globals.h>
27
29namespace zypp
30{
31
32 using std::endl;
33
44 struct MLSep
45 { bool _first = true; };
46 inline std::ostream & operator<<( std::ostream & str, MLSep & obj )
47 { if ( obj._first ) obj._first = false; else str << endl; return str; }
48
106 template<class TIterator>
107 std::ostream & dumpRange( std::ostream & str,
108 TIterator begin, TIterator end,
109 const std::string & intro = "{",
110 const std::string & pfx = "\n ",
111 const std::string & sep = "\n ",
112 const std::string & sfx = "\n",
113 const std::string & extro = "}" )
114 {
115 str << intro;
116 if ( begin != end )
117 {
118 str << pfx << *begin;
119 for ( ++begin; begin != end; ++begin )
120 str << sep << *begin;
121 str << sfx;
122 }
123 return str << extro;
124 }
125
129 template<class TIterator>
130 std::ostream & dumpRangeLine( std::ostream & str,
131 TIterator begin, TIterator end )
132 { return dumpRange( str, begin, end, "(", "", ", ", "", ")" ); }
134 template<class TContainer>
135 std::ostream & dumpRangeLine( std::ostream & str, const TContainer & cont )
136 { return dumpRangeLine( str, cont.begin(), cont.end() ); }
137
138
140 namespace iomanip
141 {
146 template<class TIterator>
147 struct RangeLine
148 {
149 RangeLine( TIterator begin, TIterator end )
150 : _begin( begin )
151 , _end( end )
152 {}
153 TIterator _begin;
154 TIterator _end;
155 };
156
158 template<class TIterator>
159 std::ostream & operator<<( std::ostream & str, const RangeLine<TIterator> & obj )
160 { return dumpRangeLine( str, obj._begin, obj._end ); }
161
162 } // namespce iomanip
164
172 template<class TIterator>
173 iomanip::RangeLine<TIterator> rangeLine( TIterator begin, TIterator end )
174 { return iomanip::RangeLine<TIterator>( begin, end ); }
176 template<class TContainer>
177 auto rangeLine( const TContainer & cont ) -> decltype( rangeLine( cont.begin(), cont.end() ) )
178 { return rangeLine( cont.begin(), cont.end() ); }
179
180 template<class Tp>
181 std::ostream & operator<<( std::ostream & str, const std::vector<Tp> & obj )
182 { return dumpRange( str, obj.begin(), obj.end() ); }
183
184 template<class Tp, class TCmp, class TAlloc>
185 std::ostream & operator<<( std::ostream & str, const std::set<Tp,TCmp,TAlloc> & obj )
186 { return dumpRange( str, obj.begin(), obj.end() ); }
187
188 template<class Tp>
189 std::ostream & operator<<( std::ostream & str, const std::unordered_set<Tp> & obj )
190 { return dumpRange( str, obj.begin(), obj.end() ); }
191
192 template<class Tp>
193 std::ostream & operator<<( std::ostream & str, const std::multiset<Tp> & obj )
194 { return dumpRange( str, obj.begin(), obj.end() ); }
195
196 template<class Tp>
197 std::ostream & operator<<( std::ostream & str, const std::list<Tp> & obj )
198 { return dumpRange( str, obj.begin(), obj.end() ); }
199
200 template<class Tp>
201 std::ostream & operator<<( std::ostream & str, const Iterable<Tp> & obj )
202 { return dumpRange( str, obj.begin(), obj.end() ); }
203
205 namespace _logtoolsdetail
206 {
207
209 // mapEntry
211
217 template<class TPair>
218 class MapEntry
219 {
220 public:
221 MapEntry( const TPair & pair_r )
222 : _pair( &pair_r )
223 {}
224
225 const TPair & pair() const
226 { return *_pair; }
227
228 private:
229 const TPair *const _pair;
230 };
231
233 template<class TPair>
234 std::ostream & operator<<( std::ostream & str, const MapEntry<TPair> & obj )
235 {
236 return str << '[' << obj.pair().first << "] = " << obj.pair().second;
237 }
238
240 template<class TPair>
241 MapEntry<TPair> mapEntry( const TPair & pair_r )
242 { return MapEntry<TPair>( pair_r ); }
243
245 // dumpMap
247
252 template<class TMap>
253 class DumpMap
254 {
255 public:
256 typedef TMap MapType;
257 typedef typename TMap::value_type PairType;
258 typedef MapEntry<PairType> MapEntryType;
259
260 struct Transformer
261 {
262 MapEntryType operator()( const PairType & pair_r ) const
263 { return mapEntry( pair_r ); }
264 };
265
268
269 public:
270 DumpMap( const TMap & map_r )
271 : _map( &map_r )
272 {}
273
274 const TMap & map() const
275 { return *_map; }
276
278 { return make_transform_iterator( map().begin(), Transformer() ); }
279
281 { return make_transform_iterator( map().end(), Transformer() );}
282
283 private:
284 const TMap *const _map;
285 };
286
288 template<class TMap>
289 std::ostream & operator<<( std::ostream & str, const DumpMap<TMap> & obj )
290 { return dumpRange( str, obj.begin(), obj.end() ); }
291
293 template<class TMap>
294 DumpMap<TMap> dumpMap( const TMap & map_r )
295 { return DumpMap<TMap>( map_r ); }
296
298 // dumpKeys
300
308 template<class TMap>
309 class DumpKeys
310 {
311 public:
313
314 public:
315 DumpKeys( const TMap & map_r )
316 : _map( &map_r )
317 {}
318
319 const TMap & map() const
320 { return *_map; }
321
323 { return make_map_key_begin( map() ); }
324
326 { return make_map_key_end( map() ); }
327
328 private:
329 const TMap *const _map;
330 };
331
333 template<class TMap>
334 std::ostream & operator<<( std::ostream & str, const DumpKeys<TMap> & obj )
335 { return dumpRange( str, obj.begin(), obj.end() ); }
336
338 template<class TMap>
339 DumpKeys<TMap> dumpKeys( const TMap & map_r )
340 { return DumpKeys<TMap>( map_r ); }
341
343 // dumpValues
345
353 template<class TMap>
354 class DumpValues
355 {
356 public:
358
359 public:
360 DumpValues( const TMap & map_r )
361 : _map( &map_r )
362 {}
363
364 const TMap & map() const
365 { return *_map; }
366
368 { return make_map_value_begin( map() ); }
369
371 { return make_map_value_end( map() ); }
372
373 private:
374 const TMap *const _map;
375 };
376
378 template<class TMap>
379 std::ostream & operator<<( std::ostream & str, const DumpValues<TMap> & obj )
380 { return dumpRange( str, obj.begin(), obj.end() ); }
381
383 template<class TMap>
384 DumpValues<TMap> dumpValues( const TMap & map_r )
385 { return DumpValues<TMap>( map_r ); }
386
388 } // namespace _logtoolsdetail
390
391 // iomanipulator
392 using _logtoolsdetail::mapEntry; // std::pair as '[key] = value'
393 using _logtoolsdetail::dumpMap; // dumpRange '[key] = value'
394 using _logtoolsdetail::dumpKeys; // dumpRange keys
395 using _logtoolsdetail::dumpValues; // dumpRange values
396
397 template<class TKey, class Tp>
398 std::ostream & operator<<( std::ostream & str, const std::map<TKey, Tp> & obj )
399 { return str << dumpMap( obj ); }
400
401 template<class TKey, class Tp>
402 std::ostream & operator<<( std::ostream & str, const std::unordered_map<TKey, Tp> & obj )
403 { return str << dumpMap( obj ); }
404
405 template<class TKey, class Tp>
406 std::ostream & operator<<( std::ostream & str, const std::multimap<TKey, Tp> & obj )
407 { return str << dumpMap( obj ); }
408
418 inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj )
419 {
420 std::string ret( "[" );
421 ret += ( obj.good() ? 'g' : '_' );
422 ret += ( obj.eof() ? 'e' : '_' );
423 ret += ( obj.fail() ? 'F' : '_' );
424 ret += ( obj.bad() ? 'B' : '_' );
425 ret += "]";
426 return str << ret;
427 }
428
430 // iomanipulator: str << dump(val) << ...
431 // calls: std::ostream & dumpOn( std::ostream & str, const Type & obj )
433
434 namespace detail
435 {
436 template<class Tp>
437 struct Dump
438 {
439 Dump( const Tp & obj_r ) : _obj( obj_r ) {}
440 const Tp & _obj;
441 };
442
443 template<class Tp>
444 std::ostream & operator<<( std::ostream & str, const Dump<Tp> & obj )
445 { return dumpOn( str, obj._obj ); }
446 }
447
448 template<class Tp>
449 detail::Dump<Tp> dump( const Tp & obj_r )
450 { return detail::Dump<Tp>(obj_r); }
451
460 inline std::ostream & hexdumpOn( std::ostream & outs, const unsigned char *ptr, size_t size )
461 {
462 size_t i,c;
463 unsigned width = 0x10;
464 outs << str::form( "hexdump %10.10ld bytes (0x%8.8lx):\n", (long)size, (long)size );
465
466 for ( i = 0; i < size; i += width ) {
467 outs << str::form( "%4.4lx: ", (long)i );
468 /* show hex to the left */
469 for ( c = 0; c < width; ++c ) {
470 if ( i+c < size )
471 outs << str::form( "%02x ", ptr[i+c] );
472 else
473 outs << (" ");
474 }
475 /* show data on the right */
476 for ( c = 0; (c < width) && (i+c < size); ++c ) {
477 char x = (ptr[i+c] >= 0x20 && ptr[i+c] < 0x7f) ? ptr[i+c] : '.';
478 outs << x;
479 }
480 outs << std::endl;
481 }
482 return outs;
483 }
485 inline std::ostream & hexdumpOn( std::ostream & outs, const char *ptr, size_t size )
486 { return hexdumpOn( outs, (const unsigned char *)ptr, size ); }
488} // namespace zypp
490#endif // ZYPP_BASE_LOGTOOLS_H
An iterator over elements which are the result of applying some functional transformation to the elem...
const TMap & map() const
Definition LogTools.h:319
MapKey_const_iterator end() const
Definition LogTools.h:325
DumpKeys(const TMap &map_r)
Definition LogTools.h:315
MapKVIteratorTraits< TMap >::Key_const_iterator MapKey_const_iterator
Definition LogTools.h:312
MapKey_const_iterator begin() const
Definition LogTools.h:322
MapEntry< PairType > MapEntryType
Definition LogTools.h:258
MapEntry_const_iterator begin() const
Definition LogTools.h:277
TMap::value_type PairType
Definition LogTools.h:257
transform_iterator< Transformer, typename MapType::const_iterator > MapEntry_const_iterator
Definition LogTools.h:267
MapEntry_const_iterator end() const
Definition LogTools.h:280
const TMap & map() const
Definition LogTools.h:274
DumpMap(const TMap &map_r)
Definition LogTools.h:270
const TMap & map() const
Definition LogTools.h:364
DumpValues(const TMap &map_r)
Definition LogTools.h:360
MapValue_const_iterator end() const
Definition LogTools.h:370
MapValue_const_iterator begin() const
Definition LogTools.h:367
MapKVIteratorTraits< TMap >::Value_const_iterator MapValue_const_iterator
Definition LogTools.h:357
MapEntry(const TPair &pair_r)
Definition LogTools.h:221
const TPair & pair() const
Definition LogTools.h:225
const TPair *const _pair
Definition LogTools.h:229
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &os, const SolutionActionList &actionlist)
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:36
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & dumpRangeLine(std::ostream &str, TIterator begin, TIterator end)
Print range defined by iterators (single line style).
Definition LogTools.h:130
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
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator 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:107
iomanip::RangeLine< TIterator > rangeLine(TIterator begin, TIterator end)
Iomanip printing dumpRangeLine style.
Definition LogTools.h:173
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::ostream & dumpOn(std::ostream &str, const Capability &obj)
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
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
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
std::ostream & hexdumpOn(std::ostream &outs, const unsigned char *ptr, size_t size)
hexdump data on stream
Definition LogTools.h:460
detail::Dump< Tp > dump(const Tp &obj_r)
Definition LogTools.h:449
bool _first
Definition LogTools.h:45
transform_iterator< GetPairFirst< typename MapType::value_type >, typename MapType::const_iterator > Key_const_iterator
The key iterator type.
Definition Iterator.h:218
transform_iterator< GetPairSecond< typename MapType::value_type >, typename MapType::const_iterator > Value_const_iterator
The value iterator type.
Definition Iterator.h:223
MapEntryType operator()(const PairType &pair_r) const
Definition LogTools.h:262
const Tp & _obj
Definition LogTools.h:440
Dump(const Tp &obj_r)
Definition LogTools.h:439
RangeLine(TIterator begin, TIterator end)
Definition LogTools.h:149