OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / designercore / exceptions / exception.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "exception.h"
35
36 #ifdef Q_OS_LINUX
37 #include <execinfo.h>
38 #include <cxxabi.h>
39 #endif
40
41 #include <QRegExp>
42
43 /*!
44 \defgroup CoreExceptions
45 */
46 /*!
47 \class QmlDesigner::Exception
48 \ingroup CoreExceptions
49 \brief This is the abstract base class for all excetions.
50     Exceptions should be used in cases there is no other way to say something goes wrong. For example
51     the result would be a inconsistent model or a crash.
52 */
53
54
55 namespace QmlDesigner {
56
57 #ifdef Q_OS_LINUX
58 const char* demangle(const char* name)
59 {
60    char buf[1024];
61    size_t size = 1024;
62    int status;
63    char* res;
64    res = abi::__cxa_demangle(name,
65      buf,
66      &size,
67      &status);
68    return res;
69 }
70 #else
71 const char* demangle(const char* name)
72 {
73    return name;
74 }
75 #endif
76
77
78 bool Exception::s_shouldAssert = false;
79
80 void Exception::setShouldAssert(bool assert)
81 {
82     s_shouldAssert = assert;
83 }
84
85 bool Exception::shouldAssert()
86 {
87     return s_shouldAssert;
88 }
89
90 /*!
91 \brief Constructor
92
93 \param line use the __LINE__ macro
94 \param function use the __FUNCTION__ or the Q_FUNC_INFO macro
95 \param file use the __FILE__ macro
96 */
97 Exception::Exception(int line,
98               const QString &function,
99               const QString &file)
100   : m_line(line),
101     m_function(function),
102     m_file(file)
103 {
104 #ifdef Q_OS_LINUX
105     void * array[50];
106     int nSize = backtrace(array, 50);
107     char ** symbols = backtrace_symbols(array, nSize);
108
109     for (int i = 0; i < nSize; i++)
110     {
111         m_backTrace.append(QString("%1\n").arg(symbols[i]));
112     }
113
114     free(symbols);
115 #endif
116
117 if (s_shouldAssert)
118     Q_ASSERT_X(false, function.toLatin1(), QString("%1:%2 - %3").arg(file).arg(line).arg(function).toLatin1());
119 }
120
121 Exception::~Exception()
122 {
123 }
124
125 /*!
126 \brief Returns the unmangled backtrace of this exception
127
128 \returns the backtrace as a string
129 */
130 QString Exception::backTrace() const
131 {
132     return m_backTrace;
133 }
134
135 /*!
136 \brief Returns the optional description of this exception
137
138 \returns the description as string
139 */
140 QString Exception::description() const
141 {
142     return QString("file: %1, function: %2, line: %3").arg(m_file, m_function, QString::number(m_line));
143 }
144
145 /*!
146 \brief Returns the line number where this exception was thrown
147
148 \returns the line number as integer
149 */
150 int Exception::line() const
151 {
152     return m_line;
153 }
154
155 /*!
156 \brief Returns the function name where this exception was thrown
157
158 \returns the function name as string
159 */
160 QString Exception::function() const
161 {
162     return m_function;
163 }
164
165 /*!
166 \brief Returns the file name where this exception was thrown
167
168 \returns the file name as string
169 */
170 QString Exception::file() const
171 {
172     return m_file;
173 }
174
175 QDebug operator<<(QDebug debug, const Exception &exception)
176 {
177     debug.nospace() << "Exception: " << exception.type() << "\n"
178                        "Function:  " << exception.function() << "\n"
179                        "File:      " << exception.file() << "\n"
180                        "Line:      " << exception.line() << "\n";
181     if (!exception.description().isEmpty())
182         debug.nospace() << exception.description();
183
184     if (!exception.backTrace().isEmpty())
185         debug.nospace() << exception.backTrace();
186
187     return debug.space();
188 }
189
190 /*!
191 \fn QString Exception::type() const
192 \brief Returns the type of this exception
193
194 \returns the type as a string
195 */
196 }