Merge lp:~dlh/armagetronad/0.2.8-armagetronad-ladderlog_script into lp:armagetronad/0.2.8

Proposed by dlh
Status: Work in progress
Proposed branch: lp:~dlh/armagetronad/0.2.8-armagetronad-ladderlog_script
Merge into: lp:armagetronad/0.2.8
Diff against target: 475 lines (+261/-124)
6 files modified
NEWS (+4/-0)
config/settings_dedicated.cfg (+8/-0)
language/english_base.txt (+6/-4)
scripts/examples/echo.sh (+7/-0)
src/engine/ePlayer.cpp (+210/-104)
src/engine/ePlayer.h (+26/-16)
To merge this branch: bzr merge lp:~dlh/armagetronad/0.2.8-armagetronad-ladderlog_script
Reviewer Review Type Date Requested Status
dlh Needs Resubmitting
Review via email: mp+125082@code.launchpad.net

Description of the change

• New commands LADDERLOG_SCRIPT_WRITE, and LADDERLOG_SCRIPT_WRITE_ALL that allow better control over which events are sent to spawned scripts.
• The CONSOLE_LADDER_LOG setting has been removed. Its functionality has been reimplemented in scripts/examples/echo.sh.

Useful for enabling events only for spawned scripts, so that they don't spam your ladderlog.txt. For example: I don't really need the CHAT, COMMAND, or GAME_TIME events to be written to ladderlog.txt, but I have a script that uses them. I also have a ladderlog sync program where it would be helpful to enable all the events I'm interested in sending.

Translatable strings
====================

• Removed: console_ladder_log_help, ladderlog_write_all_usage
• Added: ladderlog_script_write_all_help, ladderlog_script_write_help, ladderlog_script_write_enabled, ladderlog_script_write_disabled
• Modified: ladderlog_write_all_help, ladderlog_write_all_enabled

To post a comment you must log in.
Revision history for this message
dlh (dlh) :
review: Needs Resubmitting
1410. By dlh

Merge from 0.2.8

Unmerged revisions

1410. By dlh

Merge from 0.2.8

1409. By dlh

* New commands LADDERLOG_SCRIPT_WRITE, and LADDERLOG_SCRIPT_WRITE_ALL that allow better control over which events are sent to spawned scripts.
* The CONSOLE_LADDER_LOG setting has been removed. Its functionality has been reimplemented in scripts/examples/echo.sh.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'NEWS'
2--- NEWS 2013-07-24 02:45:51 +0000
3+++ NEWS 2013-07-24 20:16:23 +0000
4@@ -32,6 +32,10 @@
5 - Manage external scripts on Unix dedicated servers. New commands:
6 SPAWN_SCRIPT, RESPAWN_SCRIPT, FORCE_RESPAWN_SCRIPT, KILL_SCRIPT,
7 LIST_SCRIPTS, SCRIPT_ENV.
8+- New commands LADDERLOG_SCRIPT_WRITE, and LADDERLOG_SCRIPT_WRITE_ALL that
9+ allow better control over which events are sent to spawned scripts.
10+- The CONSOLE_LADDER_LOG setting has been removed. Its functionality has been
11+ reimplemented in scripts/examples/echo.sh.
12 - Authentication is now enabled by default.
13
14 Changes since 0.2.8.3.1:
15
16=== modified file 'config/settings_dedicated.cfg'
17--- config/settings_dedicated.cfg 2013-04-12 17:34:00 +0000
18+++ config/settings_dedicated.cfg 2013-07-24 20:16:23 +0000
19@@ -419,7 +419,15 @@
20 WAIT_FOR_EXTERNAL_SCRIPT 0 # set to 1 if you want the server to sleep between rounds until it is set back to 0 or
21 WAIT_FOR_EXTERNAL_SCRIPT_TIMEOUT 3 # seconds
22
23+# Configuring which ladderlog events are enabled or disabled
24
25+# LADDERLOG_WRITE_* Enable or disable an individual ladderlog writer for both file and script output. Usage: LADDERLOG_WRITE_* 0|1
26+# LADDERLOG_WRITE_ALL Enable or disable all ladderlog writers for both file and script output. Usage: LADDERLOG_WRITE_ALL 0|1
27+#
28+# The following LADDERLOG_SCRIPT_* commands are only available on Unix.
29+#
30+# LADDERLOG_SCRIPT_WRITE Enable or disable ladderlog writers for script output. Usage: LADDERLOG_SCRIPT_WRITE 0|1 <event1> [<event2> ...]
31+# LADDERLOG_SCRIPT_WRITE_ALL Enable or disable all ladderlog writers for script output. Usage: LADDERLOG_SCRIPT_WRITE_ALL 0|1
32
33 ############################################################################################
34 #
35
36=== modified file 'language/english_base.txt'
37--- language/english_base.txt 2013-04-12 17:34:00 +0000
38+++ language/english_base.txt 2013-07-24 20:16:23 +0000
39@@ -788,11 +788,13 @@
40 console_decorate_id_help Decorates every line of console output with the client ID
41 console_decorate_ip_help Decorates every line of console output with the client IP
42 console_decorate_timestamp_help Decorates every line of console output with the current date and time
43-console_ladder_log_help Sends ladder log output to the console
44 ladderlog_decorate_timestamp_help Decorates every line of ladderlog output with the current date and time
45-ladderlog_write_all_help Set all the LADDER_LOG_WRITE_* settings to the same value
46-ladderlog_write_all_usage Usage: LADDER_LOG_WRITE_ALL 1|0
47-ladderlog_write_all_enabled Enabled full ladderlog output.
48+ladderlog_write_all_help Enable or disable all ladderlog writers for both file and script output. Usage: LADDERLOG_WRITE_ALL 0|1
49+ladderlog_script_write_all_help Enable or disable all ladderlog writers for script output. Usage: LADDERLOG_SCRIPT_WRITE_ALL 0|1
50+ladderlog_script_write_help Enable or disable ladderlog writers for script output. Usage: LADDERLOG_SCRIPT_WRITE 0|1 <event1> [<event2> ...]
51+ladderlog_script_write_enabled Enabled \1 ladderlog writers for script output.
52+ladderlog_script_write_disabled Disabled \1 ladderlog writers for script output.
53+ladderlog_write_all_enabled Enabled ladderlog output.
54 ladderlog_write_all_disabled Disabled ladderlog output.
55 ladderlog_write_authority_blurb_help Write to ladderlog: AUTHORITY_BLURB <blurb> <player> <text>
56 ladderlog_write_basezone_conquered_help Write to ladderlog: BASEZONE_CONQUERED <team> <cx> <cy>
57
58=== added file 'scripts/examples/echo.sh'
59--- scripts/examples/echo.sh 1970-01-01 00:00:00 +0000
60+++ scripts/examples/echo.sh 2013-07-24 20:16:23 +0000
61@@ -0,0 +1,7 @@
62+#!/bin/bash
63+# A script that simply echos back ladderlog lines to the server console.
64+
65+while read line
66+do
67+ echo "# $line"
68+done
69
70=== modified file 'src/engine/ePlayer.cpp'
71--- src/engine/ePlayer.cpp 2013-07-24 02:45:51 +0000
72+++ src/engine/ePlayer.cpp 2013-07-24 20:16:23 +0000
73@@ -5999,37 +5999,24 @@
74 return object;
75 }
76
77-static bool se_consoleLadderLog = false;
78-static tSettingItem< bool > se_consoleLadderLogConf( "CONSOLE_LADDER_LOG", se_consoleLadderLog );
79-
80-static bool se_ladderlogDecorateTS = false;
81-static tSettingItem< bool > se_ladderlogDecorateTSConf( "LADDERLOG_DECORATE_TIMESTAMP", se_ladderlogDecorateTS );
82-extern bool sn_decorateTS; // from nNetwork.cpp
83-
84-void se_SaveToLadderLog( tOutput const & out )
85+typedef std::set< tString > StringSet;
86+
87+// Parses a list of tokens and adds them to "set".
88+// Returns the number of entries added.
89+int se_ParseList( StringSet & set, std::istream & s )
90 {
91- if (se_consoleLadderLog)
92- {
93- std::cout << "[L";
94- if(sn_decorateTS) {
95- std::cout << st_GetCurrentTime(" TS=%Y/%m/%d-%H:%M:%S");
96- }
97- std::cout << "] " << out << std::endl;
98- }
99- if (sn_GetNetState()!=nCLIENT && !tRecorder::IsPlayingBack())
100- {
101- std::ofstream o;
102- if ( tDirectories::Var().Open(o, "ladderlog.txt", std::ios::app) )
103+ int entries_count = 0;
104+ while ( s.good() )
105+ {
106+ tString name;
107+ s >> name;
108+ if ( name.Len() > 1 )
109 {
110- std::stringstream s;
111- if(se_ladderlogDecorateTS) {
112- s << st_GetCurrentTime("%Y/%m/%d-%H:%M:%S ");
113- }
114- s << out << std::endl;
115- sr_InputForScripts( s.str().c_str() );
116- o << s.str();
117+ std::pair< StringSet::iterator, bool > ret = set.insert( name );
118+ if ( ret.second ) entries_count++;
119 }
120 }
121+ return entries_count;
122 }
123
124 static bool se_chatLog = false;
125@@ -6052,65 +6039,196 @@
126 }
127 }
128
129-std::list<eLadderLogWriter *> &eLadderLogWriter::writers() {
130- static std::list<eLadderLogWriter *> list;
131- return list;
132-}
133-
134-eLadderLogWriter::eLadderLogWriter(char const *ID, bool enabledByDefault) :
135- id(ID),
136- enabled(enabledByDefault),
137- conf(new tSettingItem<bool>(&(tString("LADDERLOG_WRITE_") + id)(0),
138- enabled)),
139- cache(id) {
140- writers().push_back(this);
141-}
142-
143-eLadderLogWriter::~eLadderLogWriter() {
144- if(conf) {
145- delete conf;
146- }
147- // generic algorithms aren't exactly easier to understand than regular
148- // code, but anyways, let's try one...
149- std::list<eLadderLogWriter *> list = writers();
150- list.erase(std::find_if(list.begin(), list.end(), std::bind2nd(std::equal_to<eLadderLogWriter *>(), this)));
151-}
152-
153-void eLadderLogWriter::write() {
154- if(enabled) {
155- se_SaveToLadderLog(cache);
156- }
157- cache = id;
158-}
159-
160-void eLadderLogWriter::setAll(bool enabled) {
161- std::list<eLadderLogWriter *> list = writers();
162- std::list<eLadderLogWriter *>::iterator end = list.end();
163- for(std::list<eLadderLogWriter *>::iterator iter = list.begin(); iter != end; ++iter) {
164- (*iter)->enabled = enabled;
165- }
166-}
167-
168-static void LadderLogWriteAll(std::istream &s) {
169- bool enabled;
170- s >> enabled;
171- if(s.fail()) {
172- if(tConfItemBase::printErrors) {
173- con << tOutput("$ladderlog_write_all_usage") << '\n';
174- }
175- return;
176- }
177- if(tConfItemBase::printChange) {
178- if(enabled) {
179- con << tOutput("$ladderlog_write_all_enabled") << '\n';
180- } else {
181- con << tOutput("$ladderlog_write_all_disabled") << '\n';
182- }
183- }
184- eLadderLogWriter::setAll(enabled);
185-}
186-
187-static tConfItemFunc LadderLogWriteAllConf("LADDERLOG_WRITE_ALL", &LadderLogWriteAll);
188+//! Handles configuring ladderlog settings
189+class eLadderLogManager
190+{
191+ enum SetMode
192+ {
193+ SetMode_File = 1 << 0,
194+ SetMode_Script = 1 << 1
195+ };
196+ typedef std::map< tString, eLadderLogWriter * > WriterMap;
197+ WriterMap writers_;
198+public:
199+ eLadderLogManager()
200+ :writers_()
201+ {
202+ }
203+
204+ void Add( eLadderLogWriter *writer )
205+ {
206+ writers_[writer->Name()] = writer;
207+ }
208+
209+ void Remove( eLadderLogWriter *writer )
210+ {
211+ writers_.erase( writer->Name() );
212+ }
213+
214+ void WriteAll( std::istream & s )
215+ {
216+ SetAll( "$ladderlog_write_all_help", s, SetMode_File | SetMode_Script );
217+ }
218+
219+ void ScriptWriteAll( std::istream & s )
220+ {
221+ SetAll( "$ladderlog_script_write_all_help", s, SetMode_Script );
222+ }
223+
224+ void ScriptWrite( std::istream & s )
225+ {
226+ bool enable;
227+ s >> enable;
228+ if ( s.fail() )
229+ {
230+ if ( tConfItemBase::printErrors )
231+ con << tOutput("$ladderlog_script_write_help") << '\n';
232+ return;
233+ }
234+
235+ StringSet names;
236+ se_ParseList( names, s );
237+ int numberValid = 0;
238+
239+ for ( StringSet::const_iterator it = names.begin(); it != names.end(); ++it )
240+ {
241+ WriterMap::iterator conf = writers_.find( *it );
242+ if ( conf != writers_.end() )
243+ {
244+ numberValid++;
245+ conf->second->SetForScript( enable );
246+ }
247+ }
248+
249+ if ( tConfItemBase::printChange )
250+ {
251+ if ( enable )
252+ con << tOutput("$ladderlog_script_write_enabled", numberValid) << '\n';
253+ else
254+ con << tOutput("$ladderlog_script_write_disabled", numberValid) << '\n';
255+ }
256+ }
257+
258+private:
259+ void SetAll( const char *help, std::istream & s, int mode )
260+ {
261+ bool enable;
262+ s >> enable;
263+
264+ if ( s.fail() )
265+ {
266+ if ( tConfItemBase::printErrors )
267+ con << tOutput(help) << '\n';
268+ return;
269+ }
270+
271+ for ( WriterMap::iterator it = writers_.begin(); it != writers_.end(); ++it )
272+ {
273+ if ( mode & SetMode_File )
274+ it->second->SetForFile( enable );
275+ if ( mode & SetMode_Script )
276+ it->second->SetForScript( enable );
277+ }
278+
279+ if ( tConfItemBase::printChange )
280+ {
281+ if ( enable )
282+ con << tOutput("$ladderlog_write_all_enabled") << '\n';
283+ else
284+ con << tOutput("$ladderlog_write_all_disabled") << '\n';
285+ }
286+ }
287+};
288+
289+static eLadderLogManager & se_GlobalLadderLogManager()
290+{
291+ static eLadderLogManager manager;
292+ return manager;
293+}
294+
295+//! A tSettingItem that operates on two boolean values.
296+class eLadderLogSetting : public tSettingItem< bool >
297+{
298+public:
299+ eLadderLogSetting( tString name, bool & fileTarget, bool & scriptTarget )
300+ :tConfItemBase( name ), tSettingItem< bool >( name, fileTarget ),
301+ scriptTarget_( &scriptTarget )
302+ {
303+ }
304+
305+ virtual void WasChanged()
306+ {
307+ *scriptTarget_ = *target;
308+ }
309+private:
310+ bool *scriptTarget_;
311+};
312+
313+eLadderLogWriter::eLadderLogWriter( char const *name, bool enabledByDefault )
314+ :name_( name ),
315+ isEnabledForFile_( enabledByDefault ),
316+ isEnabledForScript_( enabledByDefault ),
317+ conf_( new eLadderLogSetting( tString( "LADDERLOG_WRITE_" ) + name, isEnabledForFile_, isEnabledForScript_ ) ),
318+ cache_( name )
319+{
320+ se_GlobalLadderLogManager().Add( this );
321+}
322+
323+eLadderLogWriter::~eLadderLogWriter()
324+{
325+ se_GlobalLadderLogManager().Remove( this );
326+ if ( conf_ )
327+ delete conf_;
328+}
329+
330+static bool se_ladderlogDecorateTS = false;
331+static tSettingItem< bool > se_ladderlogDecorateTSConf( "LADDERLOG_DECORATE_TIMESTAMP", se_ladderlogDecorateTS );
332+
333+void eLadderLogWriter::write()
334+{
335+ if( isEnabled() && sn_GetNetState() != nCLIENT && !tRecorder::IsPlayingBack() )
336+ {
337+ std::string line;
338+ if ( se_ladderlogDecorateTS )
339+ line += st_GetCurrentTime("%Y/%m/%d-%H:%M:%S ");
340+ line += cache_;
341+ line += '\n';
342+
343+ if ( isEnabledForFile_ )
344+ {
345+ std::ofstream o;
346+ if ( tDirectories::Var().Open( o, "ladderlog.txt", std::ios::app ) )
347+ o << line;
348+ }
349+
350+ if ( isEnabledForScript_ )
351+ {
352+ sr_InputForScripts( line.c_str() );
353+ }
354+ }
355+ cache_ = name_;
356+}
357+
358+static void se_LadderLogWriteAll( std::istream & s )
359+{
360+ se_GlobalLadderLogManager().WriteAll( s );
361+}
362+static tConfItemFunc se_ladderLogWriteAllConf( "LADDERLOG_WRITE_ALL", se_LadderLogWriteAll );
363+
364+#if HAVE_UNISTD_H
365+static void se_LadderLogScriptWriteAll( std::istream & s )
366+{
367+ se_GlobalLadderLogManager().ScriptWriteAll( s );
368+}
369+static tConfItemFunc se_ladderLogScriptWriteAllConf( "LADDERLOG_SCRIPT_WRITE_ALL", se_LadderLogScriptWriteAll );
370+
371+static void se_LadderLogScriptWrite( std::istream & s )
372+{
373+ se_GlobalLadderLogManager().ScriptWrite( s );
374+}
375+static tConfItemFunc se_ladderLogScriptWriteConf( "LADDERLOG_SCRIPT_WRITE", se_LadderLogScriptWrite );
376+#endif /* HAVE_UNISTD_H */
377+
378
379 void se_SaveToScoreFile(const tOutput &o){
380 tString s(o);
381@@ -8715,30 +8833,18 @@
382 {
383 Parse( ip_addresses_whitelist_, s );
384 }
385-protected:
386- typedef std::set< tString > StringSet;
387+protected:
388+ bool HasEntry( const StringSet & whitelist, const tString & value ) const
389+ {
390+ return whitelist.find( value ) != whitelist.end();
391+ }
392
393 void Parse( StringSet & whitelist, std::istream & s )
394 {
395- int entries_count = 0;
396- while ( s.good() )
397- {
398- tString name;
399- s >> name;
400- if ( name.Len() > 1 )
401- {
402- std::pair< StringSet::iterator, bool > ret = whitelist.insert( name );
403- if ( ret.second ) entries_count++;
404- }
405- }
406+ int entries_count = se_ParseList( whitelist, s );
407 con << tOutput( "$whitelist_enemies_success", entries_count ) << '\n';
408 }
409
410- bool HasEntry( const StringSet & whitelist, const tString & value ) const
411- {
412- return whitelist.find( value ) != whitelist.end();
413- }
414-
415 StringSet usernames_whitelist_;
416 StringSet ip_addresses_whitelist_;
417 };
418
419=== modified file 'src/engine/ePlayer.h'
420--- src/engine/ePlayer.h 2013-07-24 02:45:51 +0000
421+++ src/engine/ePlayer.h 2013-07-24 20:16:23 +0000
422@@ -518,27 +518,37 @@
423 void se_SaveToChatLog( tOutput const & out ); //!< writes something to chatlog.txt (if enabled) and/or ladderlog
424
425 //! create a global instance of this to write stuff to ladderlog.txt
426-class eLadderLogWriter {
427- static std::list<eLadderLogWriter *> &writers();
428- tString id;
429- bool enabled;
430- tSettingItem<bool> *conf;
431- tColoredString cache;
432+class eLadderLogWriter
433+{
434+ tString name_;
435+ bool isEnabledForFile_;
436+ bool isEnabledForScript_;
437+ tSettingItem<bool> *conf_;
438+ tColoredString cache_;
439 public:
440- eLadderLogWriter(char const *ID, bool enabledByDefault);
441+ eLadderLogWriter( char const *name, bool enabledByDefault );
442 ~eLadderLogWriter();
443+ tString Name() const { return name_; }
444+
445+ void SetForFile( bool enable ) { isEnabledForFile_ = enable; }
446+ void SetForScript( bool enable ) { isEnabledForScript_ = enable; }
447+
448 //! append a field to the current message. Spaces are added automatically.
449- template<typename T> eLadderLogWriter &operator<<(T const &s) {
450- if(enabled) {
451- cache << ' ' << s;
452- }
453+ template<typename T> eLadderLogWriter &operator<<( T const &s )
454+ {
455+ if( isEnabled() )
456+ cache_ << ' ' << s;
457 return *this;
458 }
459- void write(); //!< send to ladderlog and clear message
460-
461- bool isEnabled() { return enabled; } //!< check this if you're going to make expensive calculations for ladderlog output
462-
463- static void setAll(bool enabled); //!< enable or disable all writers
464+
465+ //!< send to ladderlog and clear message
466+ void write();
467+
468+ //!< check this if you're going to make expensive calculations for ladderlog output
469+ bool isEnabled() const
470+ {
471+ return isEnabledForFile_ || isEnabledForScript_;
472+ }
473 };
474
475 tColoredString & operator << (tColoredString &s,const ePlayer &p);

Subscribers

People subscribed via source and target branches

to all changes:
to status/vote changes: