2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
23 //---------------------------------------------------------
24 // This include should be moved to commons but even as it is,
25 // it wont cause a linker circular dependency since it's just
27 #include "utils/StringUtils.h"
28 #include "utils/StdString.h"
29 //---------------------------------------------------------
33 // The 'this' pointer counts as a parameter on member methods.
34 #define XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT __attribute__((format(printf,2,3)))
36 #define XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT
39 #define XBMCCOMMONS_COPYVARARGS(fmt) va_list argList; va_start(argList, fmt); Set(fmt, argList); va_end(argList)
40 #define XBMCCOMMONS_STANDARD_EXCEPTION(E) \
41 class E : public XbmcCommons::Exception \
44 inline E(const char* message,...) XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT : Exception(#E) { XBMCCOMMONS_COPYVARARGS(message); } \
46 inline E(const E& other) : Exception(other) {} \
52 * This class a superclass for exceptions that want to utilize some
53 * utility functionality including autologging with the specific
60 std::string classname;
64 static ILogger* logger;
66 inline Exception(const char* classname_) : classname(classname_) { }
67 inline Exception(const char* classname_, const char* message_) : classname(classname_), message(message_) { }
68 inline Exception(const Exception& other) : classname(other.classname), message(other.message) { }
71 * This method is called from the constructor of subclasses. It
72 * will set the message from varargs as well as call log message
74 inline void Set(const char* fmt, va_list& argList)
76 message = StringUtils::FormatV(fmt, argList);
80 * This message can be called from the constructor of subclasses.
81 * It will set the message and log the throwing.
83 inline void SetMessage(const char* fmt, ...) XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT
86 XBMCCOMMONS_COPYVARARGS(fmt);
89 inline void setClassname(const char* cn) { classname = cn; }
94 inline virtual void LogThrowMessage(const char* prefix = NULL) const
97 logger->Log(LOGERROR,"EXCEPTION Thrown (%s) : %s", classname.c_str(), message.c_str());
100 inline virtual const char* GetMessage() const { return message.c_str(); }
102 inline static void SetLogger(ILogger* exceptionLogger) { logger = exceptionLogger; }
106 * This class forms the base class for unchecked exceptions. Unchecked exceptions
107 * are those that really shouldn't be handled explicitly. For example, on windows
108 * when a access violaton is converted to a win32_exception, there's nothing
109 * that can be done in most code. The outer most stack frame might try to
110 * do some error logging prior to shutting down, but that's really it.
112 XBMCCOMMONS_STANDARD_EXCEPTION(UncheckedException);
115 * In cases where you catch(...){} you will (may) inadvertently be
116 * catching UncheckedException's. Therefore this macro will allow
117 * you to do something equivalent to:
118 * catch (anything except UncheckedException) {}
120 * In order to avoid catching UncheckedException, use the macro as follows:
123 * XBMCCOMMONS_HANDLE_UNCHECKED
126 // Yes. I recognize that the name of this macro is an oxymoron.
127 #define XBMCCOMMONS_HANDLE_UNCHECKED \
128 catch (const XbmcCommons::UncheckedException& ) { throw; } \
129 catch (const XbmcCommons::UncheckedException* ) { throw; }