summaryrefslogtreecommitdiff
path: root/src/mongo/logv2
diff options
context:
space:
mode:
authorBilly Donahue <billy.donahue@mongodb.com>2020-03-25 17:04:16 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-04-09 17:42:49 +0000
commitd7db0e15e0b97e97404e029abdf2c05d3ec9303d (patch)
treed3c209ef520af1941a7697ff15388273954d6589 /src/mongo/logv2
parent582afa698c2927266b6954b9170afb20ba58da79 (diff)
downloadmongo-d7db0e15e0b97e97404e029abdf2c05d3ec9303d.tar.gz
SERVER-46801 tabular LogComponent defs
Diffstat (limited to 'src/mongo/logv2')
-rw-r--r--src/mongo/logv2/log_component.cpp246
-rw-r--r--src/mongo/logv2/log_component.h87
-rw-r--r--src/mongo/logv2/logv2_test.cpp16
3 files changed, 119 insertions, 230 deletions
diff --git a/src/mongo/logv2/log_component.cpp b/src/mongo/logv2/log_component.cpp
index d7122f4f082..ced2b349a72 100644
--- a/src/mongo/logv2/log_component.cpp
+++ b/src/mongo/logv2/log_component.cpp
@@ -31,7 +31,6 @@
#include "mongo/logv2/log_component.h"
-#include "mongo/base/init.h"
#include "mongo/base/static_assert.h"
#include "mongo/util/assert_util.h"
@@ -39,220 +38,73 @@ namespace mongo::logv2 {
namespace {
-// Component dotted names.
-// Lazily evaluated in LogComponent::getDottedName().
-std::string _dottedNames[LogComponent::kNumLogComponents + 1];
+struct {
+ LogComponent value;
+ StringData shortName;
+ StringData logName;
+ LogComponent parent;
+} constexpr kTable[] = {
+#define X_(id, val, shortName, logName, parent) \
+ {LogComponent::id, shortName##_sd, logName##_sd, LogComponent::parent},
+ MONGO_EXPAND_LOGV2_COMPONENT(X_)
+#undef X_
+};
-//
-// Fully initialize _dottedNames before we enter multithreaded execution.
-//
-
-MONGO_INITIALIZER_WITH_PREREQUISITES(SetupDottedNamesV2, MONGO_NO_PREREQUISITES)
-(InitializerContext* context) {
- for (int i = 0; i <= int(LogComponent::kNumLogComponents); ++i) {
- logv2::LogComponent component = static_cast<logv2::LogComponent::Value>(i);
- component.getDottedName();
+// Children always come after parent component.
+// This makes it unnecessary to compute children of each component
+// when setting/clearing log severities in LogComponentSettings.
+constexpr bool correctParentOrder(LogComponent id, LogComponent parent) {
+ using V = LogComponent::Value;
+ switch (V{id}) {
+ case LogComponent::kAutomaticDetermination:
+ case LogComponent::kDefault:
+ case LogComponent::kNumLogComponents:
+ return true;
+ default:
+ using I = std::underlying_type_t<V>;
+ return I{id} > I{parent};
}
-
- return Status::OK();
}
+#define X_(id, val, shortName, logName, parent) \
+ MONGO_STATIC_ASSERT(correctParentOrder(LogComponent::id, LogComponent::parent));
+MONGO_EXPAND_LOGV2_COMPONENT(X_)
+#undef X_
} // namespace
-// Children always come after parent component.
-// This makes it unnecessary to compute children of each component
-// when setting/clearing log severities in LogComponentSettings.
-#define DECLARE_LOG_COMPONENT_PARENT(CHILD, PARENT) \
- case (CHILD): \
- do { \
- MONGO_STATIC_ASSERT(int(CHILD) > int(PARENT)); \
- return (PARENT); \
- } while (0)
-
LogComponent LogComponent::parent() const {
- switch (_value) {
- case kDefault:
- return kNumLogComponents;
- DECLARE_LOG_COMPONENT_PARENT(kJournal, kStorage);
- DECLARE_LOG_COMPONENT_PARENT(kASIO, kNetwork);
- DECLARE_LOG_COMPONENT_PARENT(kConnectionPool, kNetwork);
- DECLARE_LOG_COMPONENT_PARENT(kBridge, kNetwork);
- DECLARE_LOG_COMPONENT_PARENT(kReplicationElection, kReplication);
- DECLARE_LOG_COMPONENT_PARENT(kReplicationHeartbeats, kReplication);
- DECLARE_LOG_COMPONENT_PARENT(kReplicationInitialSync, kReplication);
- DECLARE_LOG_COMPONENT_PARENT(kReplicationRollback, kReplication);
- DECLARE_LOG_COMPONENT_PARENT(kShardingCatalogRefresh, kSharding);
- DECLARE_LOG_COMPONENT_PARENT(kShardingMigration, kSharding);
- DECLARE_LOG_COMPONENT_PARENT(kStorageRecovery, kStorage);
- case kNumLogComponents:
- return kNumLogComponents;
- default:
- return kDefault;
- }
- MONGO_UNREACHABLE;
+ return kTable[_value].parent;
}
StringData LogComponent::toStringData() const {
- switch (_value) {
- case kDefault:
- return "default"_sd;
- case kAccessControl:
- return "accessControl"_sd;
- case kCommand:
- return "command"_sd;
- case kControl:
- return "control"_sd;
- case kExecutor:
- return "executor"_sd;
- case kGeo:
- return "geo"_sd;
- case kIndex:
- return "index"_sd;
- case kNetwork:
- return "network"_sd;
- case kQuery:
- return "query"_sd;
- case kReplication:
- return "replication"_sd;
- case kReplicationElection:
- return "election"_sd;
- case kReplicationHeartbeats:
- return "heartbeats"_sd;
- case kReplicationInitialSync:
- return "initialSync"_sd;
- case kReplicationRollback:
- return "rollback"_sd;
- case kSharding:
- return "sharding"_sd;
- case kShardingCatalogRefresh:
- return "shardingCatalogRefresh"_sd;
- case kShardingMigration:
- return "migration"_sd;
- case kStorage:
- return "storage"_sd;
- case kStorageRecovery:
- return "recovery"_sd;
- case kJournal:
- return "journal"_sd;
- case kWrite:
- return "write"_sd;
- case kFTDC:
- return "ftdc"_sd;
- case kASIO:
- return "asio"_sd;
- case kBridge:
- return "bridge"_sd;
- case kTracking:
- return "tracking"_sd;
- case kTransaction:
- return "transaction"_sd;
- case kConnectionPool:
- return "connectionPool"_sd;
- case kTest:
- return "test"_sd;
- case kNumLogComponents:
- return "total"_sd;
- case kAutomaticDetermination:
- // We should not reach this
- break;
- // No default. Compiler should complain if there's a log component that's not handled.
- }
- MONGO_UNREACHABLE;
+ return kTable[_value].shortName;
+}
+
+StringData LogComponent::getNameForLog() const {
+ return kTable[_value].logName;
}
std::string LogComponent::getShortName() const {
- return toStringData().toString();
+ return std::string{toStringData()};
}
-std::string LogComponent::getDottedName() const {
- // Lazily evaluate dotted names in anonymous namespace.
- if (_dottedNames[_value].empty()) {
- switch (_value) {
- case kDefault:
- _dottedNames[_value] = getShortName();
- break;
- case kNumLogComponents:
- _dottedNames[_value] = getShortName();
- break;
- default:
- // Omit short name of 'default' component from dotted name.
- if (parent() == kDefault) {
- _dottedNames[_value] = getShortName();
- } else {
- _dottedNames[_value] = parent().getDottedName() + "." + getShortName();
- }
- break;
- }
+namespace {
+void _appendDottedName(LogComponent id, std::string* out) {
+ if (id.parent() != LogComponent::kDefault) {
+ _appendDottedName(id.parent(), out);
+ out->append(".");
}
- return _dottedNames[_value];
+ StringData shortName = id.toStringData();
+ out->append(shortName.begin(), shortName.end());
}
+} // namespace
-StringData LogComponent::getNameForLog() const {
- switch (_value) {
- case kDefault:
- return "-"_sd;
- case kAccessControl:
- return "ACCESS"_sd;
- case kCommand:
- return "COMMAND"_sd;
- case kControl:
- return "CONTROL"_sd;
- case kExecutor:
- return "EXECUTOR"_sd;
- case kGeo:
- return "GEO"_sd;
- case kIndex:
- return "INDEX"_sd;
- case kNetwork:
- return "NETWORK"_sd;
- case kQuery:
- return "QUERY"_sd;
- case kReplication:
- return "REPL"_sd;
- case kReplicationElection:
- return "ELECTION"_sd;
- case kReplicationHeartbeats:
- return "REPL_HB"_sd;
- case kReplicationInitialSync:
- return "INITSYNC"_sd;
- case kReplicationRollback:
- return "ROLLBACK"_sd;
- case kSharding:
- return "SHARDING"_sd;
- case kShardingCatalogRefresh:
- return "SH_REFR"_sd;
- case kShardingMigration:
- return "MIGRATE"_sd;
- case kStorage:
- return "STORAGE"_sd;
- case kStorageRecovery:
- return "RECOVERY"_sd;
- case kJournal:
- return "JOURNAL"_sd;
- case kWrite:
- return "WRITE"_sd;
- case kFTDC:
- return "FTDC"_sd;
- case kASIO:
- return "ASIO"_sd;
- case kBridge:
- return "BRIDGE"_sd;
- case kTracking:
- return "TRACKING"_sd;
- case kTransaction:
- return "TXN"_sd;
- case kConnectionPool:
- return "CONNPOOL"_sd;
- case kTest:
- return "TEST"_sd;
- case kNumLogComponents:
- return "TOTAL"_sd;
- case kAutomaticDetermination:
- // We should not reach this
- break;
- // No default. Compiler should complain if there's a log component that's not handled.
- }
- MONGO_UNREACHABLE;
+std::string LogComponent::getDottedName() const {
+ if (*this == kDefault || *this == kNumLogComponents)
+ return std::string{toStringData()};
+ std::string out;
+ _appendDottedName(*this, &out);
+ return out;
}
std::ostream& operator<<(std::ostream& os, LogComponent component) {
diff --git a/src/mongo/logv2/log_component.h b/src/mongo/logv2/log_component.h
index f9a2a7345c7..df5cd2daac1 100644
--- a/src/mongo/logv2/log_component.h
+++ b/src/mongo/logv2/log_component.h
@@ -36,6 +36,51 @@
namespace mongo::logv2 {
+// clang-format off
+/**
+ * id: The enum identifier for the LogComponent.
+ * val: (empty except for kDefault) an expression used to assign value to the enum.
+ * shortName: its short name, used in component related server options.
+ * logName: The key that appears in log the "c" field. Should fit into 8 columns, as
+ * we pad the `c` field in json logs to 8 columns.
+ * parent: Components are arranged in a hierarchy for the purposes of log filtering. The
+ * dottedName that is used to configure log filtering is a parent-recursive "."-join
+ * of shortName strings.
+ */
+#define MONGO_EXPAND_LOGV2_COMPONENT(X) \
+/* (id, val , shortName , logName , parent) */ \
+ X(kDefault, = 0 , "default" , "-" , kNumLogComponents) \
+ X(kAccessControl, , "accessControl" , "ACCESS" , kDefault) \
+ X(kCommand, , "command" , "COMMAND" , kDefault) \
+ X(kControl, , "control" , "CONTROL" , kDefault) \
+ X(kExecutor, , "executor" , "EXECUTOR", kDefault) \
+ X(kGeo, , "geo" , "GEO" , kDefault) \
+ X(kIndex, , "index" , "INDEX" , kDefault) \
+ X(kNetwork, , "network" , "NETWORK" , kDefault) \
+ X(kQuery, , "query" , "QUERY" , kDefault) \
+ X(kReplication, , "replication" , "REPL" , kDefault) \
+ X(kReplicationElection, , "election" , "ELECTION", kReplication) \
+ X(kReplicationHeartbeats, , "heartbeats" , "REPL_HB" , kReplication) \
+ X(kReplicationInitialSync, , "initialSync" , "INITSYNC", kReplication) \
+ X(kReplicationRollback, , "rollback" , "ROLLBACK", kReplication) \
+ X(kSharding, , "sharding" , "SHARDING", kDefault) \
+ X(kShardingCatalogRefresh, , "shardingCatalogRefresh", "SH_REFR" , kSharding) \
+ X(kShardingMigration, , "migration" , "MIGRATE" , kSharding) \
+ X(kStorage, , "storage" , "STORAGE" , kDefault) \
+ X(kStorageRecovery, , "recovery" , "RECOVERY", kStorage) \
+ X(kJournal, , "journal" , "JOURNAL" , kStorage) \
+ X(kWrite, , "write" , "WRITE" , kDefault) \
+ X(kFTDC, , "ftdc" , "FTDC" , kDefault) \
+ X(kASIO, , "asio" , "ASIO" , kNetwork) \
+ X(kBridge, , "bridge" , "BRIDGE" , kNetwork) \
+ X(kTracking, , "tracking" , "TRACKING", kDefault) \
+ X(kTransaction, , "transaction" , "TXN" , kDefault) \
+ X(kConnectionPool, , "connectionPool" , "CONNPOOL", kNetwork) \
+ X(kTest, , "test" , "TEST" , kDefault) \
+ X(kNumLogComponents, , "total" , "TOTAL" , kNumLogComponents) \
+ /**/
+// clang-format on
+
/**
* Log components.
* Debug messages logged using the LOG() or MONGO_LOG_COMPONENT().
@@ -44,43 +89,19 @@ namespace mongo::logv2 {
class LogComponent {
public:
enum Value {
- // kAutomaticDetermination is placeholder for using component set by
- // MONGO_LOGV2_DEFAULT_COMPONENT macro
+ // clang-format off
+ /** Placeholder for using the component set by the MONGO_LOGV2_DEFAULT_COMPONENT macro */
kAutomaticDetermination = -1,
- kDefault = 0,
- kAccessControl,
- kCommand,
- kControl,
- kExecutor,
- kGeo,
- kIndex,
- kNetwork,
- kQuery,
- kReplication,
- kReplicationElection,
- kReplicationHeartbeats,
- kReplicationInitialSync,
- kReplicationRollback,
- kSharding,
- kShardingCatalogRefresh,
- kShardingMigration,
- kStorage,
- kStorageRecovery,
- kJournal,
- kWrite,
- kFTDC,
- kASIO,
- kBridge,
- kTracking,
- kTransaction,
- kConnectionPool,
- kTest,
- kNumLogComponents
+#define X_(id, val, shortName, logName, parent) id val,
+MONGO_EXPAND_LOGV2_COMPONENT(X_)
+#undef X_
+ // clang-format on
};
- /* implicit */ LogComponent(Value value) : _value(value) {}
+ /* implicit */
+ constexpr LogComponent(Value value) : _value(value) {}
- operator Value() const {
+ constexpr operator Value() const {
return _value;
}
diff --git a/src/mongo/logv2/logv2_test.cpp b/src/mongo/logv2/logv2_test.cpp
index 5ee6d8c6af8..94d9e646c68 100644
--- a/src/mongo/logv2/logv2_test.cpp
+++ b/src/mongo/logv2/logv2_test.cpp
@@ -287,6 +287,22 @@ private:
std::vector<boost::shared_ptr<boost::log::sinks::sink>> _attachedSinks;
};
+TEST(LogV2Component, DottedName) {
+ struct {
+ LogComponent lc;
+ StringData expect;
+ } const kSpecs[] = {
+ {LogComponent::kDefault, "default"},
+ {LogComponent::kAccessControl, "accessControl"},
+ {LogComponent::kReplication, "replication"},
+ {LogComponent::kReplicationElection, "replication.election"},
+ {LogComponent::kNumLogComponents, "total"},
+ };
+ for (auto&& spec : kSpecs) {
+ ASSERT_EQ(spec.lc.getDottedName(), spec.expect) << spec.lc;
+ }
+}
+
TEST_F(LogV2Test, Basic) {
auto lines = makeLineCapture(PlainFormatter());