Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

in OutputManager: getInstance now only creates an instance of OutputManager, while getInstanceAndCreateListeners does additional stuff. the latter ins only used in OutputStream to ensure the default listeners (LogWriter, etc) get created. This should avoid circular calls.

  • 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{
38namespace test
39{
40    LogWriter::LogWriter()
41    {
42        this->setLevelMax(level::internal_info);
43
44        this->filename_ = "orxonox2.log";
45
46        // Get path for a temporary file
47#ifdef ORXONOX_PLATFORM_WINDOWS
48        this->path_ = getenv("TEMP");
49#else
50        this->path_ = "/tmp";
51#endif
52        this->bDefaultPath_ = true;
53
54        this->openFile();
55    }
56
57    LogWriter::~LogWriter()
58    {
59        this->closeFile();
60    }
61
62    /*static*/ LogWriter& LogWriter::getInstance()
63    {
64        static LogWriter instance;
65        return instance;
66    }
67
68    void LogWriter::output(OutputLevel level, OutputContext context, const std::vector<std::string>& lines)
69    {
70        if (!this->file_.is_open())
71            return;
72
73        const std::string& prefix = OutputManager::getInstance().getDefaultPrefix(level, context);
74        std::string blanks(prefix.length(), ' ');
75
76        for (size_t i = 0; i < lines.size(); ++i)
77            this->printLine((i == 0 ? prefix : blanks) + lines[i]);
78    }
79
80    void LogWriter::openFile()
81    {
82        std::string name = this->path_ + '/' + this->filename_;
83
84        if (this->bDefaultPath_)
85            OutputManager::getInstance().pushMessage(level::user_info, context::output, "Opening log file " + name);
86
87        this->file_.open(name.c_str(), std::fstream::out);
88
89        if (this->file_.is_open())
90            this->printLine("Log file opened");
91        else
92            OutputManager::getInstance().pushMessage(level::user_warning, context::output, "Failed to open log file. File logging disabled.");
93    }
94
95    void LogWriter::closeFile()
96    {
97        if (this->file_.is_open())
98        {
99            this->printLine("Log file closed");
100            this->file_.close();
101        }
102    }
103
104    void LogWriter::setLogPath(const std::string& path)
105    {
106        OutputManager::getInstance().pushMessage(level::internal_info, context::output, "Migrating log file from " + this->path_ + "\nto " + path);
107
108        this->closeFile();
109        this->path_ = path;
110        this->bDefaultPath_ = false;
111        this->openFile();
112
113        MemoryWriter::getInstance().resendOutput(this);
114    }
115
116    void LogWriter::printLine(const std::string& line)
117    {
118        // Get current time
119        time_t rawtime;
120        struct tm* timeinfo;
121        time(&rawtime);
122        timeinfo = localtime(&rawtime);
123
124        // print timestamp and line to the log file
125        this->file_ << (timeinfo->tm_hour < 10 ? "0" : "") << timeinfo->tm_hour << ':' <<
126                       (timeinfo->tm_min  < 10 ? "0" : "") << timeinfo->tm_min  << ':' <<
127                       (timeinfo->tm_sec  < 10 ? "0" : "") << timeinfo->tm_sec  << ' ' << line << std::endl;
128    }
129}
130}
Note: See TracBrowser for help on using the repository browser.