libH/SignalHandler.cpp

Go to the documentation of this file.
00001 
00012 /*
00013   
00014   Copyright (c) 2007, Tim Burrell
00015   Licensed under the Apache License, Version 2.0 (the "License");
00016   you may not use this file except in compliance with the License.
00017   You may obtain a copy of the License at 
00018 
00019         http://www.apache.org/licenses/LICENSE-2.0
00020 
00021   Unless required by applicable law or agreed to in writing, software
00022   distributed under the License is distributed on an "AS IS" BASIS,
00023   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00024   See the License for the specific language governing permissions and 
00025   limitations under the License. 
00026   
00027 */
00028 
00029 #include "SignalHandler.hpp"
00030 #include "Debug.hpp"
00031 #include "Exception.hpp"
00032 #include <signal.h>
00033 #include <boost/bind.hpp>
00034 
00035 using namespace H;
00036 using namespace boost;
00037 using namespace std;
00038 
00040 // Defines / Type Defs
00042 
00043 SignalHandler * SignalHandler::mpThis = NULL;
00044 
00046 // Construction
00048 
00052 SignalHandler::SignalHandler() {
00053         if (mpThis)
00054                 throw H::Exception("There can only be one SignalHandler!", __FILE__, __FUNCTION__, __LINE__);
00055         mpThis = this;
00056 }
00057 
00061 SignalHandler::~SignalHandler() {
00062         if (mpThis)
00063                 mpThis = NULL;
00064 }
00065 
00067 // Class Body
00069 
00073 void SignalHandler::initSignals() {
00074         signal(SIGINT,   signalHandler);
00075         signal(SIGHUP,   signalHandler);
00076         signal(SIGQUIT,  signalHandler);
00077         signal(SIGKILL,  signalHandler);
00078         signal(SIGTERM,  signalHandler);
00079         signal(SIGSTOP,  signalHandler);
00080 }
00081 
00085 void SignalHandler::onSignalSegv() {
00086         // override me
00087         cerr << "Unhandled SEGV Signal" << endl;
00088 }
00089 
00093 void SignalHandler::onSignalInt() {
00094         // override me
00095         cerr << "Unhandled INT Signal" << endl;
00096 }
00097 
00101 void SignalHandler::onSignalHup() {
00102         // override me
00103         cerr << "Unhandled HUP Signal" << endl;
00104 }
00105 
00109 void SignalHandler::onSignalQuit() {
00110         // override me
00111         cerr << "Unhandled QUIT Signal" << endl;
00112 }
00113 
00117 void SignalHandler::onSignalKill() {
00118         // override me
00119         cerr << "Unhandled KILL Signal" << endl;
00120 }
00121 
00125 void SignalHandler::onSignalTerm() {
00126         // override me
00127         cerr << "Unhandled TERM Signal" << endl;
00128 }
00129 
00133 void SignalHandler::onSignalStop() {
00134         // override me
00135         cerr << "Unhandled STOP Signal" << endl;
00136 }
00137 
00141 void SignalHandler::onSignalUnknown(int Signal) {
00142         // override me
00143         cerr << "Unhandled Unknown Signal" << endl;
00144 }
00145 
00149 void SignalHandler::signalHandler(int Signal) {
00150         if (!mpThis) {
00151                 cerr << "Unhandled Signal Caught!" << endl;
00152                 return;
00153         }
00154         
00155         switch (Signal) {
00156         case SIGSEGV:
00157                 mpThis->onSignalSegv();
00158                 break;          
00159         case SIGINT:
00160                 mpThis->onSignalInt();
00161                 break;
00162         case SIGHUP:
00163                 mpThis->onSignalHup();
00164                 break;
00165         case SIGQUIT:
00166                 mpThis->onSignalQuit();
00167                 break;
00168         case SIGKILL:
00169                 mpThis->onSignalKill();
00170                 break;
00171         case SIGTERM:
00172                 mpThis->onSignalTerm();
00173                 break;
00174         case SIGSTOP:
00175                 mpThis->onSignalStop();
00176                 break;          
00177         default:
00178                 mpThis->onSignalUnknown(Signal);
00179         }
00180 
00181         signal(Signal, &signalHandler); 
00182 }

Generated on Wed Nov 7 10:04:16 2007 for gizmod by  doxygen 1.5.3