File indexing completed on 2024-04-28 04:18:51

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2012 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
0019 
0020 */
0021 #ifndef GVDEBUG_H
0022 #define GVDEBUG_H
0023 
0024 #include "gwenview_lib_debug.h"
0025 
0026 /**
0027  * Uses this macro if you want your code to abort when the GV_FATAL_FAILS
0028  * environment variable is set.
0029  * Most of the time you want to use the various GV_RETURN* macros instead,
0030  * but this one can be handy in certain situations. For example in a switch
0031  * case block:
0032  *
0033  * switch (state) {
0034  * case State1:
0035  *     ...
0036  *     break;
0037  * case State2:
0038  *     ...
0039  *     break;
0040  * case State3:
0041  *     qCWarning(GWENVIEW_LIB_LOG) << "state should not be State3";
0042  *     GV_FATAL_FAILS;
0043  *     break;
0044  * }
0045  */
0046 #define GV_FATAL_FAILS                                                                                                                                         \
0047     do {                                                                                                                                                       \
0048         if (!qEnvironmentVariableIsEmpty("GV_FATAL_FAILS")) {                                                                                                  \
0049             qFatal("Aborting because environment variable 'GV_FATAL_FAILS' is set");                                                                           \
0050         }                                                                                                                                                      \
0051     } while (0)
0052 
0053 #define GV_RETURN_IF_FAIL(cond)                                                                                                                                \
0054     do {                                                                                                                                                       \
0055         if (!(cond)) {                                                                                                                                         \
0056             qCWarning(GWENVIEW_LIB_LOG) << "Condition '" << #cond << "' failed";                                                                               \
0057             GV_FATAL_FAILS;                                                                                                                                    \
0058             return;                                                                                                                                            \
0059         }                                                                                                                                                      \
0060     } while (0)
0061 
0062 #define GV_RETURN_VALUE_IF_FAIL(cond, value)                                                                                                                   \
0063     do {                                                                                                                                                       \
0064         if (!(cond)) {                                                                                                                                         \
0065             qCWarning(GWENVIEW_LIB_LOG) << "Condition '" << #cond << "' failed.";                                                                              \
0066             GV_FATAL_FAILS;                                                                                                                                    \
0067             return value;                                                                                                                                      \
0068         }                                                                                                                                                      \
0069     } while (0)
0070 
0071 #define GV_RETURN_IF_FAIL2(cond, msg)                                                                                                                          \
0072     do {                                                                                                                                                       \
0073         if (!(cond)) {                                                                                                                                         \
0074             qCWarning(GWENVIEW_LIB_LOG) << "Condition '" << #cond << "' failed" << msg;                                                                        \
0075             GV_FATAL_FAILS;                                                                                                                                    \
0076             return;                                                                                                                                            \
0077         }                                                                                                                                                      \
0078     } while (0)
0079 
0080 #define GV_RETURN_VALUE_IF_FAIL2(cond, value, msg)                                                                                                             \
0081     do {                                                                                                                                                       \
0082         if (!(cond)) {                                                                                                                                         \
0083             qCWarning(GWENVIEW_LIB_LOG) << "Condition '" << #cond << "' failed." << msg;                                                                       \
0084             GV_FATAL_FAILS;                                                                                                                                    \
0085             return value;                                                                                                                                      \
0086         }                                                                                                                                                      \
0087     } while (0)
0088 
0089 #define GV_WARN_AND_RETURN(msg)                                                                                                                                \
0090     do {                                                                                                                                                       \
0091         qCWarning(GWENVIEW_LIB_LOG) << msg;                                                                                                                    \
0092         GV_FATAL_FAILS;                                                                                                                                        \
0093         return;                                                                                                                                                \
0094     } while (0)
0095 
0096 #define GV_WARN_AND_RETURN_VALUE(value, msg)                                                                                                                   \
0097     do {                                                                                                                                                       \
0098         qCWarning(GWENVIEW_LIB_LOG) << msg;                                                                                                                    \
0099         GV_FATAL_FAILS;                                                                                                                                        \
0100         return value;                                                                                                                                          \
0101     } while (0)
0102 
0103 #define GV_LOG(var) qCDebug(GWENVIEW_LIB_LOG) << #var << '=' << (var)
0104 
0105 #endif // GVDEBUG_H