diff options
author | Shaun Verch <shaun.verch@10gen.com> | 2013-10-30 15:06:50 -0400 |
---|---|---|
committer | Shaun Verch <shaun.verch@10gen.com> | 2013-11-26 23:25:26 -0500 |
commit | 1a78c32529cd9378479170b7577b1eb2509c61ca (patch) | |
tree | 30935cb856590914347bc2d87dea053556c2d67b | |
parent | 08790c849539ff868c101050860e56154297145f (diff) | |
download | mongo-1a78c32529cd9378479170b7577b1eb2509c61ca.tar.gz |
SERVER-10886 Create function to override rawOut destination stream to avoid corrupting tool output
-rw-r--r-- | src/mongo/tools/tool_logger.cpp | 3 | ||||
-rw-r--r-- | src/mongo/util/log.cpp | 18 | ||||
-rw-r--r-- | src/mongo/util/log.h | 12 |
3 files changed, 31 insertions, 2 deletions
diff --git a/src/mongo/tools/tool_logger.cpp b/src/mongo/tools/tool_logger.cpp index 619fd7c36ca..1661c5f9fe1 100644 --- a/src/mongo/tools/tool_logger.cpp +++ b/src/mongo/tools/tool_logger.cpp @@ -26,6 +26,7 @@ #include "mongo/logger/message_event.h" #include "mongo/logger/message_event_utf8_encoder.h" #include "mongo/tools/tool_options.h" +#include "mongo/util/log.h" namespace mongo { namespace { @@ -88,6 +89,8 @@ MONGO_INITIALIZER_GENERAL(ToolLogRedirection, logger::globalLogDomain()->attachAppender(MessageLogDomain::AppenderAutoPtr( new ConsoleAppender<MessageEventEphemeral, ErrorConsole>( new MessageEventDetailsEncoder))); + + setRawOutDestination(stderr); } // 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 b778b8cc006..ae24f924ffb 100644 --- a/src/mongo/util/log.cpp +++ b/src/mongo/util/log.cpp @@ -110,16 +110,30 @@ namespace mongo { return s.str(); } + namespace { + // NOTE: We can't directly set this variable to something like "stdout" because we need to + // make sure that it is initialized properly before static initialization. + FILE *rawOutDestination = NULL; + } // namespace + + void setRawOutDestination(FILE *outDestination) { + rawOutDestination = outDestination; + } + /* * NOTE(schwerin): Called from signal handlers; should not be taking locks or allocating * memory. */ void rawOut(const StringData &s) { + FILE* dest = rawOutDestination; + if (!dest) { + dest = stdout; + } for (size_t i = 0; i < s.size(); ++i) { #ifdef _WIN32 - putc(s[i], stdout); + putc(s[i], dest); #else - putc_unlocked(s[i], stdout); + putc_unlocked(s[i], dest); #endif } } diff --git a/src/mongo/util/log.h b/src/mongo/util/log.h index 62117d519d3..f556e7708f0 100644 --- a/src/mongo/util/log.h +++ b/src/mongo/util/log.h @@ -129,6 +129,18 @@ namespace logger { string errnoWithDescription(int errorcode = -1); void rawOut( const StringData &s ); + /* + * Redirects the output of "rawOut" to "outDestination". 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 setRawOutDestination(FILE *outDestination); + /** * Write the current context (backtrace), along with the optional "msg". */ |