Utils/Console: Implement command node return value support
[senf.git] / Utils / Logger / FileTarget.cc
index c2538c4..52ebaf8 100644 (file)
@@ -1,8 +1,8 @@
 // $Id$
 //
-// Copyright (C) 2007 
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer NETwork research (NET)
+// Copyright (C) 2007
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
 //     Stefan Bund <g0dil@berlios.de>
 //
 // This program is free software; you can redistribute it and/or modify
 //#include "FileTarget.ih"
 
 // Custom includes
+#include "../Console/Console.hh"
 
 //#include "FileTarget.mpp"
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
-prefix_ senf::log::FileTarget::FileTarget(std::string file)
-    : ofstream_t(file.c_str(), std::ofstream::app), IOStreamTarget(ofstream_t::member), file_(file)
-{}
+prefix_ senf::log::FileTarget::FileTarget(std::string const & file)
+    : ofstream_t(file.c_str(), std::ofstream::app), IOStreamTarget(file, ofstream_t::member), 
+      file_(file)
+{
+    consoleDir().add( "reopen", senf::membind(
+                          static_cast<void (FileTarget::*)()>(&FileTarget::reopen),
+                          this))
+        .doc("Reopen logfile");
+    consoleDir().add("reopen", senf::membind(
+                         static_cast<void (FileTarget::*)(std::string const &)>(&FileTarget::reopen),
+                         this))
+        .arg("filename","new filename")
+        .overloadDoc("Reopen logfile under new name");
+}
 
 prefix_ void senf::log::FileTarget::reopen()
 {
@@ -42,10 +54,30 @@ prefix_ void senf::log::FileTarget::reopen()
     ofstream_t::member.open(file_.c_str(), std::ofstream::app);
 }
 
-prefix_ void senf::log::FileTarget::reopen(std::string file)
+prefix_ void senf::log::FileTarget::reopen(std::string const & file)
 {
     file_ = file;
     reopen();
+    // Rename console directory
+    console::DirectoryNode::ptr parent (consoleDir().node().parent());
+    if (parent)
+        parent->add(file, consoleDir().node().unlink());
+}
+
+prefix_ senf::log::FileTarget::RegisterConsole::RegisterConsole()
+{
+    detail::TargetRegistry::instance().consoleDir().add("file-target",&RegisterConsole::create)
+        .arg("filename", "name of logfile")
+        .doc("Create new file target.");
+}
+
+prefix_ boost::shared_ptr<senf::console::DirectoryNode>
+senf::log::FileTarget::RegisterConsole::create(std::string const & filename)
+{
+    std::auto_ptr<Target> tp (new FileTarget(filename));
+    Target & target (*tp.get());
+    detail::TargetRegistry::instance().dynamicTarget(tp);
+    return target.consoleDir().node().thisptr();
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////