Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/output/src/libraries/util/output/LogWriter.cc @ 8787

Last change on this file since 8787 was 8787, checked in by landauf, 13 years ago

removed Debug.h and OutputHandler. removed test namespace from new output system.
doesn't compile.

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Reto Grieder
26 *
27 */
28
29#include "LogWriter.h"
30
31#include <ctime>
32
33#include "OutputManager.h"
34#include "MemoryWriter.h"
35
36namespace orxonox
37{
38    LogWriter::LogWriter()
39    {
40        this->setLevelMax(level::internal_info);
41
42        this->filename_ = "orxonox2.log";
43
44        // Get path for a temporary file
45#ifdef ORXONOX_PLATFORM_WINDOWS
46        this->path_ = getenv("TEMP");
47#else
48        this->path_ = "/tmp";
49#endif
50        this->bDefaultPath_ = true;
51
52        this->openFile();
53    }
54
55    LogWriter::~LogWriter()
56    {
57        this->closeFile();
58    }
59
60    /*static*/ LogWriter& LogWriter::getInstance()
61    {
62        static LogWriter instance;
63        return instance;
64    }
65
66    void LogWriter::output(OutputLevel level, OutputContext context, const std::vector<std::string>& lines)
67    {
68        if (!this->file_.is_open())
69            return;
70
71        const std::string& prefix = OutputManager::getInstance().getDefaultPrefix(level, context);
72        std::string blanks(prefix.length(), ' ');
73
74        for (size_t i = 0; i < lines.size(); ++i)
75            this->printLine((i == 0 ? prefix : blanks) + lines[i]);
76    }
77
78    void LogWriter::openFile()
79    {
80        std::string name = this->path_ + '/' + this->filename_;
81
82        if (this->bDefaultPath_)
83            OutputManager::getInstance().pushMessage(level::user_info, context::output(), "Opening log file " + name);
84
85        this->file_.open(name.c_str(), std::fstream::out);
86
87        if (this->file_.is_open())
88            this->printLine("Log file opened");
89        else
90            OutputManager::getInstance().pushMessage(level::user_warning, context::output(), "Failed to open log file. File logging disabled.");
91    }
92
93    void LogWriter::closeFile()
94    {
95        if (this->file_.is_open())
96        {
97            this->printLine("Log file closed");
98            this->file_.close();
99        }
100    }
101
102    void LogWriter::setLogPath(const std::string& path)
103    {
104        OutputManager::getInstance().pushMessage(level::internal_info, context::output(), "Migrating log file from " + this->path_ + "\nto " + path);
105
106        this->closeFile();
107        this->path_ = path;
108        this->bDefaultPath_ = false;
109        this->openFile();
110
111        MemoryWriter::getInstance().resendOutput(this);
112    }
113
114    void LogWriter::printLine(const std::string& line)
115    {
116        // Get current time
117        time_t rawtime;
118        struct tm* timeinfo;
119        time(&rawtime);
120        timeinfo = localtime(&rawtime);
121
122        // print timestamp and line to the log file
123        this->file_ << (timeinfo->tm_hour < 10 ? "0" : "") << timeinfo->tm_hour << ':' <<
124                       (timeinfo->tm_min  < 10 ? "0" : "") << timeinfo->tm_min  << ':' <<
125                       (timeinfo->tm_sec  < 10 ? "0" : "") << timeinfo->tm_sec  << ' ' << line << std::endl;
126    }
127}
Note: See TracBrowser for help on using the repository browser.