Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

moved Scope and ScopedSingletonManager from util to core.
TODO I had to disable two tests in ScopeTest.cc because now that it runs in the core library, there are too many singletons which get loaded in graphics scope (with too many dependencies). this should be fixed

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