Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/libs/src/libraries/core/command/ConsoleCommandCompilation.cc @ 10158

Last change on this file since 10158 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: 9.0 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 *      ...
26 *
27 */
28
29/**
30    @file
31    @brief Implementation of some console commands.
32*/
33
34#include "ConsoleCommandCompilation.h"
35
36#include <fstream>
37#include <set>
38#include <string>
39
40#include "util/Output.h"
41#include "util/ExprParser.h"
42#include "util/StringUtils.h"
43#include "ConsoleCommand.h"
44#include "CommandExecutor.h"
45
46namespace orxonox
47{
48    SetConsoleCommand("echo", echo);
49
50    SetConsoleCommand("orxout", orxout_level);
51    SetConsoleCommand("orxout_context", orxout_level_context);
52
53    SetConsoleCommand("log"    , log    );
54    SetConsoleCommand("error"  , error  ).hide();
55    SetConsoleCommand("warning", warning).hide();
56    SetConsoleCommand("status" , status ).hide();
57    SetConsoleCommand("info"   , info   ).hide();
58    SetConsoleCommand("debug"  , debug  ).hide();
59
60//    SetConsoleCommand("source", source).argumentCompleter(0, autocompletion::files());  // disabled because we use the implementation in Tcl
61//    SetConsoleCommand("read", read).argumentCompleter(0, autocompletion::files());      // disabled because we use the implementation in Tcl
62//    SetConsoleCommand("append", append).argumentCompleter(0, autocompletion::files());  // disabled because we use the implementation in Tcl
63//    SetConsoleCommand("write", write).argumentCompleter(0, autocompletion::files());    // disabled because we use the implementation in Tcl
64
65    SetConsoleCommand("calculate", calculate);
66
67    /**
68        @brief Simply returns the arguments.
69    */
70    std::string echo(const std::string& text)
71    {
72        return text;
73    }
74
75    /**
76        @brief Builds a map that maps the levels of all output levels to their ID.
77    */
78    std::map<std::string, OutputLevel> getOutputLevelsMap()
79    {
80        std::map<std::string, OutputLevel> levels;
81
82        levels["message"]          = level::message;
83        levels["debug_output"]     = level::debug_output;
84        levels["user_error"]       = level::user_error;
85        levels["user_warning"]     = level::user_warning;
86        levels["user_status"]      = level::user_status;
87        levels["user_info"]        = level::user_info;
88        levels["internal_error"]   = level::internal_error;
89        levels["internal_warning"] = level::internal_warning;
90        levels["internal_status"]  = level::internal_status;
91        levels["internal_info"]    = level::internal_info;
92        levels["verbose"]          = level::verbose;
93        levels["verbose_more"]     = level::verbose_more;
94        levels["verbose_ultra"]    = level::verbose_ultra;
95
96        return levels;
97    }
98
99    /**
100        @brief Prints text to the console.
101        @param level_name The name of the output level
102    */
103    void orxout_level(const std::string& level_name, const std::string& text)
104    {
105        static std::map<std::string, OutputLevel> levels = getOutputLevelsMap();
106
107        OutputLevel level = level::debug_output;
108        std::map<std::string, OutputLevel>::iterator it = levels.find(level_name);
109        if (it != levels.end())
110            level = it->second;
111        else
112            orxout(internal_warning) << "'" << level_name << "' is not a valid output level" << endl;
113
114        orxout(level) << text << endl;
115    }
116
117    /**
118        @brief Prints text to the console.
119        @param level_name The name of the output level
120        @param context_name The name of the output context
121    */
122    void orxout_level_context(const std::string& level_name, const std::string& context_name, const std::string& text)
123    {
124        static std::map<std::string, OutputLevel> levels = getOutputLevelsMap();
125
126        OutputLevel level = level::debug_output;
127        std::map<std::string, OutputLevel>::iterator it = levels.find(level_name);
128        if (it != levels.end())
129            level = it->second;
130        else
131            orxout(internal_warning) << "'" << level_name << "' is not a valid output level" << endl;
132
133        OutputContextContainer context = registerContext(context_name);
134
135        orxout(level, context) << text << endl;
136    }
137
138    /// @brief Prints text to the console and the logfile.
139    void log(const std::string& text)
140    { orxout() << text << endl; }
141
142    /// @brief Prints output with error level.
143    void error(const std::string& text)
144    { orxout(user_error) << text << endl; }
145
146    /// @brief Prints output with warning level.
147    void warning(const std::string& text)
148    { orxout(user_warning) << text << endl; }
149
150    /// @brief Prints output with status level.
151    void status(const std::string& text)
152    { orxout(user_status) << text << endl; }
153
154    /// @brief Prints output with info level.
155    void info(const std::string& text)
156    { orxout(user_info) << text << endl; }
157
158    /// @brief Prints debug output with verbose level.
159    void debug(const std::string& text)
160    { orxout(verbose, context::tcl) << text << endl; }
161
162    /**
163        @brief Reads the content of a file and executes the commands in it line by line.
164    */
165    void source(const std::string& filename)
166    {
167        static std::set<std::string> executingFiles;
168
169        std::set<std::string>::const_iterator it = executingFiles.find(filename);
170        if (it != executingFiles.end())
171        {
172            orxout(user_error) << "Recurring source command in \"" << filename << "\". Stopped execution." << endl;
173            return;
174        }
175
176        // Open the file
177        std::ifstream file;
178        file.open(filename.c_str(), std::fstream::in);
179
180        if (!file.is_open())
181        {
182            orxout(user_error) << "Couldn't open file \"" << filename << "\"." << endl;
183            return;
184        }
185
186        executingFiles.insert(filename);
187
188        // Iterate through the file and put the lines into the CommandExecutor
189        while (file.good() && !file.eof())
190        {
191            std::string line;
192            std::getline(file, line);
193            CommandExecutor::execute(line);
194        }
195
196        executingFiles.erase(filename);
197        file.close();
198    }
199
200    /**
201        @brief Writes text to a file.
202    */
203    void write(const std::string& filename, const std::string& text)
204    {
205        std::ofstream file;
206        file.open(filename.c_str(), std::fstream::out);
207
208        if (!file.is_open())
209        {
210            orxout(user_error) << "Couldn't write to file \"" << filename << "\"." << endl;
211            return;
212        }
213
214        file << text << endl;
215        file.close();
216    }
217
218    /**
219        @brief Appends text to a file.
220    */
221    void append(const std::string& filename, const std::string& text)
222    {
223        std::ofstream file;
224        file.open(filename.c_str(), std::fstream::app);
225
226        if (!file.is_open())
227        {
228            orxout(user_error) << "Couldn't append to file \"" << filename << "\"." << endl;
229            return;
230        }
231
232        file << text << endl;
233        file.close();
234    }
235
236    /**
237        @brief Reads text from a file
238    */
239    std::string read(const std::string& filename)
240    {
241        std::ifstream file;
242        file.open(filename.c_str(), std::fstream::in);
243
244        if (!file.is_open())
245        {
246            orxout(user_error) << "Couldn't read from file \"" << filename << "\"." << endl;
247            return "";
248        }
249
250        std::string output;
251        while (file.good() && !file.eof())
252        {
253            std::string line;
254            std::getline(file, line);
255            output += line;
256            output += "\n";
257        }
258
259        file.close();
260
261        return output;
262    }
263
264    /**
265        @brief Parses the mathematical expression and returns the result.
266    */
267    float calculate(const std::string& calculation)
268    {
269        ExprParser expr;
270        expr.parse(calculation);
271        if (expr.getSuccess())
272        {
273            if (expr.getResult() == 42.0)
274            {
275                orxout(user_info) << "Greetings from the restaurant at the end of the universe." << endl;
276            }
277            if (!expr.getRemains().empty())
278            {
279                orxout(user_warning) << "Expression could not be parsed to the end! Remains: '" << expr.getRemains() << '\'' << endl;
280            }
281            return static_cast<float>(expr.getResult());
282        }
283        else
284        {
285            orxout(user_error) << "Cannot calculate expression: Parse error." << endl;
286            return 0;
287        }
288    }
289}
Note: See TracBrowser for help on using the repository browser.