libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YUIException.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or 
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:         YUIException.cc
00020 
00021                 Stolen from zypp/libzypp/base/Exception.cc
00022 
00023   Author:       Michael Andres    <ma@suse.de>
00024   Maintainer:   Stefan Hundhammer <sh@suse.de>
00025 
00026 /-*/
00027 
00028 #include <sstream>
00029 #include <string.h>     // strerror()
00030 #include <stdio.h>
00031 
00032 #define YUILogComponent "ui"
00033 #include "YUILog.h"
00034 
00035 #include "YUIException.h"
00036 #include "YWidget.h"
00037 
00038 
00039 
00040 
00041 std::string YCodeLocation::asString() const
00042 {
00043     std::string str( _file );
00044     str += "(" + _func + "):";
00045 
00046     char formatted_number[ 20 ];
00047     sprintf( formatted_number, "%u", _line );
00048 
00049     str += formatted_number;
00050 
00051     return str;
00052 }
00053 
00054 
00055 std::ostream &
00056 operator<<( std::ostream & str, const YCodeLocation & obj )
00057 {
00058     return str << obj.asString();
00059 }
00060 
00061 
00062 YUIException::YUIException()
00063 {
00064     // NOP
00065 }
00066 
00067 YUIException::YUIException( const std::string & msg_r )
00068     : _msg( msg_r )
00069 {
00070     // NOP
00071 }
00072 
00073 
00074 YUIException::~YUIException() throw()
00075 {
00076     // NOP
00077 }
00078 
00079 
00080 std::string
00081 YUIException::asString() const
00082 {
00083     std::ostringstream str;
00084     dumpOn( str );
00085     return str.str();
00086 }
00087 
00088 
00089 std::ostream &
00090 YUIException::dumpOn( std::ostream & str ) const
00091 {
00092     return str << _msg;
00093 }
00094 
00095 
00096 std::ostream &
00097 YUIException::dumpError( std::ostream & str ) const
00098 {
00099     return dumpOn( str << _where << ": " );
00100 }
00101 
00102 
00103 std::ostream &
00104 operator<<( std::ostream & str, const YUIException & obj )
00105 {
00106     return obj.dumpError( str );
00107 }
00108 
00109 
00110 std::string
00111 YUIException::strErrno( int errno_r )
00112 {
00113     return strerror( errno_r );
00114 }
00115 
00116 
00117 std::string
00118 YUIException::strErrno( int errno_r, const std::string & msg )
00119 {
00120     std::string ret( msg );
00121     ret += ": ";
00122     return ret += strErrno( errno_r );
00123 }
00124 
00125 
00126 void
00127 YUIException::log( const YUIException &         exception,
00128                    const YCodeLocation &        location,
00129                    const char * const           prefix )
00130 {
00131     YUILog::warning( YUILogComponent,
00132                      location.file().c_str(),
00133                      location.line(),
00134                      location.func().c_str() )
00135                          << "\t" << prefix << " " << exception.asString() << std::endl;
00136 }
00137 
00138 
00139 std::ostream &
00140 YUIUnknownPropertyException::dumpOn( std::ostream & str ) const
00141 {
00142     if ( widget() )
00143     {
00144         return str << widget()->widgetClass()
00145                    << " has no property named \""
00146                    << property().name()
00147                    << "\""
00148                    << std::endl;
00149     }
00150     else
00151     {
00152         return str << "Unknown property name \""
00153                    << property().name()
00154                    << "\""
00155                    << std::endl;
00156     }
00157 }
00158 
00159 
00160 std::ostream &
00161 YUIPropertyTypeMismatchException::dumpOn( std::ostream & str ) const
00162 {
00163     std::string widgetClass;
00164 
00165     if ( widget() )
00166         widgetClass = std::string( widget()->widgetClass() ) + "::";
00167 
00168     return str << "Property type mismatch: "
00169                << widgetClass
00170                << property().name()
00171                << " is type "
00172                << property().typeAsStr()
00173                << ", not "
00174                << YProperty::typeAsStr( type() )
00175                << std::endl;
00176 }
00177 
00178 
00179 std::ostream &
00180 YUISetReadOnlyPropertyException::dumpOn( std::ostream & str ) const
00181 {
00182     std::string widgetClass;
00183 
00184     if ( widget() )
00185         widgetClass = std::string( widget()->widgetClass() ) + "::";
00186 
00187     return str << "Property "
00188                << widgetClass
00189                << property().name()
00190                << "is read-only!"
00191                << std::endl;
00192 }
00193 
00194 
00195 std::ostream &
00196 YUIBadPropertyArgException::dumpOn( std::ostream & str ) const
00197 {
00198     std::string widgetClass;
00199 
00200     if ( widget() )
00201         widgetClass = std::string( widget()->widgetClass() ) + "::";
00202 
00203     return str << "Bad argument for property "
00204                << widgetClass
00205                << property().name()
00206                << ": "
00207                << msg()
00208                << std::endl;
00209 }
 All Classes Functions Variables Enumerations Friends