Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/util/output/LogWriter.cc @ 9489

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

merged output branch back to trunk.

Changes:

  • you have to include util/Output.h instead of util/Debug.h
  • COUT(x) is now called orxout(level)
  • output levels are now defined by an enum instead of numbers. see util/Output.h for the definition
  • it's possible to use output contexts with orxout(level, context). see util/Output.h for some common contexts. you can define more contexts
  • you must use 'endl' at the end of an output message, '\n' does not flush the message

Output levels:

  • instead of COUT(0) use orxout()
  • instead of COUT(1) use orxout(user_error) or orxout(internal_error)
  • instead of COUT(2) use orxout(user_warning) or orxout(internal_warning)
  • instead of COUT(3) use orxout(user_status/user_info) or orxout(internal_status/internal_info)
  • instead of COUT(4) use orxout(verbose)
  • instead of COUT(5) use orxout(verbose_more)
  • instead of COUT(6) use orxout(verbose_ultra)

Guidelines:

  • user_* levels are for the user, visible in the console and the log-file
  • internal_* levels are for developers, visible in the log-file
  • verbose_* levels are for debugging, only visible if the context of the output is activated

Usage in C++:

  • orxout() << "message" << endl;
  • orxout(level) << "message" << endl;
  • orxout(level, context) << "message" << endl;

Usage in Lua:

  • orxout("message")
  • orxout(orxonox.level.levelname, "message")
  • orxout(orxonox.level.levelname, "context", "message")

Usage in Tcl (and in the in-game-console):

  • orxout levelname message
  • orxout_context levelname context message
  • shortcuts: log message, error message, warning message, status message, info message, debug message
  • Property svn:eol-style set to native
File size: 4.8 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->path_ = getenv("TEMP");
61#else
62        this->path_ = "/tmp";
63#endif
64        this->bDefaultPath_ = true;
65
66        this->openFile();
67    }
68
69    /**
70        @brief Destructor, closes the log-file.
71    */
72    LogWriter::~LogWriter()
73    {
74        this->closeFile();
75    }
76
77    /**
78        @brief Returns the only existing instance of this class.
79    */
80    /*static*/ LogWriter& LogWriter::getInstance()
81    {
82        static LogWriter instance;
83        return instance;
84    }
85
86    /**
87        @brief Opens the log-file in order to write output to it.
88    */
89    void LogWriter::openFile()
90    {
91        // get the full file-name
92        std::string name = this->path_ + '/' + this->filename_;
93
94        // if we open the log file in the default directory, send a message to the user so that he can find the file in the case of a crash.
95        if (this->bDefaultPath_)
96            OutputManager::getInstance().pushMessage(level::user_info, context::undefined(), "Opening log file " + name);
97
98        // open the file
99        this->file_.open(name.c_str(), std::fstream::out);
100
101        // check if it worked and print some output
102        if (this->file_.is_open())
103            this->printLine("Log file opened", level::none);
104        else
105            OutputManager::getInstance().pushMessage(level::user_warning, context::undefined(), "Failed to open log file. File logging disabled.");
106    }
107
108    /**
109        @brief Closes the log-file.
110    */
111    void LogWriter::closeFile()
112    {
113        if (this->file_.is_open())
114        {
115            this->printLine("Log file closed", level::none);
116            this->file_.close();
117        }
118    }
119
120    /**
121        @brief Changes the path of the log-file. Re-writes the log-file by using MemoryWriter.
122    */
123    void LogWriter::setLogPath(const std::string& path)
124    {
125        // notify about the change of the log-file (because the old file will no longer be updated)
126        OutputManager::getInstance().pushMessage(level::internal_info, context::undefined(), "Migrating log file from " + this->path_ + "\nto " + path);
127
128        // close the old file, update the path and open the new file
129        this->closeFile();
130        this->path_ = path;
131        this->bDefaultPath_ = false;
132        this->openFile();
133
134        // request old output from MemoryWriter
135        MemoryWriter::getInstance().resendOutput(this);
136    }
137
138    /**
139        @brief Inherited function from BaseWriter, writers output together with a timestamp to the log-file.
140    */
141    void LogWriter::printLine(const std::string& line, OutputLevel)
142    {
143        if (!this->file_.is_open())
144            return;
145
146        // get the current time
147        time_t rawtime;
148        struct tm* timeinfo;
149        time(&rawtime);
150        timeinfo = localtime(&rawtime);
151
152        // print timestamp and output line to the log file
153        this->file_ << (timeinfo->tm_hour < 10 ? "0" : "") << timeinfo->tm_hour << ':' <<
154                       (timeinfo->tm_min  < 10 ? "0" : "") << timeinfo->tm_min  << ':' <<
155                       (timeinfo->tm_sec  < 10 ? "0" : "") << timeinfo->tm_sec  << ' ' << line << std::endl;
156    }
157}
Note: See TracBrowser for help on using the repository browser.