Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/chat/ChatHistory.cc @ 10219

Last change on this file since 10219 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

  • Property svn:eol-style set to native
File size: 4.5 KB
RevLine 
[6644]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 *      Sandro 'smerkli' Merkli
24 *   Co-authors:
25 *      ...
26 *
27 */
28
[6688]29#include "ChatHistory.h"
[7284]30#include "util/ScopedSingletonManager.h"
[6644]31
[6698]32#ifndef CHATTEST
[6644]33namespace orxonox
34{
[6698]35  /* singleton */
36  ManageScopedSingleton( ChatHistory, ScopeID::Root, false );
[6688]37#endif
[6644]38
39  /* constructor */
[6698]40#ifndef CHATTEST
[7127]41  ChatHistory::ChatHistory()
[6693]42#else
[6688]43  ChatHistory::ChatHistory()
[6693]44#endif
[6644]45  {
46    /* register the object */
[6698]47#ifndef CHATTEST
[6644]48    RegisterObject(ChatHistory);
[6693]49#endif
[6644]50
[6688]51    this->hist_log_enabled = true;
52
[6644]53    /* Read setting for logfiles */
[6688]54    if( hist_log_enabled ) /* NOTE Make this a check for the logfile setting */
55    { this->chat_hist_openlog();
[6644]56
[6688]57      /* push starting line */
58      this->chat_hist_logline( "--- Logfile opened ---" );
59    }
60
[6644]61    /* Read setting for maximum number of lines and set limit */
62    this->hist_maxlines = 200; /* NOTE to be changed, 200 is just for testing */
63  }
64
65  /* destructor */
[6688]66  ChatHistory::~ChatHistory()
[6644]67  {
[6688]68    chat_hist_closelog();
[7127]69
[6690]70    /* clear list */
71    this->hist_buffer.clear();
[6644]72  }
73
74  /* react to incoming chat */
[8829]75  void ChatHistory::incomingChat(const std::string& message, const std::string& /*name*/)
[6644]76  {
77    /* add the line to the history */
[8829]78    this->chat_hist_addline( message );
[6644]79
80    /* add the line to the log */
[8829]81    this->chat_hist_logline( message );
[7127]82  }
[6644]83
[6649]84  /* Synchronize logfile onto the hard drive */ /* MARK MARK */
[6688]85  int ChatHistory::syncLog()
86  {
87    //if( this->hist_logfile )
88      //this->hist_logfile.sync();
[6698]89    return 0;
[6688]90  }
[6644]91
92  /* add a line to this history */
[6688]93  int ChatHistory::chat_hist_addline( const std::string& toadd )
[6644]94  {
[6688]95    /* crop history at the end if it's too large */
96    while( this->hist_buffer.size() > this->hist_maxlines+1 )
97      this->hist_buffer.pop_front();
98
[6644]99    /* push to the front of the history */
[6688]100    this->hist_buffer.push_back( toadd );
[6698]101    return 0;
[6644]102  }
103
104  /* log a line to a logfile */
105  int ChatHistory::chat_hist_logline( const std::string& toadd )
[7127]106  {
[6644]107    /* output the line to the file if logging is enabled */
108    if( this->hist_log_enabled )
[8809]109      this->hist_logfile << toadd << endl;
[6698]110    return 0;
[6644]111  }
112
113  /* open logfile */
114  int ChatHistory::chat_hist_openlog()
115  {
[7127]116    /* TODO: find out the name of the file to log to via settings
[6644]117     *       and set the this->hist_logfile_path variable to it
118     */
[6698]119#ifndef CHATTEST
[6699]120    this->hist_logfile.open( (PathConfig::getInstance().getLogPathString() +
121      "chatlog.log").c_str(),
[6688]122      std::fstream::out | std::fstream::app );
[6693]123#else
124    this->hist_logfile.open( "/tmp/chatlog.log",
125      std::fstream::out | std::fstream::app );
126#endif
[6644]127
128    /* TODO check whether this works (not sure how you'd like it?) */
[6693]129    if( !this->hist_logfile )
130    { this->hist_log_enabled = false;
[6698]131#ifndef CHATTEST
[8809]132      orxout(internal_warning) << "Could not open logfile." << endl;
[6693]133#endif
134    }
[6644]135
136    /* if it worked */
137    return 0;
138  }
139
[6688]140  /* close logfile */
141  void ChatHistory::chat_hist_closelog()
142  {
[6910]143    /* see if we've actually got a logfile */
[6688]144    if( this->hist_logfile )
[7127]145    {
[6910]146      /* yes, we've got one, add a line that shows we're closing it */
147      this->chat_hist_logline( "--- Logfile closed ---" );
148
149      /* actually close down the file */
[6688]150      this->hist_logfile.close();
151    }
152  }
153
154  /* output history for debugging */
155  void ChatHistory::debug_printhist()
156  {
[6690]157    /* create deque iterator */
[6688]158    std::deque<std::string>::iterator it;
159
[6690]160    /* output all the strings */
[6688]161    for( it = this->hist_buffer.begin(); it != this->hist_buffer.end();
162      ++it )
[8809]163      orxout(debug_output) << *it << endl;
[6688]164
[6690]165    /* output size */
[8809]166    orxout(debug_output) << "Size: " << hist_buffer.size() << endl;
[6688]167  }
168
[6698]169#ifndef CHATTEST
[6644]170}
[6688]171#endif
Note: See TracBrowser for help on using the repository browser.