Skip to content

Commit

Permalink
storage_service: specialize fmt::formatter<storage_service::mode>
Browse files Browse the repository at this point in the history
this is a part of a series to migrating from `operator<<(ostream&, ..)`
based formatting to fmtlib based formatting. the goal here is to enable
fmtlib to print `storage_service::mode` without the help of `operator<<`.

the corresponding `operator<<()` for `storage_service::mode` is removed
in this change, as all its callers are now using fmtlib for formatting
now.

Refs scylladb#13245

Signed-off-by: Kefu Chai <[email protected]>

Closes scylladb#13640
  • Loading branch information
tchaikov authored and kbr-scylla committed Apr 25, 2023
1 parent a717c80 commit 5804eb6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
18 changes: 0 additions & 18 deletions service/storage_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2841,24 +2841,6 @@ future<std::map<gms::inet_address, float>> storage_service::effective_ownership(
});
}

static const std::map<storage_service::mode, sstring> mode_names = {
{storage_service::mode::NONE, "STARTING"},
{storage_service::mode::STARTING, "STARTING"},
{storage_service::mode::NORMAL, "NORMAL"},
{storage_service::mode::JOINING, "JOINING"},
{storage_service::mode::BOOTSTRAP, "BOOTSTRAP"},
{storage_service::mode::LEAVING, "LEAVING"},
{storage_service::mode::DECOMMISSIONED, "DECOMMISSIONED"},
{storage_service::mode::MOVING, "MOVING"},
{storage_service::mode::DRAINING, "DRAINING"},
{storage_service::mode::DRAINED, "DRAINED"},
};

std::ostream& operator<<(std::ostream& os, const storage_service::mode& m) {
os << mode_names.at(m);
return os;
}

void storage_service::set_mode(mode m) {
if (m != _operation_mode) {
slogger.info("entering {} mode", m);
Expand Down
23 changes: 22 additions & 1 deletion service/storage_service.hh
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ public:
enum class mode { NONE, STARTING, JOINING, BOOTSTRAP, NORMAL, LEAVING, DECOMMISSIONED, MOVING, DRAINING, DRAINED };
private:
mode _operation_mode = mode::NONE;
friend std::ostream& operator<<(std::ostream& os, const mode& mode);
/* Used for tracking drain progress */

endpoint_lifecycle_notifier& _lifecycle_notifier;
Expand Down Expand Up @@ -797,3 +796,25 @@ private:
};

}

template <>
struct fmt::formatter<service::storage_service::mode> : fmt::formatter<std::string_view> {
template <typename FormatContext>
auto format(service::storage_service::mode mode, FormatContext& ctx) const {
std::string_view name;
using enum service::storage_service::mode;
switch (mode) {
case NONE: name = "STARTING"; break;
case STARTING: name = "STARTING"; break;
case NORMAL: name = "NORMAL"; break;
case JOINING: name = "JOINING"; break;
case BOOTSTRAP: name = "BOOTSTRAP"; break;
case LEAVING: name = "LEAVING"; break;
case DECOMMISSIONED: name = "DECOMMISSIONED"; break;
case MOVING: name = "MOVING"; break;
case DRAINING: name = "DRAINING"; break;
case DRAINED: name = "DRAINED"; break;
}
return fmt::format_to(ctx.out(), "{}", name);
}
};

0 comments on commit 5804eb6

Please sign in to comment.