3737#include " mongo/db/commands/shutdown.h"
3838#include " mongo/db/commands/test_commands_enabled.h"
3939#include " mongo/db/log_process_details.h"
40+ #include " mongo/logv2/ramlog.h"
4041#include " mongo/scripting/engine.h"
4142#include " mongo/util/exit.h"
4243#include " mongo/util/fail_point.h"
5253namespace mongo {
5354namespace {
5455
55- using std::string;
56- using std::vector;
57-
5856class FeaturesCmd : public BasicCommand {
5957public:
6058 FeaturesCmd () : BasicCommand(" features" ) {}
@@ -71,7 +69,7 @@ class FeaturesCmd : public BasicCommand {
7169 const BSONObj& cmdObj,
7270 std::vector<Privilege>* out) const {} // No auth required
7371 virtual bool run (OperationContext* opCtx,
74- const string& ns,
72+ const std:: string& ns,
7573 const BSONObj& cmdObj,
7674 BSONObjBuilder& result) {
7775 if (getGlobalScriptEngine ()) {
@@ -112,7 +110,7 @@ class HostInfoCmd : public BasicCommand {
112110 out->push_back (Privilege (ResourcePattern::forClusterResource (), actions));
113111 }
114112 bool run (OperationContext* opCtx,
115- const string& dbname,
113+ const std:: string& dbname,
116114 const BSONObj& cmdObj,
117115 BSONObjBuilder& result) {
118116 ProcessInfo p;
@@ -165,7 +163,7 @@ class CmdGetCmdLineOpts : public BasicCommand {
165163 out->push_back (Privilege (ResourcePattern::forClusterResource (), actions));
166164 }
167165 virtual bool run (OperationContext* opCtx,
168- const string&,
166+ const std:: string&,
169167 const BSONObj& cmdObj,
170168 BSONObjBuilder& result) {
171169 result.append (" argv" , serverGlobalParams.argvArray );
@@ -195,7 +193,7 @@ class LogRotateCmd : public BasicCommand {
195193 out->push_back (Privilege (ResourcePattern::forClusterResource (), actions));
196194 }
197195 virtual bool run (OperationContext* opCtx,
198- const string& ns,
196+ const std:: string& ns,
199197 const BSONObj& cmdObj,
200198 BSONObjBuilder& result) {
201199 bool didRotate = rotateLogs (serverGlobalParams.logRenameOnRotate , serverGlobalParams.logV2 );
@@ -230,22 +228,34 @@ class GetLogCmd : public ErrmsgCommandDeprecated {
230228 return " { getLog : '*' } OR { getLog : 'global' }" ;
231229 }
232230
233- virtual bool errmsgRun (OperationContext* opCtx,
234- const string& dbname,
235- const BSONObj& cmdObj,
236- string& errmsg,
237- BSONObjBuilder& result) {
231+ bool errmsgRun (OperationContext* opCtx,
232+ const std::string& dbname,
233+ const BSONObj& cmdObj,
234+ std::string& errmsg,
235+ BSONObjBuilder& result) override {
236+ if (serverGlobalParams.logV2 ) {
237+ return errmsgRunImpl<logv2::RamLog>(opCtx, dbname, cmdObj, errmsg, result);
238+ }
239+ return errmsgRunImpl<RamLog>(opCtx, dbname, cmdObj, errmsg, result);
240+ }
241+
242+ template <typename RamLogType>
243+ bool errmsgRunImpl (OperationContext* opCtx,
244+ const std::string& dbname,
245+ const BSONObj& cmdObj,
246+ std::string& errmsg,
247+ BSONObjBuilder& result) {
238248 BSONElement val = cmdObj.firstElement ();
239249 if (val.type () != String) {
240250 uasserted (ErrorCodes::TypeMismatch,
241251 str::stream () << " Argument to getLog must be of type String; found "
242252 << val.toString (false ) << " of type " << typeName (val.type ()));
243253 }
244254
245- string p = val.String ();
255+ std:: string p = val.String ();
246256 if (p == " *" ) {
247- vector<string> names;
248- RamLog ::getNames (names);
257+ std:: vector<std:: string> names;
258+ RamLogType ::getNames (names);
249259
250260 BSONArrayBuilder arr;
251261 for (unsigned i = 0 ; i < names.size (); i++) {
@@ -254,12 +264,12 @@ class GetLogCmd : public ErrmsgCommandDeprecated {
254264
255265 result.appendArray (" names" , arr.arr ());
256266 } else {
257- RamLog * ramlog = RamLog ::getIfExists (p);
267+ RamLogType * ramlog = RamLogType ::getIfExists (p);
258268 if (!ramlog) {
259269 errmsg = str::stream () << " no RamLog named: " << p;
260270 return false ;
261271 }
262- RamLog ::LineIterator rl (ramlog);
272+ typename RamLogType ::LineIterator rl (ramlog);
263273
264274 result.appendNumber (" totalLinesWritten" , rl.getTotalLinesWritten ());
265275
@@ -298,7 +308,7 @@ class ClearLogCmd : public BasicCommand {
298308 }
299309
300310 virtual bool run (OperationContext* opCtx,
301- const string& dbname,
311+ const std:: string& dbname,
302312 const BSONObj& cmdObj,
303313 BSONObjBuilder& result) {
304314 std::string logName;
@@ -308,9 +318,15 @@ class ClearLogCmd : public BasicCommand {
308318 if (logName != " global" ) {
309319 uasserted (ErrorCodes::InvalidOptions, " Only the 'global' log can be cleared" );
310320 }
311- RamLog* ramlog = RamLog::getIfExists (logName);
312- invariant (ramlog);
313- ramlog->clear ();
321+ auto clearRamlog = [&](auto * ramlog) {
322+ invariant (ramlog);
323+ ramlog->clear ();
324+ };
325+ if (serverGlobalParams.logV2 ) {
326+ clearRamlog (logv2::RamLog::getIfExists (logName));
327+ } else {
328+ clearRamlog (RamLog::getIfExists (logName));
329+ }
314330 return true ;
315331 }
316332};
0 commit comments