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, 14 years ago

added windows implementation of SignalHandler.

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
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 */
28
29/**
30    @file
31    @ingroup Util
32    @brief Declaration of the SignalHandler class.
33*/
34
35#ifndef _SignalHandler_H__
36#define _SignalHandler_H__
37
38#include "UtilPrereqs.h"
39
40#include <cassert>
41#include <string>
42#include "Singleton.h"
43#include "SpecialConfig.h"
44
45namespace orxonox
46{
47    typedef int (*SignalCallback)( void * someData );
48}
49
50#if defined(ORXONOX_PLATFORM_LINUX)
51
52#include <list>
53#include <signal.h>
54
55namespace orxonox
56{
57    struct SignalRec
58    {
59        int signal;
60        sig_t handler;
61    };
62
63    struct SignalCallbackRec
64    {
65        SignalCallback cb;
66        void * someData;
67    };
68
69
70    typedef std::list<SignalRec> SignalRecList;
71    typedef std::list<SignalCallbackRec> SignalCallbackList;
72
73    /// The SignalHandler is used to catch signals like SIGSEGV and write a backtrace to the logfile.
74    class SignalHandler : public Singleton<SignalHandler>
75    {
76        friend class Singleton<SignalHandler>;
77    public:
78        SignalHandler()  { }
79        ~SignalHandler() { }
80
81        void registerCallback( SignalCallback cb, void * someData );
82
83        void doCatch( const std::string & appName, const std::string & filename );
84        void dontCatch();
85
86    private:
87        static void sigHandler( int sig );
88
89        void catchSignal( int sig );
90        SignalRecList sigRecList;
91
92        SignalCallbackList callbackList;
93
94        static SignalHandler* singletonPtr_s;
95
96        std::string appName;
97        std::string filename;
98    };
99}
100
101#elif defined(ORXONOX_PLATFORM_WINDOWS) && defined(DBGHELP_FOUND)
102
103#include <windows.h>
104
105namespace orxonox
106{
107    /// The SignalHandler is used to catch unhandled exceptions like access violations and write a backtrace to the logfile.
108    class _UtilExport SignalHandler : public Singleton<SignalHandler>
109    {
110        friend class Singleton<SignalHandler>;
111    public:
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:
146        void doCatch( const std::string & appName, const std::string & filename ) {}
147
148    private:
149        static SignalHandler* singletonPtr_s;
150    };
151}
152
153#endif
154
155#endif /* _SignalHandler_H__ */
Note: See TracBrowser for help on using the repository browser.