diff --git a/db/cmdline.cpp b/db/cmdline.cpp index 638253edcd3f0..012451cb64369 100644 --- a/db/cmdline.cpp +++ b/db/cmdline.cpp @@ -68,6 +68,7 @@ namespace mongo { ("nounixsocket", "disable listening on unix sockets") ("unixSocketPrefix", po::value(), "alternative directory for UNIX domain sockets (defaults to /tmp)") ("fork" , "fork server process" ) + ("syslog" , "log to system's syslog facility instead of file or stdout" ) #endif ; @@ -269,13 +270,12 @@ namespace mongo { if (params.count("fork")) { cmdLine.doFork = true; - - if ( ! params.count( "logpath" ) ) { - cout << "--fork has to be used with --logpath" << endl; + if ( ! params.count( "logpath" ) && ! params.count( "syslog" ) ) { + cout << "--fork has to be used with --logpath or --syslog" << endl; ::exit(-1); } - { + if ( params.count( "logpath" ) ) { // test logpath logpath = params["logpath"].as(); assert( logpath.size() ); @@ -357,9 +357,19 @@ namespace mongo { setupCoreSignals(); setupSignals( true ); } - + + if (params.count("syslog")) { + StringBuilder sb(128); + sb << cmdLine.binaryName << "." << cmdLine.port; + Logstream::useSyslog( sb.str().c_str() ); + } #endif if (params.count("logpath")) { + if ( params.count("syslog") ) { + cout << "Cant use both a logpath and syslog " << endl; + ::exit(-1); + } + if ( logpath.size() == 0 ) logpath = params["logpath"].as(); uassert( 10033 , "logpath has to be non-zero" , logpath.size() ); diff --git a/s/server.cpp b/s/server.cpp index 25494f1a09ac8..88f89fd86212a 100644 --- a/s/server.cpp +++ b/s/server.cpp @@ -179,10 +179,16 @@ namespace mongo { return 0; } - void printShardingVersionInfo() { - log() << mongosCommand << " " << mongodVersion() << " starting (--help for usage)" << endl; - printGitVersion(); - printSysInfo(); + void printShardingVersionInfo(bool out) { + if (out) { + cout << mongosCommand << " " << mongodVersion() << " starting (--help for usage)" << endl; + cout << "git version: " << gitVersion() << endl; + cout << "build sys info: " << sysInfo() << endl; + } else { + log() << mongosCommand << " " << mongodVersion() << " starting (--help for usage)" << endl; + printGitVersion(); + printSysInfo(); + } } void cloudCmdLineParamIs(string cmd); @@ -232,7 +238,7 @@ int _main(int argc, char* argv[]) { } if ( params.count( "version" ) ) { - printShardingVersionInfo(); + printShardingVersionInfo(true); return 0; } @@ -331,7 +337,7 @@ int _main(int argc, char* argv[]) { return 1; } - printShardingVersionInfo(); + printShardingVersionInfo(false); if ( ! configServer.init( configdbs ) ) { cout << "couldn't resolve config db address" << endl; diff --git a/util/log.cpp b/util/log.cpp index 51f59be239bd1..9e18609a07a1c 100644 --- a/util/log.cpp +++ b/util/log.cpp @@ -153,5 +153,6 @@ namespace mongo { // done *before* static initialization FILE* Logstream::logfile = stdout; + bool Logstream::isSyslog = false; } diff --git a/util/log.h b/util/log.h index fa2f6122697d0..bfd782602e708 100644 --- a/util/log.h +++ b/util/log.h @@ -22,7 +22,7 @@ #include "../bson/util/builder.h" #ifndef _WIN32 -//#include +#include #endif namespace mongo { @@ -45,6 +45,27 @@ namespace mongo { return "UNKNOWN"; } } + +#ifndef _WIN32 + inline const int logLevelToSysLogLevel( LogLevel l) { + switch ( l ) { + case LL_DEBUG: + return LOG_DEBUG; + case LL_INFO: + return LOG_INFO; + case LL_NOTICE: + return LOG_NOTICE; + case LL_WARNING: + return LOG_WARNING; + case LL_ERROR: + return LOG_ERR; + case LL_SEVERE: + return LOG_CRIT; + default: + return LL_INFO; + } + } +#endif class LabeledLevel { public: @@ -203,6 +224,7 @@ namespace mongo { static FILE* logfile; static boost::scoped_ptr stream; static vector * globalTees; + static bool isSyslog; public: inline static void logLockless( const StringData& s ); @@ -210,7 +232,20 @@ namespace mongo { scoped_lock lk(mutex); logfile = f; } +#ifndef _WIN32 + static void useSyslog(const char * name) { + cout << "using syslog ident: " << name << endl; + + // openlog requires heap allocated non changing pointer + // this should only be called once per pragram execution + char * newName = (char *) malloc( strlen(name) + 1 ); + strcpy( newName , name); + openlog( newName , LOG_ODELAY , LOG_USER ); + isSyslog = true; + } +#endif + static int magicNumber() { return 1717; } @@ -442,6 +477,11 @@ namespace mongo { return; if ( doneSetup == 1717 ) { +#ifndef _WIN32 + if ( isSyslog ) { + syslog( LOG_INFO , "%s" , s.data() ); + } else +#endif if (fwrite(s.data(), s.size(), 1, logfile)) { fflush(logfile); } @@ -496,9 +536,10 @@ namespace mongo { for ( unsigned i=0; isize(); i++ ) (*globalTees)[i]->write(logLevel,out); } - #ifndef _WIN32 - //syslog( LOG_INFO , "%s" , cc ); + if ( useSyslog ) { + syslog( logLevelToSysLogLevel(logLevel) , "%s" , out.data() ); + } else #endif if(fwrite(out.data(), out.size(), 1, logfile)) { fflush(logfile); @@ -507,12 +548,10 @@ namespace mongo { int x = errno; cout << "Failed to write to logfile: " << errnoWithDescription(x) << ": " << out << endl; } - #ifdef POSIX_FADV_DONTNEED // This only applies to pages that have already been flushed RARELY posix_fadvise(fileno(logfile), 0, 0, POSIX_FADV_DONTNEED); #endif - } _init(); }