Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10904 was 10624, checked in by landauf, 9 years ago

merged branch core7 back to trunk

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