From 1faafbcd5977ce4e04450831af17f3a0e57560c2 Mon Sep 17 00:00:00 2001 From: g0dil <g0dil@wiback.org> Date: Fri, 6 Feb 2009 23:24:00 +0000 Subject: [PATCH] Utils/Logger: Add 'message' console command --- Utils/Logger/AreaRegistry.hh | 3 +- Utils/Logger/StreamRegistry.hh | 3 +- Utils/Logger/Target.cc | 54 +++++++++++++++++++++------------- Utils/Logger/Target.ih | 11 +++++++ 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/Utils/Logger/AreaRegistry.hh b/Utils/Logger/AreaRegistry.hh index 386bb2ca8..82a43ed86 100644 --- a/Utils/Logger/AreaRegistry.hh +++ b/Utils/Logger/AreaRegistry.hh @@ -77,11 +77,12 @@ namespace log { iterator begin(); ///< Beginning of area name sequence iterator end(); ///< End of area name sequence + detail::AreaBase const * lookup(std::string const & name); + private: AreaRegistry(); void registerArea(detail::AreaBase const & area); - detail::AreaBase const * lookup(std::string const & name); Registry registry_; diff --git a/Utils/Logger/StreamRegistry.hh b/Utils/Logger/StreamRegistry.hh index c35b69da7..1d098db1d 100644 --- a/Utils/Logger/StreamRegistry.hh +++ b/Utils/Logger/StreamRegistry.hh @@ -75,11 +75,12 @@ namespace log { iterator begin(); iterator end(); + detail::StreamBase const * lookup(std::string const & name); + private: StreamRegistry(); void registerStream(detail::StreamBase const & stream); - detail::StreamBase const * lookup(std::string const & name); Registry registry_; diff --git a/Utils/Logger/Target.cc b/Utils/Logger/Target.cc index 2760ae3bb..38742bc3f 100644 --- a/Utils/Logger/Target.cc +++ b/Utils/Logger/Target.cc @@ -46,23 +46,12 @@ namespace log { SENF_CONSOLE_REGISTER_ENUM_MEMBER( Target, action_t, (ACCEPT)(REJECT) ); -}} +namespace detail { -namespace { -namespace local { - - enum Level { - VERBOSE = senf::log::VERBOSE::value, - NOTICE = senf::log::NOTICE::value, - MESSAGE = senf::log::MESSAGE::value, - IMPORTANT = senf::log::IMPORTANT::value, - CRITICAL = senf::log::CRITICAL::value, - FATAL = senf::log::FATAL::value - }; - SENF_CONSOLE_REGISTER_ENUM( Level, - (VERBOSE)(NOTICE)(MESSAGE)(IMPORTANT)(CRITICAL)(FATAL) ); - -}} + SENF_CONSOLE_REGISTER_ENUM_MEMBER( TargetRegistry, Level, + (VERBOSE)(NOTICE)(MESSAGE)(IMPORTANT)(CRITICAL)(FATAL) ); + +}}} prefix_ senf::log::Target::Target(std::string const & name) { @@ -82,7 +71,7 @@ prefix_ senf::log::Target::Target(std::string const & name) " ACTION action to take: accept or reject"); consoleDir_().add("route", boost::function<void (std::string const &, std::string const &, - local::Level, action_t, int)>( + detail::TargetRegistry::Level, action_t, int)>( senf::membind( static_cast<void (Target::*)( std::string const &, std::string const &, @@ -97,7 +86,7 @@ prefix_ senf::log::Target::Target(std::string const & name) " use '/sys/log/areas' to list all available areas", kw::default_value="") .arg("level", "log level, one of: VERBOSE, NOTICE, MESSAGE, IMPORTANT, CRITICAL, FATAL", - kw::default_value=local::VERBOSE) + kw::default_value=detail::TargetRegistry::VERBOSE) .arg("action", "routing action, one of: ACCEPT, REJECT", kw::default_value=ACCEPT) .arg("index", "index at which to insert new rule", @@ -127,7 +116,7 @@ prefix_ senf::log::Target::Target(std::string const & name) .overloadDoc("Remove routing entry with the given index"); consoleDir_().add("unroute", boost::function<void (std::string const &, std::string const &, - local::Level, action_t)>( + detail::TargetRegistry::Level, action_t)>( senf::membind( static_cast<void (Target::*)( std::string const &, std::string const &, @@ -138,7 +127,7 @@ prefix_ senf::log::Target::Target(std::string const & name) .arg("area", "area to match or empty to match any area", kw::default_value="") .arg("level", "log level, one of: VERBOSE, NOTICE, MESSAGE, IMPORTANT, CRITICAL, FATAL", - kw::default_value=local::VERBOSE) + kw::default_value=detail::TargetRegistry::VERBOSE) .arg("action", "routing action, one of: ACCEPT, REJECT", kw::default_value=ACCEPT) .overloadDoc("Remove the routing entry matching the specified arguments."); @@ -240,11 +229,22 @@ prefix_ void senf::log::Target::flush() prefix_ senf::log::detail::TargetRegistry::TargetRegistry() : fallbackRouting_(true) { + namespace kw = senf::console::kw; + console::sysdir().add("log", consoleDir_()); consoleDir_().add("areas", senf::membind(&TargetRegistry::consoleAreas, this)) .doc("List all areas"); consoleDir_().add("streams", senf::membind(&TargetRegistry::consoleStreams, this)) .doc("List all streams"); + consoleDir_().add("message", senf::membind(&TargetRegistry::consoleWrite, this)) + .arg("stream", "stream to write message to", + kw::default_value = "senf::log::Debug") + .arg("area","area to write message to", + kw::default_value = "senf::log::DefaultArea") + .arg("level", "log level", + kw::default_value = MESSAGE) + .arg("message", "message to write") + .doc("Write log message"); } prefix_ void senf::log::detail::TargetRegistry::consoleAreas(std::ostream & os) @@ -263,6 +263,20 @@ prefix_ void senf::log::detail::TargetRegistry::consoleStreams(std::ostream & os os << *i << "\n"; } +prefix_ void senf::log::detail::TargetRegistry::consoleWrite(std::string const & stream, + std::string const & area, + Level level, + std::string const & msg) +{ + detail::StreamBase const * s (StreamRegistry::instance().lookup(stream)); + if (!s) + throw Target::InvalidStreamException(); + detail::AreaBase const * a (AreaRegistry::instance().lookup(area)); + if (!a) + throw Target::InvalidAreaException(); + write(*s, *a, level, msg); +} + //////////////////////////////////////// // private members diff --git a/Utils/Logger/Target.ih b/Utils/Logger/Target.ih index 4d65aa99f..bc48061c0 100644 --- a/Utils/Logger/Target.ih +++ b/Utils/Logger/Target.ih @@ -43,6 +43,15 @@ namespace detail { : public senf::singleton<TargetRegistry> { public: + enum Level { + VERBOSE = senf::log::VERBOSE::value, + NOTICE = senf::log::NOTICE::value, + MESSAGE = senf::log::MESSAGE::value, + IMPORTANT = senf::log::IMPORTANT::value, + CRITICAL = senf::log::CRITICAL::value, + FATAL = senf::log::FATAL::value + }; + using senf::singleton<TargetRegistry>::instance; void write(StreamBase const & stream, AreaBase const & area, unsigned level, @@ -59,6 +68,8 @@ namespace detail { void consoleAreas(std::ostream & os); void consoleStreams(std::ostream & os); + void consoleWrite(std::string const & stream, std::string const & area, Level level, + std::string const & msg); typedef std::set<Target *> Targets; Targets targets_; -- GitLab