Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added BaseWriter, a common base class of ConsoleWriter, LogWriter, and also the Shell in future

  • Property svn:eol-style set to native
File size: 3.3 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::openFile()
67    {
68        std::string name = this->path_ + '/' + this->filename_;
69
70        if (this->bDefaultPath_)
71            OutputManager::getInstance().pushMessage(level::user_info, context::output(), "Opening log file " + name);
72
73        this->file_.open(name.c_str(), std::fstream::out);
74
75        if (this->file_.is_open())
76            this->printLine("Log file opened");
77        else
78            OutputManager::getInstance().pushMessage(level::user_warning, context::output(), "Failed to open log file. File logging disabled.");
79    }
80
81    void LogWriter::closeFile()
82    {
83        if (this->file_.is_open())
84        {
85            this->printLine("Log file closed");
86            this->file_.close();
87        }
88    }
89
90    void LogWriter::setLogPath(const std::string& path)
91    {
92        OutputManager::getInstance().pushMessage(level::internal_info, context::output(), "Migrating log file from " + this->path_ + "\nto " + path);
93
94        this->closeFile();
95        this->path_ = path;
96        this->bDefaultPath_ = false;
97        this->openFile();
98
99        MemoryWriter::getInstance().resendOutput(this);
100    }
101
102    void LogWriter::printLine(const std::string& line)
103    {
104        if (!this->file_.is_open())
105            return;
106
107        // Get current time
108        time_t rawtime;
109        struct tm* timeinfo;
110        time(&rawtime);
111        timeinfo = localtime(&rawtime);
112
113        // print timestamp and line to the log file
114        this->file_ << (timeinfo->tm_hour < 10 ? "0" : "") << timeinfo->tm_hour << ':' <<
115                       (timeinfo->tm_min  < 10 ? "0" : "") << timeinfo->tm_min  << ':' <<
116                       (timeinfo->tm_sec  < 10 ? "0" : "") << timeinfo->tm_sec  << ' ' << line << std::endl;
117    }
118}
Note: See TracBrowser for help on using the repository browser.