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