Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6037


Ignore:
Timestamp:
Nov 5, 2009, 1:31:31 PM (15 years ago)
Author:
rgrieder
Message:

Redirected std::cout to a stringstream object in the IOConsole. —> you can still write to the console correctly with std::cout

Location:
code/branches/console/src/libraries/core
Files:
2 edited

Legend:

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

    r6015 r6037  
    7474    {
    7575        // Method only gets called upon start to draw all the lines
    76         // or when scrolling. But scrolling is disable and the output
     76        // or when scrolling. But scrolling is disabled and the output
    7777        // is already in std::cout when we start the IOConsole
    7878    }
     
    8282    {
    8383        this->printInputLine();
    84         std::cout.flush();
     84        this->cout_.flush();
    8585    }
    8686
     
    8989    {
    9090        this->printInputLine();
    91         std::cout.flush();
     91        this->cout_.flush();
    9292    }
    9393
     
    112112#include <termios.h>
    113113#include <sys/ioctl.h>
    114 #include <sys/stat.h>
    115114
    116115namespace orxonox
     
    129128        : shell_(new Shell("IOConsole", false, true))
    130129        , buffer_(shell_->getInputBuffer())
    131         , originalTerminalSettings_(new termios())
     130        , cout_(std::cout.rdbuf())
    132131        , bStatusPrinted_(false)
    133132        , promptString_("orxonox # ")
     133        , originalTerminalSettings_(new termios())
    134134    {
    135135        this->setTerminalMode();
     
    146146        // Disable standard std::cout logging
    147147        OutputHandler::getInstance().disableCout();
     148        // Redirect std::cout to an ostringstream
     149        // (Other part is in the initialiser list)
     150        std::cout.rdbuf(this->origCout_.rdbuf());
     151
     152        // Make sure we make way for the status lines
     153        this->update(Game::getInstance().getGameClock());
    148154    }
    149155
    150156    IOConsole::~IOConsole()
    151157    {
     158        // Empty all buffers
     159        this->update(Game::getInstance().getGameClock());
    152160        // Goto last line and create a new one
    153161        if (this->bStatusPrinted_)
    154             std::cout << "\033[" << this->statusLineWidths_.size() << 'E';
    155         std::cout << std::endl;
     162            this->cout_ << "\033[" << this->statusLineWidths_.size() << 'E';
     163        this->cout_ << std::endl;
    156164
    157165        resetTerminalMode();
     
    159167        this->shell_->destroy();
    160168
     169        // Restore this->cout_ redirection
     170        std::cout.rdbuf(this->cout_.rdbuf());
    161171        // Enable standard std::cout logging again
    162172        OutputHandler::getInstance().enableCout();
     
    250260            this->buffer_->buttonPressed(KeyEvent(KeyCode::Escape, '\033', 0));
    251261
     262        // Process output written to std::cout
     263        if (!this->origCout_.str().empty())
     264        {
     265            this->shell_->addOutputLine(this->origCout_.str());
     266            this->origCout_.str("");
     267        }
     268
    252269        // Determine terminal width and height
    253270        this->lastTerminalWidth_ = this->terminalWidth_;
     
    261278            // but that line might very well be the last
    262279            int newLines = std::min((int)this->statusLineWidths_.size(), -heightDiff);
    263             std::cout << std::string(newLines, '\n');
     280            this->cout_ << std::string(newLines, '\n');
    264281            // Move cursor up again
    265             std::cout << "\033[" << newLines << 'F';
     282            this->cout_ << "\033[" << newLines << 'F';
    266283        }
    267284
     
    269286        {
    270287            // Print new lines to make way for status lines
    271             std::cout << std::string(this->statusLineWidths_.size(), '\n');
     288            this->cout_ << std::string(this->statusLineWidths_.size(), '\n');
    272289            // Move cursor up again
    273             std::cout << "\033[" << this->statusLineWidths_.size() << 'F';
     290            this->cout_ << "\033[" << this->statusLineWidths_.size() << 'F';
    274291            this->bStatusPrinted_ = true;
    275292        }
    276293        // Erase status and input lines
    277         std::cout << "\033[1G\033[J";
     294        this->cout_ << "\033[1G\033[J";
    278295        this->printInputLine();
    279296        this->printStatusLines();
    280         std::cout.flush();
     297        this->cout_.flush();
    281298    }
    282299
     
    284301    {
    285302        std::string output = text;
     303/*
    286304        int level = this->extractLogLevel(&output);
    287305
    288306        // Colour line
    289 /*
    290307        switch (level)
    291308        {
    292         case -1: std::cout << "\033[37m"; break;
    293         case  1: std::cout << "\033[91m"; break;
    294         case  2: std::cout << "\033[31m"; break;
    295         case  3: std::cout << "\033[34m"; break;
    296         case  4: std::cout << "\033[36m"; break;
    297         case  5: std::cout << "\033[35m"; break;
    298         case  6: std::cout << "\033[37m"; break;
     309        case -1: this->cout_ << "\033[37m"; break;
     310        case  1: this->cout_ << "\033[91m"; break;
     311        case  2: this->cout_ << "\033[31m"; break;
     312        case  3: this->cout_ << "\033[34m"; break;
     313        case  4: this->cout_ << "\033[36m"; break;
     314        case  5: this->cout_ << "\033[35m"; break;
     315        case  6: this->cout_ << "\033[37m"; break;
    299316        default: break;
    300317        }
     
    302319
    303320        // Print output line
    304         std::cout << output;
     321        this->cout_ << output;
    305322
    306323        // Reset colour to white
    307 //        std::cout << "\033[37m";
     324//        this->cout_ << "\033[37m";
    308325    }
    309326
     
    311328    {
    312329        // Set cursor to the beginning of the line and erase the line
    313         std::cout << "\033[1G\033[K";
     330        this->cout_ << "\033[1G\033[K";
    314331        // Indicate a command prompt
    315         std::cout << this->promptString_;
     332        this->cout_ << this->promptString_;
    316333        // Save cursor position
    317         std::cout << "\033[s";
     334        this->cout_ << "\033[s";
    318335        // Print command line buffer
    319         std::cout << this->shell_->getInput();
     336        this->cout_ << this->shell_->getInput();
    320337        // Restore cursor position and move it to the right
    321         std::cout << "\033[u";
     338        this->cout_ << "\033[u";
    322339        if (this->buffer_->getCursorPosition() > 0)
    323             std::cout << "\033[" << this->buffer_->getCursorPosition() << "C";
     340            this->cout_ << "\033[" << this->buffer_->getCursorPosition() << "C";
    324341    }
    325342
     
    329346        {
    330347            // Save cursor position
    331             std::cout << "\033[s";
     348            this->cout_ << "\033[s";
    332349            // Move cursor down (don't create a new line here because the buffer might flush then!)
    333             std::cout << "\033[1E";
    334             std::cout << std::fixed << std::setprecision(2) << std::setw(5) << Game::getInstance().getAvgFPS() << " fps, ";
    335             std::cout <<               std::setprecision(2) << std::setw(5) << Game::getInstance().getAvgTickTime() << " ms tick time";
     350            this->cout_ << "\033[1E";
     351            //this->cout_ << std::fixed << std::setprecision(2) << std::setw(5) << Game::getInstance().getAvgFPS() << " fps, ";
     352            //this->cout_ <<               std::setprecision(2) << std::setw(5) << Game::getInstance().getAvgTickTime() << " ms tick time";
     353            this->cout_ << "Terminal width: " << this->terminalWidth_ << ", height: " << this->terminalHeight_;
    336354            // Restore cursor position
    337             std::cout << "\033[u";
     355            this->cout_ << "\033[u";
    338356            this->bStatusPrinted_ = true;
    339357        }
     
    397415    {
    398416        // Save cursor position and move it to the beginning of the first output line
    399         std::cout << "\033[s\033[1F";
     417        this->cout_ << "\033[s\033[1F";
    400418        // Erase the line
    401         std::cout << "\033[K";
     419        this->cout_ << "\033[K";
    402420        // Reprint the last output line
    403421        this->printLogText(*(this->shell_->getNewestLineIterator()));
    404422        // Restore cursor
    405         std::cout << "\033[u";
    406         std::cout.flush();
     423        this->cout_ << "\033[u";
     424        this->cout_.flush();
    407425    }
    408426
     
    412430        // Move cursor to the bottom line
    413431        if (this->bStatusPrinted_)
    414             std::cout << "\033[" << this->statusLineWidths_.size() << 'E';
     432            this->cout_ << "\033[" << this->statusLineWidths_.size() << 'E';
    415433        // Create new lines on the screen
    416434        int newLines = this->shell_->getNewestLineIterator()->size() / this->terminalWidth_ + 1;
    417         std::cout << std::string(newLines, '\n');
     435        this->cout_ << std::string(newLines, '\n');
    418436        // Move cursor to the beginning of the new (last) output line
    419         std::cout << "\033[" << (newLines + this->statusLineWidths_.size()) << 'F';
     437        this->cout_ << "\033[" << (newLines + this->statusLineWidths_.size()) << 'F';
    420438        // Erase screen from here
    421         std::cout << "\033[J";
     439        this->cout_ << "\033[J";
    422440        // Print the new output line
    423441        for (int i = 0; i < newLines; ++i)
    424442            this->printLogText(this->shell_->getNewestLineIterator()->substr(i*this->terminalWidth_, this->terminalWidth_));
    425443        // Move cursor down
    426         std::cout << "\033[1E";
     444        this->cout_ << "\033[1E";
    427445        // Print status and input lines
    428446        this->printInputLine();
    429447        this->printStatusLines();
    430         std::cout.flush();
     448        this->cout_.flush();
    431449    }
    432450}
     
    457475        this->lastTerminalHeight_ = this->terminalHeight_;
    458476
    459         // Disable standard std::cout logging
     477        // Disable standard this->cout_ logging
    460478        OutputHandler::getInstance().disableCout();
    461479*/
     
    468486        this->shell_->destroy();
    469487
    470         // Enable standard std::cout logging again
     488        // Enable standard this->cout_ logging again
    471489        OutputHandler::getInstance().enableCout();
    472490*/
  • code/branches/console/src/libraries/core/IOConsole.h

    r6015 r6037  
    3333#include "CorePrereqs.h"
    3434
     35#include <sstream>
    3536#include <string>
    3637#include <vector>
     
    7576        Shell*                  shell_;
    7677        InputBuffer*            buffer_;
     78        std::ostream            cout_;
     79        std::ostringstream      origCout_;
    7780        unsigned int            terminalWidth_;
    7881        unsigned int            terminalHeight_;
Note: See TracChangeset for help on using the changeset viewer.