View | Details | Raw Unified | Return to bug 931
Collapse All | Expand All

(-)a/src/core/abort.h (-29 / +99 lines)
 Lines 1-6    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
2
/*
3
 * Copyright (c) 2008 INRIA
3
 * Copyright (c) 2008 INRIA, 2010 NICTA
4
 *
4
 *
5
 * This program is free software; you can redistribute it and/or modify
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
6
 * it under the terms of the GNU General Public License version 2 as
 Lines 15-64    Link Here 
15
 * along with this program; if not, write to the Free Software
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
17
 *
18
 * Author:  Original author unknown
19
 *          Quincy Tse <quincy.tse@nicta.com.au>
18
 */
20
 */
19
#ifndef NS3_ABORT_H
21
#ifndef NS3_ABORT_H
20
#define NS3_ABORT_H
22
#define NS3_ABORT_H
21
23
22
#include "fatal-error.h"
24
#include "fatal-error.h"
23
25
26
/**
27
 * \ingroup debugging
28
 * \brief Abnormal program termination
29
 *
30
 * \param msg message to output when this macro is hit.
31
 *
32
 * This macro is essentially equivalent to NS_FATAL_ERROR,
33
 * excepts it prepends the error message with the string
34
 * "aborted. ". When this macro is hit a runtime, the
35
 * program will be halted using std::terminate, which
36
 * triggers clean up code regestered by std::set_terminate.
37
 *
38
 * This macro is enable unconditionally in all builds,
39
 * including debug and optimized builds.
40
 *
41
 * \see NS_FATAL_ERROR
42
 */
24
#define NS_ABORT_MSG(msg)                                       \
43
#define NS_ABORT_MSG(msg)                                       \
25
  do {								\
44
  do {								                                          \
26
    std::cerr << "file=" << __FILE__ <<                         \
45
    std::cerr << "aborted. ";                                   \
27
      ", line=" << __LINE__ << ", abort, msg=\"" <<             \
46
    NS_FATAL_ERROR (msg);                                       \
28
      msg << "\"" << std::endl;                                 \
29
    int *a = 0;                                                 \
30
    *a = 0;							\
31
  } while (false)
47
  } while (false)
32
48
33
49
34
#define NS_ABORT_IF(cond)					\
50
/**
35
  do {								\
51
 * \ingroup debugging
36
    if (cond)							\
52
 * \brief Abnormal program termination if cond is true.
37
      {								\
53
 *
38
	std::cerr << "file=" << __FILE__ <<			\
54
 * \param cond condition to be evaluated.
39
	  ", line=" << __LINE__ << ", abort on=\""#cond <<	\
55
 *
40
            "\"" << std::endl;                                  \
56
 * This is similar to NS_ASSERT(!(cond)), except this check
41
	int *a = 0;						\
57
 * is enabled in all builds. If cond is evaluated to true,
42
	*a = 0;							\
58
 * the espression evaluating to true is printed to stderr,
43
      }								\
59
 * followed by a call to the NS_FATAL_ERROR_NO_MSG() macro
60
 * which prints the details of filename and line number to
61
 * stderr. The program will be halted by calling
62
 * std::terminate(), triggering any clean up code registered
63
 * by std::set_terminate (NS3 default is a stream-flushing
64
 * code, but may be overridden).
65
 *
66
 * This macro is enable unconditionally in all builds,
67
 * including debug and optimized builds.
68
 */
69
#define NS_ABORT_IF(cond)					                              \
70
  do {								                                          \
71
    if (cond)							                                      \
72
      {								                                          \
73
	      std::cerr << "aborted. cond=\"" << #cond << ", ";       \
74
	      NS_FATAL_ERROR_NO_MSG ();                               \
75
      }								                                          \
44
  } while (false)
76
  } while (false)
45
77
46
#define NS_ABORT_MSG_IF(cond, msg)				\
78
/**
47
  do {								\
79
 * \ingroup debugging
48
    if (cond)							\
80
 * \brief Abnormal program termination if cond is true.
49
      {								\
81
 *
50
	std::cerr << "file=" << __FILE__ <<			\
82
 * \param cond condition to be evaluated.
51
	  ", line=" << __LINE__ << ", abort on=\""#cond <<	\
83
 * \param msg message to output when cond is true.
52
	  "\", msg=\"" << msg << "\"" << std::endl;		\
84
 *
53
	int *a = 0;						\
85
 * This is similar to NS_ASSERT_MSG(!(cond)), except this
54
	*a = 0;							\
86
 * check is enabled in all builds. If cond is evaluated to
55
      }								\
87
 * true, the espression evaluating to true is printed to
88
 * stderr, followed by a call to the NS_FATAL_ERROR() macro
89
 * which prints the user-specified error message, and details
90
 * of filename and line number to stderr. The program will
91
 * be halted by calling std::terminate(), triggering any
92
 * clean up code registered by std::set_terminate (NS3
93
 * default is a stream-flushing code, but may be overridden).
94
 *
95
 * This macro is enable unconditionally in all builds,
96
 * including debug and optimized builds.
97
 */
98
#define NS_ABORT_MSG_IF(cond, msg)				                      \
99
  do {								                                          \
100
    if (cond)							                                      \
101
      {								                                          \
102
	      std::cerr << "aborted. cond=\"" << #cond << "\", ";       \
103
	      NS_FATAL_ERROR (msg);                                   \
104
      }								                                          \
56
  } while (false)
105
  } while (false)
57
106
58
#define NS_ABORT_UNLESS(cond)				\
107
/**
108
 * \ingroup debugging
109
 * \brief Abnormal program termination if cond is false.
110
 *
111
 * \param cond condition to be evaluated.
112
 *
113
 * This is an alias for NS_ABORT_IF(!(cond))
114
 *
115
 * \see NS_ABORT_IF
116
 */
117
#define NS_ABORT_UNLESS(cond)				                            \
59
  NS_ABORT_IF(!(cond))
118
  NS_ABORT_IF(!(cond))
60
119
61
#define NS_ABORT_MSG_UNLESS(cond, msg)			\
120
/**
121
 * \ingroup debugging
122
 * \brief Abnormal program termination if cond is false.
123
 *
124
 * \param cond condition to be evaluated.
125
 * \param msg message to output if cond is false.
126
 *
127
 * This is an alias for NS_ABORT_MSG_IF(!(cond))
128
 *
129
 * \see NS_ABORT_MSG_IF
130
 */
131
#define NS_ABORT_MSG_UNLESS(cond, msg)			                    \
62
  NS_ABORT_MSG_IF(!(cond),msg)
132
  NS_ABORT_MSG_IF(!(cond),msg)
63
133
64
#endif /* NS3_ABORT_H */
134
#endif /* NS3_ABORT_H */
(-)a/src/core/assert.h (-20 / +30 lines)
 Lines 1-6    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
2
/*
3
 * Copyright (c) 2006 INRIA
3
 * Copyright (c) 2006 INRIA, 2010 NICTA
4
 *
4
 *
5
 * This program is free software; you can redistribute it and/or modify
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
6
 * it under the terms of the GNU General Public License version 2 as
 Lines 16-29    Link Here 
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
17
 *
18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
 *         Quincy Tse <quincy.tse@nicta.com.au>
19
 */
20
 */
20
#ifndef ASSERT_H
21
#ifndef NS_ASSERT_H
21
#define ASSERT_H
22
#define NS_ASSERT_H
22
23
23
#ifdef NS3_ASSERT_ENABLE
24
#ifdef NS3_ASSERT_ENABLE
24
25
25
#include <iostream>
26
#include <iostream>
26
27
28
#include "fatal-error.h"
29
27
/**
30
/**
28
 * \ingroup core
31
 * \ingroup core
29
 * \defgroup debugging Debugging
32
 * \defgroup debugging Debugging
 Lines 39-44    Link Here 
39
 * not true, the program halts. These checks are built
42
 * not true, the program halts. These checks are built
40
 * into the program only in debugging builds. They are
43
 * into the program only in debugging builds. They are
41
 * removed in optimized builds.
44
 * removed in optimized builds.
45
 *
46
 * These macro are intended to check certain conditions
47
 * to be true. Do not put code that also have side effects
48
 * that your program relies on (eg. code that advances
49
 * an iterator and return false at end of file) because
50
 * the code will not be executed on release builds!!
51
 *
52
 * If assertion-style checks are required for release
53
 * builds, use NS_ABORT_UNLESS and NS_ABORT_MSG_UNLESS.
42
 */
54
 */
43
55
44
/**
56
/**
 Lines 47-64    Link Here 
47
 *
59
 *
48
 * At runtime, in debugging builds, if this condition is not
60
 * At runtime, in debugging builds, if this condition is not
49
 * true, the program prints the source file, line number and 
61
 * true, the program prints the source file, line number and 
50
 * unverified condition and halts by dereferencing a null pointer.
62
 * unverified condition and halts by calling std::terminate
51
 */
63
 */
52
#define NS_ASSERT(condition)                                    \
64
#define NS_ASSERT(condition)                                    \
53
  do                                                            \
65
  do                                                            \
54
    {                                                           \
66
    {                                                           \
55
      if (!(condition))                                         \
67
      if (!(condition))                                         \
56
        {                                                       \
68
        {                                                       \
57
          std::cerr << "assert failed. file=" << __FILE__ <<    \
69
          std::cerr << "assert failed. cond=\"" <<              \
58
            ", line=" << __LINE__ << ", cond=\""#condition <<   \
70
            #condition << "\", ";                               \
59
            "\"" << std::endl;                                  \
71
          NS_FATAL_ERROR_NO_MSG();                              \
60
          int *a = 0;                                           \
61
          *a = 0;                                               \
62
        }                                                       \
72
        }                                                       \
63
    }                                                           \
73
    }                                                           \
64
  while (false)
74
  while (false)
 Lines 71-88    Link Here 
71
 *
81
 *
72
 * At runtime, in debugging builds, if this condition is not
82
 * At runtime, in debugging builds, if this condition is not
73
 * true, the program prints the message to output and
83
 * true, the program prints the message to output and
74
 * halts by dereferencing a null pointer.
84
 * halts by calling std::terminate.
75
 */
85
 */
76
#define NS_ASSERT_MSG(condition, message)       \
86
#define NS_ASSERT_MSG(condition, message)             \
77
  do                                            \
87
  do                                                  \
78
    {                                           \
88
    {                                                 \
79
      if (!(condition))                         \
89
      if (!(condition))                               \
80
        {                                       \
90
        {                                             \
81
          std::cerr << message << std::endl;    \
91
          std::cerr << "assert failed. cond=\"" <<    \
82
          int *a = 0;                           \
92
            #condition << "\", ";                     \
83
          *a = 0;                               \
93
          NS_FATAL_ERROR (message);                   \
84
        }                                       \
94
        }                                             \
85
    }                                           \
95
    }                                                 \
86
  while (false)
96
  while (false)
87
97
88
#else /* NS3_ASSERT_ENABLE */
98
#else /* NS3_ASSERT_ENABLE */
(-)a/src/core/fatal-error.h (-13 / +45 lines)
 Lines 1-6    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
2
/*
3
 * Copyright (c) 2006 INRIA
3
 * Copyright (c) 2006 INRIA, 2010 NICTA
4
 *
4
 *
5
 * This program is free software; you can redistribute it and/or modify
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License version 2 as
6
 * it under the terms of the GNU General Public License version 2 as
 Lines 16-26    Link Here 
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 *
17
 *
18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
 *         Quincy Tse <quincy.tse@nicta.com.au>
19
 */
20
 */
20
#ifndef FATAL_ERROR_H
21
#ifndef NS3_FATAL_ERROR_H
21
#define FATAL_ERROR_H
22
#define NS3_FATAL_ERROR_H
22
23
23
#include <iostream>
24
#include <iostream>
25
#include <exception>
26
#include <cstdlib>
27
28
/**
29
 * \ingroup debugging
30
 * \brief fatal error handling
31
 *
32
 * When this macro is hit at runtime, details of filename
33
 * and line number is printed to stderr, and the program
34
 * is halted by calling std::terminate(). This will
35
 * trigger any clean up code registered by
36
 * std::set_terminate (NS3 default is a stream-flushing
37
 * code), but may be overridden.
38
 *
39
 * This macro is enabled unconditionally in all builds,
40
 * including debug and optimized builds.
41
 */
42
#define NS_FATAL_ERROR_NO_MSG()                           \
43
  do                                                      \
44
    {                                                     \
45
      std::set_terminate (std::abort);                    \
46
                                                          \
47
      std::cerr << "file=" << __FILE__ << ", line=" <<    \
48
        __LINE__ << std::endl;                            \
49
      std::terminate ();                                  \
50
    }                                                     \
51
  while (false)
24
52
25
/**
53
/**
26
 * \ingroup debugging
54
 * \ingroup debugging
 Lines 28-47    Link Here 
28
 *
56
 *
29
 * \param msg message to output when this macro is hit.
57
 * \param msg message to output when this macro is hit.
30
 *
58
 *
31
 * When this macro is hit at runtime, the user-specified 
59
 * When this macro is hit at runtime, the user-specified
32
 * error message is output and the program is halted by
60
 * error message is printed to stderr, followed by a call
33
 * dereferencing a null pointer. This macro is enabled 
61
 * to the NS_FATAL_ERROR_NO_MSG() macro which prints the
34
 * unconditionally in all builds, including debug and 
62
 * details of filename and line number to stderr. The
35
 * optimized builds.
63
 * program will be halted by calling std::terminate(),
64
 * triggering any clean up code registered by
65
 * std::set_terminate (NS3 default is a stream-flushing
66
 * code, but may be overridden).
67
 *
68
 * This macro is enabled unconditionally in all builds,
69
 * including debug and optimized builds.
36
 */
70
 */
37
#define NS_FATAL_ERROR(msg)				\
71
#define NS_FATAL_ERROR(msg)				                      \
38
  do                                                    \
72
  do                                                    \
39
    {                                                   \
73
    {                                                   \
40
      std::cerr << msg << std::endl;			\
74
      std::cerr << "msg=\"" << msg << "\", ";           \
41
      int *a = 0;                                       \
75
      NS_FATAL_ERROR_NO_MSG();                          \
42
      *a = 0;                                           \
43
    }                                                   \
76
    }                                                   \
44
  while (false)
77
  while (false)
45
78
46
47
#endif /* FATAL_ERROR_H */
79
#endif /* FATAL_ERROR_H */

Return to bug 931