OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
NCMLDebug.h
Go to the documentation of this file.
1 // 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 <assert.h>
34 #include <sstream>
35 #include <string>
36 #include "BESDebug.h"
37 #include "BESInternalError.h"
38 #include "BESSyntaxUserError.h"
39 #include "BESNotFoundError.h"
40 
41 /*
42  * Some basic macros to reduce code clutter, cut & pasting, and to greatly improve readability.
43  * I would have made them functions somewhere, but the __FILE__ and __LINE__ are useful.
44  * We can also specialize these based on debug vs release builds etc. or new error types later as well.
45  * */
46 
47 // Where my BESDEBUG output goes
48 #define NCML_MODULE_DBG_CHANNEL "ncml"
49 
50 // For more verbose stuff, level 2
51 #define NCML_MODULE_DBG_CHANNEL_2 "ncml:2"
52 
53 // Shorthand macro for printing debug info that includes the containing fully qualified function name,
54 // even though it's a nightmare of verbosity in some cases like the usage of STL containers.
55 // Switch this out if it gets too ugly...
56 #define NCML_MODULE_FUNCTION_NAME_MACRO __PRETTY_FUNCTION__
57 // #define NCML_MODULE_FUNCTION_NAME_MACRO __func__
58 #define BESDEBUG_FUNC(channel, info) BESDEBUG( (channel), "[" << std::string(NCML_MODULE_FUNCTION_NAME_MACRO) << "]: " << info )
59 
60 // Spew the std::string msg to debug channel then throw BESInternalError. for those errors that are internal problems, not user/parse errors.
61 #define THROW_NCML_INTERNAL_ERROR(msg) { \
62  std::ostringstream __NCML_PARSE_ERROR_OSS__; \
63  __NCML_PARSE_ERROR_OSS__ << std::string("NCMLModule InternalError: ") << "[" << __PRETTY_FUNCTION__ << "]: " << (msg); \
64  BESDEBUG(NCML_MODULE_DBG_CHANNEL, __NCML_PARSE_ERROR_OSS__.str() << endl); \
65  throw BESInternalError( __NCML_PARSE_ERROR_OSS__.str(), \
66  __FILE__, __LINE__); }
67 
68 // Spew the std::string msg to debug channel then throw a BESSyntaxUserError. For parse and syntax errors in the NCML.
69 #define THROW_NCML_PARSE_ERROR(parseLine, msg) { \
70  std::ostringstream __NCML_PARSE_ERROR_OSS__; \
71  __NCML_PARSE_ERROR_OSS__ << "NCMLModule ParseError: at *.ncml line=" << (parseLine) << ": " \
72  << (msg); \
73  BESDEBUG(NCML_MODULE_DBG_CHANNEL, \
74  __NCML_PARSE_ERROR_OSS__.str() << endl); \
75  throw BESSyntaxUserError( __NCML_PARSE_ERROR_OSS__.str(), \
76  __FILE__, \
77  __LINE__); }
78 
79 // My own assert to throw an internal error instead of assert() which calls abort(), which is not so nice to do on a server.
80 #define NCML_ASSERT(cond) { if (!(cond)) { THROW_NCML_INTERNAL_ERROR(std::string("ASSERTION FAILED: ") + std::string(#cond)); } }
81 
82 // An assert that can carry a std::string msg
83 #define NCML_ASSERT_MSG(cond, msg) { if (!(cond)) { \
84  BESDEBUG(NCML_MODULE_DBG_CHANNEL, __PRETTY_FUNCTION__ << ": " << (msg) << endl); \
85  THROW_NCML_INTERNAL_ERROR(std::string("ASSERTION FAILED: condition=( ") + std::string(#cond) + std::string(" ) ") + std::string(msg)); } }
86 
87 // Quick macro to check pointers before dereferencing them.
88 #define VALID_PTR(ptr) NCML_ASSERT_MSG((ptr), std::string("Null pointer:" + std::string(#ptr)));
89 
90 #endif // __NCML_MODULE__NCML_DEBUG__