- merge 3728:3847 in trunk.
[platform/upstream/libzypp.git] / zypp / base / LogTools.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/LogTools.h
10  *
11 */
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 "zypp/base/Logger.h"
21
22 ///////////////////////////////////////////////////////////////////
23 namespace zypp
24 { /////////////////////////////////////////////////////////////////
25
26   /** Print range defined by iterators.
27    * \code
28    * intro [ pfx ITEM [ { sep ITEM }+ ] sfx ] extro
29    * \endcode
30    * The defaults print the range enclosed in \c {}, one item per
31    * line indented by 2 spaces.
32    * \code
33    * {
34    *   item1
35    *   item2
36    * }
37    * {} // on empty rande
38    * \endcode
39    * A comma separated list enclosed in \c () would be
40    * \code
41    * dumpRange( stream, begin, end, "(", "", ", ", "", ")" );
42    * \endcode
43   */
44   template<class _Iterator>
45     std::ostream & dumpRange( std::ostream & str,
46                               _Iterator begin, _Iterator end,
47                               const std::string & intro = "{",
48                               const std::string & pfx   = "\n  ",
49                               const std::string & sep   = "\n  ",
50                               const std::string & sfx   = "\n",
51                               const std::string & extro = "}" )
52     {
53       str << intro;
54       if ( begin != end )
55         {
56           str << pfx << *begin;
57           for (  ++begin; begin != end; ++begin )
58             str << sep << *begin;
59           str << sfx;
60         }
61       return str << extro;
62     }
63
64   template<class _Iterator>
65     std::ostream & dumpRangeLine( std::ostream & str,
66                                   _Iterator begin, _Iterator end )
67     { return dumpRange( str, begin, end, "(", "", ", ", "", ")" ); }
68
69   template<class _Tp>
70     std::ostream & operator<<( std::ostream & str, const std::vector<_Tp> & obj )
71     { return dumpRange( str, obj.begin(), obj.end() ); }
72
73   template<class _Tp>
74     std::ostream & operator<<( std::ostream & str, const std::set<_Tp> & obj )
75     { return dumpRange( str, obj.begin(), obj.end() ); }
76
77   template<class _Tp>
78     std::ostream & operator<<( std::ostream & str, const std::list<_Tp> & obj )
79     { return dumpRange( str, obj.begin(), obj.end() ); }
80
81   /** Print stream status bits.
82    * Prints the values of a streams \c good, \c eof, \c failed and \c bad bit.
83    *
84    * \code
85    *  [g___] - good
86    *  [_eF_] - eof and fail bit set
87    *  [__FB] - fail and bad bit set
88    * \endcode
89   */
90   inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj )
91   {
92     std::string ret( "[" );
93     ret += ( obj.good() ? 'g' : '_' );
94     ret += ( obj.eof()  ? 'e' : '_' );
95     ret += ( obj.fail() ? 'F' : '_' );
96     ret += ( obj.bad()  ? 'B' : '_' );
97     ret += "]";
98     return str << ret;
99   }
100
101   /////////////////////////////////////////////////////////////////
102 } // namespace zypp
103 ///////////////////////////////////////////////////////////////////
104 #endif // ZYPP_BASE_LOGTOOLS_H