├── qexceptionstream.pri ├── README.md ├── Sample ├── Sample.pro └── main.cpp ├── .gitignore ├── qpm.json ├── qpmx.json ├── LICENSE └── qexceptionstream.h /qexceptionstream.pri: -------------------------------------------------------------------------------- 1 | CONFIG += exceptions 2 | 3 | HEADERS += \ 4 | $$PWD/qexceptionstream.h 5 | 6 | INCLUDEPATH += $$PWD 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QExceptionStream 2 | A wrapper around QDebug that allows you to create exception messages like you would write to QDebug 3 | -------------------------------------------------------------------------------- /Sample/Sample.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | include(../qexceptionstream.pri) 7 | 8 | SOURCES += \ 9 | main.cpp 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # C++ objects and libs 2 | *.slo 3 | *.lo 4 | *.o 5 | *.a 6 | *.la 7 | *.lai 8 | *.so 9 | *.dll 10 | *.dylib 11 | 12 | # Qt-es 13 | object_script.*.Release 14 | object_script.*.Debug 15 | *_plugin_import.cpp 16 | /.qmake.cache 17 | /.qmake.stash 18 | *.pro.user 19 | *.pro.user.* 20 | *.qbs.user 21 | *.qbs.user.* 22 | *.moc 23 | moc_*.cpp 24 | moc_*.h 25 | qrc_*.cpp 26 | ui_*.h 27 | *.qmlc 28 | *.jsc 29 | Makefile* 30 | *build-* 31 | 32 | # Qt unit tests 33 | target_wrapper.* 34 | 35 | # QtCreator 36 | *.autosave 37 | 38 | # QtCreator Qml 39 | *.qmlproject.user 40 | *.qmlproject.user.* 41 | 42 | # QtCreator CMake 43 | CMakeLists.txt.user* 44 | -------------------------------------------------------------------------------- /Sample/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class TestExc : public std::exception 6 | { 7 | public: 8 | TestExc(const QString &msg) : 9 | _msg{msg.toUtf8()} 10 | {} 11 | 12 | const char *what() const noexcept override { 13 | return _msg.constData(); 14 | } 15 | 16 | private: 17 | const QByteArray _msg; 18 | }; 19 | 20 | int main(int argc, char *argv[]) 21 | { 22 | QCoreApplication a(argc, argv); 23 | 24 | try { 25 | qThrow(TestExc) << "Hello you little" << 42 26 | << "- it is currently" << QDateTime::currentDateTime(); 27 | } catch(TestExc &e) { 28 | qDebug() << "CAUGHT:" << e.what(); 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /qpm.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": { 3 | "email": "skycoder42.de@gmx.de", 4 | "name": "Skycoder" 5 | }, 6 | "dependencies": [ 7 | ], 8 | "description": "A wrapper around QDebug that allows you to create exception messages like you would write to QDebug", 9 | "license": "BSD_3_CLAUSE", 10 | "name": "de.skycoder42.qexceptionstream", 11 | "pri_filename": "qexceptionstream.pri", 12 | "repository": { 13 | "type": "GIT", 14 | "url": "https://github.com/Skycoder42/QExceptionStream.git" 15 | }, 16 | "version": { 17 | "fingerprint": "", 18 | "label": "1.0.0", 19 | "revision": "" 20 | }, 21 | "webpage": "https://github.com/Skycoder42/QExceptionStream" 22 | } 23 | -------------------------------------------------------------------------------- /qpmx.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": [ 3 | ], 4 | "license": { 5 | "file": "LICENSE", 6 | "name": "BSD_3_CLAUSE" 7 | }, 8 | "prcFile": "qexceptionstream.pri", 9 | "priFile": "qexceptionstream.pri", 10 | "priIncludes": [ 11 | ], 12 | "publishers": { 13 | "qpm": { 14 | "author": { 15 | "email": "skycoder42.de@gmx.de", 16 | "name": "Skycoder" 17 | }, 18 | "description": "A wrapper around QDebug that allows you to create exception messages like you would write to QDebug", 19 | "name": "de.skycoder42.qexceptionstream", 20 | "repository": { 21 | "type": "GIT", 22 | "url": "https://github.com/Skycoder42/QExceptionStream.git" 23 | }, 24 | "webpage": "https://github.com/Skycoder42/QExceptionStream" 25 | } 26 | }, 27 | "qbsFile": "", 28 | "source": false 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2018, Felix Barz 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /qexceptionstream.h: -------------------------------------------------------------------------------- 1 | #ifndef QEXCEPTIONSTREAM_H 2 | #define QEXCEPTIONSTREAM_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | template 10 | class QExceptionStream : public QDebug 11 | { 12 | static_assert(std::is_base_of::value, "TException must implement std::exception"); 13 | Q_DISABLE_COPY(QExceptionStream) 14 | 15 | public: 16 | QExceptionStream(); 17 | ~QExceptionStream() noexcept(false); 18 | 19 | QExceptionStream &allowThrow(); 20 | QExceptionStream &disallowThrow(); 21 | 22 | private: 23 | QString _msg; 24 | bool _canThrow = true; 25 | }; 26 | 27 | #define qThrow(type) QExceptionStream{} 28 | 29 | 30 | 31 | template 32 | QExceptionStream::QExceptionStream() : 33 | QDebug{QtDebugMsg} 34 | { 35 | QDebug tmpDbg{&_msg}; 36 | QDebug::swap(tmpDbg); //because of initialization order 37 | } 38 | 39 | template 40 | QExceptionStream::~QExceptionStream() noexcept(false) 41 | { 42 | { 43 | QDebug tmpDbg{QtDebugMsg}; 44 | QDebug::swap(tmpDbg); // force flushing by "destroying" the internal debug early 45 | } 46 | if(_canThrow) 47 | throw TException{std::move(_msg)}; 48 | } 49 | 50 | template 51 | QExceptionStream &QExceptionStream::allowThrow() 52 | { 53 | _canThrow = true; 54 | return *this; 55 | } 56 | 57 | template 58 | QExceptionStream &QExceptionStream::disallowThrow() 59 | { 60 | _canThrow = false; 61 | return *this; 62 | } 63 | 64 | 65 | #endif // QEXCEPTIONSTREAM_H 66 | --------------------------------------------------------------------------------