bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
NCMLDebug.h
1
2// This file is part of the "NcML Module" project, a BES module designed
3// to allow NcML files to be used to be used as a wrapper to add
4// AIS to existing datasets of any format.
5//
6// Copyright (c) 2009 OPeNDAP, Inc.
7// Author: Michael Johnson <m.johnson@opendap.org>
8//
9// For more information, please also see the main website: http://opendap.org/
10//
11// This library is free software; you can redistribute it and/or
12// modify it under the terms of the GNU Lesser General Public
13// License as published by the Free Software Foundation; either
14// version 2.1 of the License, or (at your option) any later version.
15//
16// This library is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19// Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public
22// License along with this library; if not, write to the Free Software
23// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24//
25// Please see the files COPYING and COPYRIGHT for more information on the GLPL.
26//
27// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
29
30#ifndef __NCML_MODULE__NCML_DEBUG__
31#define __NCML_MODULE__NCML_DEBUG__
32
33#include "config.h"
34
35#include <sstream>
36#include <string>
37#include "BESDebug.h"
38#include "BESInternalError.h"
39#include "BESSyntaxUserError.h"
40#include "BESNotFoundError.h"
41
42/*
43 * Some basic macros to reduce code clutter, cut & pasting, and to greatly improve readability.
44 * I would have made them functions somewhere, but the __FILE__ and __LINE__ are useful.
45 * We can also specialize these based on debug vs release builds etc. or new error types later as well.
46 */
47
48// I modified these macros so that the assert-like things compile to null statements
49// when NDEBUG is defined. The bes configure script's --enable-developer will suppress
50// that, otherwise it is defined. 10/16/15 jhrg
51
52// Where my BESDEBUG output goes
53#define NCML_MODULE_DBG_CHANNEL "ncml"
54
55// For more verbose stuff, level 2
56#define NCML_MODULE_DBG_CHANNEL_2 "ncml:2"
57
58// Shorthand macro for printing debug info that includes the containing fully qualified function name,
59// even though it's a nightmare of verbosity in some cases like the usage of STL containers.
60// Switch this out if it gets too ugly...
61#define NCML_MODULE_FUNCTION_NAME_MACRO __PRETTY_FUNCTION__
62// #define NCML_MODULE_FUNCTION_NAME_MACRO __func__
63
64// These are called only when performance no longer matters... 10/16/15 jhrg
65
66// Spew the std::string msg to debug channel then throw BESInternalError. for those errors that are internal problems, not user/parse errors.
67#define THROW_NCML_INTERNAL_ERROR(msg) do { \
68 std::ostringstream __NCML_PARSE_ERROR_OSS__; \
69 __NCML_PARSE_ERROR_OSS__ << std::string("NCMLModule InternalError: ") << "[" << __PRETTY_FUNCTION__ << "]: " << (msg); \
70 BESDEBUG(NCML_MODULE_DBG_CHANNEL, __NCML_PARSE_ERROR_OSS__.str() << std::endl); \
71 throw BESInternalError( __NCML_PARSE_ERROR_OSS__.str(), \
72 __FILE__, __LINE__); } while(false)
73
74// Spew the std::string msg to debug channel then throw a BESSyntaxUserError. For parse and syntax errors in the NCML.
75#define THROW_NCML_PARSE_ERROR(parseLine, msg) { \
76 std::ostringstream __NCML_PARSE_ERROR_OSS__; \
77 __NCML_PARSE_ERROR_OSS__ << "NCMLModule ParseError: at *.ncml line=" << (parseLine) << ": " \
78 << (msg); \
79 BESDEBUG(NCML_MODULE_DBG_CHANNEL, \
80 __NCML_PARSE_ERROR_OSS__.str() << std::endl); \
81 throw BESSyntaxUserError( __NCML_PARSE_ERROR_OSS__.str(), \
82 __FILE__, \
83 __LINE__); }
84
85
86#ifdef NDEBUG
87#define BESDEBUG_FUNC(channel, info)
88#else
89#define BESDEBUG_FUNC(channel, info) BESDEBUG( (channel), "[" << std::string(NCML_MODULE_FUNCTION_NAME_MACRO) << "]: " << info )
90#endif
91
92#ifdef NDEBUG
93#define NCML_ASSERT(cond)
94#else
95// My own assert to throw an internal error instead of assert() which calls abort(), which is not so nice to do on a server.
96#define NCML_ASSERT(cond) do { if (!(cond)) { THROW_NCML_INTERNAL_ERROR(std::string("ASSERTION FAILED: ") + std::string(#cond)); } } while (false)
97#endif
98
99#ifdef NDEBUG
100#define NCML_ASSERT_MSG(cond, msg)
101#else
102// An assert that can carry a std::string msg
103#define NCML_ASSERT_MSG(cond, msg) do { if (!(cond)) { \
104 BESDEBUG(NCML_MODULE_DBG_CHANNEL, __PRETTY_FUNCTION__ << ": " << (msg) << std::endl); \
105 THROW_NCML_INTERNAL_ERROR(std::string("ASSERTION FAILED: condition=( ") + std::string(#cond) + std::string(" ) ") + std::string(msg)); } } while(false)
106#endif
107
108#ifdef NDEBUG
109#define VALID_PTR(ptr)
110#else
111// Quick macro to check pointers before dereferencing them.
112#define VALID_PTR(ptr) NCML_ASSERT_MSG((ptr), std::string("Null pointer:" + std::string(#ptr)))
113#endif
114
115#endif // __NCML_MODULE__NCML_DEBUG__