LibreOffice Module comphelper (master) 1
diagnose_ex.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19#ifndef INCLUDED_COMPHELPER_DIAGNOSE_EX_HXX
20#define INCLUDED_COMPHELPER_DIAGNOSE_EX_HXX
21
22#include <osl/diagnose.h>
23#include <rtl/ustring.hxx>
24
25#include <com/sun/star/uno/RuntimeException.hpp>
26#include <com/sun/star/lang/IllegalArgumentException.hpp>
27
28#include <sal/log.hxx>
31
32COMPHELPER_DLLPUBLIC void DbgUnhandledException(const css::uno::Any& caughtException,
33 const char* currentFunction, const char* fileAndLineNo,
34 const char* area, const char* explanatory = nullptr);
35
36//getCaughtException throws exceptions in never-going-to-happen situations which
37//floods coverity with warnings
38inline css::uno::Any DbgGetCaughtException()
39{
40#if defined(__COVERITY__)
41 try
42 {
43 return ::cppu::getCaughtException();
44 }
45 catch (...)
46 {
47 std::abort();
48 }
49#else
50 return ::cppu::getCaughtException();
51#endif
52}
53
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 );
67
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
77
78
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 ); }
96
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 >() ); }
106
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 ); }
113
117#define ENSURE_OR_RETURN(c, m, r) if( !(c) ) { \
118 OSL_ENSURE(c, m); \
119 return r; }
120
124#define ENSURE_OR_RETURN_FALSE(c, m) \
125 ENSURE_OR_RETURN(c, m, false)
126
130#define ENSURE_OR_RETURN_VOID( c, m ) \
131 if( !(c) ) \
132 { \
133 OSL_ENSURE( c, m ); \
134 return; \
135 }
136
139COMPHELPER_DLLPUBLIC OString exceptionToString(css::uno::Any const & caughtEx);
140
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
157
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
174
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
191
192#endif
193
194/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define COMPHELPER_DLLPUBLIC
css::uno::Any DbgGetCaughtException()
Definition: diagnose_ex.hxx:38
COMPHELPER_DLLPUBLIC void DbgUnhandledException(const css::uno::Any &caughtException, const char *currentFunction, const char *fileAndLineNo, const char *area, const char *explanatory=nullptr)
COMPHELPER_DLLPUBLIC OString exceptionToString(css::uno::Any const &caughtEx)
Convert a caught exception to a string suitable for logging.