Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/explosionChunksHS15/src/orxonox/chat/ChatHistory.cc @ 10930

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