libzypp  13.10.6
Exception.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include <sstream>
14 
15 #include "zypp/base/Logger.h"
16 #include "zypp/base/LogTools.h"
17 #include "zypp/base/Gettext.h"
18 #include "zypp/base/String.h"
19 #include "zypp/base/Exception.h"
20 
21 using std::endl;
22 
24 namespace zypp
25 {
26  namespace exception_detail
28  {
29 
30  std::string CodeLocation::asString() const
31  {
32  return str::form( "%s(%s):%u",
33  _file.c_str(),
34  _func.c_str(),
35  _line );
36  }
37 
38  std::ostream & operator<<( std::ostream & str, const CodeLocation & obj )
39  { return str << obj.asString(); }
40 
42  } // namespace exception_detail
44 
46  {}
47 
48  Exception::Exception( const std::string & msg_r )
49  : _msg( msg_r )
50  {}
51 
52  Exception::Exception( const std::string & msg_r, const Exception & history_r )
53  : _msg( msg_r )
54  { remember( history_r ); }
55 
57  {}
58 
59  std::string Exception::asString() const
60  {
61  std::ostringstream str;
62  dumpOn( str );
63  return str.str();
64  }
65 
66  std::string Exception::asUserString() const
67  {
68  std::ostringstream str;
69  dumpOn( str );
70  // call gettext to translate the message. This will
71  // not work if dumpOn() uses composed messages.
72  return _(str.str().c_str());
73  }
74 
75  std::string Exception::asUserHistory() const
76  {
77  if ( historyEmpty() )
78  return asUserString();
79 
80  std::string ret( asUserString() );
81  if ( ret.empty() )
82  return historyAsString();
83 
84  ret += '\n';
85  ret += historyAsString();
86  return ret;
87  }
88 
89  void Exception::remember( const Exception & old_r )
90  {
91  if ( &old_r != this ) // no self-remember
92  {
93  History newh( old_r._history.begin(), old_r._history.end() );
94  newh.push_front( old_r.asUserString() );
95  _history.swap( newh );
96  }
97  }
98 
99  void Exception::addHistory( const std::string & msg_r )
100  {
101  _history.push_front( msg_r );
102  }
103 
104  std::string Exception::historyAsString() const
105  {
106  // TranslatorExplanation followed by the list of error messages that lead to this exception
107  std::string history( _("History:") );
108  std::ostringstream ret;
109  dumpRange( ret, historyBegin(), historyEnd(),
110  "", history+"\n - ", "\n - ", "\n", "" );
111  return ret.str();
112  }
113 
114  std::ostream & Exception::dumpOn( std::ostream & str ) const
115  { return str << _msg; }
116 
117  std::ostream & Exception::dumpError( std::ostream & str ) const
118  { return dumpOn( str << _where << ": " ); }
119 
120  std::ostream & operator<<( std::ostream & str, const Exception & obj )
121  { return obj.dumpError( str ); }
122 
123 
124  std::string Exception::strErrno( int errno_r )
125  { return str::strerror( errno_r ); }
126 
127  std::string Exception::strErrno( int errno_r, const std::string & msg_r )
128  {
129  std::string ret( msg_r );
130  ret += ": ";
131  return ret += strErrno( errno_r );
132  }
133 
134  void Exception::log( const Exception & excpt_r, const CodeLocation & where_r,
135  const char *const prefix_r )
136  {
137  INT << where_r << " " << prefix_r << " " << excpt_r.asUserHistory() << endl;
138  }
139 
141 } // namespace zypp
Interface to gettext.
std::string historyAsString() const
The history as string.
Definition: Exception.cc:104
std::ostream & operator<<(std::ostream &str, const CodeLocation &obj)
Definition: Exception.cc:38
CodeLocation _where
Definition: Exception.h:262
bool historyEmpty() const
Whether the history list is empty.
Definition: Exception.h:220
#define INT
Definition: Logger.h:51
static std::string strErrno(int errno_r)
Make a string from errno_r.
Definition: Exception.cc:124
void addHistory(const std::string &msg_r)
Add some message text to the history.
Definition: Exception.cc:99
virtual std::ostream & dumpOn(std::ostream &str) const
Overload this to print a proper error message.
Definition: Exception.cc:114
std::string _msg
Definition: Exception.h:263
HistoryIterator historyBegin() const
Iterator pointing to the most recent message.
Definition: Exception.h:212
void remember(const Exception &old_r)
Store an other Exception as history.
Definition: Exception.cc:89
static void log(const Exception &excpt_r, const CodeLocation &where_r, const char *const prefix_r)
Drop a logline on throw, catch or rethrow.
Definition: Exception.cc:134
std::ostream & dumpRange(std::ostream &str, _Iterator begin, _Iterator 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:91
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:120
std::list< std::string > History
Definition: Exception.h:149
std::ostream & dumpError(std::ostream &str) const
Called by std::ostream &amp; operator&lt;&lt;.
Definition: Exception.cc:117
std::string asString() const
Error message provided by dumpOn as string.
Definition: Exception.cc:59
virtual ~Exception()
Dtor.
Definition: Exception.cc:56
#define _(MSG)
Return translated text.
Definition: Gettext.h:21
History _history
Definition: Exception.h:264
std::string asString() const
Location as string.
Definition: Exception.cc:30
Exception()
Default ctor.
Definition: Exception.cc:45
std::string form(const char *format,...)
Printf style construction of std::string.
Definition: String.cc:34
HistoryIterator historyEnd() const
Iterator pointing behind the last message.
Definition: Exception.h:216
Base class for Exception.
Definition: Exception.h:143
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition: Exception.cc:75
Keep FILE, FUNCTION and LINE.
Definition: Exception.h:32
std::string strerror(int errno_r)
Return string describing the error_r code.
Definition: String.cc:51
std::string asUserString() const
Translated error message as string suitable for the user.
Definition: Exception.cc:66