From 741957914073eb9f4a1fa0cd5ae90cc17078a623 Mon Sep 17 00:00:00 2001 From: Tycho Date: Thu, 23 Jan 2014 10:25:56 -0800 Subject: Switched cEvent to use strerror_r for error messages --- src/OSSupport/Event.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/OSSupport/Event.cpp b/src/OSSupport/Event.cpp index cbacbba17..fe1128dc9 100644 --- a/src/OSSupport/Event.cpp +++ b/src/OSSupport/Event.cpp @@ -7,7 +7,9 @@ #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "Event.h" - +#if not defined(_WIN32) +#include +#endif @@ -35,14 +37,18 @@ cEvent::cEvent(void) m_Event = sem_open(EventName.c_str(), O_CREAT, 777, 0 ); if (m_Event == SEM_FAILED) { - LOGERROR("cEvent: Cannot create event, errno = %i. Aborting server.", errno); + char buffer[1024]; + strerror_r(errno,buffer,1024); + LOGERROR("cEvent: Cannot create event, err = %s. Aborting server.", buffer); abort(); } // Unlink the semaphore immediately - it will continue to function but will not pollute the namespace // We don't store the name, so can't call this in the destructor if (sem_unlink(EventName.c_str()) != 0) { - LOGWARN("ERROR: Could not unlink cEvent. (%i)", errno); + char buffer[1024]; + strerror_r(errno,buffer,1024); + LOGWARN("ERROR: Could not unlink cEvent. (%s)", buffer); } } #endif // *nix @@ -61,7 +67,9 @@ cEvent::~cEvent() { if (sem_close(m_Event) != 0) { - LOGERROR("ERROR: Could not close cEvent. (%i)", errno); + char buffer[1024]; + strerror_r(errno,buffer,1024); + LOGERROR("ERROR: Could not close cEvent. (%s)", buffer); } } else @@ -88,7 +96,9 @@ void cEvent::Wait(void) int res = sem_wait(m_Event); if (res != 0 ) { - LOGWARN("cEvent: waiting for the event failed: %i, errno = %i. Continuing, but server may be unstable.", res, errno); + char buffer[1024]; + strerror_r(errno,buffer,1024); + LOGWARN("cEvent: waiting for the event failed: %i, err = %s. Continuing, but server may be unstable.", res, buffer); } #endif } @@ -108,7 +118,9 @@ void cEvent::Set(void) int res = sem_post(m_Event); if (res != 0) { - LOGWARN("cEvent: Could not set cEvent: %i, errno = %d", res, errno); + char buffer[1024]; + strerror_r(errno,buffer,1024); + LOGWARN("cEvent: Could not set cEvent: %i, err = %s", res, buffer); } #endif } -- cgit v1.2.3 From 59b8205f02eb7bf2954acba56fd63f485a30ece5 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 25 Jan 2014 05:51:03 -0800 Subject: Extracted cSocket::GetErrorString into GetOSErrorString --- src/OSSupport/BlockingTCPLink.cpp | 4 +-- src/OSSupport/Errors.cpp | 52 +++++++++++++++++++++++++++++++++++++++ src/OSSupport/Errors.h | 3 +++ src/OSSupport/Socket.cpp | 52 --------------------------------------- src/OSSupport/Socket.h | 7 +++--- src/OSSupport/SocketThreads.cpp | 3 ++- 6 files changed, 62 insertions(+), 59 deletions(-) create mode 100644 src/OSSupport/Errors.cpp create mode 100644 src/OSSupport/Errors.h diff --git a/src/OSSupport/BlockingTCPLink.cpp b/src/OSSupport/BlockingTCPLink.cpp index 08aec0c65..af50eda5d 100644 --- a/src/OSSupport/BlockingTCPLink.cpp +++ b/src/OSSupport/BlockingTCPLink.cpp @@ -2,7 +2,7 @@ #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "BlockingTCPLink.h" - +#include "Errors.h" @@ -75,7 +75,7 @@ bool cBlockingTCPLink::Connect(const char * iAddress, unsigned int iPort) server.sin_port = htons( (unsigned short)iPort); if (connect(m_Socket, (struct sockaddr *)&server, sizeof(server))) { - LOGWARN("cTCPLink: Connection to \"%s:%d\" failed (%s)", iAddress, iPort, cSocket::GetErrorString( cSocket::GetLastError() ).c_str() ); + LOGWARN("cTCPLink: Connection to \"%s:%d\" failed (%s)", iAddress, iPort,GetOSErrorString( cSocket::GetLastError() ).c_str() ); CloseSocket(); return false; } diff --git a/src/OSSupport/Errors.cpp b/src/OSSupport/Errors.cpp new file mode 100644 index 000000000..b2e8880bb --- /dev/null +++ b/src/OSSupport/Errors.cpp @@ -0,0 +1,52 @@ + +#include "Globals.h" + +#include "Errors.h" + +AString GetOSErrorString( int a_ErrNo ) +{ + char buffer[ 1024 ]; + AString Out; + + #ifdef _WIN32 + + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, a_ErrNo, 0, buffer, ARRAYCOUNT(buffer), NULL); + Printf(Out, "%d: %s", a_ErrNo, buffer); + if (!Out.empty() && (Out[Out.length() - 1] == '\n')) + { + Out.erase(Out.length() - 2); + } + return Out; + + #else // _WIN32 + + // According to http://linux.die.net/man/3/strerror_r there are two versions of strerror_r(): + + #if ( _GNU_SOURCE ) && !defined(ANDROID_NDK) // GNU version of strerror_r() + + char * res = strerror_r( errno, buffer, ARRAYCOUNT(buffer) ); + if( res != NULL ) + { + Printf(Out, "%d: %s", a_ErrNo, res); + return Out; + } + + #else // XSI version of strerror_r(): + + int res = strerror_r( errno, buffer, ARRAYCOUNT(buffer) ); + if( res == 0 ) + { + Printf(Out, "%d: %s", a_ErrNo, buffer); + return Out; + } + + #endif // strerror_r() version + + else + { + Printf(Out, "Error %d while getting error string for error #%d!", errno, a_ErrNo); + return Out; + } + + #endif // else _WIN32 +} diff --git a/src/OSSupport/Errors.h b/src/OSSupport/Errors.h new file mode 100644 index 000000000..72ba98862 --- /dev/null +++ b/src/OSSupport/Errors.h @@ -0,0 +1,3 @@ + +AString GetOSErrorString(int a_ErrNo); + diff --git a/src/OSSupport/Socket.cpp b/src/OSSupport/Socket.cpp index d80c9bb3d..4226a7535 100644 --- a/src/OSSupport/Socket.cpp +++ b/src/OSSupport/Socket.cpp @@ -105,58 +105,6 @@ void cSocket::ShutdownReadWrite(void) - -AString cSocket::GetErrorString( int a_ErrNo ) -{ - char buffer[ 1024 ]; - AString Out; - - #ifdef _WIN32 - - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, a_ErrNo, 0, buffer, ARRAYCOUNT(buffer), NULL); - Printf(Out, "%d: %s", a_ErrNo, buffer); - if (!Out.empty() && (Out[Out.length() - 1] == '\n')) - { - Out.erase(Out.length() - 2); - } - return Out; - - #else // _WIN32 - - // According to http://linux.die.net/man/3/strerror_r there are two versions of strerror_r(): - - #if ( _GNU_SOURCE ) && !defined(ANDROID_NDK) // GNU version of strerror_r() - - char * res = strerror_r( errno, buffer, ARRAYCOUNT(buffer) ); - if( res != NULL ) - { - Printf(Out, "%d: %s", a_ErrNo, res); - return Out; - } - - #else // XSI version of strerror_r(): - - int res = strerror_r( errno, buffer, ARRAYCOUNT(buffer) ); - if( res == 0 ) - { - Printf(Out, "%d: %s", a_ErrNo, buffer); - return Out; - } - - #endif // strerror_r() version - - else - { - Printf(Out, "Error %d while getting error string for error #%d!", errno, a_ErrNo); - return Out; - } - - #endif // else _WIN32 -} - - - - int cSocket::GetLastError() { #ifdef _WIN32 diff --git a/src/OSSupport/Socket.h b/src/OSSupport/Socket.h index 91c9ca5fd..4ca3d61f4 100644 --- a/src/OSSupport/Socket.h +++ b/src/OSSupport/Socket.h @@ -14,7 +14,7 @@ #endif - +#include "Errors.h" class cSocket @@ -57,11 +57,10 @@ public: /// Initializes the network stack. Returns 0 on success, or another number as an error code. static int WSAStartup(void); - static AString GetErrorString(int a_ErrNo); static int GetLastError(); static AString GetLastErrorString(void) { - return GetErrorString(GetLastError()); + return GetOSErrorString(GetLastError()); } /// Creates a new socket of the specified address family @@ -115,4 +114,4 @@ public: private: xSocket m_Socket; AString m_IPString; -}; \ No newline at end of file +}; diff --git a/src/OSSupport/SocketThreads.cpp b/src/OSSupport/SocketThreads.cpp index b8069cf00..74932daf8 100644 --- a/src/OSSupport/SocketThreads.cpp +++ b/src/OSSupport/SocketThreads.cpp @@ -7,6 +7,7 @@ #include "Globals.h" #include "SocketThreads.h" +#include "Errors.h" @@ -556,7 +557,7 @@ void cSocketThreads::cSocketThread::WriteToSockets(fd_set * a_Write) if (Sent < 0) { int Err = cSocket::GetLastError(); - LOGWARNING("Error %d while writing to client \"%s\", disconnecting. \"%s\"", Err, m_Slots[i].m_Socket.GetIPString().c_str(), cSocket::GetErrorString(Err).c_str()); + LOGWARNING("Error %d while writing to client \"%s\", disconnecting. \"%s\"", Err, m_Slots[i].m_Socket.GetIPString().c_str(), GetOSErrorString(Err).c_str()); m_Slots[i].m_Socket.CloseSocket(); if (m_Slots[i].m_Client != NULL) { -- cgit v1.2.3 From 977e277094750a22284b4a738269db295af3cad3 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 25 Jan 2014 06:02:20 -0800 Subject: Switched cEvent to GetOSErrorString --- src/OSSupport/Errors.cpp | 1 + src/OSSupport/Errors.h | 2 ++ src/OSSupport/Event.cpp | 29 +++++++++++------------------ 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/OSSupport/Errors.cpp b/src/OSSupport/Errors.cpp index b2e8880bb..2e05f1df1 100644 --- a/src/OSSupport/Errors.cpp +++ b/src/OSSupport/Errors.cpp @@ -50,3 +50,4 @@ AString GetOSErrorString( int a_ErrNo ) #endif // else _WIN32 } + diff --git a/src/OSSupport/Errors.h b/src/OSSupport/Errors.h index 72ba98862..8ce9deb10 100644 --- a/src/OSSupport/Errors.h +++ b/src/OSSupport/Errors.h @@ -1,3 +1,5 @@ +#pragma once + AString GetOSErrorString(int a_ErrNo); diff --git a/src/OSSupport/Event.cpp b/src/OSSupport/Event.cpp index fe1128dc9..649a0a3cf 100644 --- a/src/OSSupport/Event.cpp +++ b/src/OSSupport/Event.cpp @@ -7,9 +7,7 @@ #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "Event.h" -#if not defined(_WIN32) -#include -#endif +#include "Errors.h" @@ -37,18 +35,16 @@ cEvent::cEvent(void) m_Event = sem_open(EventName.c_str(), O_CREAT, 777, 0 ); if (m_Event == SEM_FAILED) { - char buffer[1024]; - strerror_r(errno,buffer,1024); - LOGERROR("cEvent: Cannot create event, err = %s. Aborting server.", buffer); + AString error = GetOSErrorString(errno); + LOGERROR("cEvent: Cannot create event, err = %s. Aborting server.", error.c_str()); abort(); } // Unlink the semaphore immediately - it will continue to function but will not pollute the namespace // We don't store the name, so can't call this in the destructor if (sem_unlink(EventName.c_str()) != 0) { - char buffer[1024]; - strerror_r(errno,buffer,1024); - LOGWARN("ERROR: Could not unlink cEvent. (%s)", buffer); + AString error = GetOSErrorString(errno); + LOGWARN("ERROR: Could not unlink cEvent. (%s)", error.c_str()); } } #endif // *nix @@ -67,9 +63,8 @@ cEvent::~cEvent() { if (sem_close(m_Event) != 0) { - char buffer[1024]; - strerror_r(errno,buffer,1024); - LOGERROR("ERROR: Could not close cEvent. (%s)", buffer); + AString error = GetOSErrorString(errno); + LOGERROR("ERROR: Could not close cEvent. (%s)", error.c_str()); } } else @@ -96,9 +91,8 @@ void cEvent::Wait(void) int res = sem_wait(m_Event); if (res != 0 ) { - char buffer[1024]; - strerror_r(errno,buffer,1024); - LOGWARN("cEvent: waiting for the event failed: %i, err = %s. Continuing, but server may be unstable.", res, buffer); + AString error = GetOSErrorString(errno); + LOGWARN("cEvent: waiting for the event failed: %i, err = %s. Continuing, but server may be unstable.", res, error.c_str()); } #endif } @@ -118,9 +112,8 @@ void cEvent::Set(void) int res = sem_post(m_Event); if (res != 0) { - char buffer[1024]; - strerror_r(errno,buffer,1024); - LOGWARN("cEvent: Could not set cEvent: %i, err = %s", res, buffer); + AString error = GetOSErrorString(errno); + LOGWARN("cEvent: Could not set cEvent: %i, err = %s", res, error.c_str()); } #endif } -- cgit v1.2.3