error.cc 4.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*! \file error.cc
 * \brief exceptions and error handling macros (implementation)
 * 
 * ----------------------------------------------------------------------------
 * 
 * $Id$
 * \author Thomas Forbriger
 * \date 27/11/2002
 * 
 * exceptions and error handling macros (implementation)
 * 
 * Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt) 
 *
 * ----
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
 * 
 * REVISIONS and CHANGES 
 *  - 27/11/2002   V1.0   Thomas Forbriger
32
 *  - 25/01/2014   V1.1   copied from libtsxx
33
34
35
 * 
 * ============================================================================
 */
36
#define TSXX_ERROR_CC_VERSION \
37
  "TSXX_ERROR_CC   V1.1"
38
#define TSXX_ERROR_CC_CVSID \
39
40
41
  "$Id$"

#include <iostream>
42
#include <tsxx/error.h>
43
44
45
46

using std::cerr;
using std::endl;

47
namespace ts {
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
namespace error {

  //! initialize and instantiate
  bool Exception::Mreport_on_construct=true;
  std::stack<bool> Exception::Mprevious_report_state;

  //! construct from nothing
  Exception::Exception(): 
    Mmessage(NULL), Mfile(NULL), Mline(0), Mcondition(NULL)
    { if (this->report_on_construct_is_true()) { base_report(); } }

  //! construct with message
  Exception::Exception(const char* message):
    Mmessage(message), Mfile(NULL), Mline(0), Mcondition(NULL)
    { if (this->report_on_construct_is_true()) { base_report(); } }

  //! construct with message and file info
  Exception::Exception(const char* message,
                       const char* condition):
    Mmessage(message), Mfile(NULL), Mline(0), Mcondition(condition)
    { if (this->report_on_construct_is_true()) { base_report(); } }

  //! construct with message and file info
  Exception::Exception(const char* message,
                       const char* file,
                       const int& line):
    Mmessage(message), Mfile(file), Mline(line), Mcondition(NULL)
    { if (this->report_on_construct_is_true()) { base_report(); } }

  //! construct with message and file info and condition
  Exception::Exception(const char* message,
                       const char* file,
                       const int& line,
                       const char* condition):
    Mmessage(message), Mfile(file), Mline(line), Mcondition(condition)
    { if (this->report_on_construct_is_true()) { base_report(); } }
      
  //! switch on
  void Exception::report_on_construct() 
  {
    Mprevious_report_state.push(Mreport_on_construct);
    Mreport_on_construct=true;
  }

  //! switch off
  void Exception::dont_report_on_construct()
  {
    Mprevious_report_state.push(Mreport_on_construct);
    Mreport_on_construct=false;
  }

  //! restore
  void Exception::restore_report_state()
  {
    if (!Mprevious_report_state.empty())
    {
      Mreport_on_construct=Mprevious_report_state.top();
      Mprevious_report_state.pop();
    }
  }

  //! report
  void Exception::report() const
  {
    base_report();
  }

  //! report
  void Exception::base_report() const
  {
    std::cout.flush();
    cerr << "Exception report:" << endl;
    if (Mmessage==NULL)
    {
      cerr << "  No message" << endl;
    }
    else
    {
      cerr << "  message: " << Mmessage << endl;
    }
    if (Mfile!=NULL)
    {
      cerr << "  triggered in \"" << Mfile << "\" at line #" << Mline << endl;
    }
    if (Mcondition!=NULL)
    {
      cerr << "  by test condition:" << endl
        << "    \"" << Mcondition << "\"" << endl;
    }
    cerr.flush();
  }

} // namespace error
141
} // namespace ts
142
143

/* ----- END OF error.cc ----- */