Changeset 2115

Show
Ignore:
Timestamp:
28/03/2008 22:18:44 (9 months ago)
Author:
chris
Message:

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.

Location:
box/trunk
Files:
16 modified

Legend:

Unmodified
Added
Removed
  • box/trunk/bin/bbackupd/BackupClientContext.cpp

    r1783 r2115  
    147147                        if (!mpExtendedLogFileHandle) 
    148148                        { 
    149                                 BOX_ERROR("Failed to open extended log " 
    150                                         "file: " << strerror(errno)); 
     149                                BOX_LOG_SYS_ERROR("Failed to open extended " 
     150                                        "log file: " << mExtendedLogFile); 
    151151                        } 
    152152                        else 
  • box/trunk/bin/bbackupd/BackupDaemon.cpp

    r2109 r2115  
    18131813#endif // HAVE_STRUCT_STATVFS_F_MNTONNAME 
    18141814                                { 
    1815                                         BOX_WARNING("Failed to stat location " 
     1815                                        BOX_LOG_SYS_WARNING("Failed to stat location " 
    18161816                                                "path '" << apLoc->mPath << 
    1817                                                 "' (" << strerror(errno) << 
    1818                                                 "), skipping location '" << 
     1817                                                "', skipping location '" << 
    18191818                                                apLoc->mName << "'"); 
    18201819                                        continue; 
     
    21902189                if(::rename(newmap.c_str(), target.c_str()) != 0) 
    21912190                { 
    2192                         BOX_ERROR("failed to rename ID map: " << newmap 
    2193                                 << " to " << target << ": "  
    2194                                 << strerror(errno)); 
     2191                        BOX_LOG_SYS_ERROR("Failed to rename ID map: " << 
     2192                                newmap << " to " << target); 
    21952193                        THROW_EXCEPTION(CommonException, OSFileError) 
    21962194                } 
     
    30563054        if(!FileExists(storeObjectInfoFile.c_str())) 
    30573055        { 
    3058                 // File doesn't exist -- so can't be deleted. But something isn't quite right, so log a message 
    3059                 BOX_WARNING("Store object info file did not exist when it " 
    3060                         "was supposed to. (" << storeObjectInfoFile << ")"); 
     3056                // File doesn't exist -- so can't be deleted. But something 
     3057                // isn't quite right, so log a message 
     3058                BOX_WARNING("StoreObjectInfoFile did not exist when it " 
     3059                        "was supposed to: " << storeObjectInfoFile); 
    30613060 
    30623061                // Return true to stop things going around in a loop 
     
    30673066        if(::unlink(storeObjectInfoFile.c_str()) != 0) 
    30683067        { 
    3069                 BOX_ERROR("Failed to delete the old store object info file: " 
    3070                         << storeObjectInfoFile << ": "<< strerror(errno)); 
     3068                BOX_LOG_SYS_ERROR("Failed to delete the old " 
     3069                        "StoreObjectInfoFile: " << storeObjectInfoFile); 
    30713070                return false; 
    30723071        } 
  • box/trunk/bin/bbackupd/Win32ServiceFunctions.cpp

    r1892 r2115  
    208208                if (emu_stat(pConfigFileName, &st) != 0) 
    209209                { 
    210                         BOX_ERROR("Failed to open configuration file '" << 
    211                                 pConfigFileName << "': " << strerror(errno)); 
     210                        BOX_LOG_SYS_ERROR("Failed to open configuration file " 
     211                                "'" << pConfigFileName << "'"); 
    212212                        return 1; 
    213213                } 
     
    222222        } 
    223223 
    224         SC_HANDLE scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE); 
     224        SC_HANDLE scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE); 
    225225 
    226226        if (!scm)  
  • box/trunk/bin/bbackupquery/BackupQueries.cpp

    r1926 r2115  
    811811                else 
    812812                { 
    813                         BOX_ERROR("Error changing to directory '" << 
    814                                 args[0] << ": " << strerror(errno)); 
     813                        BOX_LOG_SYS_ERROR("Failed to change to directory " 
     814                                "'" << args[0] << "'"); 
    815815                } 
    816816 
     
    823823        if(::getcwd(wd, PATH_MAX) == 0) 
    824824        { 
    825                 BOX_ERROR("Error getting current directory: " << 
    826                         strerror(errno)); 
     825                BOX_LOG_SYS_ERROR("Error getting current directory"); 
    827826                SetReturnCode(COMMAND_RETURN_ERROR); 
    828827                return; 
     
    14001399                else 
    14011400                { 
    1402                         BOX_WARNING("Failed to access local directory '" << 
    1403                                 localDirDisplay << ": " << strerror(errno) << 
    1404                                 "'."); 
     1401                        BOX_LOG_SYS_WARNING("Failed to access local directory " 
     1402                                "'" << localDirDisplay << "'"); 
    14051403                        rParams.mUncheckedFiles ++; 
    14061404                } 
     
    14501448        if(dirhandle == 0) 
    14511449        { 
    1452                 BOX_WARNING("Failed to open local directory '" <<  
    1453                         localDirDisplay << "': " << strerror(errno)); 
     1450                BOX_LOG_SYS_WARNING("Failed to open local directory '" <<  
     1451                        localDirDisplay << "'"); 
    14541452                rParams.mUncheckedFiles ++; 
    14551453                return; 
     
    15191517                if(::closedir(dirhandle) != 0) 
    15201518                { 
    1521                         BOX_ERROR("Failed to close local directory '" << 
    1522                                 localDirDisplay << "': " << strerror(errno)); 
     1519                        BOX_LOG_SYS_ERROR("Failed to close local directory " 
     1520                                "'" << localDirDisplay << "'"); 
    15231521                } 
    15241522                dirhandle = 0; 
  • box/trunk/bin/bbackupquery/bbackupquery.cpp

    r2104 r2115  
    175175                        if(logFile == 0) 
    176176                        { 
    177                                 BOX_ERROR("Failed to open log file '" << 
    178                                         optarg << "': " << strerror(errno)); 
     177                                BOX_LOG_SYS_ERROR("Failed to open log file " 
     178                                        "'" << optarg << "'"); 
    179179                        } 
    180180                        break; 
  • box/trunk/lib/backupclient/BackupClientRestore.cpp

    r1921 r2115  
    268268                                if(::unlink(rLocalDirectoryName.c_str()) != 0) 
    269269                                { 
    270                                         BOX_ERROR("Failed to delete file " << 
    271                                                 rLocalDirectoryName << ": " << 
    272                                                 strerror(errno)); 
     270                                        BOX_LOG_SYS_ERROR("Failed to delete " 
     271                                                "file '" <<  
     272                                                rLocalDirectoryName << "'"); 
    273273                                        return Restore_UnknownError; 
    274274                                } 
    275275                                BOX_TRACE("In restore, directory name "  
    276                                         "collision with file " << 
    277                                         rLocalDirectoryName); 
     276                                        "collision with file '" << 
     277                                        rLocalDirectoryName << "'"); 
    278278                        } 
    279279                        break; 
     
    379379                ::mkdir(rLocalDirectoryName.c_str(), S_IRWXU) != 0) 
    380380        { 
    381                 BOX_ERROR("Failed to create directory '" << 
    382                         rLocalDirectoryName << "': " <<  
    383                         strerror(errno)); 
     381                BOX_LOG_SYS_ERROR("Failed to create directory '" << 
     382                        rLocalDirectoryName << "'"); 
    384383                return Restore_UnknownError; 
    385384        } 
     
    452451                                // Local name 
    453452                                BackupStoreFilenameClear nm(en->GetName()); 
    454                                 std::string localFilename(rLocalDirectoryName + DIRECTORY_SEPARATOR_ASCHAR + nm.GetClearFilename()); 
     453                                std::string localFilename(rLocalDirectoryName + 
     454                                        DIRECTORY_SEPARATOR_ASCHAR + 
     455                                        nm.GetClearFilename()); 
    455456                                 
    456457                                // Unlink anything which already exists: 
     
    460461                                        ::unlink(localFilename.c_str()) != 0) 
    461462                                { 
    462                                         BOX_ERROR("Failed to delete file '" << 
    463                                                 localFilename << "': " << 
    464                                                 strerror(errno)); 
     463                                        BOX_LOG_SYS_ERROR("Failed to delete " 
     464                                                "file '" << localFilename <<  
     465                                                "'"); 
    465466                                        return Restore_UnknownError; 
    466467                                } 
    467468                                 
    468469                                // Request it from the store 
    469                                 rConnection.QueryGetFile(DirectoryID, en->GetObjectID()); 
     470                                rConnection.QueryGetFile(DirectoryID, 
     471                                        en->GetObjectID()); 
    470472                 
    471473                                // Stream containing encoded file 
    472                                 std::auto_ptr<IOStream> objectStream(rConnection.ReceiveStream()); 
    473                  
    474                                 // Decode the file -- need to do different things depending on whether  
    475                                 // the directory entry has additional attributes 
     474                                std::auto_ptr<IOStream> objectStream( 
     475                                        rConnection.ReceiveStream()); 
     476                 
     477                                // Decode the file -- need to do different 
     478                                // things depending on whether the directory 
     479                                // entry has additional attributes 
    476480                                try 
    477481                                { 
  • box/trunk/lib/common/BoxTime.cpp

    r1783 r2115  
    4040                if (gettimeofday(&tv, NULL) != 0) 
    4141                { 
    42                         BOX_ERROR("Failed to gettimeofday(), dropping " 
    43                                 "precision: " << strerror(errno)); 
     42                        BOX_LOG_SYS_ERROR("Failed to gettimeofday(), " 
     43                                "dropping precision"); 
    4444                } 
    4545                else 
  • box/trunk/lib/common/EventWatchFilesystemObject.cpp

    r1694 r2115  
    2727// 
    2828// Function 
    29 //              Name:    EventWatchFilesystemObject::EventWatchFilesystemObject(const char *) 
    30 //              Purpose: Constructor -- opens the file object 
    31 //              Created: 12/3/04 
     29//      Name:    EventWatchFilesystemObject::EventWatchFilesystemObject 
     30//               (const char *) 
     31//      Purpose: Constructor -- opens the file object 
     32//      Created: 12/3/04 
    3233// 
    3334// -------------------------------------------------------------------------- 
     
    4041        if(mDescriptor == -1) 
    4142        { 
    42                 BOX_ERROR("EventWatchFilesystemObject: " 
    43                         "Failed to open file '" << Filename << "': " << 
    44                         strerror(errno)); 
     43                BOX_LOG_SYS_ERROR("EventWatchFilesystemObject: " 
     44                        "Failed to open file '" << Filename << "'"); 
    4545                THROW_EXCEPTION(CommonException, OSFileOpenError) 
    4646        } 
     
    5454// 
    5555// Function 
    56 //              Name:    EventWatchFilesystemObject::~EventWatchFilesystemObject() 
    57 //              Purpose: Destructor 
    58 //              Created: 12/3/04 
     56//      Name:    EventWatchFilesystemObject::~EventWatchFilesystemObject() 
     57//      Purpose: Destructor 
     58//      Created: 12/3/04 
    5959// 
    6060// -------------------------------------------------------------------------- 
     
    7171// 
    7272// Function 
    73 //              Name:    EventWatchFilesystemObject::EventWatchFilesystemObject(const EventWatchFilesystemObject &) 
    74 //              Purpose: Copy constructor 
    75 //              Created: 12/3/04 
     73//      Name:    EventWatchFilesystemObject::EventWatchFilesystemObject 
     74//               (const EventWatchFilesystemObject &) 
     75//      Purpose: Copy constructor 
     76//      Created: 12/3/04 
    7677// 
    7778// -------------------------------------------------------------------------- 
    78 EventWatchFilesystemObject::EventWatchFilesystemObject(const EventWatchFilesystemObject &rToCopy) 
     79EventWatchFilesystemObject::EventWatchFilesystemObject( 
     80        const EventWatchFilesystemObject &rToCopy) 
    7981        : mDescriptor(::dup(rToCopy.mDescriptor)) 
    8082{ 
     
    9092// 
    9193// Function 
    92 //              Name:    EventWatchFilesystemObject::FillInKEvent(struct kevent &, int) 
    93 //              Purpose: For WaitForEvent 
    94 //              Created: 12/3/04 
     94//      Name:    EventWatchFilesystemObject::FillInKEvent(struct kevent &, int) 
     95//      Purpose: For WaitForEvent 
     96//      Created: 12/3/04 
    9597// 
    9698// -------------------------------------------------------------------------- 
    97 void EventWatchFilesystemObject::FillInKEvent(struct kevent &rEvent, int Flags) const 
     99void EventWatchFilesystemObject::FillInKEvent(struct kevent &rEvent, 
     100        int Flags) const 
    98101{ 
    99         EV_SET(&rEvent, mDescriptor, EVFILT_VNODE, EV_CLEAR, NOTE_DELETE | NOTE_WRITE, 0, (void*)this); 
     102        EV_SET(&rEvent, mDescriptor, EVFILT_VNODE, EV_CLEAR, 
     103                NOTE_DELETE | NOTE_WRITE, 0, (void*)this); 
    100104} 
    101105#else 
    102 void EventWatchFilesystemObject::FillInPoll(int &fd, short &events, int Flags) const 
     106void EventWatchFilesystemObject::FillInPoll(int &fd, short &events, 
     107        int Flags) const 
    103108{ 
    104109        THROW_EXCEPTION(CommonException, KQueueNotSupportedOnThisPlatform) 
  • box/trunk/lib/common/FileStream.cpp

    r1369 r2115  
    3131        : mOSFileHandle(::open(Filename, flags, mode)), 
    3232#endif 
    33           mIsEOF(false) 
     33          mIsEOF(false), 
     34          mFileName(Filename) 
    3435{ 
    3536#ifdef WIN32 
     
    5051                } 
    5152        } 
    52 #ifdef WIN32 
    53         this->fileName = Filename; 
    54 #endif 
    5553} 
    5654 
     
    6664FileStream::FileStream(tOSFileHandle FileDescriptor) 
    6765        : mOSFileHandle(FileDescriptor), 
    68           mIsEOF(false) 
     66          mIsEOF(false), 
     67          mFileName("HANDLE") 
    6968{ 
    7069#ifdef WIN32 
     
    7877                THROW_EXCEPTION(CommonException, OSFileOpenError) 
    7978        } 
    80 #ifdef WIN32 
    81         this->fileName = "HANDLE"; 
    82 #endif 
    8379} 
    8480 
     
    151147                ); 
    152148 
    153         if ( valid ) 
     149        if(valid) 
    154150        { 
    155151                r = numBytesRead; 
    156152        } 
    157         else if (GetLastError() == ERROR_BROKEN_PIPE) 
     153        else if(GetLastError() == ERROR_BROKEN_PIPE) 
    158154        { 
    159155                r = 0; 
     
    161157        else 
    162158        { 
    163                 BOX_ERROR("Failed to read from file: " << 
    164                         GetErrorMessage(GetLastError())); 
     159                BOX_LOG_WIN_ERROR("Failed to read from file: " << mFileName); 
    165160                r = -1; 
    166161        } 
    167162#else 
    168163        int r = ::read(mOSFileHandle, pBuffer, NBytes); 
    169 #endif 
    170164        if(r == -1) 
    171165        { 
     166                BOX_LOG_SYS_ERROR("Failed to read from file: " << mFileName); 
     167        } 
     168#endif 
     169 
     170        if(r == -1) 
     171        { 
    172172                THROW_EXCEPTION(CommonException, OSFileReadError) 
    173173        } 
     174 
    174175        if(r == 0) 
    175176        { 
  • box/trunk/lib/common/FileStream.h

    r950 r2115  
    5858        FileStream(const FileStream &rToCopy) { /* do not call */ } 
    5959 
    60 #ifdef WIN32 
    6160        // for debugging.. 
    62         std::string fileName; 
    63 #endif 
     61        std::string mFileName; 
    6462}; 
    6563 
  • box/trunk/lib/common/Guards.h

    r1777 r2115  
    3838                if(mOSFileHandle < 0) 
    3939                { 
    40                         BOX_ERROR("FileHandleGuard: failed to open file '" << 
    41                                 rFilename << "': " << strerror(errno)); 
     40                        BOX_LOG_SYS_ERROR("FileHandleGuard: failed to open "  
     41                                "file '" << rFilename << "'"); 
    4242                        THROW_EXCEPTION(CommonException, OSFileOpenError) 
    4343                } 
  • box/trunk/lib/common/Logging.h

    r2102 r2115  
    4848        if (Logging::IsEnabled(Log::TRACE)) \ 
    4949        { BOX_LOG(Log::TRACE, stuff) } 
     50 
     51#define BOX_LOG_SYS_WARNING(stuff) \ 
     52        BOX_WARNING(stuff << ": " << strerror(errno) << " (" << errno << ")") 
     53#define BOX_LOG_SYS_ERROR(stuff) \ 
     54        BOX_ERROR(stuff << ": " << strerror(errno) << " (" << errno << ")") 
     55#define BOX_LOG_SYS_FATAL(stuff) \ 
     56        BOX_FATAL(stuff << ": " << strerror(errno) << " (" << errno << ")") 
     57 
     58#ifdef WIN32 
     59        #define BOX_LOG_WIN_ERROR(stuff) \ 
     60                BOX_ERROR(stuff << ": " << GetErrorMessage(GetLastError())) 
     61        #define BOX_LOG_WIN_ERROR_NUMBER(stuff, number) \ 
     62                BOX_ERROR(stuff << ": " << GetErrorMessage(number)) 
     63#endif 
    5064 
    5165#define BOX_FORMAT_ACCOUNT(accno) \ 
  • box/trunk/lib/server/Daemon.cpp

    r2110 r2115  
    466466                        if(::setsid() == -1) 
    467467                        { 
    468                                 BOX_ERROR("Failed to setsid(): " <<  
    469                                         strerror(errno)); 
     468                                BOX_LOG_SYS_ERROR("Failed to setsid()"); 
    470469                                THROW_EXCEPTION(ServerException, DaemoniseFailed) 
    471470                        } 
     
    476475                        case -1: 
    477476                                // error 
     477                                BOX_LOG_SYS_ERROR("Failed to fork() a child"); 
    478478                                THROW_EXCEPTION(ServerException, DaemoniseFailed) 
    479479                                break; 
     
    498498                sa.sa_handler = SignalHandler; 
    499499                sa.sa_flags = 0; 
    500                 sigemptyset(&sa.sa_mask);               // macro 
    501                 if(::sigaction(SIGHUP, &sa, NULL) != 0 || ::sigaction(SIGTERM, &sa, NULL) != 0) 
    502                 { 
     500                sigemptyset(&sa.sa_mask); // macro 
     501                if(::sigaction(SIGHUP, &sa, NULL) != 0 || 
     502                        ::sigaction(SIGTERM, &sa, NULL) != 0) 
     503                { 
     504                        BOX_LOG_SYS_ERROR("Failed to set signal handlers"); 
    503505                        THROW_EXCEPTION(ServerException, DaemoniseFailed) 
    504506                } 
     
    516518                if(::write(pidFile, pid, pidsize) != pidsize) 
    517519                { 
    518                         BOX_FATAL("can't write pid file"); 
     520                        BOX_LOG_SYS_FATAL("Failed to write PID file: " << 
     521                                pidFileName); 
    519522                        THROW_EXCEPTION(ServerException, DaemoniseFailed) 
    520523                } 
     
    545548                        if(devnull == -1) 
    546549                        { 
     550                                BOX_LOG_SYS_ERROR("Failed to open /dev/null"); 
    547551                                THROW_EXCEPTION(CommonException, OSFileError); 
    548552                        } 
     
    891895                        return 0; 
    892896                } 
     897                BOX_LOG_SYS_ERROR("Failed to stat configuration file: " << 
     898                        GetConfigFileName()); 
    893899                THROW_EXCEPTION(CommonException, OSFileError) 
    894900        } 
  • box/trunk/lib/server/ServerControl.h

    r1999 r2115  
    143143        if (!killed) 
    144144        { 
    145                 BOX_ERROR("Failed to kill process " << pid << ": " << 
    146                         strerror(errno)); 
     145                BOX_LOG_SYS_ERROR("Failed to kill process " << pid); 
    147146        } 
    148147        TEST_THAT(killed); 
  • box/trunk/lib/server/SocketListen.h