libzypp: zypp-core/base/LogTools.h Source File

libzypp 17.36.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 <optional>
17#include <string>
18#include <vector>
19#include <list>
20#include <set>
21#include <map>
22
23#include <zypp-core/base/Hash.h>
24#include <zypp-core/base/Logger.h>
25#include <zypp-core/base/String.h>
26#include <zypp-core/base/Iterator.h>
27#include <zypp-core/Globals.h>
28
29#ifdef __GNUG__
30#include <cstdlib>
31#include <memory>
32#include <cxxabi.h>
33#endif
34
36namespace zypp
37{
38
39 using std::endl;
40
52 struct MLSep
53 {
54 MLSep() {}
55 MLSep( char sep_r ) : _sep { sep_r } {}
56 bool _first = true;
57 char _sep = '\n';
58 };
59 inline std::ostream & operator<<( std::ostream & str, MLSep & obj )
60 { if ( obj._first ) obj._first = false; else str << obj._sep; return str; }
61
119 template<class TIterator>
120 std::ostream & dumpRange( std::ostream & str,
121 TIterator begin, TIterator end,
122 const std::string & intro = "{",
123 const std::string & pfx = "\n ",
124 const std::string & sep = "\n ",
125 const std::string & sfx = "\n",
126 const std::string & extro = "}" )
127 {
128 str << intro;
129 if ( begin != end )
130 {
131 str << pfx << *begin;
132 for ( ++begin; begin != end; ++begin )
133 str << sep << *begin;
134 str << sfx;
135 }
136 return str << extro;
137 }
138
142 template<class TIterator>
143 std::ostream & dumpRangeLine( std::ostream & str,
144 TIterator begin, TIterator end )
145 { return dumpRange( str, begin, end, "(", "", ", ", "", ")" ); }
146
147 template<class TContainer>
148 std::ostream & dumpRangeLine( std::ostream & str, const TContainer & cont )
149 { return dumpRangeLine( str, cont.begin(), cont.end() ); }
150
151
153 namespace iomanip
154 {
159 template<class TIterator>
160 struct RangeLine
161 {
162 RangeLine( TIterator begin, TIterator end )
163 : _begin( begin )
164 , _end( end )
165 {}
166 TIterator _begin;
167 TIterator _end;
168 };
169
171 template<class TIterator>
172 std::ostream & operator<<( std::ostream & str, const RangeLine<TIterator> & obj )
173 { return dumpRangeLine( str, obj._begin, obj._end ); }
174
175 } // namespce iomanip
176
177
185 template<class TIterator>
186 iomanip::RangeLine<TIterator> rangeLine( TIterator begin, TIterator end )
187 { return iomanip::RangeLine<TIterator>( begin, end ); }
188
189 template<class TContainer>
190 auto rangeLine( const TContainer & cont ) -> decltype( rangeLine( cont.begin(), cont.end() ) )
191 { return rangeLine( cont.begin(), cont.end() ); }
192
193 template<class Tp>
194 std::ostream & operator<<( std::ostream & str, const std::vector<Tp> & obj )
195 { return dumpRange( str, obj.begin(), obj.end() ); }
196
197 template<class Tp, class TCmp, class TAlloc>
198 std::ostream & operator<<( std::ostream & str, const std::set<Tp,TCmp,TAlloc> & obj )
199 { return dumpRange( str, obj.begin(), obj.end() ); }
200
201 template<class Tp>
202 std::ostream & operator<<( std::ostream & str, const std::unordered_set<Tp> & obj )
203 { return dumpRange( str, obj.begin(), obj.end() ); }
204
205 template<class Tp>
206 std::ostream & operator<<( std::ostream & str, const std::multiset<Tp> & obj )
207 { return dumpRange( str, obj.begin(), obj.end() ); }
208
209 template<class Tp>
210 std::ostream & operator<<( std::ostream & str, const std::list<Tp> & obj )
211 { return dumpRange( str, obj.begin(), obj.end() ); }
212
213 template<class Tp>
214 std::ostream & operator<<( std::ostream & str, const Iterable<Tp> & obj )
215 { return dumpRange( str, obj.begin(), obj.end() ); }
216
218 namespace _logtoolsdetail
219 {
220
222 // mapEntry
224
230 template<class TPair>
231 class MapEntry
232 {
233 public:
234 MapEntry( const TPair & pair_r )
235 : _pair( &pair_r )
236 {}
237
238 const TPair & pair() const
239 { return *_pair; }
240
241 private:
242 const TPair *const _pair;
243 };
244
246 template<class TPair>
247 std::ostream & operator<<( std::ostream & str, const MapEntry<TPair> & obj )
248 {
249 return str << '[' << obj.pair().first << "] = " << obj.pair().second;
250 }
251
253 template<class TPair>
254 MapEntry<TPair> mapEntry( const TPair & pair_r )
255 { return MapEntry<TPair>( pair_r ); }
256
258 // dumpMap
260
265 template<class TMap>
266 class DumpMap
267 {
268 public:
269 using MapType = TMap;
270 using PairType = typename TMap::value_type;
271 using MapEntryType = MapEntry<PairType>;
272
273 struct Transformer
274 {
275 MapEntryType operator()( const PairType & pair_r ) const
276 { return mapEntry( pair_r ); }
277 };
278
279 using MapEntry_const_iterator = transform_iterator<Transformer, typename MapType::const_iterator>;
280
281 public:
282 DumpMap( const TMap & map_r )
283 : _map( &map_r )
284 {}
285
286 const TMap & map() const
287 { return *_map; }
288
289 MapEntry_const_iterator begin() const
290 { return make_transform_iterator( map().begin(), Transformer() ); }
291
292 MapEntry_const_iterator end() const
293 { return make_transform_iterator( map().end(), Transformer() );}
294
295 private:
296 const TMap *const _map;
297 };
298
300 template<class TMap>
301 std::ostream & operator<<( std::ostream & str, const DumpMap<TMap> & obj )
302 { return dumpRange( str, obj.begin(), obj.end() ); }
303
305 template<class TMap>
306 DumpMap<TMap> dumpMap( const TMap & map_r )
307 { return DumpMap<TMap>( map_r ); }
308
310 // dumpKeys
312
320 template<class TMap>
321 class DumpKeys
322 {
323 public:
324 using MapKey_const_iterator = typename MapKVIteratorTraits<TMap>::Key_const_iterator;
325
326 public:
327 DumpKeys( const TMap & map_r )
328 : _map( &map_r )
329 {}
330
331 const TMap & map() const
332 { return *_map; }
333
334 MapKey_const_iterator begin() const
335 { return make_map_key_begin( map() ); }
336
337 MapKey_const_iterator end() const
338 { return make_map_key_end( map() ); }
339
340 private:
341 const TMap *const _map;
342 };
343
345 template<class TMap>
346 std::ostream & operator<<( std::ostream & str, const DumpKeys<TMap> & obj )
347 { return dumpRange( str, obj.begin(), obj.end() ); }
348
350 template<class TMap>
351 DumpKeys<TMap> dumpKeys( const TMap & map_r )
352 { return DumpKeys<TMap>( map_r ); }
353
355 // dumpValues
357
365 template<class TMap>
366 class DumpValues
367 {
368 public:
369 using MapValue_const_iterator = typename MapKVIteratorTraits<TMap>::Value_const_iterator;
370
371 public:
372 DumpValues( const TMap & map_r )
373 : _map( &map_r )
374 {}
375
376 const TMap & map() const
377 { return *_map; }
378
379 MapValue_const_iterator begin() const
380 { return make_map_value_begin( map() ); }
381
382 MapValue_const_iterator end() const
383 { return make_map_value_end( map() ); }
384
385 private:
386 const TMap *const _map;
387 };
388
390 template<class TMap>
391 std::ostream & operator<<( std::ostream & str, const DumpValues<TMap> & obj )
392 { return dumpRange( str, obj.begin(), obj.end() ); }
393
395 template<class TMap>
396 DumpValues<TMap> dumpValues( const TMap & map_r )
397 { return DumpValues<TMap>( map_r ); }
398
400 } // namespace _logtoolsdetail
401
402
403 // iomanipulator
404 using _logtoolsdetail::mapEntry; // std::pair as '[key] = value'
405 using _logtoolsdetail::dumpMap; // dumpRange '[key] = value'
406 using _logtoolsdetail::dumpKeys; // dumpRange keys
407 using _logtoolsdetail::dumpValues; // dumpRange values
408
409 template<class TKey, class Tp>
410 std::ostream & operator<<( std::ostream & str, const std::map<TKey, Tp> & obj )
411 { return str << dumpMap( obj ); }
412
413 template<class TKey, class Tp>
414 std::ostream & operator<<( std::ostream & str, const std::unordered_map<TKey, Tp> & obj )
415 { return str << dumpMap( obj ); }
416
417 template<class TKey, class Tp>
418 std::ostream & operator<<( std::ostream & str, const std::multimap<TKey, Tp> & obj )
419 { return str << dumpMap( obj ); }
420
430 inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj )
431 {
432 std::string ret( "[" );
433 ret += ( obj.good() ? 'g' : '_' );
434 ret += ( obj.eof() ? 'e' : '_' );
435 ret += ( obj.fail() ? 'F' : '_' );
436 ret += ( obj.bad() ? 'B' : '_' );
437 ret += "]";
438 return str << ret;
439 }
440
442 // iomanipulator: str << dump(val) << ...
443 // calls: std::ostream & dumpOn( std::ostream & str, const Type & obj )
445
446 namespace detail
447 {
448 template<class Tp>
449 struct Dump
450 {
451 Dump( const Tp & obj_r ) : _obj( obj_r ) {}
452 const Tp & _obj;
453 };
454
455 template<class Tp>
456 std::ostream & operator<<( std::ostream & str, const Dump<Tp> & obj )
457 { return dumpOn( str, obj._obj ); }
458 }
459
460 template<class Tp>
461 detail::Dump<Tp> dump( const Tp & obj_r )
462 { return detail::Dump<Tp>(obj_r); }
463
472 inline std::ostream & hexdumpOn( std::ostream & outs, const unsigned char *ptr, size_t size )
473 {
474 size_t i = 0,c = 0;
475 unsigned width = 0x10;
476 outs << str::form( "hexdump %10.10ld bytes (0x%8.8lx):\n", (long)size, (long)size );
477
478 for ( i = 0; i < size; i += width ) {
479 outs << str::form( "%4.4lx: ", (long)i );
480 /* show hex to the left */
481 for ( c = 0; c < width; ++c ) {
482 if ( i+c < size )
483 outs << str::form( "%02x ", ptr[i+c] );
484 else
485 outs << (" ");
486 }
487 /* show data on the right */
488 for ( c = 0; (c < width) && (i+c < size); ++c ) {
489 char x = (ptr[i+c] >= 0x20 && ptr[i+c] < 0x7f) ? ptr[i+c] : '.';
490 outs << x;
491 }
492 outs << std::endl;
493 }
494 return outs;
495 }
496
497 inline std::ostream & hexdumpOn( std::ostream & outs, const char *ptr, size_t size )
498 { return hexdumpOn( outs, reinterpret_cast<const unsigned char*>(ptr), size ); }
499
504 inline std::ostream & operator<<( std::ostream & str, const std::type_info &info )
505 {
506#ifdef __GNUG__
507 int status = -4; // some arbitrary value to eliminate the compiler warning
508
509 // enable c++11 by passing the flag -std=c++11 to g++
510 std::unique_ptr<char, void(*)(void*)> res {
511 abi::__cxa_demangle(info.name(), NULL, NULL, &status),
512 std::free
513 };
514 return str << std::string((status==0) ? res.get() : info.name());
515#else
516 return str << info.name();
517#endif
518 }
519
520#ifdef __cpp_lib_optional // YAST/PK explicitly use c++11 until 15-SP3
521 template<class Tp>
522 inline std::ostream & operator<<( std::ostream & str, const std::optional<Tp> & obj )
523 {
524 if ( obj )
525 str << "opt(" << *obj << ")";
526 else
527 str << "nullopt";
528 return str;
529 }
530#endif
531
532
534} // namespace zypp
536#endif // ZYPP_BASE_LOGTOOLS_H
transform_iterator
An iterator over elements which are the result of applying some functional transformation to the elem...
zypp::Iterable
Definition Iterable.h:37
zypp::Iterable::begin
iterator_type begin() const
Definition Iterable.h:63
zypp::Iterable::end
iterator_type end() const
Definition Iterable.h:66
zypp::_logtoolsdetail::DumpKeys::map
const TMap & map() const
Definition LogTools.h:331
zypp::_logtoolsdetail::DumpKeys::end
MapKey_const_iterator end() const
Definition LogTools.h:337
zypp::_logtoolsdetail::DumpKeys::dumpKeys
DumpKeys< TMap > dumpKeys(const TMap &map_r)
Convenience function to create DumpKeys from std::map.
Definition LogTools.h:351
zypp::_logtoolsdetail::DumpKeys::MapKey_const_iterator
typename MapKVIteratorTraits< TMap >::Key_const_iterator MapKey_const_iterator
Definition LogTools.h:324
zypp::_logtoolsdetail::DumpKeys::DumpKeys
DumpKeys(const TMap &map_r)
Definition LogTools.h:327
zypp::_logtoolsdetail::DumpKeys::_map
const TMap *const _map
Definition LogTools.h:341
zypp::_logtoolsdetail::DumpKeys::begin
MapKey_const_iterator begin() const
Definition LogTools.h:334
zypp::_logtoolsdetail::DumpKeys::operator<<
std::ostream & operator<<(std::ostream &str, const DumpKeys< TMap > &obj)
Stream output.
Definition LogTools.h:346
zypp::_logtoolsdetail::DumpMap::MapEntry_const_iterator
transform_iterator< Transformer, typename MapType::const_iterator > MapEntry_const_iterator
Definition LogTools.h:279
zypp::_logtoolsdetail::DumpMap::operator<<
std::ostream & operator<<(std::ostream &str, const DumpMap< TMap > &obj)
Stream output.
Definition LogTools.h:301
zypp::_logtoolsdetail::DumpMap::begin
MapEntry_const_iterator begin() const
Definition LogTools.h:289
zypp::_logtoolsdetail::DumpMap::MapEntryType
MapEntry< PairType > MapEntryType
Definition LogTools.h:271
zypp::_logtoolsdetail::DumpMap::PairType
typename TMap::value_type PairType
Definition LogTools.h:270
zypp::_logtoolsdetail::DumpMap::end
MapEntry_const_iterator end() const
Definition LogTools.h:292
zypp::_logtoolsdetail::DumpMap::_map
const TMap *const _map
Definition LogTools.h:296
zypp::_logtoolsdetail::DumpMap::dumpMap
DumpMap< TMap > dumpMap(const TMap &map_r)
Convenience function to create DumpMap from std::map.
Definition LogTools.h:306
zypp::_logtoolsdetail::DumpMap::map
const TMap & map() const
Definition LogTools.h:286
zypp::_logtoolsdetail::DumpMap::DumpMap
DumpMap(const TMap &map_r)
Definition LogTools.h:282
zypp::_logtoolsdetail::DumpMap::MapType
TMap MapType
Definition LogTools.h:269
zypp::_logtoolsdetail::DumpValues::operator<<
std::ostream & operator<<(std::ostream &str, const DumpValues< TMap > &obj)
Stream output.
Definition LogTools.h:391
zypp::_logtoolsdetail::DumpValues::map
const TMap & map() const
Definition LogTools.h:376
zypp::_logtoolsdetail::DumpValues::dumpValues
DumpValues< TMap > dumpValues(const TMap &map_r)
Convenience function to create DumpValues from std::map.
Definition LogTools.h:396
zypp::_logtoolsdetail::DumpValues::_map
const TMap *const _map
Definition LogTools.h:386
zypp::_logtoolsdetail::DumpValues::DumpValues
DumpValues(const TMap &map_r)
Definition LogTools.h:372
zypp::_logtoolsdetail::DumpValues::MapValue_const_iterator
typename MapKVIteratorTraits< TMap >::Value_const_iterator MapValue_const_iterator
Definition LogTools.h:369
zypp::_logtoolsdetail::DumpValues::end
MapValue_const_iterator end() const
Definition LogTools.h:382
zypp::_logtoolsdetail::DumpValues::begin
MapValue_const_iterator begin() const
Definition LogTools.h:379
zypp::_logtoolsdetail::MapEntry
std::pair wrapper for std::map output.
Definition LogTools.h:232
zypp::_logtoolsdetail::MapEntry::MapEntry
MapEntry(const TPair &pair_r)
Definition LogTools.h:234
zypp::_logtoolsdetail::MapEntry::pair
const TPair & pair() const
Definition LogTools.h:238
zypp::_logtoolsdetail::MapEntry::operator<<
std::ostream & operator<<(std::ostream &str, const MapEntry< TPair > &obj)
Stream output.
Definition LogTools.h:247
zypp::_logtoolsdetail::MapEntry::mapEntry
MapEntry< TPair > mapEntry(const TPair &pair_r)
Convenience function to create MapEntry from std::pair.
Definition LogTools.h:254
zypp::_logtoolsdetail::MapEntry< PairType >::_pair
const PairType *const _pair
Definition LogTools.h:242
str
String related utilities and Regular expression matching.
zypp::_logtoolsdetail
Definition LogTools.h:219
zypp::iomanip
Definition LogTools.h:154
zypp::solver::detail::operator<<
std::ostream & operator<<(std::ostream &str, const SolutionActionList &actionlist)
Definition SolutionAction.cc:58
zypp::str::form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:37
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::dumpRangeLine
std::ostream & dumpRangeLine(std::ostream &str, TIterator begin, TIterator end)
Print range defined by iterators (single line style).
Definition LogTools.h:143
zypp::make_map_key_begin
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:226
zypp::dumpRange
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:120
zypp::rangeLine
iomanip::RangeLine< TIterator > rangeLine(TIterator begin, TIterator end)
Iomanip printing dumpRangeLine style.
Definition LogTools.h:186
zypp::make_map_value_begin
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:236
zypp::dumpOn
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
Definition Capability.cc:580
zypp::make_map_key_end
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:231
zypp::make_map_value_end
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:241
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition SerialNumber.cc:52
zypp::hexdumpOn
std::ostream & hexdumpOn(std::ostream &outs, const unsigned char *ptr, size_t size)
hexdump data on stream
Definition LogTools.h:472
zypp::dump
detail::Dump< Tp > dump(const Tp &obj_r)
Definition LogTools.h:461
zypp::dumpMap
DumpMap< TMap > dumpMap(const TMap &map_r)
Convenience function to create DumpMap from std::map.
Definition LogTools.h:306
zypp::MLSep
Helper to produce not-NL-terminated multi line output.
Definition LogTools.h:53
zypp::MLSep::MLSep
MLSep(char sep_r)
Definition LogTools.h:55
zypp::MLSep::_first
bool _first
Definition LogTools.h:56
zypp::MLSep::_sep
char _sep
Definition LogTools.h:57
zypp::MLSep::MLSep
MLSep()
Definition LogTools.h:54
zypp::MapKVIteratorTraits::Key_const_iterator
transform_iterator< GetPairFirst< typename MapType::value_type >, typename MapType::const_iterator > Key_const_iterator
The key iterator type.
Definition Iterator.h:217
zypp::MapKVIteratorTraits::Value_const_iterator
transform_iterator< GetPairSecond< typename MapType::value_type >, typename MapType::const_iterator > Value_const_iterator
The value iterator type.
Definition Iterator.h:221
zypp::_logtoolsdetail::DumpMap::Transformer
Definition LogTools.h:274
zypp::_logtoolsdetail::DumpMap::Transformer::operator()
MapEntryType operator()(const PairType &pair_r) const
Definition LogTools.h:275
zypp::detail::Dump
Definition LogTools.h:450
zypp::detail::Dump::_obj
const Tp & _obj
Definition LogTools.h:452
zypp::detail::Dump::Dump
Dump(const Tp &obj_r)
Definition LogTools.h:451
zypp::iomanip::RangeLine
Definition LogTools.h:161
zypp::iomanip::RangeLine::operator<<
std::ostream & operator<<(std::ostream &str, const RangeLine< TIterator > &obj)
<TIterator>
Definition LogTools.h:172
zypp::iomanip::RangeLine::_end
TIterator _end
Definition LogTools.h:167
zypp::iomanip::RangeLine::RangeLine
RangeLine(TIterator begin, TIterator end)
Definition LogTools.h:162
zypp::iomanip::RangeLine::_begin
TIterator _begin
Definition LogTools.h:166
Globals.h
Provides API related macros.
Hash.h
Iterator.h
Logger.h
String.h