0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 01:21:03 +01:00

getLog command so you access RamLog entries

This commit is contained in:
Eliot Horowitz 2011-05-02 14:26:02 -04:00
parent 008e375fe4
commit b501648032
5 changed files with 106 additions and 7 deletions

View File

@ -44,6 +44,7 @@
#include "stats/counters.h"
#include "background.h"
#include "../util/version.h"
#include "../util/ramlog.h"
namespace mongo {
@ -344,4 +345,50 @@ namespace mongo {
} availableQueryOptionsCmd;
class GetLogCmd : public Command {
public:
GetLogCmd() : Command( "getLog" ){}
virtual bool slaveOk() const { return true; }
virtual LockType locktype() const { return NONE; }
virtual bool requiresAuth() { return false; }
virtual bool adminOnly() const { return true; }
virtual void help( stringstream& help ) const {
help << "{ getLog : '*' } OR { getLog : 'global' }";
}
virtual bool run(const string& dbname , BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
string p = cmdObj.firstElement().String();
if ( p == "*" ) {
vector<string> names;
RamLog::getNames( names );
BSONArrayBuilder arr;
for ( unsigned i=0; i<names.size(); i++ ) {
arr.append( names[i] );
}
result.appendArray( "names" , arr.arr() );
}
else {
RamLog* rl = RamLog::get( p );
if ( ! rl ) {
errmsg = str::stream() << "no RamLog named: " << p;
return false;
}
vector<const char*> lines;
rl->get( lines );
BSONArrayBuilder arr( result.subarrayStart( "log" ) );
for ( unsigned i=0; i<lines.size(); i++ )
arr.append( lines[i] );
arr.done();
}
return true;
}
} getLogCmd;
}

View File

@ -313,7 +313,7 @@ namespace mongo {
virtual void init() {
assert( ! _log );
_log = new RamLog();
_log = new RamLog("global");
Logstream::get().addGlobalTee( _log );
}

View File

@ -43,8 +43,8 @@ namespace mongo {
using namespace mongoutils::html;
using namespace bson;
static RamLog _rsLog;
Tee *rsLog = &_rsLog;
static RamLog * _rsLog = new RamLog( "rs" );
Tee *rsLog = _rsLog;
extern bool replSetBlind;
string ago(time_t t) {
@ -340,7 +340,7 @@ namespace mongo {
void fillRsLog(stringstream& s) {
_rsLog.toHTML( s );
_rsLog->toHTML( s );
}
const Member* ReplSetImpl::findById(unsigned id) const {

View File

@ -25,10 +25,26 @@ namespace mongo {
using namespace mongoutils;
RamLog::RamLog() {
RamLog::RamLog( string name ) : _name(name) {
h = 0; n = 0;
for( int i = 0; i < N; i++ )
lines[i][C-1] = 0;
if ( name.size() ) {
if ( ! _namedLock )
_namedLock = new mongo::mutex("RamLog::_namedLock");
scoped_lock lk( *_namedLock );
if ( ! _named )
_named = new RM();
(*_named)[name] = this;
}
}
RamLog::~RamLog() {
}
void RamLog::write(LogLevel ll, const string& str) {
@ -129,5 +145,31 @@ namespace mongo {
s << "</pre>\n";
}
// ---------------
// static things
// ---------------
RamLog* RamLog::get( string name ) {
if ( ! _named )
return 0;
scoped_lock lk( *_namedLock );
RM::iterator i = _named->find( name );
if ( i == _named->end() )
return 0;
return i->second;
}
void RamLog::getNames( vector<string>& names ) {
if ( ! _named )
return;
scoped_lock lk( *_namedLock );
for ( RM::iterator i=_named->begin(); i!=_named->end(); ++i ) {
names.push_back( i->first );
}
}
mongo::mutex* RamLog::_namedLock;
RamLog::RM* RamLog::_named = 0;
}

View File

@ -23,7 +23,7 @@ namespace mongo {
class RamLog : public Tee {
public:
RamLog();
RamLog( string name );
virtual void write(LogLevel ll, const string& str);
@ -31,6 +31,10 @@ namespace mongo {
void toHTML(stringstream& s);
static RamLog* get( string name );
static void getNames( vector<string>& names );
protected:
static int repeats(const vector<const char *>& v, int i);
static string clean(const vector<const char *>& v, int i, string line="");
static string color(string line);
@ -38,8 +42,9 @@ namespace mongo {
/* turn http:... into an anchor */
static string linkify(const char *s);
private:
~RamLog(); // want this private as we want to leak so we can use them till the very end
enum {
N = 128, // number of links
C = 256 // max size of line
@ -47,6 +52,11 @@ namespace mongo {
char lines[N][C];
unsigned h; // current position
unsigned n; // numer of lines stores 0 o N
string _name;
typedef map<string,RamLog*> RM;
static mongo::mutex* _namedLock;
static RM* _named;
};
}