Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6015


Ignore:
Timestamp:
Nov 2, 2009, 5:54:13 PM (15 years ago)
Author:
rgrieder
Message:

IOConsole cleanup and added ConsoleWriter to the OutputHandler so that we get std::cout output before the creation of the IOConsole.

Location:
code/branches/console/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • code/branches/console/src/libraries/core/Core.cc

    r6004 r6015  
    218218        this->languageInstance_.reset(new Language());
    219219
     220        // create persistent io console
     221        this->ioConsole_.reset(new IOConsole());
     222
    220223        // creates the class hierarchy for all classes with factories
    221224        Identifier::createClassHierarchy();
     
    231234        this->tclBind_.reset(new TclBind(PathConfig::getDataPathString()));
    232235        this->tclThreadManager_.reset(new TclThreadManager(tclBind_->getTclInterpreter()));
    233 
    234         // create persistent io console
    235         this->ioConsole_.reset(new IOConsole());
    236236
    237237        // Create singletons that always exist (in other libraries)
  • code/branches/console/src/libraries/core/Core.h

    r6004 r6015  
    9191            scoped_ptr<ConfigFileManager> configFileManager_;
    9292            scoped_ptr<Language>          languageInstance_;
     93            scoped_ptr<IOConsole>         ioConsole_;
    9394            scoped_ptr<CoreConfiguration> configuration_;
    9495            scoped_ptr<TclBind>           tclBind_;
    9596            scoped_ptr<TclThreadManager>  tclThreadManager_;
    96             scoped_ptr<IOConsole>         ioConsole_;
    9797            // graphical
    9898            scoped_ptr<GraphicsManager>   graphicsManager_;     //!< Interface to OGRE
  • code/branches/console/src/libraries/core/IOConsole.cc

    r6014 r6015  
    3030#include "IOConsole.h"
    3131
    32 #include <cstring>
    3332#include <iomanip>
    3433#include <iostream>
    35 
    36 #include "util/Clock.h"
    37 #include "util/Debug.h"
    38 #include "util/Sleep.h"
    39 #include "core/CommandExecutor.h"
    4034#include "core/Game.h"
    41 #include "core/GameMode.h"
    42 #include "core/Shell.h"
    4335#include "core/input/InputBuffer.h"
    4436
     37// ##########################
     38// ###   Mutual methods   ###
     39// ##########################
     40namespace orxonox
     41{
     42    IOConsole* IOConsole::singletonPtr_s = NULL;
     43
     44    int IOConsole::extractLogLevel(std::string* text)
     45    {
     46        // Handle line colouring by inspecting the first letter
     47        char level = 0;
     48        if (!text->empty())
     49        {
     50            level = (*text)[0];
     51            if (level == -1 || level >= 1 && level <= 6)
     52            {
     53                *text = text->substr(1);
     54                if (level != -1)
     55                    return level;
     56            }
     57        }
     58        return 0;
     59    }
     60
     61    inline bool IOConsole::willPrintStatusLines()
     62    {
     63        return !this->statusLineWidths_.empty()
     64             && this->terminalWidth_  >= this->statusLineMaxWidth_
     65             && this->terminalHeight_ >= (this->minOutputLines_ + this->statusLineWidths_.size());
     66    }
     67
     68    // ###############################
     69    // ###  ShellListener methods  ###
     70    // ###############################
     71
     72    //! Called if all output-lines have to be reprinted
     73    void IOConsole::linesChanged()
     74    {
     75        // Method only gets called upon start to draw all the lines
     76        // or when scrolling. But scrolling is disable and the output
     77        // is already in std::cout when we start the IOConsole
     78    }
     79
     80    //! Called if the text in the input-line has changed
     81    void IOConsole::inputChanged()
     82    {
     83        this->printInputLine();
     84        std::cout.flush();
     85    }
     86
     87    //! Called if the position of the cursor in the input-line has changed
     88    void IOConsole::cursorChanged()
     89    {
     90        this->printInputLine();
     91        std::cout.flush();
     92    }
     93
     94    //! Called if a command is about to be executed
     95    void IOConsole::executed()
     96    {
     97        this->shell_->addOutputLine(this->promptString_ + this->shell_->getInput());
     98    }
     99
     100    //! Called if the console gets closed
     101    void IOConsole::exit()
     102    {
     103        // Exit is not an option, just do nothing (Shell doesn't really exit too)
     104    }
     105}
     106
    45107#ifdef ORXONOX_PLATFORM_UNIX
     108// ###############################
     109// ###   Unix Implementation   ###
     110// ###############################
     111
    46112#include <termios.h>
    47113#include <sys/ioctl.h>
    48114#include <sys/stat.h>
    49 #endif
    50115
    51116namespace orxonox
    52117{
    53     IOConsole* IOConsole::singletonPtr_s = NULL;
    54 
    55 #ifdef ORXONOX_PLATFORM_UNIX
    56 
    57118    namespace EscapeMode
    58119    {
     
    82143        this->lastTerminalWidth_ = this->terminalWidth_;
    83144        this->lastTerminalHeight_ = this->terminalHeight_;
     145
     146        // Disable standard std::cout logging
     147        OutputHandler::getInstance().disableCout();
    84148    }
    85149
    86150    IOConsole::~IOConsole()
    87151    {
    88         // Goto last line and erase it
     152        // Goto last line and create a new one
    89153        if (this->bStatusPrinted_)
    90154            std::cout << "\033[" << this->statusLineWidths_.size() << 'E';
    91         std::cout << "\033[1G\033[K";
    92         std::cout.flush();
     155        std::cout << std::endl;
     156
    93157        resetTerminalMode();
    94158        delete this->originalTerminalSettings_;
    95159        this->shell_->destroy();
    96     }
    97 
    98     void IOConsole::setTerminalMode()
    99     {
    100         termios new_settings;
    101 
    102         tcgetattr(0, this->originalTerminalSettings_);
    103         new_settings = *this->originalTerminalSettings_;
    104         new_settings.c_lflag &= ~(ICANON | ECHO);
    105         //         new_settings.c_lflag |= ( ISIG | IEXTEN );
    106         new_settings.c_cc[VTIME] = 0;
    107         new_settings.c_cc[VMIN]  = 0;
    108         tcsetattr(0, TCSANOW, &new_settings);
    109         COUT(0) << endl;
    110         //       atexit(&IOConsole::resetTerminalMode);
    111     }
    112 
    113     void IOConsole::resetTerminalMode()
    114     {
    115         tcsetattr(0, TCSANOW, IOConsole::originalTerminalSettings_);
     160
     161        // Enable standard std::cout logging again
     162        OutputHandler::getInstance().enableCout();
    116163    }
    117164
     
    121168        std::string escapeSequence;
    122169        EscapeMode::Value escapeMode = EscapeMode::None;
    123         while (read(STDIN_FILENO, &c, 1) == 1)
    124         {
     170        while (std::cin.good())
     171        {
     172            c = std::cin.get();
     173            if (std::cin.bad())
     174                break;
     175
    125176            if (escapeMode == EscapeMode::First && (c == '[' || c=='O') )
    126177                escapeMode = EscapeMode::Second;
     
    191242            }
    192243        }
     244        // Reset error flags in std::cin
     245        std::cin.clear();
    193246
    194247        // If there is still an escape key pending (escape key ONLY), then
     
    205258        if (this->bStatusPrinted_ && heightDiff < 0)
    206259        {
    207             // Terminal width has shrinked. The cursor will still be on the input line,
     260            // Terminal width has shrunk. The cursor will still be on the input line,
    208261            // but that line might very well be the last
    209262            int newLines = std::min((int)this->statusLineWidths_.size(), -heightDiff);
     
    230283    void IOConsole::printLogText(const std::string& text)
    231284    {
    232         std::string output;
    233 
    234         // Handle line colouring by inspecting the first letter
    235         char level = 0;
    236         if (!text.empty())
    237             level = text[0];
    238         if (level >= -1 && level <= 6)
    239             output = text.substr(1);
    240         else
    241             output = text;
     285        std::string output = text;
     286        int level = this->extractLogLevel(&output);
    242287
    243288        // Colour line
     
    297342    }
    298343
    299     inline bool IOConsole::willPrintStatusLines()
    300     {
    301         return !this->statusLineWidths_.empty()
    302              && this->terminalWidth_  >= this->statusLineMaxWidth_
    303              && this->terminalHeight_ >= (this->minOutputLines_ + this->statusLineWidths_.size());
     344    void IOConsole::setTerminalMode()
     345    {
     346        termios new_settings;
     347
     348        tcgetattr(0, this->originalTerminalSettings_);
     349        new_settings = *this->originalTerminalSettings_;
     350        new_settings.c_lflag &= ~(ICANON | ECHO);
     351        //new_settings.c_lflag |= (ISIG | IEXTEN);
     352        new_settings.c_cc[VTIME] = 0;
     353        new_settings.c_cc[VMIN]  = 0;
     354        tcsetattr(0, TCSANOW, &new_settings);
     355    }
     356
     357    void IOConsole::resetTerminalMode()
     358    {
     359        tcsetattr(0, TCSANOW, IOConsole::originalTerminalSettings_);
    304360    }
    305361
     
    333389    }
    334390
    335 #elif defined(ORXONOX_PLATFORM_WINDOWS)
    336 
    337     IOConsole::IOConsole()
    338         : shell_(new Shell("IOConsole", false))
    339         , buffer_(shell_->getInputBuffer())
    340     {
    341         this->setTerminalMode();
    342     }
    343 
    344     IOConsole::~IOConsole()
    345     {
    346     }
    347 
    348     void IOConsole::setTerminalMode()
    349     {
    350     }
    351 
    352     void IOConsole::resetTerminalMode()
    353     {
    354     }
    355 
    356     void IOConsole::update(const Clock& time)
    357     {
    358     }
    359 
    360     void IOConsole::printLogText(const std::string& text)
    361     {
    362     }
    363 
    364     void IOConsole::printInputLine()
    365     {
    366     }
    367 
    368     void IOConsole::printStatusLines()
    369     {
    370     }
    371 
    372 #endif /* ORXONOX_PLATFORM_UNIX */
    373 
    374391    // ###############################
    375392    // ###  ShellListener methods  ###
    376393    // ###############################
    377394
    378     /**
    379     @brief
    380         Called if all output-lines have to be redrawn.
    381     */
    382     void IOConsole::linesChanged()
    383     {
    384         // Method only gets called upon start to draw all the lines
    385         // But we are using std::cout anyway, so do nothing here
    386     }
    387 
    388     /**
    389     @brief
    390         Called if only the last output-line has changed.
    391     */
     395    //! Called if only the last output-line has changed
    392396    void IOConsole::onlyLastLineChanged()
    393397    {
     
    403407    }
    404408
    405     /**
    406     @brief
    407         Called if a new output-line was added.
    408     */
     409    //! Called if a new output-line was added
    409410    void IOConsole::lineAdded()
    410411    {
     
    429430        std::cout.flush();
    430431    }
    431 
    432     /**
    433     @brief
    434         Called if the text in the input-line has changed.
    435     */
    436     void IOConsole::inputChanged()
    437     {
    438         this->printInputLine();
    439         std::cout.flush();
    440     }
    441 
    442     /**
    443     @brief
    444         Called if the position of the cursor in the input-line has changed.
    445     */
    446     void IOConsole::cursorChanged()
    447     {
    448         this->printInputLine();
    449         std::cout.flush();
    450     }
    451 
    452     /**
    453     @brief
    454         Called if a command is about to be executed
    455     */
    456     void IOConsole::executed()
    457     {
    458         this->shell_->addOutputLine(this->promptString_ + this->shell_->getInput());
    459     }
    460 
    461 
    462     /**
    463     @brief
    464         Called if the console gets closed.
    465     */
    466     void IOConsole::exit()
    467     {
    468         // Exit is not an option, IOConsole always exists
    469     }
    470 
    471432}
     433
     434#elif defined(ORXONOX_PLATFORM_WINDOWS)
     435// ##################################
     436// ###   Windows Implementation   ###
     437// ##################################
     438
     439namespace orxonox
     440{
     441    IOConsole::IOConsole()
     442        : shell_(new Shell("IOConsole", false, true))
     443        , buffer_(shell_->getInputBuffer())
     444        , bStatusPrinted_(false)
     445        , promptString_("orxonox # ")
     446    {
     447/*
     448        this->setTerminalMode();
     449        this->shell_->registerListener(this);
     450
     451        // Manually set the widths of the individual status lines
     452        this->statusLineWidths_.push_back(29);
     453        this->statusLineMaxWidth_ = 29;
     454
     455        this->getTerminalSize();
     456        this->lastTerminalWidth_ = this->terminalWidth_;
     457        this->lastTerminalHeight_ = this->terminalHeight_;
     458
     459        // Disable standard std::cout logging
     460        OutputHandler::getInstance().disableCout();
     461*/
     462    }
     463
     464    IOConsole::~IOConsole()
     465    {
     466/*
     467        resetTerminalMode();
     468        this->shell_->destroy();
     469
     470        // Enable standard std::cout logging again
     471        OutputHandler::getInstance().enableCout();
     472*/
     473    }
     474
     475    void IOConsole::update(const Clock& time)
     476    {
     477/*
     478        unsigned char c = 0;
     479        while (std::cin.good())
     480        {
     481            c = std::cin.get();
     482            if (std::cin.bad())
     483                break;
     484        }
     485        // Reset error flags in std::cin
     486        std::cin.clear();
     487
     488        // Determine terminal width and height
     489        this->lastTerminalWidth_ = this->terminalWidth_;
     490        this->lastTerminalHeight_ = this->terminalHeight_;
     491        this->getTerminalSize();
     492*/
     493    }
     494
     495    void IOConsole::printLogText(const std::string& text)
     496    {
     497    }
     498
     499    void IOConsole::printInputLine()
     500    {
     501    }
     502
     503    void IOConsole::printStatusLines()
     504    {
     505/*
     506        if (this->willPrintStatusLines())
     507        {
     508            this->bStatusPrinted_ = true;
     509        }
     510        else
     511            this->bStatusPrinted_ = false;
     512*/
     513    }
     514
     515    void IOConsole::setTerminalMode()
     516    {
     517    }
     518
     519    void IOConsole::resetTerminalMode()
     520    {
     521    }
     522
     523    void IOConsole::getTerminalSize()
     524    {
     525    }
     526
     527    // ###############################
     528    // ###  ShellListener methods  ###
     529    // ###############################
     530
     531    //! Called if only the last output-line has changed
     532    void IOConsole::onlyLastLineChanged()
     533    {
     534    }
     535
     536    //! Called if a new output-line was added
     537    void IOConsole::lineAdded()
     538    {
     539    }
     540}
     541
     542#endif /* ORXONOX_PLATFORM_UNIX */
  • code/branches/console/src/libraries/core/IOConsole.h

    r6014 r6015  
    3333#include "CorePrereqs.h"
    3434
    35 #include <queue>
    36 #include <boost/scoped_ptr.hpp>
     35#include <string>
     36#include <vector>
    3737#include "util/Singleton.h"
    3838#include "core/Shell.h"
    3939
     40#ifdef ORXONOX_PLATFORM_UNIX
    4041struct termios;
     42#endif
    4143
    4244namespace orxonox
     
    5355
    5456    private:
    55 
    5657        void setTerminalMode();
    5758        void resetTerminalMode();
    5859        void getTerminalSize();
    5960        bool willPrintStatusLines();
     61        int extractLogLevel(std::string* text);
    6062
    6163        void printLogText(const std::string& line);
     
    7173        void executed();
    7274        void exit();
    73 
    7475        Shell*                  shell_;
    7576        InputBuffer*            buffer_;
    76         termios*                originalTerminalSettings_;
    7777        unsigned int            terminalWidth_;
    7878        unsigned int            terminalHeight_;
     
    8686        static const unsigned   minOutputLines_ = 3;
    8787
     88#ifdef ORXONOX_PLATFORM_UNIX
     89        termios*                originalTerminalSettings_;
     90#endif
     91
    8892        static IOConsole* singletonPtr_s;
    8993    };
  • code/branches/console/src/libraries/util/OutputHandler.cc

    r6007 r6015  
    118118        std::ofstream logFile_;     //! File handle for the log file
    119119        std::string   logFilename_; //! Filename of the log file
     120    };
     121
     122
     123    /////////////////////////
     124    ///// ConsoleWriter /////
     125    /////////////////////////
     126    /**
     127    @brief
     128        Writes the output to std::cout.
     129    @note
     130        This listener will usually be disable once an actual shell with console is instantiated.
     131    */
     132    class ConsoleWriter : public OutputListener
     133    {
     134    public:
     135        //! Only assigns the output stream with std::cout
     136        ConsoleWriter()
     137            : OutputListener("consoleLog")
     138        {
     139            this->outputStream_ = &std::cout;
     140        }
    120141    };
    121142
     
    176197        : outputLevel_(OutputLevel::Verbose)
    177198    {
     199#ifdef ORXONOX_RELEASE
     200        const OutputLevel::Value defaultLevelConsole = OutputLevel::Error;
     201        const OutputLevel::Value defaultLevelLogFile = OutputLevel::Info;
     202#else
     203        const OutputLevel::Value defaultLevelConsole = OutputLevel::Info;
     204        const OutputLevel::Value defaultLevelLogFile = OutputLevel::Debug;
     205#endif
     206
    178207        this->logFile_ = new LogFileWriter();
    179208        // Use default level until we get the configValue from the Core
    180         this->logFile_->softDebugLevel_ = OutputLevel::Debug;
     209        this->logFile_->softDebugLevel_ = defaultLevelLogFile;
    181210        this->registerOutputListener(this->logFile_);
     211
     212        this->consoleWriter_ = new ConsoleWriter();
     213        this->consoleWriter_->softDebugLevel_ = defaultLevelConsole;
     214        this->registerOutputListener(this->consoleWriter_);
    182215
    183216        this->output_  = new MemoryLogWriter();
     
    225258    }
    226259
     260    void OutputHandler::disableCout()
     261    {
     262        this->unregisterOutputListener(this->consoleWriter_);
     263    }
     264
     265    void OutputHandler::enableCout()
     266    {
     267        this->registerOutputListener(this->consoleWriter_);
     268    }
     269
    227270    OutputHandler::OutputVectorIterator OutputHandler::getOutputVectorBegin() const
    228271    {
  • code/branches/console/src/libraries/util/OutputHandler.h

    r6004 r6015  
    7474    // Forward declarations for classes in the source file
    7575    class LogFileWriter;
     76    class ConsoleWriter;
    7677    class MemoryLogWriter;
    7778
     
    129130            //! Set the log path once the program has been properly initialised
    130131            void setLogPath(const std::string& path);
     132            //! Disables the std::cout stream for output
     133            void disableCout();
     134            //! Enables the std::cout stream for output (startup behaviour)
     135            void enableCout();
    131136
    132137            //! Sets the level of the incoming output and returns the OutputHandler
     
    210215            OutputHandler(const OutputHandler& rhs); //! Unused and undefined
    211216
    212             std::list<OutputListener*> listeners_; //!< Array with all registered output listeners
    213             int outputLevel_;                      //!< The level of the incoming output
    214             LogFileWriter* logFile_;               //!< Listener that writes to the log file
    215             MemoryLogWriter* output_;              //!< Listener that Stores ALL output below the current soft debug level
    216             static int softDebugLevel_s;           //!< Maximum of all soft debug levels. @note This is only static for faster access
     217            std::list<OutputListener*> listeners_;        //!< Array with all registered output listeners
     218            int                        outputLevel_;      //!< The level of the incoming output
     219            LogFileWriter*             logFile_;          //!< Listener that writes to the log file
     220            ConsoleWriter*             consoleWriter_;    //!< Listener for std::cout (just program beginning)
     221            MemoryLogWriter*           output_;           //!< Listener that Stores ALL output below the current soft debug level
     222            static int                 softDebugLevel_s;  //!< Maximum of all soft debug levels. @note This is only static for faster access
    217223    };
    218224
  • code/branches/console/src/orxonox/overlays/InGameConsole.cc

    r6010 r6015  
    336336
    337337    /**
     338        @brief Called if a command is about to be executed
     339    */
     340    void InGameConsole::executed()
     341    {
     342        this->shell_->addOutputLine(this->shell_->getInput());
     343    }
     344
     345    /**
    338346        @brief Called if the console gets closed.
    339347    */
  • code/branches/console/src/orxonox/overlays/InGameConsole.h

    r6004 r6015  
    6868        void inputChanged();
    6969        void cursorChanged();
     70        void executed();
    7071        void exit();
    7172
Note: See TracChangeset for help on using the changeset viewer.