Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/util/SignalHandler.h @ 7449

Last change on this file since 7449 was 7449, checked in by landauf, 15 years ago

added windows implementation of SignalHandler.

  • Property svn:eol-style set to native
File size: 4.0 KB
RevLine 
[1505]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Christoph Renner
24 *   Co-authors:
25 *      ...
26 *
27 */
[682]28
[1505]29/**
[2030]30    @file
[7401]31    @ingroup Util
[1755]32    @brief Declaration of the SignalHandler class.
[1505]33*/
34
[497]35#ifndef _SignalHandler_H__
36#define _SignalHandler_H__
37
[2030]38#include "UtilPrereqs.h"
[1062]39
[2662]40#include <cassert>
[497]41#include <string>
[3370]42#include "Singleton.h"
[7449]43#include "SpecialConfig.h"
[497]44
[2171]45namespace orxonox
46{
47    typedef int (*SignalCallback)( void * someData );
48}
[497]49
[7449]50#if defined(ORXONOX_PLATFORM_LINUX)
51
52#include <list>
[497]53#include <signal.h>
54
[2171]55namespace orxonox
[497]56{
[2171]57    struct SignalRec
58    {
59        int signal;
60        sig_t handler;
61    };
[497]62
[2171]63    struct SignalCallbackRec
64    {
65        SignalCallback cb;
66        void * someData;
67    };
[497]68
69
[2171]70    typedef std::list<SignalRec> SignalRecList;
71    typedef std::list<SignalCallbackRec> SignalCallbackList;
[497]72
[7401]73    /// The SignalHandler is used to catch signals like SIGSEGV and write a backtrace to the logfile.
[3370]74    class SignalHandler : public Singleton<SignalHandler>
[2171]75    {
[3370]76        friend class Singleton<SignalHandler>;
[2171]77    public:
[3370]78        SignalHandler()  { }
79        ~SignalHandler() { }
[497]80
[2171]81        void registerCallback( SignalCallback cb, void * someData );
[497]82
[2171]83        void doCatch( const std::string & appName, const std::string & filename );
84        void dontCatch();
[497]85
[2171]86    private:
87        static void sigHandler( int sig );
[497]88
[2171]89        void catchSignal( int sig );
90        SignalRecList sigRecList;
[497]91
[2171]92        SignalCallbackList callbackList;
[497]93
[3370]94        static SignalHandler* singletonPtr_s;
[497]95
[2171]96        std::string appName;
97        std::string filename;
98    };
99}
[497]100
[7449]101#elif defined(ORXONOX_PLATFORM_WINDOWS) && defined(DBGHELP_FOUND)
[682]102
[7449]103#include <windows.h>
104
[2171]105namespace orxonox
[497]106{
[7449]107    /// The SignalHandler is used to catch unhandled exceptions like access violations and write a backtrace to the logfile.
[3370]108    class _UtilExport SignalHandler : public Singleton<SignalHandler>
[2171]109    {
[3370]110        friend class Singleton<SignalHandler>;
[2171]111    public:
[7449]112        SignalHandler();
113        ~SignalHandler();
114
115        void doCatch( const std::string & appName, const std::string & filename );
116
117    private:
118        static LONG WINAPI exceptionFilter(PEXCEPTION_POINTERS pExceptionInfo);
119
120        static std::string getStackTrace(PEXCEPTION_POINTERS pExceptionInfo = NULL);
121        static std::string getExceptionType(PEXCEPTION_POINTERS pExceptionInfo);
122        static std::string getModuleName(const std::string& path);
123        static DWORD getModuleBase(DWORD dwAddress);
124
125        template <typename T>
126        static std::string pointerToString(T pointer);
127        template <typename T>
128        static std::string pointerToString(T* pointer);
129
130        static SignalHandler* singletonPtr_s;
131
132        std::string filename_;
133        LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter_;
134    };
135}
136
137#else
138
139namespace orxonox
140{
141    /// The SignalHandler is used to catch signals like SIGSEGV and write a backtrace to the logfile. Not implemented on systems except Linux and Windows.
142    class _UtilExport SignalHandler : public Singleton<SignalHandler>
143    {
144        friend class Singleton<SignalHandler>;
145    public:
[2662]146        void doCatch( const std::string & appName, const std::string & filename ) {}
[497]147
[2171]148    private:
[3370]149        static SignalHandler* singletonPtr_s;
[2171]150    };
151}
[497]152
[7449]153#endif
[1607]154
[497]155#endif /* _SignalHandler_H__ */
Note: See TracBrowser for help on using the repository browser.