Important Update:
- Added SignalCore for handling system signals - Implemented LogMessages (only output to stdout, later to logfile) - Updated #defines for all header files
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
PROJECT(lib_redAcore)
|
||||
|
||||
ADD_LIBRARY(redAcore LogCore.cpp TimingCore.cpp PortCore.cpp SocketCore.cpp SelectCore.cpp)
|
||||
ADD_LIBRARY(redAcore LogCore.cpp SignalCore.cpp TimingCore.cpp PortCore.cpp SocketCore.cpp SelectCore.cpp)
|
||||
|
||||
24
LogCore.cpp
24
LogCore.cpp
@@ -13,10 +13,31 @@
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Global vars
|
||||
char ProcessName[20] = "Connect";
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool LogMessage( const char * Heading, const char *Message )
|
||||
{
|
||||
// Validate values
|
||||
if (!Message)
|
||||
return false;
|
||||
|
||||
// Show normal output
|
||||
if (!Heading) {
|
||||
printf( "%s\n", Message );
|
||||
}
|
||||
else {
|
||||
printf( "%s: %s\n", Heading, Message );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool ShowOutput( const char * Heading, const short Show, const char * Buffer, int Len )
|
||||
{
|
||||
// Validate values
|
||||
@@ -60,4 +81,3 @@ bool ShowOutput( const char * Heading, const short Show, const char * Buffer, in
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
* Author: wentzelc
|
||||
*/
|
||||
|
||||
#ifndef LOGCORE_H_
|
||||
#define LOGCORE_H_
|
||||
#ifndef REDACORE_LOGCORE_H_
|
||||
#define REDACORE_LOGCORE_H_
|
||||
|
||||
// redA Libraries
|
||||
/* none */
|
||||
@@ -25,8 +25,10 @@ const short
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool LogMessage( const char * Heading, const char * Message );
|
||||
|
||||
bool ShowOutput( const char * Heading, const short Show, const char * Buffer, int Len = -1 );
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#endif /* LOGCORE_H_ */
|
||||
#endif /* REDACORE_OGCORE_H_ */
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
* Author: wentzelc
|
||||
*/
|
||||
|
||||
#ifndef PORTCORE_H_
|
||||
#define PORTCORE_H_
|
||||
#ifndef REDACORE_PORTCORE_H_
|
||||
#define REDACORE_PORTCORE_H_
|
||||
|
||||
// redA Libraries
|
||||
/* none */
|
||||
@@ -60,4 +60,4 @@ public:
|
||||
};
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#endif /* PORTCORE_H_ */
|
||||
#endif /* REDACORE_PORTCORE_H_ */
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
* Author: wentzelc
|
||||
*/
|
||||
|
||||
#ifndef SELECTCORE_H_
|
||||
#define SELECTCORE_H_
|
||||
#ifndef REDACORE_SELECTCORE_H_
|
||||
#define REDACORE_SELECTCORE_H_
|
||||
|
||||
// redA Libraries
|
||||
/* none */
|
||||
@@ -25,4 +25,4 @@ bool SelectCheck( int FD, bool &Read, bool &Write );
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#endif /* SELECTCORE_H_ */
|
||||
#endif /* REDACORE_SELECTCORE_H_ */
|
||||
|
||||
129
SignalCore.cpp
Normal file
129
SignalCore.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* SignalCore.cpp
|
||||
*
|
||||
* Created on: 17 May 2016
|
||||
* Author: wentzelc
|
||||
*/
|
||||
|
||||
// redA Libraries
|
||||
#include "SignalCore.h"
|
||||
#include "LogCore.h"
|
||||
|
||||
// Standard C/C++ Libraries
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Global vars
|
||||
extern char ProcessName[];
|
||||
extern bool Terminate;
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Configure Signal handling
|
||||
void ConfigureSignalHandlers()
|
||||
{
|
||||
struct sigaction TermAct;
|
||||
struct sigaction AbortAct;
|
||||
|
||||
// Signals to be ignored
|
||||
signal( SIGCHLD, SIG_IGN );
|
||||
|
||||
// Signals for normal termination
|
||||
TermAct.sa_handler = SignalTerminate;
|
||||
sigemptyset( &TermAct.sa_mask );
|
||||
TermAct.sa_flags = SA_RESTART;
|
||||
|
||||
sigaction( SIGHUP, &TermAct, 0 );
|
||||
sigaction( SIGINT, &TermAct, 0 );
|
||||
sigaction( SIGQUIT, &TermAct, 0 );
|
||||
sigaction( SIGTERM, &TermAct, 0 );
|
||||
sigaction( SIGTSTP, &TermAct, 0 );
|
||||
|
||||
// Signals for immediate termination
|
||||
AbortAct.sa_handler = SignalAbort;
|
||||
sigemptyset( &AbortAct.sa_mask );
|
||||
AbortAct.sa_flags = 0;
|
||||
|
||||
sigaction( SIGABRT, &AbortAct, 0 );
|
||||
sigaction( SIGFPE, &AbortAct, 0 );
|
||||
sigaction( SIGILL, &AbortAct, 0 );
|
||||
sigaction( SIGPIPE, &AbortAct, 0 );
|
||||
sigaction( SIGSEGV, &AbortAct, 0 );
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Signal handler to terminate program the standard way
|
||||
void SignalTerminate( int sig )
|
||||
{
|
||||
char SigName[10];
|
||||
char LogMsg[200];
|
||||
|
||||
// Determine signal name
|
||||
switch (sig)
|
||||
{
|
||||
case SIGHUP : strcpy( SigName, "SIGHUP " ); break;
|
||||
case SIGINT : strcpy( SigName, "SIGINT " ); break;
|
||||
case SIGQUIT : strcpy( SigName, "SIGQUIT" ); break;
|
||||
case SIGTERM : strcpy( SigName, "SIGTERM" ); break;
|
||||
case SIGTSTP : strcpy( SigName, "SIGTSTP" ); break;
|
||||
default : strcpy( SigName, "UNKNOWN" ); break;
|
||||
}
|
||||
|
||||
// Show signal received if not already terminated
|
||||
std::cerr << "\r\n" << ProcessName << ": *****************************************\n";
|
||||
|
||||
// Create Log Entry
|
||||
sprintf( LogMsg, " ** Terminate signal received (%s) **", SigName );
|
||||
LogMessage( ProcessName, LogMsg );//, ltDefault, -1 );
|
||||
|
||||
std::cerr << ProcessName << ": ** Terminating normally... **\n";
|
||||
std::cerr << ProcessName << ": *****************************************\n\n";
|
||||
|
||||
// Allow process to terminate normally
|
||||
Terminate = true;
|
||||
|
||||
return;
|
||||
}//---------------------------------------------------------------------------
|
||||
|
||||
// Signal Handler to terminate program immediately
|
||||
void SignalAbort( int sig )
|
||||
{
|
||||
char SigName[10];
|
||||
char LogMsg[200];
|
||||
|
||||
// Determine Signal name
|
||||
switch (sig)
|
||||
{
|
||||
case SIGABRT : strcpy( SigName, "SIGABRT" ); break;
|
||||
case SIGFPE : strcpy( SigName, "SIGFPE " ); break;
|
||||
case SIGILL : strcpy( SigName, "SIGILL " ); break;
|
||||
case SIGPIPE : strcpy( SigName, "SIGPIPE" ); break;
|
||||
case SIGSEGV : strcpy( SigName, "SIGSEGV" ); break;
|
||||
default : strcpy( SigName, "UNKNOWN" ); break;
|
||||
}
|
||||
|
||||
// Show signal received
|
||||
std::cerr << "\n" << ProcessName << ": *************************************\n";
|
||||
|
||||
// Create Log Entry - but don't post
|
||||
sprintf( LogMsg, " ** Abort signal received (%s) **", SigName );
|
||||
LogMessage( ProcessName, LogMsg );//, ltDefault, -1 );
|
||||
|
||||
std::cerr << ProcessName << ": ** Terminating immediately! **\n";
|
||||
std::cerr << ProcessName << ": *************************************\n\n";
|
||||
|
||||
// Try to stop communications
|
||||
// if (SocketManager)
|
||||
// delete SocketManager;
|
||||
|
||||
// Terminate with signal
|
||||
exit( sig );
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
23
SignalCore.h
Normal file
23
SignalCore.h
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* SignalCore.h
|
||||
*
|
||||
* Created on: 17 May 2016
|
||||
* Author: wentzelc
|
||||
*/
|
||||
|
||||
#ifndef REDACORE_SIGNALCORE_H_
|
||||
#define REDACORE_SIGNALCORE_H_
|
||||
|
||||
// redA Libraries
|
||||
/* none */
|
||||
|
||||
// Standard C/C++ Libraries
|
||||
/* none */
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
void ConfigureSignalHandlers();
|
||||
void SignalTerminate( int sig );
|
||||
void SignalAbort( int sig );
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
#endif /* REDACORE_SIGNALCORE_H_ */
|
||||
@@ -5,8 +5,8 @@
|
||||
* Author: wentzelc
|
||||
*/
|
||||
|
||||
#ifndef SOCKETCORE_H_
|
||||
#define SOCKETCORE_H_
|
||||
#ifndef REDACORE_SOCKETCORE_H_
|
||||
#define REDACORE_SOCKETCORE_H_
|
||||
|
||||
// redA Libraries
|
||||
/* none */
|
||||
@@ -39,4 +39,4 @@ bool MaintainSocket( int PortHandle );
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#endif /* SOCKETCORE_H_ */
|
||||
#endif /* REDACORE_SOCKETCORE_H_ */
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
* Author: wentzelc
|
||||
*/
|
||||
|
||||
#ifndef TIMINGCORE_H_
|
||||
#define TIMINGCORE_H_
|
||||
#ifndef REDACORE_TIMINGCORE_H_
|
||||
#define REDACORE_TIMINGCORE_H_
|
||||
|
||||
// redA Libraries
|
||||
/* none */
|
||||
@@ -22,4 +22,4 @@ long TimePassed( timeval StartTime );
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#endif /* TIMINGCORE_H_ */
|
||||
#endif /* REDACORE_TIMINGCORE_H_ */
|
||||
|
||||
Reference in New Issue
Block a user