From fda2dca224c42a24fc54e6f5192469084c49e0cc Mon Sep 17 00:00:00 2001 From: Mathias Stearn Date: Tue, 1 Apr 2014 13:17:36 -0400 Subject: SERVER-13429 Eliminate rawOut and related functions (cherry picked from commit 4e1eccfe64f424daead6ed22542377d2e1ce798e) --- src/mongo/db/db.cpp | 15 +++++++-------- src/mongo/db/instance.cpp | 30 ++++-------------------------- src/mongo/s/server.cpp | 3 +-- src/mongo/shell/dbshell.cpp | 12 +++--------- src/mongo/tools/bridge.cpp | 3 +-- src/mongo/tools/tool_logger.cpp | 2 -- src/mongo/util/log.cpp | 36 ------------------------------------ src/mongo/util/log.h | 13 ------------- 8 files changed, 16 insertions(+), 98 deletions(-) diff --git a/src/mongo/db/db.cpp b/src/mongo/db/db.cpp index d9bd9907b82..4f19610d1b6 100644 --- a/src/mongo/db/db.cpp +++ b/src/mongo/db/db.cpp @@ -1067,15 +1067,14 @@ namespace mongo { // this will be called in certain c++ error cases, for example if there are two active // exceptions void myterminate() { - rawOut( "terminate() called, printing stack (if implemented for platform):" ); - printStackTrace(); + printStackTrace(severe().stream() + << "terminate() called, printing stack (if implemented for platform):\n"); ::abort(); } // this gets called when new fails to allocate memory void my_new_handler() { - rawOut( "out of memory, printing stack and exiting:" ); - printStackTrace(); + printStackTrace(severe().stream() << "out of memory, printing stack and exiting:\n"); ::_exit(EXIT_ABRUPT); } @@ -1131,17 +1130,17 @@ namespace mongo { switch( fdwCtrlType ) { case CTRL_C_EVENT: - rawOut( "Ctrl-C signal" ); + log() << "Ctrl-C signal"; consoleTerminate( "CTRL_C_EVENT" ); return TRUE ; case CTRL_CLOSE_EVENT: - rawOut( "CTRL_CLOSE_EVENT signal" ); + log() << "CTRL_CLOSE_EVENT signal"; consoleTerminate( "CTRL_CLOSE_EVENT" ); return TRUE ; case CTRL_BREAK_EVENT: - rawOut( "CTRL_BREAK_EVENT signal" ); + log() << "CTRL_BREAK_EVENT signal"; consoleTerminate( "CTRL_BREAK_EVENT" ); return TRUE; @@ -1150,7 +1149,7 @@ namespace mongo { return FALSE; case CTRL_SHUTDOWN_EVENT: - rawOut( "CTRL_SHUTDOWN_EVENT signal" ); + log() << "CTRL_SHUTDOWN_EVENT signal"; consoleTerminate( "CTRL_SHUTDOWN_EVENT" ); return TRUE; diff --git a/src/mongo/db/instance.cpp b/src/mongo/db/instance.cpp index 4c1ad157ee9..caa27969c7d 100644 --- a/src/mongo/db/instance.cpp +++ b/src/mongo/db/instance.cpp @@ -1075,22 +1075,6 @@ namespace { return numExitCalls > 0; } - void tryToOutputFatal( const string& s ) { - try { - rawOut( s ); - return; - } - catch ( ... ) {} - - try { - cerr << s << endl; - return; - } - catch ( ... ) {} - - // uh - oh, not sure there is anything else we can do... - } - static void shutdownServer() { log() << "shutdown: going to close listening sockets..." << endl; @@ -1187,25 +1171,19 @@ namespace { // this means something horrible has happened ::_exit( rc ); } - stringstream ss; - ss << "dbexit: " << why << "; exiting immediately"; - tryToOutputFatal( ss.str() ); + log() << "dbexit: " << why << "; exiting immediately"; if ( c ) c->shutdown(); ::_exit( rc ); } } - { - stringstream ss; - ss << "dbexit: " << why; - tryToOutputFatal( ss.str() ); - } + log() << "dbexit: " << why; try { shutdownServer(); // gracefully shutdown instance } catch ( ... ) { - tryToOutputFatal( "shutdown failed with exception" ); + severe() << "shutdown failed with exception"; } #if defined(_DEBUG) @@ -1228,7 +1206,7 @@ namespace { return; } #endif - tryToOutputFatal( "dbexit: really exiting now\n" ); + log() << "dbexit: really exiting now"; if ( c ) c->shutdown(); ::_exit(rc); } diff --git a/src/mongo/s/server.cpp b/src/mongo/s/server.cpp index 3a3bb821ad3..2d725e77af9 100644 --- a/src/mongo/s/server.cpp +++ b/src/mongo/s/server.cpp @@ -186,8 +186,7 @@ namespace mongo { // this gets called when new fails to allocate memory void my_new_handler() { - rawOut( "out of memory, printing stack and exiting:" ); - printStackTrace(); + printStackTrace(severe().stream() << "out of memory, printing stack and exiting:\n"); ::_exit(EXIT_ABRUPT); } diff --git a/src/mongo/shell/dbshell.cpp b/src/mongo/shell/dbshell.cpp index b00ef67e522..e51e91aaf2e 100644 --- a/src/mongo/shell/dbshell.cpp +++ b/src/mongo/shell/dbshell.cpp @@ -225,13 +225,8 @@ char * strsignal(int sig){ #endif void quitAbruptly( int sig ) { - ostringstream ossSig; - ossSig << "mongo got signal " << sig << " (" << strsignal( sig ) << "), stack trace: " << endl; - mongo::rawOut( ossSig.str() ); - - ostringstream ossBt; - mongo::printStackTrace( ossBt ); - mongo::rawOut( ossBt.str() ); + log() << "mongo got signal " << sig << " (" << strsignal( sig ) << "), stack trace: "; + mongo::printStackTrace(); mongo::shell_utils::KillMongoProgramInstances(); ::_exit( 14 ); @@ -240,8 +235,7 @@ void quitAbruptly( int sig ) { // this will be called in certain c++ error cases, for example if there are two active // exceptions void myterminate() { - mongo::rawOut( "terminate() called in shell, printing stack:" ); - mongo::printStackTrace(); + mongo::printStackTrace(severe().stream() << "terminate() called in shell, printing stack:\n"); ::_exit( 14 ); } diff --git a/src/mongo/tools/bridge.cpp b/src/mongo/tools/bridge.cpp index a648321508f..422237c5b00 100644 --- a/src/mongo/tools/bridge.cpp +++ b/src/mongo/tools/bridge.cpp @@ -144,8 +144,7 @@ void cleanup( int sig ) { } #if !defined(_WIN32) void myterminate() { - rawOut( "bridge terminate() called, printing stack:" ); - printStackTrace(); + printStackTrace(severe().stream() << "bridge terminate() called, printing stack:\n"); ::abort(); } diff --git a/src/mongo/tools/tool_logger.cpp b/src/mongo/tools/tool_logger.cpp index 284d7186dc9..fd906ef18d1 100644 --- a/src/mongo/tools/tool_logger.cpp +++ b/src/mongo/tools/tool_logger.cpp @@ -89,8 +89,6 @@ MONGO_INITIALIZER_GENERAL(ToolLogRedirection, logger::globalLogDomain()->attachAppender(MessageLogDomain::AppenderAutoPtr( new ConsoleAppender( new MessageEventDetailsEncoder))); - - setRawOutToStderr(); } // Only put an appender on our informational messages if we did not use --quiet diff --git a/src/mongo/util/log.cpp b/src/mongo/util/log.cpp index 302e21d41e1..52fba9ae070 100644 --- a/src/mongo/util/log.cpp +++ b/src/mongo/util/log.cpp @@ -38,7 +38,6 @@ using namespace std; // TODO: Extra log context appending, and re-enable log_user_*.js // TODO: Eliminate cout/cerr. // TODO: LogIndent (for mongodump). -// TODO: Eliminate rawOut. namespace mongo { @@ -116,41 +115,6 @@ namespace mongo { return s.str(); } - namespace { - bool rawOutToStderr = false; - } // namespace - - void setRawOutToStderr() { - rawOutToStderr = true; - } - - /* - * NOTE(schwerin): Called from signal handlers; should not be taking locks or allocating - * memory. - */ - void rawOut(const StringData &s) { - // Can't use STDxxx_FILENO macros since they don't exist on windows. - const int fd = rawOutToStderr - ? 2 // STDERR_FILENO - : 1 // STDOUT_FILENO - ; - - const char* ptr = s.rawData(); - size_t bytesRemaining = s.size(); - while (bytesRemaining) { -#ifdef _WIN32 - int ret = _write(fd, ptr, bytesRemaining); -#else - ssize_t ret = write(fd, ptr, bytesRemaining); -#endif - if (ret < 0) - return; // Nothing to do. Can't even log since that is what is failing. - - ptr += ret; - bytesRemaining -= ret; - } - } - void logContext(const char *errmsg) { if ( errmsg ) { problem() << errmsg << endl; diff --git a/src/mongo/util/log.h b/src/mongo/util/log.h index 82aa5854f6e..bba73ff29c0 100644 --- a/src/mongo/util/log.h +++ b/src/mongo/util/log.h @@ -127,19 +127,6 @@ namespace logger { extern Tee* const startupWarningsLog; // Things put here get reported in MMS string errnoWithDescription(int errorcode = -1); - void rawOut( const StringData &s ); - - /* - * Redirects the output of "rawOut" to stderr. The default is stdout. - * - * NOTE: This needs to be here because the tools such as mongoexport and mongodump sometimes - * send data to stdout and share this code, so they need to be able to redirect output to - * stderr. Eventually rawOut should be replaced with something better and our tools should not - * need to call internal server shutdown functions. - * - * NOTE: This function is not thread safe and should not be called from a multithreaded context. - */ - void setRawOutToStderr(); /** * Write the current context (backtrace), along with the optional "msg". -- cgit v1.2.1