Exceptions.cpp 3.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2011
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This 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 3 of the License, or
// (at your option) any later version.
//
// This software 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, see <http://www.gnu.org/licenses/>.
//###########################################################################
22
#include "lima/Exceptions.h"
23

24
25
#include <sstream>

26
using namespace lima;
27
using namespace std;
28

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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

ExcDebProxy::ExcDebProxy(DebProxy *deb_proxy)
{
	m_d = new Data(deb_proxy);
}

ExcDebProxy::ExcDebProxy(const ExcDebProxy& o)
{
	m_d = o.getData();
}

ExcDebProxy::~ExcDebProxy()
{
	if (m_d->deb_proxy) {
		*m_d->deb_proxy << " [thrown]";
		delete m_d->deb_proxy;
		m_d->deb_proxy = NULL;
	}

	if (putData())
		delete m_d;
}

struct ExcDebProxy::Data *ExcDebProxy::getData() const
{
	m_d->count.get();
	return m_d;
}

bool ExcDebProxy::putData() const
{
	return m_d->count.put();
}

ExcDebProxy::Data::Data(DebProxy *d)
	: deb_proxy(d)
{
}

ExcDebProxy::operator DebProxy *() const
{
	return m_d->deb_proxy;
}


Exception::Exception(Layer layer, ErrorType err_type, const string& err_desc,
		     const string& file_name, const string& funct_name, 
		     int line_nr, ExcDebProxy exc_deb_proxy)
77
	: m_layer(layer), m_err_type(err_type), m_err_desc(err_desc),
78
79
	  m_file_name(file_name), m_funct_name(funct_name), m_line_nr(line_nr),
	  m_exc_deb_proxy(exc_deb_proxy)
80
{
81
82
83
84
	DebProxy *deb_proxy = m_exc_deb_proxy;
	if (deb_proxy)
		*deb_proxy << "Exception(" << getErrType() << "): " 
			   << getErrDesc();
85
86
	else
		std::cerr << "********* Exception(" << getErrType() << "): " 
87
			  << getErrDesc() << " *********" << std::endl;
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
Layer Exception::getLayer() const
{
	return m_layer;
}

ErrorType Exception::getErrType() const
{
	return m_err_type;
}

string Exception::getErrDesc() const
{
	return m_err_desc;
}

string Exception::getFileName() const
{
	return m_file_name;
}

string Exception::getFunctName() const
{
	return m_funct_name;
}

115
string Exception::getErrMsg() const
116
117
118
119
120
121
122
123
124
{
	ostringstream os;
	os << m_layer << ": " 
	   << m_funct_name  << "(" << m_file_name << ", " 
	   << m_line_nr << "): " 
	   << m_err_type << ": " << m_err_desc;
	return os.str();
}

125
ostream& lima::operator <<(ostream& os, Layer layer)
126
{
127
128
	string name = "Unknown";
	switch (layer) {
129
130
131
132
	case Common:		name = "Common";	break;
	case Control:		name = "Control";	break;
	case Hardware:		name = "Hardware";	break;
	case CameraPlugin:	name = "CameraPlugin";	break;
133
134
	}
	return os << name;
135
}
136

137
ostream& lima::operator <<(ostream& os, ErrorType err_type)
138
139
140
141
142
143
144
145
146
147
{
	string name = "Unknown";
	switch (err_type) {
	case InvalidValue:	name = "InvalidValue";	break;
	case NotSupported:	name = "NotSupported";	break;
	case Error:		name = "Error";		break;
	}
	return os << name;
}

148
ostream& lima::operator <<(ostream& os, const Exception& e)
149
150
151
{
	return os << e.getErrMsg();
}