Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 23, 2011, 12:45:53 AM (13 years ago)
Author:
landauf
Message:

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
Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/core/command/ConsoleCommandCompilation.cc

    r8079 r8858  
    3838#include <string>
    3939
    40 #include "util/Debug.h"
     40#include "util/Output.h"
    4141#include "util/ExprParser.h"
    4242#include "util/StringUtils.h"
     
    4646namespace orxonox
    4747{
     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
    4860//    SetConsoleCommand("source", source).argumentCompleter(0, autocompletion::files());  // disabled because we use the implementation in Tcl
    49     SetConsoleCommand("echo", echo);
    50 //    SetConsoleCommand("puts", puts);                                                    // disabled because we use the implementation in Tcl
    51 
    5261//    SetConsoleCommand("read", read).argumentCompleter(0, autocompletion::files());      // disabled because we use the implementation in Tcl
    5362//    SetConsoleCommand("append", append).argumentCompleter(0, autocompletion::files());  // disabled because we use the implementation in Tcl
     
    5766
    5867    /**
     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    /**
    59163        @brief Reads the content of a file and executes the commands in it line by line.
    60164    */
     
    66170        if (it != executingFiles.end())
    67171        {
    68             COUT(1) << "Error: Recurring source command in \"" << filename << "\". Stopped execution." << std::endl;
     172            orxout(user_error) << "Recurring source command in \"" << filename << "\". Stopped execution." << endl;
    69173            return;
    70174        }
     
    76180        if (!file.is_open())
    77181        {
    78             COUT(1) << "Error: Couldn't open file \"" << filename << "\"." << std::endl;
     182            orxout(user_error) << "Couldn't open file \"" << filename << "\"." << endl;
    79183            return;
    80184        }
     
    95199
    96200    /**
    97         @brief Simply returns the arguments.
    98     */
    99     std::string echo(const std::string& text)
    100     {
    101         return text;
    102     }
    103 
    104     /**
    105         @brief Writes text to the console, depending on the first argument with or without a line-break after it.
    106     */
    107     void puts(bool newline, const std::string& text)
    108     {
    109         if (newline)
    110         {
    111             COUT(0) << stripEnclosingBraces(text) << std::endl;
    112         }
    113         else
    114         {
    115             COUT(0) << stripEnclosingBraces(text);
    116         }
    117     }
    118 
    119     /**
    120201        @brief Writes text to a file.
    121202    */
     
    127208        if (!file.is_open())
    128209        {
    129             COUT(1) << "Error: Couldn't write to file \"" << filename << "\"." << std::endl;
    130             return;
    131         }
    132 
    133         file << text << std::endl;
     210            orxout(user_error) << "Couldn't write to file \"" << filename << "\"." << endl;
     211            return;
     212        }
     213
     214        file << text << endl;
    134215        file.close();
    135216    }
     
    145226        if (!file.is_open())
    146227        {
    147             COUT(1) << "Error: Couldn't append to file \"" << filename << "\"." << std::endl;
    148             return;
    149         }
    150 
    151         file << text << std::endl;
     228            orxout(user_error) << "Couldn't append to file \"" << filename << "\"." << endl;
     229            return;
     230        }
     231
     232        file << text << endl;
    152233        file.close();
    153234    }
     
    163244        if (!file.is_open())
    164245        {
    165             COUT(1) << "Error: Couldn't read from file \"" << filename << "\"." << std::endl;
     246            orxout(user_error) << "Couldn't read from file \"" << filename << "\"." << endl;
    166247            return "";
    167248        }
     
    192273            if (expr.getResult() == 42.0)
    193274            {
    194                 COUT(3) << "Greetings from the restaurant at the end of the universe." << std::endl;
     275                orxout(user_info) << "Greetings from the restaurant at the end of the universe." << endl;
    195276            }
    196277            if (!expr.getRemains().empty())
    197278            {
    198                 COUT(2) << "Warning: Expression could not be parsed to the end! Remains: '" << expr.getRemains() << '\'' << std::endl;
     279                orxout(user_warning) << "Expression could not be parsed to the end! Remains: '" << expr.getRemains() << '\'' << endl;
    199280            }
    200281            return static_cast<float>(expr.getResult());
     
    202283        else
    203284        {
    204             COUT(1) << "Error: Cannot calculate expression: Parse error." << std::endl;
     285            orxout(user_error) << "Cannot calculate expression: Parse error." << endl;
    205286            return 0;
    206287        }
Note: See TracChangeset for help on using the changeset viewer.