Utils/Logger: Add 'message' console command
g0dil [Fri, 6 Feb 2009 23:24:00 +0000 (23:24 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1104 270642c3-0616-0410-b53a-bc976706d245

Utils/Logger/AreaRegistry.hh
Utils/Logger/StreamRegistry.hh
Utils/Logger/Target.cc
Utils/Logger/Target.ih

index 386bb2c..82a43ed 100644 (file)
@@ -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_;
 
index c35b69d..1d098db 100644 (file)
@@ -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_;
 
index 2760ae3..38742bc 100644 (file)
@@ -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
 
index 4d65aa9..bc48061 100644 (file)
@@ -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_;