cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmnAssert.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Ankur Kapoor
7  Created on: 2003-06-25
8 
9  (C) Copyright 2003-2007 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 
20 */
21 
22 
27 #pragma once
28 
29 #ifndef _cmnAssert_h
30 #define _cmnAssert_h
31 
32 
34 #include <cisstCommon/cmnThrow.h>
35 
36 #include <assert.h>
37 #include <cstdlib>
38 #include <sstream>
39 
40 #include <cisstCommon/cmnLogger.h>
41 
73 #ifdef CISST_CMN_ASSERT_DISABLED
74  #define CMN_ASSERT(expr)
75 #else // CISST_CMN_ASSERT_DISABLED
76 
77 #ifdef CISST_CMN_ASSERT_THROWS_EXCEPTION
78 
79 #define CMN_ASSERT(expr) \
80  if (!(expr)) { \
81  std::stringstream messageBuffer; \
82  messageBuffer << __FILE__ << ": Assertion '" << #expr \
83  << "' failed in: " << CMN_PRETTY_FUNCTION \
84  << ", line #" << __LINE__; \
85  cmnThrow(std::logic_error(messageBuffer.str())); \
86  }
87 
88 #else // CISST_CMN_ASSERT_THROWS_EXCEPTION
89 
90 #define CMN_ASSERT(expr) \
91  if (!(expr)) { \
92  std::stringstream messageBuffer; \
93  messageBuffer << __FILE__ << ": Assertion '" << #expr \
94  << "' failed in: " << CMN_PRETTY_FUNCTION \
95  << ", line #" << __LINE__; \
96  std::cerr << messageBuffer.str() << std::endl; \
97  CMN_LOG_INIT_ERROR << messageBuffer.str() << std::endl; \
98  abort(); \
99  }
100 
101 #endif // CISST_CMN_ASSERT_THROWS_EXCEPTION
102 
103 #endif // CISST_CMN_ASSERT_DISABLED
104 
105 
106 #endif // _cmnAssert_h
107 
Portability across compilers and operating systems tools.
Declaration of cmnLogger amd macros for human readable logging.
Declaration of the template function cmnThrow.