Mercurial > projects > mde
comparison mde/Init.d @ 18:56a42ec95024
Changes to Init and logging.
Moved an Init function; hence events now depends on Init rather than vice-versa.
Paths and logging set up by Init's static this(). Logging location set at compile time.
committer: Diggory Hardy <diggory.hardy@gmail.com>
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Tue, 18 Mar 2008 17:51:52 +0000 |
parents | 5f90774ea1ef |
children | db0b48f02b69 |
comparison
equal
deleted
inserted
replaced
17:5f90774ea1ef | 18:56a42ec95024 |
---|---|
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ | 14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ |
15 | 15 |
16 /************************************************************************************************** | 16 /************************************************************************************************** |
17 * Initialisation setup and exit cleanup module. | 17 * Initialisation setup and exit cleanup module. |
18 * | 18 * |
19 * This module controls most of the initialisation and deinitialisation of the program. | 19 * This module provides an infrastructure for handling much of the initialisation and |
20 * deinitialisation of the program. It does not, however, provide much of the (de)initialisation | |
21 * code; with the exception of that for the logger. | |
20 *************************************************************************************************/ | 22 *************************************************************************************************/ |
21 module mde.Init; | 23 module mde.Init; |
22 | 24 |
23 import mde.exception; | 25 import mde.exception; |
24 | |
25 import mde.options; | 26 import mde.options; |
26 import mde.events; | 27 import paths = mde.resource.paths; |
27 import global = mde.global; | |
28 import mde.input.input; | |
29 | 28 |
30 // tango imports | 29 // tango imports |
31 import tango.core.Thread; | 30 import tango.core.Thread; |
32 import tango.core.Exception; | 31 import tango.core.Exception; |
33 import tango.util.log.Log : Log, Logger; | 32 import tango.util.log.Log : Log, Logger; |
34 import tango.util.log.ConsoleAppender : ConsoleAppender; | 33 import tango.util.log.ConsoleAppender : ConsoleAppender; |
34 import tango.util.log.SwitchingFileAppender : SwitchingFileAppender; | |
35 import tango.stdc.stringz : fromStringz; | 35 import tango.stdc.stringz : fromStringz; |
36 | 36 |
37 /** | 37 /** |
38 * Static CTOR | 38 * Static CTOR |
39 * | 39 * |
40 * This should handle a minimal amount of functionality where useful. For instance, configuring the | 40 * This should handle a minimal amount of functionality where useful. For instance, configuring the |
41 * logger here and not in Init allows unittests to use the logger. | 41 * logger here and not in Init allows unittests to use the logger. |
42 */ | 42 */ |
43 static this() | 43 static this() |
44 { | 44 { |
45 version (mdeTest) {} // test.mdeTest sets up its own root logger | 45 // Find/create paths: |
46 try { | |
47 paths.resolvePaths(); | |
48 } catch (Exception e) { | |
49 throw new InitException ("Resolving paths failed: " ~ e.msg); | |
50 } | |
51 | |
52 // Set up the logger: | |
53 version (mdeTest) {} // test.mdeTest sets up its own root logger | |
46 else { | 54 else { |
47 // For now, just log to the console: | 55 // Where logging is done to is determined at compile-time, currently just via static ifs. |
48 Logger root = Log.getRootLogger(); | 56 Logger root = Log.getRootLogger(); |
49 root.addAppender(new ConsoleAppender); | 57 |
58 static if (true ) { // Log to the console | |
59 root.addAppender(new ConsoleAppender); | |
60 } | |
61 static if (true ) { // Log to files | |
62 // Use 2 log files with a maximum size of 1 MB: | |
63 root.addAppender (new SwitchingFileAppender (paths.logDir~"/log-.txt", 5)); | |
64 } | |
50 | 65 |
51 // Set the level here, but set it again once options have been loaded: | 66 // Set the level here, but set it again once options have been loaded: |
52 debug root.setLevel(root.Level.Trace); | 67 debug root.setLevel(root.Level.Trace); |
53 else root.setLevel(root.Level.Info); | 68 else root.setLevel(root.Level.Info); |
54 } | 69 } |
55 | |
56 Init.addFunc (&miscInit); | |
57 } | 70 } |
58 static ~this() | 71 static ~this() |
59 { | 72 { |
60 } | 73 } |
61 | 74 |
87 * must not run where the initialisation functions have failed. | 100 * must not run where the initialisation functions have failed. |
88 * Hence a list of clean-up functions is built similarly to scope(failure) --- see addCleanupFct. | 101 * Hence a list of clean-up functions is built similarly to scope(failure) --- see addCleanupFct. |
89 */ | 102 */ |
90 this() | 103 this() |
91 { | 104 { |
92 | |
93 logger.info ("Init: starting"); | 105 logger.info ("Init: starting"); |
106 | |
94 //BEGIN Pre-init (loading options) | 107 //BEGIN Pre-init (loading options) |
95 // Load options FIRST. Should be fast, most work is probably disk access, | 108 /* Load options now. Don't load in a thread since: |
96 // and it's a really good idea to let the options apply to all other loading. | 109 * Loading should be fast |
110 * Most work is probably disk access | |
111 * It's a really good idea to let the options apply to all other loading. */ | |
97 try { | 112 try { |
98 Options.load(); | 113 Options.load(); |
99 } catch (optionsLoadException e) { | 114 } catch (optionsLoadException e) { |
100 throw new InitException ("Loading options failed; message: " ~ e.msg); | 115 throw new InitException ("Loading options failed; message: " ~ e.msg); |
101 } | 116 } |
102 addCleanupFct (&Options.save); // not strictly cleanup, but needs to be called somewhere | 117 addCleanupFct (&Options.save); // not strictly cleanup, but needs to be called somewhere |
103 | 118 |
119 // Now re-set the logging level: | |
104 Log.getRootLogger.setLevel (cast(Log.Level) Options.misc.logLevel, true); // set the stored log level | 120 Log.getRootLogger.setLevel (cast(Log.Level) Options.misc.logLevel, true); // set the stored log level |
105 //END Pre-init | 121 //END Pre-init |
106 | 122 |
107 | 123 |
108 //BEGIN Init (actual function calling) | 124 //BEGIN Init (actual function calling) |
238 assert (!initialised); | 254 assert (!initialised); |
239 | 255 |
240 logger.info ("Unittest complete."); | 256 logger.info ("Unittest complete."); |
241 } | 257 } |
242 } | 258 } |
243 | |
244 void miscInit () { | |
245 /* Some miscellaneous short calls. | |
246 * | |
247 * Was executed in the main loop, but for the sake of simplicity use another thread. | |
248 */ | |
249 try { | |
250 global.input = new Input(); | |
251 global.input.loadConfig (); // (may also create instance) | |
252 | |
253 addEventsSchedule (); | |
254 } catch (Exception e) { | |
255 init.setFailure (); // must clean up properly | |
256 } | |
257 } |