Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9543 was 9540, checked in by landauf, 11 years ago

some refactoring in OutputManager - for unittests it is sometimes necessary to create a new instance of OutputManager (or a mock), hence we should avoid storing dead references

  • Property svn:eol-style set to native
File size: 4.5 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/**
30    @file
31    @brief Implementation of the LogWriter singleton.
32*/
33
34#include "LogWriter.h"
35
36#include <ctime>
37#include <cstdlib>
38
39#include "OutputManager.h"
40#include "MemoryWriter.h"
41
42namespace orxonox
43{
44    /**
45        @brief Constructor, initializes the desired output levels and the name and path of the log-file, and opens the log-file.
46
47        By default, LogWriter receives all output up to level::internal_info.
48        The log-file has a default name which usually doesn't change. The path
49        is initialized with a temporary directory, depending on the system,
50        and can be changed later.
51    */
52    LogWriter::LogWriter() : BaseWriter("Log")
53    {
54        this->setLevelMax(level::internal_info);
55
56        this->filename_ = "orxonox.log";
57
58        // get the path for a temporary file, depending on the system
59#ifdef ORXONOX_PLATFORM_WINDOWS
60        this->directory_ = getenv("TEMP");
61#else
62        this->directory_ = "/tmp";
63#endif
64
65        // send a message to the user so that he can find the file in the case of a crash.
66        OutputManager::getInstance().pushMessage(level::user_info, context::undefined(), "Opening log file " + this->getPath());
67
68        this->openFile();
69    }
70
71    /**
72        @brief Destructor, closes the log-file.
73    */
74    LogWriter::~LogWriter()
75    {
76        this->closeFile();
77    }
78
79    /**
80        @brief Opens the log-file in order to write output to it.
81    */
82    void LogWriter::openFile()
83    {
84        // open the file
85        this->file_.open(this->getPath().c_str(), std::fstream::out);
86
87        // check if it worked and print some output
88        if (this->file_.is_open())
89            this->printLine("Log file opened", level::none);
90        else
91            OutputManager::getInstance().pushMessage(level::user_warning, context::undefined(), "Failed to open log file. File logging disabled.");
92    }
93
94    /**
95        @brief Closes the log-file.
96    */
97    void LogWriter::closeFile()
98    {
99        if (this->file_.is_open())
100        {
101            this->printLine("Log file closed", level::none);
102            this->file_.close();
103        }
104    }
105
106    /**
107        @brief Changes the path of the log-file. Re-writes the log-file by using MemoryWriter.
108    */
109    void LogWriter::setLogDirectory(const std::string& directory)
110    {
111        // notify about the change of the log-file (because the old file will no longer be updated)
112        OutputManager::getInstance().pushMessage(level::internal_info, context::undefined(), "Migrating log file from " + this->directory_ + "\nto " + directory);
113
114        // close the old file, update the path and open the new file
115        this->closeFile();
116        this->directory_ = directory;
117        this->openFile();
118
119        // request old output from MemoryWriter
120        if (&OutputManager::getInstance().getMemoryWriter())
121            OutputManager::getInstance().getMemoryWriter().resendOutput(this);
122    }
123
124    /**
125        @brief Inherited function from BaseWriter, writers output together with a timestamp to the log-file.
126    */
127    void LogWriter::printLine(const std::string& line, OutputLevel)
128    {
129        if (!this->file_.is_open())
130            return;
131
132        // get the current time
133        time_t rawtime;
134        struct tm* timeinfo;
135        time(&rawtime);
136        timeinfo = localtime(&rawtime);
137
138        // print timestamp and output line to the log file
139        this->file_ << (timeinfo->tm_hour < 10 ? "0" : "") << timeinfo->tm_hour << ':' <<
140                       (timeinfo->tm_min  < 10 ? "0" : "") << timeinfo->tm_min  << ':' <<
141                       (timeinfo->tm_sec  < 10 ? "0" : "") << timeinfo->tm_sec  << ' ' << line << std::endl;
142    }
143}
Note: See TracBrowser for help on using the repository browser.