diff options
author | Andy Schwerin <schwerin@10gen.com> | 2013-07-10 10:27:46 -0400 |
---|---|---|
committer | Andy Schwerin <schwerin@10gen.com> | 2013-07-10 12:26:15 -0400 |
commit | 61c4b0358b61b4cf4630999f45ca34d0cafd4a66 (patch) | |
tree | 6b4af4f55d8ed753ddda4f2dc44e3a6f5f27ef82 | |
parent | 2412e834cf3505f69a57b5443dc3484b171c0b68 (diff) | |
download | mongo-61c4b0358b61b4cf4630999f45ca34d0cafd4a66.tar.gz |
SERVER-10084 When --logpath is specified and --logappend is not, rotate away preexisting log files.
This fixes jstests/slowNightly/logpath.js, and brings behavior in line with prior versions.
-rw-r--r-- | src/mongo/db/initialize_server_global_state.cpp | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/src/mongo/db/initialize_server_global_state.cpp b/src/mongo/db/initialize_server_global_state.cpp index 2276b165e47..084109d11b7 100644 --- a/src/mongo/db/initialize_server_global_state.cpp +++ b/src/mongo/db/initialize_server_global_state.cpp @@ -206,13 +206,40 @@ namespace mongo { if (!cmdLine.logpath.empty()) { fassert(16448, !cmdLine.logWithSyslog); - string absoluteLogpath = boost::filesystem::absolute( + std::string absoluteLogpath = boost::filesystem::absolute( cmdLine.logpath, cmdLine.cwd).string(); + + const bool exists = boost::filesystem::exists(absoluteLogpath); + + if (exists) { + if (boost::filesystem::is_directory(absoluteLogpath)) { + return Status(ErrorCodes::FileNotOpen, mongoutils::str::stream() << + "logpath \"" << absoluteLogpath << + "\" should name a file, not a directory."); + } + + if (!cmdLine.logAppend && boost::filesystem::is_regular(absoluteLogpath)) { + std::string renameTarget = absoluteLogpath + "." + terseCurrentTime(false); + if (0 == rename(absoluteLogpath.c_str(), renameTarget.c_str())) { + log() << "log file \"" << absoluteLogpath + << "\" exists; moved to \"" << renameTarget << "\"."; + } + else { + return Status(ErrorCodes::FileRenameFailed, mongoutils::str::stream() << + "Could not rename preexisting log file \"" << + absoluteLogpath << "\" to \"" << renameTarget << + "\"; run with --logappend or manually remove file: " << + errnoWithDescription()); + } + } + } + StatusWithRotatableFileWriter writer = logger::globalRotatableFileManager()->openFile(absoluteLogpath, cmdLine.logAppend); if (!writer.isOK()) { return writer.getStatus(); } + LogManager* manager = logger::globalLogManager(); manager->getGlobalDomain()->clearAppenders(); manager->getGlobalDomain()->attachAppender( @@ -223,6 +250,18 @@ namespace mongo { MessageLogDomain::AppenderAutoPtr( new RotatableFileAppender<MessageEventEphemeral>( new MessageEventDetailsEncoder, writer.getValue()))); + + if (cmdLine.logAppend && exists) { + log() << std::endl; + log() << std::endl; + log() << "***** SERVER RESTARTED *****"; + log() << std::endl; + log() << std::endl; + Status status = + logger::RotatableFileWriter::Use(writer.getValue()).status(); + if (!status.isOK()) + return status; + } } return Status::OK(); |