| 1 |
/* |
| 2 |
Copyright (c) 2017, Michael Kazhdan |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above copyright notice, this list of |
| 9 |
conditions and the following disclaimer. Redistributions in binary form must reproduce |
| 10 |
the above copyright notice, this list of conditions and the following disclaimer |
| 11 |
in the documentation and/or other materials provided with the distribution. |
| 12 |
|
| 13 |
Neither the name of the Johns Hopkins University nor the names of its contributors |
| 14 |
may be used to endorse or promote products derived from this software without specific |
| 15 |
prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 18 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 20 |
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 22 |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 |
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 |
DAMAGE. |
| 27 |
*/ |
| 28 |
#ifndef MY_EXCEPTIONS_INCLUDED |
| 29 |
#define MY_EXCEPTIONS_INCLUDED |
| 30 |
|
| 31 |
///////////////////////////////////// |
| 32 |
// Exception, Warnings, and Errors // |
| 33 |
///////////////////////////////////// |
| 34 |
#include <exception> |
| 35 |
#include <string> |
| 36 |
#include <iostream> |
| 37 |
#include <sstream> |
| 38 |
#include <algorithm> |
| 39 |
|
| 40 |
namespace PoissonRecon |
| 41 |
{ |
| 42 |
template< typename ... Arguments > void _AddToMessageStream( std::stringstream &stream , Arguments ... arguments ); |
| 43 |
inline void _AddToMessageStream( std::stringstream &stream ){ return; } |
| 44 |
template< typename Argument , typename ... Arguments > void _AddToMessageStream( std::stringstream &stream , Argument argument , Arguments ... arguments ) |
| 45 |
{ |
| 46 |
stream << argument; |
| 47 |
_AddToMessageStream( stream , arguments ... ); |
| 48 |
} |
| 49 |
|
| 50 |
template< typename ... Arguments > |
| 51 |
std::string MakeMessageString( std::string header , std::string fileName , int line , std::string functionName , Arguments ... arguments ) |
| 52 |
{ |
| 53 |
size_t headerSize = header.size(); |
| 54 |
std::stringstream stream; |
| 55 |
|
| 56 |
// The first line is the header, the file name , and the line number |
| 57 |
stream << header << " " << fileName << " (Line " << line << ")" << std::endl; |
| 58 |
|
| 59 |
// Inset the second line by the size of the header and write the function name |
| 60 |
for( size_t i=0 ; i<=headerSize ; i++ ) stream << " "; |
| 61 |
stream << functionName << std::endl; |
| 62 |
|
| 63 |
// Inset the third line by the size of the header and write the rest |
| 64 |
for( size_t i=0 ; i<=headerSize ; i++ ) stream << " "; |
| 65 |
_AddToMessageStream( stream , arguments ... ); |
| 66 |
|
| 67 |
return stream.str(); |
| 68 |
} |
| 69 |
struct Exception : public std::exception |
| 70 |
{ |
| 71 |
const char *what( void ) const noexcept { return _message.c_str(); } |
| 72 |
template< typename ... Args > |
| 73 |
Exception( const char *fileName , int line , const char *functionName , const char *format , Args ... args ) |
| 74 |
{ |
| 75 |
_message = MakeMessageString( "[EXCEPTION]" , fileName , line , functionName , format , args ... ); |
| 76 |
} |
| 77 |
private: |
| 78 |
std::string _message; |
| 79 |
}; |
| 80 |
|
| 81 |
template< typename ... Args > void Throw( const char *fileName , int line , const char *functionName , const char *format , Args ... args ){ throw Exception( fileName , line , functionName , format , args ... ); } |
| 82 |
template< typename ... Args > |
| 83 |
void Warn( const char *fileName , int line , const char *functionName , const char *format , Args ... args ) |
| 84 |
{ |
| 85 |
std::cerr << MakeMessageString( "[WARNING]" , fileName , line , functionName , format , args ... ) << std::endl; |
| 86 |
} |
| 87 |
template< typename ... Args > |
| 88 |
void ErrorOut( const char *fileName , int line , const char *functionName , const char *format , Args ... args ) |
| 89 |
{ |
| 90 |
std::cerr << MakeMessageString( "[ERROR]" , fileName , line , functionName , format , args ... ) << std::endl; |
| 91 |
exit( EXIT_FAILURE ); |
| 92 |
} |
| 93 |
} |
| 94 |
#ifndef MK_WARN |
| 95 |
#define MK_WARN( ... ) Warn( __FILE__ , __LINE__ , __FUNCTION__ , __VA_ARGS__ ) |
| 96 |
#endif // MK_WARN |
| 97 |
#ifndef MK_WARN_ONCE |
| 98 |
#define MK_WARN_ONCE( ... ) { static bool firstTime = true ; if( firstTime ) Warn( __FILE__ , __LINE__ , __FUNCTION__ , __VA_ARGS__ ) ; firstTime = false; } |
| 99 |
#endif // MK_WARN_ONCE |
| 100 |
#ifndef MK_THROW |
| 101 |
#define MK_THROW( ... ) Throw( __FILE__ , __LINE__ , __FUNCTION__ , __VA_ARGS__ ) |
| 102 |
#endif // MK_THROW |
| 103 |
#ifndef MK_ERROR_OUT |
| 104 |
#define MK_ERROR_OUT( ... ) ErrorOut( __FILE__ , __LINE__ , __FUNCTION__ , __VA_ARGS__ ) |
| 105 |
#endif // MK_ERROR_OUT |
| 106 |
|
| 107 |
#endif // MY_EXCEPTIONS_INCLUDED |