Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/orxonox/chat/ChatHistory.cc @ 10458

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

renamed ScopedSingletonManager to ScopedSingletonWrapper. removed static maps.

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