1 /************************************************************************
2 ************************************************************************
3 FAUST compiler
4 Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 ************************************************************************
18 ************************************************************************/
19 
20 #ifndef _FAUST_EXCEPTION_
21 #define _FAUST_EXCEPTION_
22 
23 #include <iostream>
24 #include <sstream>
25 #include <stdexcept>
26 #ifndef WIN32
27 #include <unistd.h>
28 #else
29 //#include <io.h>
30 #endif
31 
32 #if !defined(EMCC) && !defined(WIN32) && !defined(ANDROID) && !defined(ALPINE)
33 #include <execinfo.h>
34 #endif
35 
36 class faustexception : public std::runtime_error {
37    public:
38 #ifdef EMCC
39     static const char* gJSExceptionMsg;
40 
faustexception(const std::string & msg="")41     faustexception(const std::string& msg = "") : std::runtime_error(msg) { gJSExceptionMsg = strdup(msg.c_str()); }
faustexception(char * msg)42     faustexception(char* msg) : std::runtime_error(msg) { gJSExceptionMsg = strdup(msg); }
faustexception(const char * msg)43     faustexception(const char* msg) : std::runtime_error(msg) { gJSExceptionMsg = strdup(msg); }
44 #else
45     faustexception(const std::string& msg = "") : std::runtime_error(msg) {}
46     faustexception(char* msg) : std::runtime_error(msg) {}
47     faustexception(const char* msg) : std::runtime_error(msg) {}
48 #endif
49 
Message()50     std::string Message() { return what(); }
51 
PrintMessage()52     void PrintMessage() { std::cerr << what(); }
53 };
54 
stacktrace(std::stringstream & str,int val)55 inline void stacktrace(std::stringstream& str, int val)
56 {
57 #if !defined(EMCC) && !defined(WIN32) && !defined(ANDROID) && !defined(ALPINE)
58     void*  callstack[val];
59     int    frames = backtrace(callstack, val);
60     char** strs   = backtrace_symbols(callstack, frames);
61     str << "====== stack trace start ======\n";
62     for (int i = 0; i < frames; ++i) {
63         str << strs[i] << "\n";
64     }
65     str << "====== stack trace stop ======\n";
66     free(strs);
67 #endif
68 }
69 
70 #define faustassert(cond) faustassertaux((cond), __FILE__, __LINE__)
71 void faustassertaux(bool cond, const std::string& file, int line);
72 
73 #endif
74