From 4fca88ed02c1f0b5208abf2420f73023de54c23e Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Fri, 28 Mar 2008 22:18:44 +0000 Subject: Improve logging with macros that consistently output strerror(errno) and errno, replacing almost all use of strerror() in the main code. Log a more detailed error message before throwing an exception for some more system call failures. Make FileStream store its filename on all platforms, not just Windows. Wrap some long lines at less than 80 characters to improve readability. Fix some minor violations of coding standard (white space) and a typo in a comment. --- lib/common/BoxTime.cpp | 4 +-- lib/common/EventWatchFilesystemObject.cpp | 43 +++++++++++++++++-------------- lib/common/FileStream.cpp | 25 +++++++++--------- lib/common/FileStream.h | 4 +-- lib/common/Guards.h | 4 +-- lib/common/Logging.h | 14 ++++++++++ 6 files changed, 56 insertions(+), 38 deletions(-) (limited to 'lib/common') diff --git a/lib/common/BoxTime.cpp b/lib/common/BoxTime.cpp index 1ddcffd4..7d7b1b40 100644 --- a/lib/common/BoxTime.cpp +++ b/lib/common/BoxTime.cpp @@ -39,8 +39,8 @@ box_time_t GetCurrentBoxTime() struct timeval tv; if (gettimeofday(&tv, NULL) != 0) { - BOX_ERROR("Failed to gettimeofday(), dropping " - "precision: " << strerror(errno)); + BOX_LOG_SYS_ERROR("Failed to gettimeofday(), " + "dropping precision"); } else { diff --git a/lib/common/EventWatchFilesystemObject.cpp b/lib/common/EventWatchFilesystemObject.cpp index 84781113..43533fc8 100644 --- a/lib/common/EventWatchFilesystemObject.cpp +++ b/lib/common/EventWatchFilesystemObject.cpp @@ -26,9 +26,10 @@ // -------------------------------------------------------------------------- // // Function -// Name: EventWatchFilesystemObject::EventWatchFilesystemObject(const char *) -// Purpose: Constructor -- opens the file object -// Created: 12/3/04 +// Name: EventWatchFilesystemObject::EventWatchFilesystemObject +// (const char *) +// Purpose: Constructor -- opens the file object +// Created: 12/3/04 // // -------------------------------------------------------------------------- EventWatchFilesystemObject::EventWatchFilesystemObject(const char *Filename) @@ -39,9 +40,8 @@ EventWatchFilesystemObject::EventWatchFilesystemObject(const char *Filename) #ifdef HAVE_KQUEUE if(mDescriptor == -1) { - BOX_ERROR("EventWatchFilesystemObject: " - "Failed to open file '" << Filename << "': " << - strerror(errno)); + BOX_LOG_SYS_ERROR("EventWatchFilesystemObject: " + "Failed to open file '" << Filename << "'"); THROW_EXCEPTION(CommonException, OSFileOpenError) } #else @@ -53,9 +53,9 @@ EventWatchFilesystemObject::EventWatchFilesystemObject(const char *Filename) // -------------------------------------------------------------------------- // // Function -// Name: EventWatchFilesystemObject::~EventWatchFilesystemObject() -// Purpose: Destructor -// Created: 12/3/04 +// Name: EventWatchFilesystemObject::~EventWatchFilesystemObject() +// Purpose: Destructor +// Created: 12/3/04 // // -------------------------------------------------------------------------- EventWatchFilesystemObject::~EventWatchFilesystemObject() @@ -70,12 +70,14 @@ EventWatchFilesystemObject::~EventWatchFilesystemObject() // -------------------------------------------------------------------------- // // Function -// Name: EventWatchFilesystemObject::EventWatchFilesystemObject(const EventWatchFilesystemObject &) -// Purpose: Copy constructor -// Created: 12/3/04 +// Name: EventWatchFilesystemObject::EventWatchFilesystemObject +// (const EventWatchFilesystemObject &) +// Purpose: Copy constructor +// Created: 12/3/04 // // -------------------------------------------------------------------------- -EventWatchFilesystemObject::EventWatchFilesystemObject(const EventWatchFilesystemObject &rToCopy) +EventWatchFilesystemObject::EventWatchFilesystemObject( + const EventWatchFilesystemObject &rToCopy) : mDescriptor(::dup(rToCopy.mDescriptor)) { if(mDescriptor == -1) @@ -89,17 +91,20 @@ EventWatchFilesystemObject::EventWatchFilesystemObject(const EventWatchFilesyste // -------------------------------------------------------------------------- // // Function -// Name: EventWatchFilesystemObject::FillInKEvent(struct kevent &, int) -// Purpose: For WaitForEvent -// Created: 12/3/04 +// Name: EventWatchFilesystemObject::FillInKEvent(struct kevent &, int) +// Purpose: For WaitForEvent +// Created: 12/3/04 // // -------------------------------------------------------------------------- -void EventWatchFilesystemObject::FillInKEvent(struct kevent &rEvent, int Flags) const +void EventWatchFilesystemObject::FillInKEvent(struct kevent &rEvent, + int Flags) const { - EV_SET(&rEvent, mDescriptor, EVFILT_VNODE, EV_CLEAR, NOTE_DELETE | NOTE_WRITE, 0, (void*)this); + EV_SET(&rEvent, mDescriptor, EVFILT_VNODE, EV_CLEAR, + NOTE_DELETE | NOTE_WRITE, 0, (void*)this); } #else -void EventWatchFilesystemObject::FillInPoll(int &fd, short &events, int Flags) const +void EventWatchFilesystemObject::FillInPoll(int &fd, short &events, + int Flags) const { THROW_EXCEPTION(CommonException, KQueueNotSupportedOnThisPlatform) } diff --git a/lib/common/FileStream.cpp b/lib/common/FileStream.cpp index e0806e10..57fb8274 100644 --- a/lib/common/FileStream.cpp +++ b/lib/common/FileStream.cpp @@ -30,7 +30,8 @@ FileStream::FileStream(const char *Filename, int flags, int mode) #else : mOSFileHandle(::open(Filename, flags, mode)), #endif - mIsEOF(false) + mIsEOF(false), + mFileName(Filename) { #ifdef WIN32 if(mOSFileHandle == INVALID_HANDLE_VALUE) @@ -49,9 +50,6 @@ FileStream::FileStream(const char *Filename, int flags, int mode) THROW_EXCEPTION(CommonException, OSFileOpenError) } } -#ifdef WIN32 - this->fileName = Filename; -#endif } @@ -65,7 +63,8 @@ FileStream::FileStream(const char *Filename, int flags, int mode) // -------------------------------------------------------------------------- FileStream::FileStream(tOSFileHandle FileDescriptor) : mOSFileHandle(FileDescriptor), - mIsEOF(false) + mIsEOF(false), + mFileName("HANDLE") { #ifdef WIN32 if(mOSFileHandle == INVALID_HANDLE_VALUE) @@ -77,9 +76,6 @@ FileStream::FileStream(tOSFileHandle FileDescriptor) BOX_ERROR("FileStream: called with invalid file handle"); THROW_EXCEPTION(CommonException, OSFileOpenError) } -#ifdef WIN32 - this->fileName = "HANDLE"; -#endif } #if 0 @@ -150,27 +146,32 @@ int FileStream::Read(void *pBuffer, int NBytes, int Timeout) NULL ); - if ( valid ) + if(valid) { r = numBytesRead; } - else if (GetLastError() == ERROR_BROKEN_PIPE) + else if(GetLastError() == ERROR_BROKEN_PIPE) { r = 0; } else { - BOX_ERROR("Failed to read from file: " << - GetErrorMessage(GetLastError())); + BOX_LOG_WIN_ERROR("Failed to read from file: " << mFileName); r = -1; } #else int r = ::read(mOSFileHandle, pBuffer, NBytes); + if(r == -1) + { + BOX_LOG_SYS_ERROR("Failed to read from file: " << mFileName); + } #endif + if(r == -1) { THROW_EXCEPTION(CommonException, OSFileReadError) } + if(r == 0) { mIsEOF = true; diff --git a/lib/common/FileStream.h b/lib/common/FileStream.h index 721bf3dd..23cc8e75 100644 --- a/lib/common/FileStream.h +++ b/lib/common/FileStream.h @@ -57,10 +57,8 @@ private: bool mIsEOF; FileStream(const FileStream &rToCopy) { /* do not call */ } -#ifdef WIN32 // for debugging.. - std::string fileName; -#endif + std::string mFileName; }; diff --git a/lib/common/Guards.h b/lib/common/Guards.h index d2fb84e0..cd2e4628 100644 --- a/lib/common/Guards.h +++ b/lib/common/Guards.h @@ -37,8 +37,8 @@ public: { if(mOSFileHandle < 0) { - BOX_ERROR("FileHandleGuard: failed to open file '" << - rFilename << "': " << strerror(errno)); + BOX_LOG_SYS_ERROR("FileHandleGuard: failed to open " + "file '" << rFilename << "'"); THROW_EXCEPTION(CommonException, OSFileOpenError) } } diff --git a/lib/common/Logging.h b/lib/common/Logging.h index 2d726627..2d09b319 100644 --- a/lib/common/Logging.h +++ b/lib/common/Logging.h @@ -48,6 +48,20 @@ if (Logging::IsEnabled(Log::TRACE)) \ { BOX_LOG(Log::TRACE, stuff) } +#define BOX_LOG_SYS_WARNING(stuff) \ + BOX_WARNING(stuff << ": " << strerror(errno) << " (" << errno << ")") +#define BOX_LOG_SYS_ERROR(stuff) \ + BOX_ERROR(stuff << ": " << strerror(errno) << " (" << errno << ")") +#define BOX_LOG_SYS_FATAL(stuff) \ + BOX_FATAL(stuff << ": " << strerror(errno) << " (" << errno << ")") + +#ifdef WIN32 + #define BOX_LOG_WIN_ERROR(stuff) \ + BOX_ERROR(stuff << ": " << GetErrorMessage(GetLastError())) + #define BOX_LOG_WIN_ERROR_NUMBER(stuff, number) \ + BOX_ERROR(stuff << ": " << GetErrorMessage(number)) +#endif + #define BOX_FORMAT_ACCOUNT(accno) \ std::hex << \ std::showbase << \ -- cgit v1.2.3