Skip to content

Commit

Permalink
Merge pull request mongodb#135 from hoffrocket/master
Browse files Browse the repository at this point in the history
syslog support
  • Loading branch information
erh committed Oct 28, 2011
2 parents 5fdc11d + 377f484 commit 6bf3004
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 16 deletions.
20 changes: 15 additions & 5 deletions db/cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ namespace mongo {
("nounixsocket", "disable listening on unix sockets")
("unixSocketPrefix", po::value<string>(), "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
;

Expand Down Expand Up @@ -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<string>();
assert( logpath.size() );
Expand Down Expand Up @@ -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<string>();
uassert( 10033 , "logpath has to be non-zero" , logpath.size() );
Expand Down
18 changes: 12 additions & 6 deletions s/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -232,7 +238,7 @@ int _main(int argc, char* argv[]) {
}

if ( params.count( "version" ) ) {
printShardingVersionInfo();
printShardingVersionInfo(true);
return 0;
}

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions util/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,6 @@ namespace mongo {

// done *before* static initialization
FILE* Logstream::logfile = stdout;
bool Logstream::isSyslog = false;

}
49 changes: 44 additions & 5 deletions util/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "../bson/util/builder.h"

#ifndef _WIN32
//#include <syslog.h>
#include <syslog.h>
#endif

namespace mongo {
Expand All @@ -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:
Expand Down Expand Up @@ -203,14 +224,28 @@ namespace mongo {
static FILE* logfile;
static boost::scoped_ptr<ostream> stream;
static vector<Tee*> * globalTees;
static bool isSyslog;
public:
inline static void logLockless( const StringData& s );

static void setLogFile(FILE* f) {
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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -496,9 +536,10 @@ namespace mongo {
for ( unsigned i=0; i<globalTees->size(); 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);
Expand All @@ -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();
}
Expand Down

0 comments on commit 6bf3004

Please sign in to comment.