diff options
Diffstat (limited to 'lib/common')
-rw-r--r-- | lib/common/Box.h | 19 | ||||
-rw-r--r-- | lib/common/BoxTime.cpp | 4 | ||||
-rw-r--r-- | lib/common/Configuration.cpp | 208 | ||||
-rw-r--r-- | lib/common/Configuration.h | 55 | ||||
-rw-r--r-- | lib/common/DebugMemLeakFinder.cpp | 19 | ||||
-rw-r--r-- | lib/common/EventWatchFilesystemObject.cpp | 43 | ||||
-rw-r--r-- | lib/common/FileStream.cpp | 25 | ||||
-rw-r--r-- | lib/common/FileStream.h | 4 | ||||
-rw-r--r-- | lib/common/Guards.h | 4 | ||||
-rw-r--r-- | lib/common/Logging.h | 33 | ||||
-rw-r--r-- | lib/common/PartialReadStream.cpp | 3 | ||||
-rw-r--r-- | lib/common/Utils.cpp | 63 | ||||
-rw-r--r-- | lib/common/Utils.h | 3 |
13 files changed, 340 insertions, 143 deletions
diff --git a/lib/common/Box.h b/lib/common/Box.h index d0e7ab1e..dd91dfe2 100644 --- a/lib/common/Box.h +++ b/lib/common/Box.h @@ -52,15 +52,6 @@ extern bool BoxDebugTraceOn; int BoxDebug_printf(const char *format, ...); int BoxDebugTrace(const char *format, ...); - #define TRACE0(msg) {BoxDebugTrace("%s", msg);} - #define TRACE1(msg, a0) {BoxDebugTrace(msg, a0);} - #define TRACE2(msg, a0, a1) {BoxDebugTrace(msg, a0, a1);} - #define TRACE3(msg, a0, a1, a2) {BoxDebugTrace(msg, a0, a1, a2);} - #define TRACE4(msg, a0, a1, a2, a3) {BoxDebugTrace(msg, a0, a1, a2, a3);} - #define TRACE5(msg, a0, a1, a2, a3, a4) {BoxDebugTrace(msg, a0, a1, a2, a3, a4);} - #define TRACE6(msg, a0, a1, a2, a3, a4, a5) {BoxDebugTrace(msg, a0, a1, a2, a3, a4, a5);} - #define TRACE7(msg, a0, a1, a2, a3, a4, a5, a6) {BoxDebugTrace(msg, a0, a1, a2, a3, a4, a5, a6);} - #define TRACE8(msg, a0, a1, a2, a3, a4, a5, a6, a7) {BoxDebugTrace(msg, a0, a1, a2, a3, a4, a5, a6, a7);} #ifndef PLATFORM_DISABLE_MEM_LEAK_TESTING #define BOX_MEMORY_LEAK_TESTING @@ -76,16 +67,6 @@ #define TRACE_TO_SYSLOG(x) {} #define TRACE_TO_STDOUT(x) {} - #define TRACE0(msg) - #define TRACE1(msg, a0) - #define TRACE2(msg, a0, a1) - #define TRACE3(msg, a0, a1, a2) - #define TRACE4(msg, a0, a1, a2, a3) - #define TRACE5(msg, a0, a1, a2, a3, a4) - #define TRACE6(msg, a0, a1, a2, a3, a4, a5) - #define TRACE7(msg, a0, a1, a2, a3, a4, a5, a6) - #define TRACE8(msg, a0, a1, a2, a3, a4, a5, a6, a7) - // Box Backup builds release get extra information for exception logging #define EXCEPTION_CODENAMES_EXTENDED #define EXCEPTION_CODENAMES_EXTENDED_WITH_DESCRIPTION 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/Configuration.cpp b/lib/common/Configuration.cpp index 4d76e0e0..10e06680 100644 --- a/lib/common/Configuration.cpp +++ b/lib/common/Configuration.cpp @@ -12,6 +12,8 @@ #include <stdlib.h> #include <limits.h> +#include <sstream> + #include "Configuration.h" #include "CommonException.h" #include "Guards.h" @@ -29,7 +31,105 @@ inline bool iw(int c) static const char *sValueBooleanStrings[] = {"yes", "true", "no", "false", 0}; static const bool sValueBooleanValue[] = {true, true, false, false}; +ConfigurationVerifyKey::ConfigurationVerifyKey +( + std::string name, + int flags, + void *testFunction +) +: mName(name), + mHasDefaultValue(false), + mFlags(flags), + mTestFunction(testFunction) +{ } + +// to allow passing NULL for default ListenAddresses + +ConfigurationVerifyKey::ConfigurationVerifyKey +( + std::string name, + int flags, + NoDefaultValue_t t, + void *testFunction +) +: mName(name), + mHasDefaultValue(false), + mFlags(flags), + mTestFunction(testFunction) +{ } + +ConfigurationVerifyKey::ConfigurationVerifyKey +( + std::string name, + int flags, + std::string defaultValue, + void *testFunction +) +: mName(name), + mDefaultValue(defaultValue), + mHasDefaultValue(true), + mFlags(flags), + mTestFunction(testFunction) +{ } + +ConfigurationVerifyKey::ConfigurationVerifyKey +( + std::string name, + int flags, + const char *defaultValue, + void *testFunction +) +: mName(name), + mDefaultValue(defaultValue), + mHasDefaultValue(true), + mFlags(flags), + mTestFunction(testFunction) +{ } + +ConfigurationVerifyKey::ConfigurationVerifyKey +( + std::string name, + int flags, + int defaultValue, + void *testFunction +) +: mName(name), + mHasDefaultValue(true), + mFlags(flags), + mTestFunction(testFunction) +{ + ASSERT(flags & ConfigTest_IsInt); + std::ostringstream val; + val << defaultValue; + mDefaultValue = val.str(); +} +ConfigurationVerifyKey::ConfigurationVerifyKey +( + std::string name, + int flags, + bool defaultValue, + void *testFunction +) +: mName(name), + mHasDefaultValue(true), + mFlags(flags), + mTestFunction(testFunction) +{ + ASSERT(flags & ConfigTest_IsBool); + mDefaultValue = defaultValue ? "yes" : "no"; +} + +ConfigurationVerifyKey::ConfigurationVerifyKey +( + const ConfigurationVerifyKey& rToCopy +) +: mName(rToCopy.mName), + mDefaultValue(rToCopy.mDefaultValue), + mHasDefaultValue(rToCopy.mHasDefaultValue), + mFlags(rToCopy.mFlags), + mTestFunction(rToCopy.mTestFunction) +{ } // -------------------------------------------------------------------------- // @@ -108,8 +208,8 @@ std::auto_ptr<Configuration> Configuration::LoadAndVerify( if(!rErrorMsg.empty()) { // An error occured, return now - //TRACE1("Error message from LoadInto: %s", rErrorMsg.c_str()); - TRACE0("Error at Configuration::LoadInfo\n"); + BOX_ERROR("Error in Configuration::LoadInfo: " << + rErrorMsg); delete pconfig; pconfig = 0; return std::auto_ptr<Configuration>(0); @@ -120,8 +220,8 @@ std::auto_ptr<Configuration> Configuration::LoadAndVerify( { if(!Verify(*pconfig, *pVerify, std::string(), rErrorMsg)) { - //TRACE1("Error message from Verify: %s", rErrorMsg.c_str()); - TRACE0("Error at Configuration::Verify\n"); + BOX_ERROR("Error verifying configuration: " << + rErrorMsg); delete pconfig; pconfig = 0; return std::auto_ptr<Configuration>(0); @@ -189,7 +289,8 @@ bool Configuration::LoadInto(Configuration &rConfig, FdGetLine &rGetLine, std::s } else { - rErrorMsg += "Unexpected start block in " + rConfig.mName + "\n"; + rErrorMsg += "Unexpected start block in " + + rConfig.mName + "\n"; } } else @@ -290,36 +391,32 @@ bool Configuration::LoadInto(Configuration &rConfig, FdGetLine &rGetLine, std::s // -------------------------------------------------------------------------- // // Function -// Name: Configuration::KeyExists(const char *) +// Name: Configuration::KeyExists(const std::string&) // Purpose: Checks to see if a key exists // Created: 2003/07/23 // // -------------------------------------------------------------------------- -bool Configuration::KeyExists(const char *pKeyName) const +bool Configuration::KeyExists(const std::string& rKeyName) const { - if(pKeyName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} - - return mKeys.find(pKeyName) != mKeys.end(); + return mKeys.find(rKeyName) != mKeys.end(); } // -------------------------------------------------------------------------- // // Function -// Name: Configuration::GetKeyValue(const char *) +// Name: Configuration::GetKeyValue(const std::string&) // Purpose: Returns the value of a configuration variable // Created: 2003/07/23 // // -------------------------------------------------------------------------- -const std::string &Configuration::GetKeyValue(const char *pKeyName) const +const std::string &Configuration::GetKeyValue(const std::string& rKeyName) const { - if(pKeyName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} - - std::map<std::string, std::string>::const_iterator i(mKeys.find(pKeyName)); + std::map<std::string, std::string>::const_iterator i(mKeys.find(rKeyName)); if(i == mKeys.end()) { - BOX_ERROR("Missing configuration key: " << pKeyName); + BOX_ERROR("Missing configuration key: " << rKeyName); THROW_EXCEPTION(CommonException, ConfigNoKey) } else @@ -332,16 +429,14 @@ const std::string &Configuration::GetKeyValue(const char *pKeyName) const // -------------------------------------------------------------------------- // // Function -// Name: Configuration::GetKeyValueInt(const char *) +// Name: Configuration::GetKeyValueInt(const std::string& rKeyName) // Purpose: Gets a key value as an integer // Created: 2003/07/23 // // -------------------------------------------------------------------------- -int Configuration::GetKeyValueInt(const char *pKeyName) const +int Configuration::GetKeyValueInt(const std::string& rKeyName) const { - if(pKeyName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} - - std::map<std::string, std::string>::const_iterator i(mKeys.find(pKeyName)); + std::map<std::string, std::string>::const_iterator i(mKeys.find(rKeyName)); if(i == mKeys.end()) { @@ -362,16 +457,14 @@ int Configuration::GetKeyValueInt(const char *pKeyName) const // -------------------------------------------------------------------------- // // Function -// Name: Configuration::GetKeyValueBool(const char *) const +// Name: Configuration::GetKeyValueBool(const std::string&) // Purpose: Gets a key value as a boolean // Created: 17/2/04 // // -------------------------------------------------------------------------- -bool Configuration::GetKeyValueBool(const char *pKeyName) const +bool Configuration::GetKeyValueBool(const std::string& rKeyName) const { - if(pKeyName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} - - std::map<std::string, std::string>::const_iterator i(mKeys.find(pKeyName)); + std::map<std::string, std::string>::const_iterator i(mKeys.find(rKeyName)); if(i == mKeys.end()) { @@ -428,22 +521,21 @@ std::vector<std::string> Configuration::GetKeyNames() const // -------------------------------------------------------------------------- // // Function -// Name: Configuration::SubConfigurationExists(const char *) +// Name: Configuration::SubConfigurationExists(const +// std::string&) // Purpose: Checks to see if a sub configuration exists // Created: 2003/07/23 // // -------------------------------------------------------------------------- -bool Configuration::SubConfigurationExists(const char *pSubName) const +bool Configuration::SubConfigurationExists(const std::string& rSubName) const { - if(pSubName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} - // Attempt to find it... std::list<std::pair<std::string, Configuration> >::const_iterator i(mSubConfigurations.begin()); for(; i != mSubConfigurations.end(); ++i) { // This the one? - if(i->first == pSubName) + if(i->first == rSubName) { // Yes. return true; @@ -458,22 +550,22 @@ bool Configuration::SubConfigurationExists(const char *pSubName) const // -------------------------------------------------------------------------- // // Function -// Name: Configuration::GetSubConfiguration(const char *) +// Name: Configuration::GetSubConfiguration(const +// std::string&) // Purpose: Gets a sub configuration // Created: 2003/07/23 // // -------------------------------------------------------------------------- -const Configuration &Configuration::GetSubConfiguration(const char *pSubName) const +const Configuration &Configuration::GetSubConfiguration(const std::string& + rSubName) const { - if(pSubName == 0) {THROW_EXCEPTION(CommonException, BadArguments)} - // Attempt to find it... std::list<std::pair<std::string, Configuration> >::const_iterator i(mSubConfigurations.begin()); for(; i != mSubConfigurations.end(); ++i) { // This the one? - if(i->first == pSubName) + if(i->first == rSubName) { // Yes. return i->second; @@ -528,15 +620,14 @@ bool Configuration::Verify(Configuration &rConfig, const ConfigurationVerify &rV do { // Can the key be found? - ASSERT(pvkey->mpName); - if(rConfig.KeyExists(pvkey->mpName)) + if(rConfig.KeyExists(pvkey->Name())) { // Get value - const std::string &rval = rConfig.GetKeyValue(pvkey->mpName); + const std::string &rval = rConfig.GetKeyValue(pvkey->Name()); const char *val = rval.c_str(); // Check it's a number? - if((pvkey->Tests & ConfigTest_IsInt) == ConfigTest_IsInt) + if((pvkey->Flags() & ConfigTest_IsInt) == ConfigTest_IsInt) { // Test it... char *end; @@ -545,12 +636,12 @@ bool Configuration::Verify(Configuration &rConfig, const ConfigurationVerify &rV { // not a good value ok = false; - rErrorMsg += rLevel + rConfig.mName +"." + pvkey->mpName + " (key) is not a valid integer.\n"; + rErrorMsg += rLevel + rConfig.mName + "." + pvkey->Name() + " (key) is not a valid integer.\n"; } } // Check it's a bool? - if((pvkey->Tests & ConfigTest_IsBool) == ConfigTest_IsBool) + if((pvkey->Flags() & ConfigTest_IsBool) == ConfigTest_IsBool) { // See if it's one of the allowed strings. bool found = false; @@ -568,37 +659,38 @@ bool Configuration::Verify(Configuration &rConfig, const ConfigurationVerify &rV if(!found) { ok = false; - rErrorMsg += rLevel + rConfig.mName +"." + pvkey->mpName + " (key) is not a valid boolean value.\n"; + rErrorMsg += rLevel + rConfig.mName + "." + pvkey->Name() + " (key) is not a valid boolean value.\n"; } } // Check for multi valued statments where they're not allowed - if((pvkey->Tests & ConfigTest_MultiValueAllowed) == 0) + if((pvkey->Flags() & ConfigTest_MultiValueAllowed) == 0) { // Check to see if this key is a multi-value -- it shouldn't be if(rval.find(MultiValueSeparator) != rval.npos) { ok = false; - rErrorMsg += rLevel + rConfig.mName +"." + pvkey->mpName + " (key) multi value not allowed (duplicated key?).\n"; + rErrorMsg += rLevel + rConfig.mName +"." + pvkey->Name() + " (key) multi value not allowed (duplicated key?).\n"; } } } else { // Is it required to exist? - if((pvkey->Tests & ConfigTest_Exists) == ConfigTest_Exists) + if((pvkey->Flags() & ConfigTest_Exists) == ConfigTest_Exists) { // Should exist, but doesn't. ok = false; - rErrorMsg += rLevel + rConfig.mName + "." + pvkey->mpName + " (key) is missing.\n"; + rErrorMsg += rLevel + rConfig.mName + "." + pvkey->Name() + " (key) is missing.\n"; } - else if(pvkey->mpDefaultValue) + else if(pvkey->HasDefaultValue()) { - rConfig.mKeys[std::string(pvkey->mpName)] = std::string(pvkey->mpDefaultValue); + rConfig.mKeys[pvkey->Name()] = + pvkey->DefaultValue(); } } - if((pvkey->Tests & ConfigTest_LastEntry) == ConfigTest_LastEntry) + if((pvkey->Flags() & ConfigTest_LastEntry) == ConfigTest_LastEntry) { // No more! todo = false; @@ -618,14 +710,14 @@ bool Configuration::Verify(Configuration &rConfig, const ConfigurationVerify &rV bool found = false; while(scan) { - if(scan->mpName == i->first) + if(scan->Name() == i->first) { found = true; break; } // Next? - if((scan->Tests & ConfigTest_LastEntry) == ConfigTest_LastEntry) + if((scan->Flags() & ConfigTest_LastEntry) == ConfigTest_LastEntry) { break; } @@ -650,8 +742,7 @@ bool Configuration::Verify(Configuration &rConfig, const ConfigurationVerify &rV const ConfigurationVerify *scan = rVerify.mpSubConfigurations; while(scan) { - ASSERT(scan->mpName); - if(scan->mpName[0] == '*') + if(scan->mName.length() > 0 && scan->mName[0] == '*') { wildcardverify = scan; } @@ -659,7 +750,8 @@ bool Configuration::Verify(Configuration &rConfig, const ConfigurationVerify &rV // Required? if((scan->Tests & ConfigTest_Exists) == ConfigTest_Exists) { - if(scan->mpName[0] == '*') + if(scan->mName.length() > 0 && + scan->mName[0] == '*') { // Check something exists if(rConfig.mSubConfigurations.size() < 1) @@ -672,11 +764,11 @@ bool Configuration::Verify(Configuration &rConfig, const ConfigurationVerify &rV else { // Check real thing exists - if(!rConfig.SubConfigurationExists(scan->mpName)) + if(!rConfig.SubConfigurationExists(scan->mName)) { // Should exist, but doesn't. ok = false; - rErrorMsg += rLevel + rConfig.mName + "." + scan->mpName + " (block) is missing.\n"; + rErrorMsg += rLevel + rConfig.mName + "." + scan->mName + " (block) is missing.\n"; } } } @@ -701,7 +793,7 @@ bool Configuration::Verify(Configuration &rConfig, const ConfigurationVerify &rV ASSERT(name); while(scan) { - if(strcmp(scan->mpName, name) == 0) + if(scan->mName == name) { // found it! subverify = scan; diff --git a/lib/common/Configuration.h b/lib/common/Configuration.h index 64e7568e..80b3614b 100644 --- a/lib/common/Configuration.h +++ b/lib/common/Configuration.h @@ -29,20 +29,51 @@ enum class ConfigurationVerifyKey { public: - const char *mpName; // "*" for all other keys (not implemented yet) - const char *mpDefaultValue; // default for when it's not present - int Tests; - void *TestFunction; // set to zero for now, will implement later + typedef enum + { + NoDefaultValue = 1 + } NoDefaultValue_t; + + ConfigurationVerifyKey(std::string name, int flags, + void *testFunction = NULL); + // to allow passing ConfigurationVerifyKey::NoDefaultValue + // for default ListenAddresses + ConfigurationVerifyKey(std::string name, int flags, + NoDefaultValue_t t, void *testFunction = NULL); + ConfigurationVerifyKey(std::string name, int flags, + std::string defaultValue, void *testFunction = NULL); + ConfigurationVerifyKey(std::string name, int flags, + const char* defaultValue, void *testFunction = NULL); + ConfigurationVerifyKey(std::string name, int flags, + int defaultValue, void *testFunction = NULL); + ConfigurationVerifyKey(std::string name, int flags, + bool defaultValue, void *testFunction = NULL); + const std::string& Name() const { return mName; } + const std::string& DefaultValue() const { return mDefaultValue; } + const bool HasDefaultValue() const { return mHasDefaultValue; } + const int Flags() const { return mFlags; } + const void* TestFunction() const { return mTestFunction; } + ConfigurationVerifyKey(const ConfigurationVerifyKey& rToCopy); + +private: + ConfigurationVerifyKey& operator=(const ConfigurationVerifyKey& + noAssign); + + std::string mName; // "*" for all other keys (not implemented yet) + std::string mDefaultValue; // default for when it's not present + bool mHasDefaultValue; + int mFlags; + void *mTestFunction; // set to zero for now, will implement later }; class ConfigurationVerify { public: - const char *mpName; // "*" for all other sub config names + std::string mName; // "*" for all other sub config names const ConfigurationVerify *mpSubConfigurations; const ConfigurationVerifyKey *mpKeys; int Tests; - void *TestFunction; // set to zero for now, will implement later + void *TestFunction; // set to zero for now, will implement later }; class FdGetLine; @@ -79,14 +110,14 @@ public: std::string &rErrorMsg) { return LoadAndVerify(rFilename, 0, rErrorMsg); } - bool KeyExists(const char *pKeyName) const; - const std::string &GetKeyValue(const char *pKeyName) const; - int GetKeyValueInt(const char *pKeyName) const; - bool GetKeyValueBool(const char *pKeyName) const; + bool KeyExists(const std::string& rKeyName) const; + const std::string &GetKeyValue(const std::string& rKeyName) const; + int GetKeyValueInt(const std::string& rKeyName) const; + bool GetKeyValueBool(const std::string& rKeyName) const; std::vector<std::string> GetKeyNames() const; - bool SubConfigurationExists(const char *pSubName) const; - const Configuration &GetSubConfiguration(const char *pSubName) const; + bool SubConfigurationExists(const std::string& rSubName) const; + const Configuration &GetSubConfiguration(const std::string& rSubName) const; std::vector<std::string> GetSubConfigurationNames() const; std::string mName; diff --git a/lib/common/DebugMemLeakFinder.cpp b/lib/common/DebugMemLeakFinder.cpp index 87cdf00d..094820b3 100644 --- a/lib/common/DebugMemLeakFinder.cpp +++ b/lib/common/DebugMemLeakFinder.cpp @@ -146,7 +146,9 @@ void *memleakfinder_realloc(void *ptr, size_t size) std::map<void *, MallocBlockInfo>::iterator i(sMallocBlocks.find(ptr)); if(ptr && i == sMallocBlocks.end()) { - TRACE1("Block %x realloc(), but not in list. Error? Or allocated in startup static objects?\n", ptr); + BOX_WARNING("Block " << ptr << " realloc()ated, but not " + "in list. Error? Or allocated in startup static " + "objects?"); } void *b = ::realloc(ptr, size); @@ -193,7 +195,9 @@ void memleakfinder_free(void *ptr) } else { - TRACE1("Block %p freed, but not known. Error? Or allocated in startup static allocation?\n", ptr); + BOX_WARNING("Block " << ptr << " freed, but not " + "known. Error? Or allocated in startup " + "static allocation?"); } if(sTrackMallocInSection) @@ -293,16 +297,21 @@ void memleakfinder_traceblocksinsection() std::map<void *, MallocBlockInfo>::const_iterator i(sMallocBlocks.find(*s)); if(i == sMallocBlocks.end()) { - TRACE0("Logical error in section block finding\n"); + BOX_WARNING("Logical error in section block finding"); } else { - TRACE4("Block %p size %d allocated at %s:%d\n", i->first, i->second.size, i->second.file, i->second.line); + BOX_TRACE("Block " << i->first << " size " << + i->second.size << " allocated at " << + i->second.file << ":" << i->second.line); } } for(std::map<void *, ObjectInfo>::const_iterator i(sSectionObjectBlocks.begin()); i != sSectionObjectBlocks.end(); ++i) { - TRACE5("Object%s %p size %d allocated at %s:%d\n", i->second.array?" []":"", i->first, i->second.size, i->second.file, i->second.line); + BOX_TRACE("Object" << (i->second.array?" []":"") << " " << + i->first << " size " << i->second.size << + " allocated at " << i->second.file << + ":" << i->second.line); } } 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..2c967c89 100644 --- a/lib/common/Logging.h +++ b/lib/common/Logging.h @@ -27,16 +27,16 @@ #define BOX_LOG(level, stuff) \ { \ - std::ostringstream line; \ - line << stuff; \ - Logging::Log(level, __FILE__, __LINE__, line.str()); \ + std::ostringstream _box_log_line; \ + _box_log_line << stuff; \ + Logging::Log(level, __FILE__, __LINE__, _box_log_line.str()); \ } #define BOX_SYSLOG(level, stuff) \ { \ - std::ostringstream line; \ - line << stuff; \ - Logging::LogToSyslog(level, __FILE__, __LINE__, line.str()); \ + std::ostringstream _box_log_line; \ + _box_log_line << stuff; \ + Logging::LogToSyslog(level, __FILE__, __LINE__, _box_log_line.str()); \ } #define BOX_FATAL(stuff) BOX_LOG(Log::FATAL, stuff) @@ -48,15 +48,32 @@ if (Logging::IsEnabled(Log::TRACE)) \ { BOX_LOG(Log::TRACE, stuff) } -#define BOX_FORMAT_ACCOUNT(accno) \ +#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_HEX32(number) \ std::hex << \ std::showbase << \ std::internal << \ std::setw(10) << \ std::setfill('0') << \ - (accno) << \ + (number) << \ std::dec +#define BOX_FORMAT_ACCOUNT(accno) \ + BOX_FORMAT_HEX32(accno) + #define BOX_FORMAT_OBJECTID(objectid) \ std::hex << \ std::showbase << \ diff --git a/lib/common/PartialReadStream.cpp b/lib/common/PartialReadStream.cpp index 76096738..f2f79715 100644 --- a/lib/common/PartialReadStream.cpp +++ b/lib/common/PartialReadStream.cpp @@ -44,7 +44,8 @@ PartialReadStream::~PartialReadStream() // Warn in debug mode if(mBytesLeft != 0) { - TRACE1("PartialReadStream::~PartialReadStream when mBytesLeft = %d\n", mBytesLeft); + BOX_TRACE("PartialReadStream destroyed with " << mBytesLeft << + " bytes remaining"); } } diff --git a/lib/common/Utils.cpp b/lib/common/Utils.cpp index 83a12ccf..90421299 100644 --- a/lib/common/Utils.cpp +++ b/lib/common/Utils.cpp @@ -53,10 +53,10 @@ void SplitString(const std::string &String, char SplitOn, std::vector<std::strin rOutput.push_back(String.substr(b)); } /*#ifndef NDEBUG - TRACE2("Splitting string '%s' on %c\n", String.c_str(), SplitOn); + BOX_TRACE("Splitting string '" << String << " on " << (char)SplitOn); for(unsigned int l = 0; l < rOutput.size(); ++l) { - TRACE2("%d = '%s'\n", l, rOutput[l].c_str()); + BOX_TRACE(l << " = '" << rOutput[l] << "'"); } #endif*/ } @@ -159,6 +159,65 @@ int ObjectExists(const std::string& rFilename) return ((st.st_mode & S_IFDIR) == 0)?ObjectExists_File:ObjectExists_Dir; } +std::string HumanReadableSize(int64_t Bytes) +{ + double readableValue = Bytes; + std::string units = " B"; + if (readableValue > 1024) + { + readableValue /= 1024; + units = "kB"; + } + + if (readableValue > 1024) + { + readableValue /= 1024; + units = "MB"; + } + + if (readableValue > 1024) + { + readableValue /= 1024; + units = "GB"; + } + + std::ostringstream result; + result << std::fixed << std::setprecision(2) << readableValue << + " " << units; + return result.str(); +} +std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max) +{ + std::ostringstream result; + + // Bar graph + char bar[17]; + unsigned int b = (int)((Bytes * (sizeof(bar)-1)) / Max); + if(b > sizeof(bar)-1) {b = sizeof(bar)-1;} + for(unsigned int l = 0; l < b; l++) + { + bar[l] = '*'; + } + for(unsigned int l = b; l < sizeof(bar) - 1; l++) + { + bar[l] = ' '; + } + bar[sizeof(bar)-1] = '\0'; + + result << std::fixed << + std::setw(10) << Blocks << " blocks, " << + std::setw(10) << HumanReadableSize(Bytes) << ", " << + std::setw(3) << std::setprecision(0) << + ((Bytes*100)/Max) << "% |" << bar << "|"; + + return result.str(); +} +std::string FormatUsageLineStart(const std::string& rName) +{ + std::ostringstream result; + result << std::setw(20) << std::right << rName << ": "; + return result.str(); +} diff --git a/lib/common/Utils.h b/lib/common/Utils.h index d0842b51..78bcbd6b 100644 --- a/lib/common/Utils.h +++ b/lib/common/Utils.h @@ -30,6 +30,9 @@ enum ObjectExists_Dir = 2 }; int ObjectExists(const std::string& rFilename); +std::string HumanReadableSize(int64_t Bytes); +std::string FormatUsageBar(int64_t Blocks, int64_t Bytes, int64_t Max); +std::string FormatUsageLineStart(const std::string& rName); #include "MemLeakFindOff.h" |