libzypp 17.31.23
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
91 template<class TIterator>
92 std::ostream & dumpRange( std::ostream & str,
93 TIterator begin, TIterator end,
94 const std::string & intro = "{",
95 const std::string & pfx = "\n ",
96 const std::string & sep = "\n ",
97 const std::string & sfx = "\n",
98 const std::string & extro = "}" )
99 {
100 str << intro;
101 if ( begin != end )
102 {
103 str << pfx << *begin;
104 for ( ++begin; begin != end; ++begin )
105 str << sep << *begin;
106 str << sfx;
107 }
108 return str << extro;
109 }
110
114 template<class TIterator>
115 std::ostream & dumpRangeLine( std::ostream & str,
116 TIterator begin, TIterator end )
117 { return dumpRange( str, begin, end, "(", "", ", ", "", ")" ); }
119 template<class TContainer>
120 std::ostream & dumpRangeLine( std::ostream & str, const TContainer & cont )
121 { return dumpRangeLine( str, cont.begin(), cont.end() ); }
122
123
125 namespace iomanip
126 {
131 template<class TIterator>
132 struct RangeLine
133 {
134 RangeLine( TIterator begin, TIterator end )
135 : _begin( begin )
136 , _end( end )
137 {}
138 TIterator _begin;
139 TIterator _end;
140 };
141
143 template<class TIterator>
144 std::ostream & operator<<( std::ostream & str, const RangeLine<TIterator> & obj )
145 { return dumpRangeLine( str, obj._begin, obj._end ); }
146
147 } // namespce iomanip
149
157 template<class TIterator>
158 iomanip::RangeLine<TIterator> rangeLine( TIterator begin, TIterator end )
159 { return iomanip::RangeLine<TIterator>( begin, end ); }
161 template<class TContainer>
162 auto rangeLine( const TContainer & cont ) -> decltype( rangeLine( cont.begin(), cont.end() ) )
163 { return rangeLine( cont.begin(), cont.end() ); }
164
165 template<class Tp>
166 std::ostream & operator<<( std::ostream & str, const std::vector<Tp> & obj )
167 { return dumpRange( str, obj.begin(), obj.end() ); }
168
169 template<class Tp, class TCmp, class TAlloc>
170 std::ostream & operator<<( std::ostream & str, const std::set<Tp,TCmp,TAlloc> & obj )
171 { return dumpRange( str, obj.begin(), obj.end() ); }
172
173 template<class Tp>
174 std::ostream & operator<<( std::ostream & str, const std::unordered_set<Tp> & obj )
175 { return dumpRange( str, obj.begin(), obj.end() ); }
176
177 template<class Tp>
178 std::ostream & operator<<( std::ostream & str, const std::multiset<Tp> & obj )
179 { return dumpRange( str, obj.begin(), obj.end() ); }
180
181 template<class Tp>
182 std::ostream & operator<<( std::ostream & str, const std::list<Tp> & obj )
183 { return dumpRange( str, obj.begin(), obj.end() ); }
184
185 template<class Tp>
186 std::ostream & operator<<( std::ostream & str, const Iterable<Tp> & obj )
187 { return dumpRange( str, obj.begin(), obj.end() ); }
188
190 namespace _logtoolsdetail
191 {
192
194 // mapEntry
196
202 template<class TPair>
203 class MapEntry
204 {
205 public:
206 MapEntry( const TPair & pair_r )
207 : _pair( &pair_r )
208 {}
209
210 const TPair & pair() const
211 { return *_pair; }
212
213 private:
214 const TPair *const _pair;
215 };
216
218 template<class TPair>
219 std::ostream & operator<<( std::ostream & str, const MapEntry<TPair> & obj )
220 {
221 return str << '[' << obj.pair().first << "] = " << obj.pair().second;
222 }
223
225 template<class TPair>
226 MapEntry<TPair> mapEntry( const TPair & pair_r )
227 { return MapEntry<TPair>( pair_r ); }
228
230 // dumpMap
232
237 template<class TMap>
238 class DumpMap
239 {
240 public:
241 typedef TMap MapType;
242 typedef typename TMap::value_type PairType;
243 typedef MapEntry<PairType> MapEntryType;
244
245 struct Transformer
246 {
247 MapEntryType operator()( const PairType & pair_r ) const
248 { return mapEntry( pair_r ); }
249 };
250
253
254 public:
255 DumpMap( const TMap & map_r )
256 : _map( &map_r )
257 {}
258
259 const TMap & map() const
260 { return *_map; }
261
263 { return make_transform_iterator( map().begin(), Transformer() ); }
264
266 { return make_transform_iterator( map().end(), Transformer() );}
267
268 private:
269 const TMap *const _map;
270 };
271
273 template<class TMap>
274 std::ostream & operator<<( std::ostream & str, const DumpMap<TMap> & obj )
275 { return dumpRange( str, obj.begin(), obj.end() ); }
276
278 template<class TMap>
279 DumpMap<TMap> dumpMap( const TMap & map_r )
280 { return DumpMap<TMap>( map_r ); }
281
283 // dumpKeys
285
293 template<class TMap>
294 class DumpKeys
295 {
296 public:
298
299 public:
300 DumpKeys( const TMap & map_r )
301 : _map( &map_r )
302 {}
303
304 const TMap & map() const
305 { return *_map; }
306
308 { return make_map_key_begin( map() ); }
309
311 { return make_map_key_end( map() ); }
312
313 private:
314 const TMap *const _map;
315 };
316
318 template<class TMap>
319 std::ostream & operator<<( std::ostream & str, const DumpKeys<TMap> & obj )
320 { return dumpRange( str, obj.begin(), obj.end() ); }
321
323 template<class TMap>
324 DumpKeys<TMap> dumpKeys( const TMap & map_r )
325 { return DumpKeys<TMap>( map_r ); }
326
328 // dumpValues
330
338 template<class TMap>
339 class DumpValues
340 {
341 public:
343
344 public:
345 DumpValues( const TMap & map_r )
346 : _map( &map_r )
347 {}
348
349 const TMap & map() const
350 { return *_map; }
351
353 { return make_map_value_begin( map() ); }
354
356 { return make_map_value_end( map() ); }
357
358 private:
359 const TMap *const _map;
360 };
361
363 template<class TMap>
364 std::ostream & operator<<( std::ostream & str, const DumpValues<TMap> & obj )
365 { return dumpRange( str, obj.begin(), obj.end() ); }
366
368 template<class TMap>
369 DumpValues<TMap> dumpValues( const TMap & map_r )
370 { return DumpValues<TMap>( map_r ); }
371
373 } // namespace _logtoolsdetail
375
376 // iomanipulator
377 using _logtoolsdetail::mapEntry; // std::pair as '[key] = value'
378 using _logtoolsdetail::dumpMap; // dumpRange '[key] = value'
379 using _logtoolsdetail::dumpKeys; // dumpRange keys
380 using _logtoolsdetail::dumpValues; // dumpRange values
381
382 template<class TKey, class Tp>
383 std::ostream & operator<<( std::ostream & str, const std::map<TKey, Tp> & obj )
384 { return str << dumpMap( obj ); }
385
386 template<class TKey, class Tp>
387 std::ostream & operator<<( std::ostream & str, const std::unordered_map<TKey, Tp> & obj )
388 { return str << dumpMap( obj ); }
389
390 template<class TKey, class Tp>
391 std::ostream & operator<<( std::ostream & str, const std::multimap<TKey, Tp> & obj )
392 { return str << dumpMap( obj ); }
393
403 inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj )
404 {
405 std::string ret( "[" );
406 ret += ( obj.good() ? 'g' : '_' );
407 ret += ( obj.eof() ? 'e' : '_' );
408 ret += ( obj.fail() ? 'F' : '_' );
409 ret += ( obj.bad() ? 'B' : '_' );
410 ret += "]";
411 return str << ret;
412 }
413
415 // iomanipulator: str << dump(val) << ...
416 // calls: std::ostream & dumpOn( std::ostream & str, const Type & obj )
418
419 namespace detail
420 {
421 template<class Tp>
422 struct Dump
423 {
424 Dump( const Tp & obj_r ) : _obj( obj_r ) {}
425 const Tp & _obj;
426 };
427
428 template<class Tp>
429 std::ostream & operator<<( std::ostream & str, const Dump<Tp> & obj )
430 { return dumpOn( str, obj._obj ); }
431 }
432
433 template<class Tp>
434 detail::Dump<Tp> dump( const Tp & obj_r )
435 { return detail::Dump<Tp>(obj_r); }
436
445 inline std::ostream & hexdumpOn( std::ostream & outs, const unsigned char *ptr, size_t size )
446 {
447 size_t i,c;
448 unsigned width = 0x10;
449 outs << str::form( "hexdump %10.10ld bytes (0x%8.8lx):\n", (long)size, (long)size );
450
451 for ( i = 0; i < size; i += width ) {
452 outs << str::form( "%4.4lx: ", (long)i );
453 /* show hex to the left */
454 for ( c = 0; c < width; ++c ) {
455 if ( i+c < size )
456 outs << str::form( "%02x ", ptr[i+c] );
457 else
458 outs << (" ");
459 }
460 /* show data on the right */
461 for ( c = 0; (c < width) && (i+c < size); ++c ) {
462 char x = (ptr[i+c] >= 0x20 && ptr[i+c] < 0x7f) ? ptr[i+c] : '.';
463 outs << x;
464 }
465 outs << std::endl;
466 }
467 return outs;
468 }
470 inline std::ostream & hexdumpOn( std::ostream & outs, const char *ptr, size_t size )
471 { return hexdumpOn( outs, (const unsigned char *)ptr, size ); }
473} // namespace zypp
475#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:304
MapKey_const_iterator end() const
Definition: LogTools.h:310
DumpKeys(const TMap &map_r)
Definition: LogTools.h:300
MapKVIteratorTraits< TMap >::Key_const_iterator MapKey_const_iterator
Definition: LogTools.h:297
const TMap *const _map
Definition: LogTools.h:314
MapKey_const_iterator begin() const
Definition: LogTools.h:307
MapEntry< PairType > MapEntryType
Definition: LogTools.h:243
MapEntry_const_iterator begin() const
Definition: LogTools.h:262
TMap::value_type PairType
Definition: LogTools.h:242
transform_iterator< Transformer, typename MapType::const_iterator > MapEntry_const_iterator
Definition: LogTools.h:252
MapEntry_const_iterator end() const
Definition: LogTools.h:265
const TMap *const _map
Definition: LogTools.h:269
const TMap & map() const
Definition: LogTools.h:259
DumpMap(const TMap &map_r)
Definition: LogTools.h:255
const TMap & map() const
Definition: LogTools.h:349
DumpValues(const TMap &map_r)
Definition: LogTools.h:345
MapValue_const_iterator end() const
Definition: LogTools.h:355
MapValue_const_iterator begin() const
Definition: LogTools.h:352
MapKVIteratorTraits< TMap >::Value_const_iterator MapValue_const_iterator
Definition: LogTools.h:342
MapEntry(const TPair &pair_r)
Definition: LogTools.h:206
const TPair & pair() const
Definition: LogTools.h:210
const TPair *const _pair
Definition: LogTools.h:214
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.
Definition: CodePitfalls.doc:2
std::ostream & dumpRangeLine(std::ostream &str, TIterator begin, TIterator end)
Print range defined by iterators (single line style).
Definition: LogTools.h:115
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:92
iomanip::RangeLine< TIterator > rangeLine(TIterator begin, TIterator end)
Iomanip printing dumpRangeLine style.
Definition: LogTools.h:158
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)
Definition: Capability.cc:580
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)
Definition: SerialNumber.cc:52
std::ostream & hexdumpOn(std::ostream &outs, const unsigned char *ptr, size_t size)
hexdump data on stream
Definition: LogTools.h:445
detail::Dump< Tp > dump(const Tp &obj_r)
Definition: LogTools.h:434
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:247
const Tp & _obj
Definition: LogTools.h:425
Dump(const Tp &obj_r)
Definition: LogTools.h:424
RangeLine(TIterator begin, TIterator end)
Definition: LogTools.h:134