[Orxonox-commit 4103] r8774 - in code/branches/output/src/libraries/util: . output

landauf at orxonox.net landauf at orxonox.net
Sun Jul 24 23:50:20 CEST 2011


Author: landauf
Date: 2011-07-24 23:50:20 +0200 (Sun, 24 Jul 2011)
New Revision: 8774

Added:
   code/branches/output/src/libraries/util/output/LogWriter.cc
   code/branches/output/src/libraries/util/output/LogWriter.h
   code/branches/output/src/libraries/util/output/MemoryWriter.cc
   code/branches/output/src/libraries/util/output/MemoryWriter.h
Modified:
   code/branches/output/src/libraries/util/CMakeLists.txt
   code/branches/output/src/libraries/util/output/OutputDefinitions.h
   code/branches/output/src/libraries/util/output/OutputListener.h
   code/branches/output/src/libraries/util/output/OutputManager.cc
   code/branches/output/src/libraries/util/output/OutputManager.h
Log:
added LogWriter and MemoryWriter

Modified: code/branches/output/src/libraries/util/CMakeLists.txt
===================================================================
--- code/branches/output/src/libraries/util/CMakeLists.txt	2011-07-24 20:40:19 UTC (rev 8773)
+++ code/branches/output/src/libraries/util/CMakeLists.txt	2011-07-24 21:50:20 UTC (rev 8774)
@@ -42,6 +42,8 @@
   output/OutputStream.cc
   output/OutputManager.cc
   output/OutputListener.cc
+  output/LogWriter.cc
+  output/MemoryWriter.cc
 )
 
 ORXONOX_ADD_LIBRARY(util

Added: code/branches/output/src/libraries/util/output/LogWriter.cc
===================================================================
--- code/branches/output/src/libraries/util/output/LogWriter.cc	                        (rev 0)
+++ code/branches/output/src/libraries/util/output/LogWriter.cc	2011-07-24 21:50:20 UTC (rev 8774)
@@ -0,0 +1,130 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      Reto Grieder
+ *
+ */
+
+#include "LogWriter.h"
+
+#include <ctime>
+
+#include "OutputManager.h"
+#include "MemoryWriter.h"
+
+namespace orxonox
+{
+namespace test
+{
+    LogWriter::LogWriter()
+    {
+        this->setLevelMax(level::internal_info);
+
+        this->filename_ = "orxonox2.log";
+
+        // Get path for a temporary file
+#ifdef ORXONOX_PLATFORM_WINDOWS
+        this->path_ = getenv("TEMP");
+#else
+        this->path_ = "/tmp";
+#endif
+        this->bDefaultPath_ = true;
+
+        this->openFile();
+    }
+
+    LogWriter::~LogWriter()
+    {
+        this->closeFile();
+    }
+
+    /*static*/ LogWriter& LogWriter::getInstance()
+    {
+        static LogWriter instance;
+        return instance;
+    }
+
+    void LogWriter::output(OutputLevel level, OutputContext context, const std::vector<std::string>& lines)
+    {
+        if (!this->file_.is_open())
+            return;
+
+        const std::string& prefix = OutputManager::getInstance().getDefaultPrefix(level, context);
+        std::string blanks(prefix.length(), ' ');
+
+        for (size_t i = 0; i < lines.size(); ++i)
+            this->printLine((i == 0 ? prefix : blanks) + lines[i]);
+    }
+
+    void LogWriter::openFile()
+    {
+        std::string name = this->path_ + '/' + this->filename_;
+
+        if (this->bDefaultPath_)
+            OutputManager::getInstanceInternal().pushMessage(level::user_info, context::output, "Opening log file " + name);
+
+        this->file_.open(name.c_str(), std::fstream::out);
+
+        if (this->file_.is_open())
+            this->printLine("Log file opened");
+        else
+            OutputManager::getInstance().pushMessage(level::user_warning, context::output, "Failed to open log file. File logging disabled.");
+    }
+
+    void LogWriter::closeFile()
+    {
+        if (this->file_.is_open())
+        {
+            this->printLine("Log file closed");
+            this->file_.close();
+        }
+    }
+
+    void LogWriter::setLogPath(const std::string& path)
+    {
+        OutputManager::getInstance().pushMessage(level::internal_info, context::output, "Migrating log file from " + this->path_ + "\nto " + path);
+
+        this->closeFile();
+        this->path_ = path;
+        this->bDefaultPath_ = false;
+        this->openFile();
+
+        MemoryWriter::getInstance().resendOutput(this);
+    }
+
+    void LogWriter::printLine(const std::string& line)
+    {
+        // Get current time
+        time_t rawtime;
+        struct tm* timeinfo;
+        time(&rawtime);
+        timeinfo = localtime(&rawtime);
+
+        // print timestamp and line to the log file
+        this->file_ << (timeinfo->tm_hour < 10 ? "0" : "") << timeinfo->tm_hour << ':' <<
+                       (timeinfo->tm_min  < 10 ? "0" : "") << timeinfo->tm_min  << ':' <<
+                       (timeinfo->tm_sec  < 10 ? "0" : "") << timeinfo->tm_sec  << ' ' << line << std::endl;
+    }
+}
+}
\ No newline at end of file


Property changes on: code/branches/output/src/libraries/util/output/LogWriter.cc
___________________________________________________________________
Added: svn:eol-style
   + native

Added: code/branches/output/src/libraries/util/output/LogWriter.h
===================================================================
--- code/branches/output/src/libraries/util/output/LogWriter.h	                        (rev 0)
+++ code/branches/output/src/libraries/util/output/LogWriter.h	2011-07-24 21:50:20 UTC (rev 8774)
@@ -0,0 +1,71 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#ifndef _LogWriter_H__
+#define _LogWriter_H__
+
+#include "util/UtilPrereqs.h"
+
+#include <fstream>
+
+#include "OutputListener.h"
+
+namespace orxonox
+{
+namespace test
+{
+    class _UtilExport LogWriter : public OutputListener
+    {
+        public:
+            static LogWriter& getInstance();
+
+            void setLogPath(const std::string& path);
+
+        protected:
+            virtual void output(OutputLevel level, OutputContext context, const std::vector<std::string>& lines);
+
+        private:
+            LogWriter();
+            LogWriter(const LogWriter&);
+            ~LogWriter();
+
+            void openFile();
+            void closeFile();
+
+            void printLine(const std::string& line);
+
+            std::string filename_;
+            std::string path_;
+            bool bDefaultPath_;
+
+            std::ofstream file_;
+    };
+}
+}
+
+#endif /* _LogWriter_H__ */


Property changes on: code/branches/output/src/libraries/util/output/LogWriter.h
___________________________________________________________________
Added: svn:eol-style
   + native

Added: code/branches/output/src/libraries/util/output/MemoryWriter.cc
===================================================================
--- code/branches/output/src/libraries/util/output/MemoryWriter.cc	                        (rev 0)
+++ code/branches/output/src/libraries/util/output/MemoryWriter.cc	2011-07-24 21:50:20 UTC (rev 8774)
@@ -0,0 +1,72 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      Reto Grieder
+ *
+ */
+
+#include "MemoryWriter.h"
+
+#include "OutputManager.h"
+
+namespace orxonox
+{
+namespace test
+{
+    MemoryWriter::MemoryWriter()
+    {
+        this->setLevelMask(level::all);
+    }
+
+    MemoryWriter::~MemoryWriter()
+    {
+    }
+
+    /*static*/ MemoryWriter& MemoryWriter::getInstance()
+    {
+        static MemoryWriter instance;
+        return instance;
+    }
+
+    void MemoryWriter::output(OutputLevel level, OutputContext context, const std::vector<std::string>& lines)
+    {
+        this->messages_.push_back(Message(level, context, lines));
+    }
+
+    void MemoryWriter::resendOutput(OutputListener* listener) const
+    {
+        for (size_t i = 0; i < this->messages_.size(); ++i)
+        {
+            const Message& message = this->messages_[i];
+            listener->unfilteredOutput(message.level, message.context, message.lines);
+        }
+    }
+
+    void MemoryWriter::disable()
+    {
+        OutputManager::getInstance().unregisterListener(this);
+        this->output(level::debug_output, context::undefined, std::vector<std::string>(1, "MemoryWriter disabled, further messages may be lost"));
+    }
+}
+}
\ No newline at end of file


Property changes on: code/branches/output/src/libraries/util/output/MemoryWriter.cc
___________________________________________________________________
Added: svn:eol-style
   + native

Added: code/branches/output/src/libraries/util/output/MemoryWriter.h
===================================================================
--- code/branches/output/src/libraries/util/output/MemoryWriter.h	                        (rev 0)
+++ code/branches/output/src/libraries/util/output/MemoryWriter.h	2011-07-24 21:50:20 UTC (rev 8774)
@@ -0,0 +1,70 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Fabian 'x3n' Landau
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#ifndef _MemoryWriter_H__
+#define _MemoryWriter_H__
+
+#include "util/UtilPrereqs.h"
+#include "OutputListener.h"
+
+namespace orxonox
+{
+namespace test
+{
+    class _UtilExport MemoryWriter : public OutputListener
+    {
+        struct Message
+        {
+            Message(OutputLevel level, OutputContext context, const std::vector<std::string>& lines)
+                : level(level), context(context), lines(lines) {}
+
+            OutputLevel level;
+            OutputContext context;
+            std::vector<std::string> lines;
+        };
+
+        public:
+            static MemoryWriter& getInstance();
+
+            void resendOutput(OutputListener* listener) const;
+            void disable();
+
+        protected:
+            virtual void output(OutputLevel level, OutputContext context, const std::vector<std::string>& lines);
+
+        private:
+            MemoryWriter();
+            MemoryWriter(const MemoryWriter&);
+            ~MemoryWriter();
+
+            std::vector<Message> messages_;
+    };
+}
+}
+
+#endif /* _MemoryWriter_H__ */


Property changes on: code/branches/output/src/libraries/util/output/MemoryWriter.h
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: code/branches/output/src/libraries/util/output/OutputDefinitions.h
===================================================================
--- code/branches/output/src/libraries/util/output/OutputDefinitions.h	2011-07-24 20:40:19 UTC (rev 8773)
+++ code/branches/output/src/libraries/util/output/OutputDefinitions.h	2011-07-24 21:50:20 UTC (rev 8774)
@@ -68,6 +68,7 @@
 
         static const OutputContext test1     = registerContext("test1");
         static const OutputContext test2     = registerContext("test2");
+        static const OutputContext output    = registerContext("output");
     }
 }
 }

Modified: code/branches/output/src/libraries/util/output/OutputListener.h
===================================================================
--- code/branches/output/src/libraries/util/output/OutputListener.h	2011-07-24 20:40:19 UTC (rev 8773)
+++ code/branches/output/src/libraries/util/output/OutputListener.h	2011-07-24 21:50:20 UTC (rev 8774)
@@ -41,8 +41,6 @@
 {
     class _UtilExport OutputListener
     {
-        friend class OutputManager;
-
         public:
             OutputListener();
             ~OutputListener();
@@ -62,6 +60,10 @@
             inline bool acceptsOutput(OutputLevel level, OutputContext context) const
                 { return ((this->levelMask_ & level) && (this->contextMask_ & context)); }
 
+            inline void unfilteredOutput(OutputLevel level, OutputContext context, const std::vector<std::string>& lines)
+                { if (this->acceptsOutput(level, context)) this->output(level, context, lines); }
+
+        protected:
             virtual void output(OutputLevel level, OutputContext context, const std::vector<std::string>& lines) = 0;
 
         private:

Modified: code/branches/output/src/libraries/util/output/OutputManager.cc
===================================================================
--- code/branches/output/src/libraries/util/output/OutputManager.cc	2011-07-24 20:40:19 UTC (rev 8773)
+++ code/branches/output/src/libraries/util/output/OutputManager.cc	2011-07-24 21:50:20 UTC (rev 8774)
@@ -30,6 +30,8 @@
 
 #include "util/Debug.h"
 #include "OutputListener.h"
+#include "MemoryWriter.h"
+#include "LogWriter.h"
 
 namespace orxonox
 {
@@ -43,16 +45,10 @@
                 this->setLevelMax(level::user_info);
             }
 
+        protected:
             virtual void output(OutputLevel level, OutputContext context, const std::vector<std::string>& lines)
             {
-                std::string prefix = OutputManager::getInstance().getLevelName(level) + ": ";
-                if (context != context::undefined)
-                {
-                    std::string context_name = OutputManager::getInstance().getContextName(context);
-                    if (context_name == "")
-                        context_name = OutputManager::getInstance().getComposedContextName(context);
-                    prefix += "[" + context_name + "] ";
-                }
+                const std::string& prefix = OutputManager::getInstance().getDefaultPrefix(level, context);
                 std::string blanks(prefix.length(), ' ');
 
                 for (size_t i = 0; i < lines.size(); ++i)
@@ -73,7 +69,11 @@
     /*static*/ OutputManager& OutputManager::getInstance()
     {
         static OutputManager& instance = OutputManager::getInstanceInternal();
+
         static ConsoleOutput consoleOutputInstance;
+        static MemoryWriter& memoryWriterInstance = MemoryWriter::getInstance(); (void)memoryWriterInstance;
+        static LogWriter& logWriterInstance = LogWriter::getInstance(); (void)logWriterInstance;
+
         return instance;
     }
 
@@ -93,8 +93,7 @@
         }
 
         for (size_t i = 0; i < this->listeners_.size(); ++i)
-            if (this->listeners_[i]->acceptsOutput(level, context))
-                this->listeners_[i]->output(level, context, lines);
+            this->listeners_[i]->unfilteredOutput(level, context, lines);
     }
 
     void OutputManager::registerListener(OutputListener* listener)
@@ -210,5 +209,18 @@
         }
         return name;
     }
+
+    std::string OutputManager::getDefaultPrefix(OutputLevel level, OutputContext context) const
+    {
+        std::string prefix = this->getLevelName(level) + ": ";
+        if (context != context::undefined)
+        {
+            std::string context_name = this->getContextName(context);
+            if (context_name == "")
+                context_name = this->getComposedContextName(context);
+            prefix += "[" + context_name + "] ";
+        }
+        return prefix;
+    }
 }
 }

Modified: code/branches/output/src/libraries/util/output/OutputManager.h
===================================================================
--- code/branches/output/src/libraries/util/output/OutputManager.h	2011-07-24 20:40:19 UTC (rev 8773)
+++ code/branches/output/src/libraries/util/output/OutputManager.h	2011-07-24 21:50:20 UTC (rev 8774)
@@ -45,10 +45,9 @@
 
     class _UtilExport OutputManager
     {
-        friend class OutputListener;
-
         public:
             static OutputManager& getInstance();
+            static OutputManager& getInstanceInternal();
 
             void pushMessage(OutputLevel level, OutputContext context, const std::string& message);
 
@@ -72,14 +71,13 @@
             const std::string& getLevelName(OutputLevel level) const;
             const std::string& getContextName(OutputContext context) const;
             std::string getComposedContextName(OutputContext context) const;
+            std::string getDefaultPrefix(OutputLevel level, OutputContext context) const;
 
         private:
             OutputManager();
             OutputManager(const OutputManager&);
             ~OutputManager();
 
-            static OutputManager& getInstanceInternal();
-
             std::vector<OutputListener*> listeners_;
 
             OutputLevel   combinedLevelMask_;




More information about the Orxonox-commit mailing list