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