diff options
author | Benoit Foucher <benoit@zeroc.com> | 2008-01-23 11:31:53 +0100 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2008-01-23 11:31:53 +0100 |
commit | c7fb26230801e62f3e690a8948d37c33517c4c13 (patch) | |
tree | f17689f60e13fbbd20d12473272a6f0652f39a78 /cpp/src/IceUtil | |
parent | removing EventHandler in C# (diff) | |
download | ice-c7fb26230801e62f3e690a8948d37c33517c4c13.tar.bz2 ice-c7fb26230801e62f3e690a8948d37c33517c4c13.tar.xz ice-c7fb26230801e62f3e690a8948d37c33517c4c13.zip |
- Added IceUtil::SyscallException and cleaned up few IceUtil exceptions
- Added errorToString() and lastErrorToString() functions to IceUtil/StringUtil.h
- Replaced multiple implementations of errorToString methods with the IceUtil one.
- Fixed bug 2641.
Diffstat (limited to 'cpp/src/IceUtil')
-rw-r--r-- | cpp/src/IceUtil/Exception.cpp | 61 | ||||
-rw-r--r-- | cpp/src/IceUtil/Random.cpp | 76 | ||||
-rw-r--r-- | cpp/src/IceUtil/StringUtil.cpp | 224 | ||||
-rw-r--r-- | cpp/src/IceUtil/ThreadException.cpp | 46 | ||||
-rw-r--r-- | cpp/src/IceUtil/Time.cpp | 91 |
5 files changed, 360 insertions, 138 deletions
diff --git a/cpp/src/IceUtil/Exception.cpp b/cpp/src/IceUtil/Exception.cpp index e893cb08397..cc8e8e0c4aa 100644 --- a/cpp/src/IceUtil/Exception.cpp +++ b/cpp/src/IceUtil/Exception.cpp @@ -9,6 +9,7 @@ #include <IceUtil/Exception.h> #include <IceUtil/StaticMutex.h> +#include <IceUtil/StringUtil.h> #include <ostream> #include <cstdlib> @@ -148,7 +149,7 @@ IceUtil::IllegalArgumentException::IllegalArgumentException(const char* file, in IceUtil::IllegalArgumentException::IllegalArgumentException(const char* file, int line, const string& r) : Exception(file, line), - reason(r) + _reason(r) { } @@ -164,6 +165,13 @@ IceUtil::IllegalArgumentException::ice_name() const return _name; } +void +IceUtil::IllegalArgumentException::ice_print(ostream& out) const +{ + Exception::ice_print(out); + out << ": " << _reason; +} + IceUtil::Exception* IceUtil::IllegalArgumentException::ice_clone() const { @@ -176,10 +184,51 @@ IceUtil::IllegalArgumentException::ice_throw() const throw *this; } -ostream& -IceUtil::operator<<(ostream& out, const IceUtil::IllegalArgumentException& ex) +string +IceUtil::IllegalArgumentException::reason() const { - ex.ice_print(out); - out << ": " << ex.reason; - return out; + return _reason; +} + +IceUtil::SyscallException::SyscallException(const char* file, int line, int err ): + Exception(file, line), + _error(err) +{ +} + +const char* IceUtil::SyscallException::_name = "IceUtil::SyscallException"; + +string +IceUtil::SyscallException::ice_name() const +{ + return _name; +} + +void +IceUtil::SyscallException::ice_print(ostream& os) const +{ + Exception::ice_print(os); + if(_error != 0) + { + os << ":\nsyscall exception: " << IceUtilInternal::errorToString(_error); + } } + +IceUtil::Exception* +IceUtil::SyscallException::ice_clone() const +{ + return new SyscallException(*this); +} + +void +IceUtil::SyscallException::ice_throw() const +{ + throw *this; +} + +int +IceUtil::SyscallException::error() const +{ + return _error; +} + diff --git a/cpp/src/IceUtil/Random.cpp b/cpp/src/IceUtil/Random.cpp index d884dbb9a35..1712bb4394f 100644 --- a/cpp/src/IceUtil/Random.cpp +++ b/cpp/src/IceUtil/Random.cpp @@ -20,14 +20,6 @@ using namespace std; using namespace IceUtil; -IceUtilInternal::RandomGeneratorException::RandomGeneratorException(const char* file, int line, int error) : - Exception(file, line), - _error(error) -{ -} - -const char* IceUtilInternal::RandomGeneratorException::_name = "IceUtilInternal::RandomGeneratorException"; - // // The static mutex is required to lazy initialize the file // descriptor for /dev/urandom (Unix) or the cryptographic @@ -78,60 +70,6 @@ public: static RandomCleanup uuidCleanup; } -string -IceUtilInternal::RandomGeneratorException::ice_name() const -{ - return _name; -} - -void -IceUtilInternal::RandomGeneratorException::ice_print(ostream& os) const -{ - Exception::ice_print(os); - if(_error != 0) - { - os << ":\nrandom generator exception: "; -#ifdef _WIN32 - LPVOID lpMsgBuf = 0; - DWORD ok = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - _error, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR)&lpMsgBuf, - 0, - NULL); - - if(ok) - { - LPCTSTR msg = (LPCTSTR)lpMsgBuf; - assert(msg && strlen((char*)msg) > 0); - os << msg; - LocalFree(lpMsgBuf); - } - else - { - os << "unknown random generator error"; - } -#else - os << strerror(_error); -#endif - } -} - -Exception* -IceUtilInternal::RandomGeneratorException::ice_clone() const -{ - return new RandomGeneratorException(*this); -} - -void -IceUtilInternal::RandomGeneratorException::ice_throw() const -{ - throw *this; -} - void IceUtilInternal::generateRandom(char* buffer, int size) { @@ -148,13 +86,13 @@ IceUtilInternal::generateRandom(char* buffer, int size) { if(!CryptAcquireContext(&context, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { - throw RandomGeneratorException(__FILE__, __LINE__, GetLastError()); + throw SyscallException(__FILE__, __LINE__, GetLastError()); } } if(!CryptGenRandom(context, size, reinterpret_cast<unsigned char*>(buffer))) { - throw RandomGeneratorException(__FILE__, __LINE__, GetLastError()); + throw SyscallException(__FILE__, __LINE__, GetLastError()); } #else @@ -168,7 +106,7 @@ IceUtilInternal::generateRandom(char* buffer, int size) if(fd == -1) { assert(0); - throw RandomGeneratorException(__FILE__, __LINE__); + throw SyscallException(__FILE__, __LINE__, errno); } } @@ -184,10 +122,10 @@ IceUtilInternal::generateRandom(char* buffer, int size) if(bytesRead == -1 && errno != EINTR) { - int err = errno; - cerr << "Reading /dev/urandom returned " << strerror(err) << endl; + SyscallException ex(__FILE__, __LINE__, errno); + cerr << "Reading /dev/urandom failed:\n" << ex << endl; assert(0); - throw RandomGeneratorException(__FILE__, __LINE__, errno); + throw ex; } else { @@ -199,7 +137,7 @@ IceUtilInternal::generateRandom(char* buffer, int size) if(index != static_cast<size_t>(size)) { assert(0); - throw RandomGeneratorException(__FILE__, __LINE__); + throw SyscallException(__FILE__, __LINE__, 0); } #endif } diff --git a/cpp/src/IceUtil/StringUtil.cpp b/cpp/src/IceUtil/StringUtil.cpp index 8a96f795358..3f0806dfb0f 100644 --- a/cpp/src/IceUtil/StringUtil.cpp +++ b/cpp/src/IceUtil/StringUtil.cpp @@ -446,3 +446,227 @@ IceUtilInternal::match(const string& s, const string& pat, bool emptyMatch) return true; } + +#ifdef _WIN32 + +string +IceUtilInternal::errorToString(int error, LPCVOID source) +{ + if(error < WSABASEERR) + { + LPVOID lpMsgBuf = 0; + DWORD ok = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS | + (source != NULL ? FORMAT_MESSAGE_FROM_HMODULE : 0), + source, + error, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR)&lpMsgBuf, + 0, + NULL); + if(ok) + { + LPCTSTR msg = (LPCTSTR)lpMsgBuf; + assert(msg && strlen((const char*)msg) > 0); + string result = (const char*)msg; + if(result[result.length() - 1] == '\n') + { + result = result.substr(0, result.length() - 2); + } + LocalFree(lpMsgBuf); + return result; + } + else + { + ostringstream os; + os << "unknown error: " << error; + return os.str(); + } + } + + switch(error) + { + case WSAEINTR: + return "WSAEINTR"; + + case WSAEBADF: + return "WSAEBADF"; + + case WSAEACCES: + return "WSAEACCES"; + + case WSAEFAULT: + return "WSAEFAULT"; + + case WSAEINVAL: + return "WSAEINVAL"; + + case WSAEMFILE: + return "WSAEMFILE"; + + case WSAEWOULDBLOCK: + return "WSAEWOULDBLOCK"; + + case WSAEINPROGRESS: + return "WSAEINPROGRESS"; + + case WSAEALREADY: + return "WSAEALREADY"; + + case WSAENOTSOCK: + return "WSAENOTSOCK"; + + case WSAEDESTADDRREQ: + return "WSAEDESTADDRREQ"; + + case WSAEMSGSIZE: + return "WSAEMSGSIZE"; + + case WSAEPROTOTYPE: + return "WSAEPROTOTYPE"; + + case WSAENOPROTOOPT: + return "WSAENOPROTOOPT"; + + case WSAEPROTONOSUPPORT: + return "WSAEPROTONOSUPPORT"; + + case WSAESOCKTNOSUPPORT: + return "WSAESOCKTNOSUPPORT"; + + case WSAEOPNOTSUPP: + return "WSAEOPNOTSUPP"; + + case WSAEPFNOSUPPORT: + return "WSAEPFNOSUPPORT"; + + case WSAEAFNOSUPPORT: + return "WSAEAFNOSUPPORT"; + + case WSAEADDRINUSE: + return "WSAEADDRINUSE"; + + case WSAEADDRNOTAVAIL: + return "WSAEADDRNOTAVAIL"; + + case WSAENETDOWN: + return "WSAENETDOWN"; + + case WSAENETUNREACH: + return "WSAENETUNREACH"; + + case WSAENETRESET: + return "WSAENETRESET"; + + case WSAECONNABORTED: + return "WSAECONNABORTED"; + + case WSAECONNRESET: + return "WSAECONNRESET"; + + case WSAENOBUFS: + return "WSAENOBUFS"; + + case WSAEISCONN: + return "WSAEISCONN"; + + case WSAENOTCONN: + return "WSAENOTCONN"; + + case WSAESHUTDOWN: + return "WSAESHUTDOWN"; + + case WSAETOOMANYREFS: + return "WSAETOOMANYREFS"; + + case WSAETIMEDOUT: + return "WSAETIMEDOUT"; + + case WSAECONNREFUSED: + return "WSAECONNREFUSED"; + + case WSAELOOP: + return "WSAELOOP"; + + case WSAENAMETOOLONG: + return "WSAENAMETOOLONG"; + + case WSAEHOSTDOWN: + return "WSAEHOSTDOWN"; + + case WSAEHOSTUNREACH: + return "WSAEHOSTUNREACH"; + + case WSAENOTEMPTY: + return "WSAENOTEMPTY"; + + case WSAEPROCLIM: + return "WSAEPROCLIM"; + + case WSAEUSERS: + return "WSAEUSERS"; + + case WSAEDQUOT: + return "WSAEDQUOT"; + + case WSAESTALE: + return "WSAESTALE"; + + case WSAEREMOTE: + return "WSAEREMOTE"; + + case WSAEDISCON: + return "WSAEDISCON"; + + case WSASYSNOTREADY: + return "WSASYSNOTREADY"; + + case WSAVERNOTSUPPORTED: + return "WSAVERNOTSUPPORTED"; + + case WSANOTINITIALISED: + return "WSANOTINITIALISED"; + + case WSAHOST_NOT_FOUND: + return "WSAHOST_NOT_FOUND"; + + case WSATRY_AGAIN: + return "WSATRY_AGAIN"; + + case WSANO_RECOVERY: + return "WSANO_RECOVERY"; + + case WSANO_DATA: + return "WSANO_DATA"; + + default: + { + ostringstream os; + os << "unknown socket error: " << error; + return os.str(); + } + } +} + +string +IceUtilInternal::lastErrorToString() +{ + return errorToString(GetLastError()); +} + +#else + +string +IceUtilInternal::errorToString(int error) +{ + return strerror(error); +} + +string +IceUtilInternal::lastErrorToString() +{ + return errorToString(errno); +} + +#endif diff --git a/cpp/src/IceUtil/ThreadException.cpp b/cpp/src/IceUtil/ThreadException.cpp index 7a17a98185e..c24eae6754c 100644 --- a/cpp/src/IceUtil/ThreadException.cpp +++ b/cpp/src/IceUtil/ThreadException.cpp @@ -12,8 +12,7 @@ using namespace std; IceUtil::ThreadSyscallException::ThreadSyscallException(const char* file, int line, int err ): - Exception(file, line), - _error(err) + SyscallException(file, line, err) { } @@ -25,42 +24,6 @@ IceUtil::ThreadSyscallException::ice_name() const return _name; } -void -IceUtil::ThreadSyscallException::ice_print(ostream& os) const -{ - Exception::ice_print(os); - if(_error != 0) - { - os << ":\nthread syscall exception: "; -#ifdef _WIN32 - LPVOID lpMsgBuf = 0; - DWORD ok = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - _error, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR)&lpMsgBuf, - 0, - NULL); - - if(ok) - { - LPCTSTR msg = (LPCTSTR)lpMsgBuf; - assert(msg && strlen((char*)msg) > 0); - os << msg; - LocalFree(lpMsgBuf); - } - else - { - os << "unknown thread error"; - } -#else - os << strerror(_error); -#endif - } -} - IceUtil::Exception* IceUtil::ThreadSyscallException::ice_clone() const { @@ -73,13 +36,6 @@ IceUtil::ThreadSyscallException::ice_throw() const throw *this; } -int -IceUtil::ThreadSyscallException::error() const -{ - return _error; -} - - IceUtil::ThreadLockedException::ThreadLockedException(const char* file, int line) : Exception(file, line) { diff --git a/cpp/src/IceUtil/Time.cpp b/cpp/src/IceUtil/Time.cpp index d562d7a72b4..0c747fb7104 100644 --- a/cpp/src/IceUtil/Time.cpp +++ b/cpp/src/IceUtil/Time.cpp @@ -8,6 +8,7 @@ // ********************************************************************** #include <IceUtil/DisableWarnings.h> +#include <IceUtil/Exception.h> #include <IceUtil/Time.h> #include <iomanip> @@ -20,15 +21,48 @@ using namespace IceUtil; +#ifdef _WIN32 + +namespace +{ + +static double frequency = -1.0; + +// +// Initialize the frequency +// +class InitializeFrequency +{ +public: + + InitializeFrequency() + { + // + // Get the frequency of performance counters. We also make a call to + // QueryPerformanceCounter to ensure it works. If it fails or if the + // call to QueryPerformanceFrequency fails, the frequency will remain + // set to -1.0 and ftime will be used instead. + // + Int64 v; + if(QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&v))) + { + if(QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&v))) + { + frequency = static_cast<double>(v); + } + } + } +}; +static InitializeFrequency frequencyInitializer; + +}; +#endif + Time::Time() : _usec(0) { } -#ifdef _WIN32 -Int64 IceUtil::Time::_frequency = -1; -#endif - Time IceUtil::Time::now(Clock clock) { @@ -42,38 +76,59 @@ IceUtil::Time::now(Clock clock) struct timeb tb; ftime(&tb); # endif - return Time(static_cast<Int64>(tb.time) * ICE_INT64(1000000) + - tb.millitm * 1000); + return Time(static_cast<Int64>(tb.time) * ICE_INT64(1000000) + tb.millitm * 1000); #else struct timeval tv; - gettimeofday(&tv, 0); + if(gettimeofday(&tv, 0) < 0) + { + assert(0); + throw SyscallException(__FILE__, __LINE__, errno); + } return Time(tv.tv_sec * ICE_INT64(1000000) + tv.tv_usec); #endif } else // Monotonic { #if defined(_WIN32) - if(_frequency == -1) + if(frequency > 0.0) + { + Int64 count; + if(!QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&count))) + { + assert(0); + throw SyscallException(__FILE__, __LINE__, GetLastError()); + } + return Time(static_cast<Int64>(count / frequency * 1000000.0)); + } + else { - // - // Frequency cannot change while machine is running so it - // only needs to be retrieved once. - // - QueryPerformanceFrequency((LARGE_INTEGER*)&_frequency); +# if defined(_MSC_VER) + struct _timeb tb; + _ftime(&tb); +# elif defined(__BCPLUSPLUS__) + struct timeb tb; + ftime(&tb); +# endif + return Time(static_cast<Int64>(tb.time) * ICE_INT64(1000000) + tb.millitm * 1000); } - Int64 count; - QueryPerformanceCounter((LARGE_INTEGER*)&count); - return Time((Int64)(1000000.0 / _frequency * count)); #elif defined(__hpux) || defined(__APPLE__) // // HP/MacOS does not support CLOCK_MONOTONIC // struct timeval tv; - gettimeofday(&tv, 0); + if(gettimeofday(&tv, 0) < 0) + { + assert(0); + throw SyscallException(__FILE__, __LINE__, errno); + } return Time(tv.tv_sec * ICE_INT64(1000000) + tv.tv_usec); #else struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); + if(clock_gettime(CLOCK_MONOTONIC, &ts) < 0) + { + assert(0); + throw SyscallException(__FILE__, __LINE__, errno); + } return Time(ts.tv_sec * ICE_INT64(1000000) + ts.tv_nsec / ICE_INT64(1000)); #endif } |