1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_COMPHELPER_DIAGNOSE_EX_HXX
20 #define INCLUDED_COMPHELPER_DIAGNOSE_EX_HXX
22 #include <osl/diagnose.h>
23 #include <rtl/ustring.hxx>
25 #include <com/sun/star/uno/RuntimeException.hpp>
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
28 #include <sal/log.hxx>
29 #include <comphelper/comphelperdllapi.h>
30 #include <cppuhelper/exc_hlp.hxx>
32 COMPHELPER_DLLPUBLIC
void DbgUnhandledException(const css::uno::Any
& caughtException
,
33 const char* currentFunction
, const char* fileAndLineNo
,
34 const char* area
, const char* explanatory
= nullptr);
36 //getCaughtException throws exceptions in never-going-to-happen situations which
37 //floods coverity with warnings
38 inline css::uno::Any
DbgGetCaughtException()
40 #if defined(__COVERITY__) && __COVERITY_MAJOR__ <= 2023
43 return ::cppu::getCaughtException();
50 return ::cppu::getCaughtException();
54 /** reports a caught UNO exception via OSL diagnostics
56 Note that whenever you use this, it might be an indicator that your error
57 handling is not correct...
58 This takes two optional parameters: area and explanatory
60 #if defined SAL_LOG_WARN
61 #define DBG_UNHANDLED_EXCEPTION_0_ARGS() \
62 DbgUnhandledException( DbgGetCaughtException(), __func__, SAL_DETAIL_WHERE );
63 #define DBG_UNHANDLED_EXCEPTION_1_ARGS(area) \
64 DbgUnhandledException( DbgGetCaughtException(), __func__, SAL_DETAIL_WHERE, area );
65 #define DBG_UNHANDLED_EXCEPTION_2_ARGS(area, explanatory) \
66 DbgUnhandledException( DbgGetCaughtException(), __func__, SAL_DETAIL_WHERE, area, explanatory );
68 #define DBG_UNHANDLED_FUNC_CHOOSER(_f1, _f2, _f3, ...) _f3
69 #define DBG_UNHANDLED_FUNC_RECOMPOSER(argsWithParentheses) DBG_UNHANDLED_FUNC_CHOOSER argsWithParentheses
70 #define DBG_UNHANDLED_CHOOSE_FROM_ARG_COUNT(...) DBG_UNHANDLED_FUNC_RECOMPOSER((__VA_ARGS__, DBG_UNHANDLED_EXCEPTION_2_ARGS, DBG_UNHANDLED_EXCEPTION_1_ARGS, DBG_UNHANDLED_EXCEPTION_0_ARGS, ))
71 #define DBG_UNHANDLED_NO_ARG_EXPANDER() ,,DBG_UNHANDLED_EXCEPTION_0_ARGS
72 #define DBG_UNHANDLED_MACRO_CHOOSER(...) DBG_UNHANDLED_CHOOSE_FROM_ARG_COUNT(DBG_UNHANDLED_NO_ARG_EXPANDER __VA_ARGS__ ())
73 #define DBG_UNHANDLED_EXCEPTION(...) DBG_UNHANDLED_MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
75 #define DBG_UNHANDLED_EXCEPTION(...)
79 /** This macro asserts the given condition (in debug mode), and throws
80 an IllegalArgumentException afterwards.
82 #define ENSURE_ARG_OR_THROW(c, m) if( !(c) ) { \
84 throw css::lang::IllegalArgumentException( \
86 + OUString::Concat(u",\n" m), \
87 css::uno::Reference< css::uno::XInterface >(), \
89 #define ENSURE_ARG_OR_THROW2(c, m, ifc, arg) if( !(c) ) { \
91 throw css::lang::IllegalArgumentException( \
93 + OUString::Concat(u",\n" m), \
97 /** This macro asserts the given condition (in debug mode), and throws
98 a RuntimeException afterwards.
100 #define ENSURE_OR_THROW(c, m) \
103 throw css::uno::RuntimeException( \
104 __func__ + OUString::Concat(u",\n" m), \
105 css::uno::Reference< css::uno::XInterface >() ); }
107 #define ENSURE_OR_THROW2(c, m, ifc) \
110 throw css::uno::RuntimeException( \
111 __func__ + OUString::Concat(u",\n" m), \
114 /** This macro asserts the given condition (in debug mode), and
115 returns the given value afterwards.
117 #define ENSURE_OR_RETURN(c, m, r) if( !(c) ) { \
121 /** This macro asserts the given condition (in debug mode), and
122 returns false afterwards.
124 #define ENSURE_OR_RETURN_FALSE(c, m) \
125 ENSURE_OR_RETURN(c, m, false)
127 /** This macro asserts the given condition (in debug mode), and
128 returns afterwards, without return value "void".
130 #define ENSURE_OR_RETURN_VOID( c, m ) \
133 OSL_ENSURE( c, m ); \
137 /** Convert a caught exception to a string suitable for logging.
139 COMPHELPER_DLLPUBLIC OString
exceptionToString(css::uno::Any
const & caughtEx
);
142 Logs an message along with a nicely formatted version of the current exception.
143 This must be called as the FIRST thing in a catch block.
145 #if defined SAL_LOG_WARN
146 #define TOOLS_WARN_EXCEPTION(area, stream) \
148 css::uno::Any tools_warn_exception( DbgGetCaughtException() ); \
149 SAL_WARN(area, stream << " " << exceptionToString(tools_warn_exception)); \
152 #define TOOLS_WARN_EXCEPTION(area, stream) \
154 SAL_WARN(area, stream); \
159 Logs an message along with a nicely formatted version of the current exception.
160 This must be called as the FIRST thing in a catch block.
162 #if defined SAL_LOG_WARN
163 #define TOOLS_WARN_EXCEPTION_IF(cond, area, stream) \
165 css::uno::Any tools_warn_exception( DbgGetCaughtException() ); \
166 SAL_WARN_IF(cond, area, stream << " " << exceptionToString(tools_warn_exception)); \
169 #define TOOLS_WARN_EXCEPTION_IF(cond, area, stream) \
171 SAL_WARN_IF(cond, area, stream); \
176 Logs an message along with a nicely formatted version of the current exception.
177 This must be called as the FIRST thing in a catch block.
179 #if defined SAL_LOG_INFO
180 #define TOOLS_INFO_EXCEPTION(area, stream) \
182 css::uno::Any tools_warn_exception( DbgGetCaughtException() ); \
183 SAL_INFO(area, stream << " " << exceptionToString(tools_warn_exception)); \
186 #define TOOLS_INFO_EXCEPTION(area, stream) \
188 SAL_INFO(area, stream); \
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */