diff --git a/src/mgr/Action.cc b/src/mgr/Action.cc index 2c8bf63bf57..7afa0f6a366 100644 --- a/src/mgr/Action.cc +++ b/src/mgr/Action.cc @@ -9,6 +9,7 @@ /* DEBUG: section 16 Cache Manager API */ #include "squid.h" +#include "base/PackableStream.h" #include "CacheManager.h" #include "comm/Connection.h" #include "HttpReply.h" @@ -112,7 +113,7 @@ Mgr::Action::fillEntry(StoreEntry* entry, bool writeHttpHeader) entry->replaceHttpReply(rep); } - dump(entry); + dump(entry); // TODO: replace with report() when all children are converted entry->flush(); @@ -120,3 +121,10 @@ Mgr::Action::fillEntry(StoreEntry* entry, bool writeHttpHeader) entry->complete(); } +void +Mgr::Action::dump(StoreEntry *entry) +{ + PackableStream os(*entry); + report(os); +} + diff --git a/src/mgr/Action.h b/src/mgr/Action.h index bf09b0e1519..9100f32cad6 100644 --- a/src/mgr/Action.h +++ b/src/mgr/Action.h @@ -14,6 +14,8 @@ #include "ipc/forward.h" #include "mgr/forward.h" +#include + class StoreEntry; namespace Mgr @@ -76,11 +78,14 @@ class Action: public RefCountable /// calculate and keep local action-specific information virtual void collect() {} + /// write manager report output to a stream. + virtual void report(std::ostream &) {} + /** start writing action-specific info to Store entry; * may collect info during dump, especially if collect() did nothing * non-atomic() actions may continue writing asynchronously after returning */ - virtual void dump(StoreEntry *) {} + virtual void dump(StoreEntry *); private: const CommandPointer cmd; ///< the command that caused this action diff --git a/src/mgr/BasicActions.cc b/src/mgr/BasicActions.cc index 341dcfad755..49c55c4c695 100644 --- a/src/mgr/BasicActions.cc +++ b/src/mgr/BasicActions.cc @@ -30,12 +30,6 @@ Mgr::IndexAction::IndexAction(const Command::Pointer &aCmd): Action(aCmd) debugs(16, 5, MYNAME); } -void -Mgr::IndexAction::dump(StoreEntry *) -{ - debugs(16, 5, MYNAME); -} - Mgr::MenuAction::Pointer Mgr::MenuAction::Create(const Command::Pointer &cmd) { @@ -47,20 +41,26 @@ Mgr::MenuAction::MenuAction(const Command::Pointer &aCmd): Action(aCmd) debugs(16, 5, MYNAME); } +/// A table summarizing available Cache Manager actions: +/// table-row = SP 1*VCHAR 1*( HTAB 0*VCHAR ) void -Mgr::MenuAction::dump(StoreEntry* entry) +Mgr::MenuAction::report(std::ostream &os) { - debugs(16, 5, MYNAME); - Must(entry != nullptr); + const auto &menu = CacheManager::GetInstance()->menu(); - typedef CacheManager::Menu::const_iterator Iterator; - const CacheManager::Menu& menu = CacheManager::GetInstance()->menu(); + const auto savedFlags = os.flags(); + const auto savedFill = os.fill(); - for (Iterator a = menu.begin(); a != menu.end(); ++a) { - storeAppendPrintf(entry, " %-22s\t%-32s\t%s\n", - (*a)->name, (*a)->desc, - CacheManager::GetInstance()->ActionProtection(*a)); + os << std::left; + for (const auto &a : menu) { + os << ' ' << std::setw(22) << a->name << std::setw(0) + << '\t' << std::setw(32) << a->desc << std::setw(0) + << '\t' << CacheManager::GetInstance()->ActionProtection(a) + << '\n'; } + + os.fill(savedFill); + os.flags(savedFlags); } Mgr::ShutdownAction::Pointer @@ -75,7 +75,7 @@ Mgr::ShutdownAction::ShutdownAction(const Command::Pointer &aCmd): Action(aCmd) } void -Mgr::ShutdownAction::dump(StoreEntry *) +Mgr::ShutdownAction::report(std::ostream &) { debugs(16, DBG_CRITICAL, "Shutdown by Cache Manager command."); shut_down(SIGTERM); @@ -94,10 +94,10 @@ Mgr::ReconfigureAction::ReconfigureAction(const Command::Pointer &aCmd): } void -Mgr::ReconfigureAction::dump(StoreEntry* entry) +Mgr::ReconfigureAction::report(std::ostream &os) { debugs(16, DBG_IMPORTANT, "Reconfigure by Cache Manager command."); - storeAppendPrintf(entry, "Reconfiguring Squid Process ...."); + os << "Reconfiguring Squid Process ... \n"; reconfigure(SIGHUP); } @@ -113,10 +113,10 @@ Mgr::RotateAction::RotateAction(const Command::Pointer &aCmd): Action(aCmd) } void -Mgr::RotateAction::dump(StoreEntry* entry) +Mgr::RotateAction::report(std::ostream &os) { debugs(16, DBG_IMPORTANT, "Rotate Logs by Cache Manager command."); - storeAppendPrintf(entry, "Rotating Squid Process Logs ...."); + os << "Rotating Squid Process Logs ... \n"; #if defined(_SQUID_LINUX_THREADS_) rotate_logs(SIGQUIT); #else @@ -137,13 +137,12 @@ Mgr::OfflineToggleAction::OfflineToggleAction(const Command::Pointer &aCmd): } void -Mgr::OfflineToggleAction::dump(StoreEntry* entry) +Mgr::OfflineToggleAction::report(std::ostream &os) { Config.onoff.offline = !Config.onoff.offline; debugs(16, DBG_IMPORTANT, "offline_mode now " << (Config.onoff.offline ? "ON" : "OFF") << " by Cache Manager request."); - storeAppendPrintf(entry, "offline_mode is now %s\n", - Config.onoff.offline ? "ON" : "OFF"); + os << "offline_mode is now " << (Config.onoff.offline ? "ON" : "OFF") << '\n'; } void diff --git a/src/mgr/BasicActions.h b/src/mgr/BasicActions.h index 08ba3ebb02c..5f2a926b831 100644 --- a/src/mgr/BasicActions.h +++ b/src/mgr/BasicActions.h @@ -27,7 +27,7 @@ class IndexAction: public Action public: static Pointer Create(const CommandPointer &cmd); /* Action API */ - void dump(StoreEntry *entry) override; + void report(std::ostream &) override {} protected: IndexAction(const CommandPointer &cmd); @@ -39,7 +39,7 @@ class MenuAction: public Action public: static Pointer Create(const CommandPointer &cmd); /* Action API */ - void dump(StoreEntry *entry) override; + void report(std::ostream &) override; protected: MenuAction(const CommandPointer &cmd); @@ -51,7 +51,7 @@ class ShutdownAction: public Action public: static Pointer Create(const CommandPointer &cmd); /* Action API */ - void dump(StoreEntry *entry) override; + void report(std::ostream &) override; protected: ShutdownAction(const CommandPointer &cmd); @@ -63,7 +63,7 @@ class ReconfigureAction: public Action public: static Pointer Create(const CommandPointer &cmd); /* Action API */ - void dump(StoreEntry *entry) override; + void report(std::ostream &) override; protected: ReconfigureAction(const CommandPointer &cmd); @@ -75,7 +75,7 @@ class RotateAction: public Action public: static Pointer Create(const CommandPointer &cmd); /* Action API */ - void dump(StoreEntry *entry) override; + void report(std::ostream &) override; protected: RotateAction(const CommandPointer &cmd); @@ -87,7 +87,7 @@ class OfflineToggleAction: public Action public: static Pointer Create(const CommandPointer &cmd); /* Action API */ - void dump(StoreEntry *entry) override; + void report(std::ostream &) override; protected: OfflineToggleAction(const CommandPointer &cmd); diff --git a/src/tests/stub_libmgr.cc b/src/tests/stub_libmgr.cc index 8177995bd7c..6d1921a5954 100644 --- a/src/tests/stub_libmgr.cc +++ b/src/tests/stub_libmgr.cc @@ -34,6 +34,7 @@ const char * Mgr::Action::name() const STUB_RETVAL(nullptr) static Mgr::Command static_Command; const Mgr::Command & Mgr::Action::command() const STUB_RETVAL(static_Command) StoreEntry * Mgr::Action::createStoreEntry() const STUB_RETVAL(nullptr) +void Mgr::Action::dump(StoreEntry *) STUB static Mgr::Action::Pointer dummyAction; #include "mgr/ActionParams.h" @@ -48,27 +49,27 @@ void Mgr::ActionWriter::start() STUB #include "mgr/BasicActions.h" Mgr::Action::Pointer Mgr::MenuAction::Create(const Mgr::CommandPointer &) STUB_RETVAL(dummyAction) -void Mgr::MenuAction::dump(StoreEntry *) STUB +void Mgr::MenuAction::report(std::ostream &) STUB //protected: //Mgr::MenuAction::MenuAction(const CommandPointer &cmd) STUB Mgr::Action::Pointer Mgr::ShutdownAction::Create(const Mgr::CommandPointer &) STUB_RETVAL(dummyAction) -void Mgr::ShutdownAction::dump(StoreEntry *) STUB +void Mgr::ShutdownAction::report(std::ostream &) STUB // protected: //Mgr::ShutdownAction::ShutdownAction(const CommandPointer &) STUB Mgr::Action::Pointer Mgr::ReconfigureAction::Create(const Mgr::CommandPointer &) STUB_RETVAL(dummyAction) -void Mgr::ReconfigureAction::dump(StoreEntry *) STUB +void Mgr::ReconfigureAction::report(std::ostream &) STUB //protected: //Mgr::ReconfigureAction::ReconfigureAction(const CommandPointer &) STUB Mgr::Action::Pointer Mgr::RotateAction::Create(const Mgr::CommandPointer &) STUB_RETVAL(dummyAction) -void Mgr::RotateAction::dump(StoreEntry *) STUB +void Mgr::RotateAction::report(std::ostream &) STUB //protected: //Mgr::RotateAction::RotateAction(const CommandPointer &) STUB Mgr::Action::Pointer Mgr::OfflineToggleAction::Create(const CommandPointer &) STUB_RETVAL(dummyAction) -void Mgr::OfflineToggleAction::dump(StoreEntry *) STUB +void Mgr::OfflineToggleAction::report(std::ostream &) STUB //protected: //Mgr::OfflineToggleAction::OfflineToggleAction(const CommandPointer &) STUB