Skip to content

Commit

Permalink
errno doesnt work with winsock, handle
Browse files Browse the repository at this point in the history
  • Loading branch information
dwight committed Jul 7, 2010
1 parent 16d3dc3 commit a5a05a8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
37 changes: 35 additions & 2 deletions util/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,43 @@ namespace mongo {
void initLogging( const string& logpath , bool append );
void rotateLogs( int signal = 0 );

std::string toUtf8String(const std::wstring& wide);

inline string errnoWithDescription(int x = errno) {
stringstream s;
s << "errno:" << x << ' ' << strerror(x);
return s.str();
s << "errno:" << x << ' ';

#if defined(_WIN32)
LPTSTR errorText = NULL;
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM
|FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
x, 0,
(LPTSTR) &errorText, // output
0, // minimum size for output buffer
NULL);
if( errorText ) {
string x = toUtf8String(errorText);
s << x;
LocalFree(errorText);
}
else
s << strerror(x);
/*
DWORD n = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, x,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf, 0, NULL);
*/
#else
s << strerror(x);
#endif
return s.str();
}

/** output the error # and error message with prefix.
Expand Down
11 changes: 9 additions & 2 deletions util/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@

#ifndef _WIN32
#include <sys/resource.h>
#else

// errno doesn't work for winsock.
#undef errno
#define errno WSAGetLastError()

#endif

namespace mongo {
Expand Down Expand Up @@ -566,8 +572,9 @@ namespace mongo {
throw SocketException();
}
if ( ret == -1 ) {
if ( errno != EAGAIN || _timeout == 0 ) {
log(_logLevel) << "MessagingPort recv() " << errnoWithDescription() << " " << farEnd.toString()<<endl;
int e = errno;
if ( e != EAGAIN || _timeout == 0 ) {
log(_logLevel) << "MessagingPort recv() " << errnoWithDescription(e) << " " << farEnd.toString()<<endl;
throw SocketException();
} else {
if ( !serverAlive( farEnd.toString() ) ) {
Expand Down

0 comments on commit a5a05a8

Please sign in to comment.