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