tdf#162786, tdf#161947: Add support for setuptools and pip
[LibreOffice.git] / include / comphelper / diagnose_ex.hxx
blob658398db76e376ab27aeafd6b54095108c9f2712
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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
41 try
43 return ::cppu::getCaughtException();
45 catch (...)
47 std::abort();
49 #else
50 return ::cppu::getCaughtException();
51 #endif
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__)
74 #else // SAL_LOG_WARN
75 #define DBG_UNHANDLED_EXCEPTION(...)
76 #endif
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) ) { \
83 OSL_ENSURE(c, m); \
84 throw css::lang::IllegalArgumentException( \
85 __func__ \
86 + OUString::Concat(u",\n" m), \
87 css::uno::Reference< css::uno::XInterface >(), \
88 0 ); }
89 #define ENSURE_ARG_OR_THROW2(c, m, ifc, arg) if( !(c) ) { \
90 OSL_ENSURE(c, m); \
91 throw css::lang::IllegalArgumentException( \
92 __func__ \
93 + OUString::Concat(u",\n" m), \
94 ifc, \
95 arg ); }
97 /** This macro asserts the given condition (in debug mode), and throws
98 a RuntimeException afterwards.
100 #define ENSURE_OR_THROW(c, m) \
101 if( !(c) ){ \
102 OSL_ENSURE(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) \
108 if( !(c) ) { \
109 OSL_ENSURE(c, m); \
110 throw css::uno::RuntimeException( \
111 __func__ + OUString::Concat(u",\n" m), \
112 ifc ); }
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) ) { \
118 OSL_ENSURE(c, m); \
119 return r; }
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 ) \
131 if( !(c) ) \
133 OSL_ENSURE( c, m ); \
134 return; \
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) \
147 do { \
148 css::uno::Any tools_warn_exception( DbgGetCaughtException() ); \
149 SAL_WARN(area, stream << " " << exceptionToString(tools_warn_exception)); \
150 } while (false)
151 #else
152 #define TOOLS_WARN_EXCEPTION(area, stream) \
153 do { \
154 SAL_WARN(area, stream); \
155 } while (false)
156 #endif
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) \
164 do { \
165 css::uno::Any tools_warn_exception( DbgGetCaughtException() ); \
166 SAL_WARN_IF(cond, area, stream << " " << exceptionToString(tools_warn_exception)); \
167 } while (false)
168 #else
169 #define TOOLS_WARN_EXCEPTION_IF(cond, area, stream) \
170 do { \
171 SAL_WARN_IF(cond, area, stream); \
172 } while (false)
173 #endif
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) \
181 do { \
182 css::uno::Any tools_warn_exception( DbgGetCaughtException() ); \
183 SAL_INFO(area, stream << " " << exceptionToString(tools_warn_exception)); \
184 } while (false)
185 #else
186 #define TOOLS_INFO_EXCEPTION(area, stream) \
187 do { \
188 SAL_INFO(area, stream); \
189 } while (false)
190 #endif
192 #endif
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */