Ignore:
Timestamp:
28/03/2008 22:18:44 (4 years 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • box/trunk/lib/server/SocketStream.cpp

    r1894 r2115  
    151151 
    152152        // Create the socket 
    153         mSocketHandle = ::socket(sockDomain, SOCK_STREAM, 0 /* let OS choose protocol */); 
     153        mSocketHandle = ::socket(sockDomain, SOCK_STREAM, 
     154                0 /* let OS choose protocol */); 
    154155        if(mSocketHandle == INVALID_SOCKET_VALUE) 
    155156        { 
     157                BOX_LOG_SYS_ERROR("Failed to create a network socket"); 
    156158                THROW_EXCEPTION(ServerException, SocketOpenError) 
    157159        } 
     
    164166                DWORD err = WSAGetLastError(); 
    165167                ::closesocket(mSocketHandle); 
    166 #else 
     168                BOX_LOG_WIN_ERROR_NUMBER("Failed to connect to socket "  
     169                        "(type " << Type << ", name " << Name << 
     170                        ", port " << Port << ")", err); 
     171#else // !WIN32 
    167172                int err = errno; 
    168173                ::close(mSocketHandle); 
    169 #endif 
    170  
    171 #ifdef WIN32 
    172                 BOX_ERROR("Failed to connect to socket (type " << Type << 
    173                         ", name " << Name << ", port " << Port << "): " << 
    174                                 GetErrorMessage(err) 
    175                         ); 
    176 #else 
    177                 BOX_ERROR("Failed to connect to socket (type " << Type << 
    178                         ", name " << Name << ", port " << Port << "): " << 
    179                                 strerror(err) << " (" << err << ")" 
    180                         ); 
    181 #endif 
     174                BOX_LOG_SYS_ERROR("Failed to connect to socket (type " << 
     175                        Type << ", name " << Name << ", port " << Port << 
     176                        ")"); 
     177#endif // WIN32 
    182178 
    183179                mSocketHandle = INVALID_SOCKET_VALUE; 
     
    221217                        { 
    222218                                // Bad! 
    223                                 THROW_EXCEPTION(ServerException, SocketPollError) 
     219                                BOX_LOG_SYS_ERROR("Failed to poll socket"); 
     220                                THROW_EXCEPTION(ServerException, 
     221                                        SocketPollError) 
    224222                        } 
    225223                        break; 
     
    251249                { 
    252250                        // Other error 
    253                         THROW_EXCEPTION(ConnectionException, Conn_SocketReadError) 
     251                        BOX_LOG_SYS_ERROR("Failed to read from socket"); 
     252                        THROW_EXCEPTION(ConnectionException, 
     253                                Conn_SocketReadError); 
    254254                } 
    255255        } 
     256 
    256257        // Closed for reading? 
    257258        if(r == 0) 
     
    298299                        // Error. 
    299300                        mWriteClosed = true;    // assume can't write again 
    300                         THROW_EXCEPTION(ConnectionException, Conn_SocketWriteError) 
     301                        BOX_LOG_SYS_ERROR("Failed to write to socket"); 
     302                        THROW_EXCEPTION(ConnectionException, 
     303                                Conn_SocketWriteError); 
    301304                } 
    302305                 
     
    311314                if(bytesLeft > 0) 
    312315                { 
    313                         TRACE3("Waiting to send data on socket %d, (%d to send of %d)\n", mSocketHandle, bytesLeft, NBytes); 
     316                        BOX_TRACE("Waiting to send data on socket " <<  
     317                                mSocketHandle << " (" << bytesLeft << 
     318                                " of " << NBytes << " bytes left)"); 
    314319                         
    315320                        // Wait for data to send. 
     
    324329                                if(errno != EINTR) 
    325330                                { 
    326                                         THROW_EXCEPTION(ServerException, SocketPollError) 
     331                                        BOX_LOG_SYS_ERROR("Failed to poll " 
     332                                                "socket"); 
     333                                        THROW_EXCEPTION(ServerException, 
     334                                                SocketPollError) 
    327335                                } 
    328336                        } 
     
    351359#endif 
    352360        { 
     361                BOX_LOG_SYS_ERROR("Failed to close socket"); 
    353362                THROW_EXCEPTION(ServerException, SocketCloseError) 
    354363        } 
     
    381390        if(::shutdown(mSocketHandle, how) == -1) 
    382391        { 
     392                BOX_LOG_SYS_ERROR("Failed to shutdown socket"); 
    383393                THROW_EXCEPTION(ConnectionException, Conn_SocketShutdownError) 
    384394        } 
     
    459469        socklen_t credLen = sizeof(cred); 
    460470 
    461         if(::getsockopt(mSocketHandle, SOL_SOCKET, SO_PEERCRED, &cred, &credLen) == 0) 
     471        if(::getsockopt(mSocketHandle, SOL_SOCKET, SO_PEERCRED, &cred, 
     472                &credLen) == 0) 
    462473        { 
    463474                rUidOut = cred.uid; 
     
    465476                return true; 
    466477        } 
     478 
     479        BOX_LOG_SYS_ERROR("Failed to get peer credentials on socket"); 
    467480#endif 
    468481 
Note: See TracChangeset for help on using the changeset viewer.