Mercurial > projects > mde
annotate mde/scheduler/Init.d @ 28:b5fadd8d930b
Small addition to GUI, paths work-around for Windows.
New GUI widget containing a widget.
Paths on windows now uses "." and "./user" as a temporary measure.
committer: Diggory Hardy <diggory.hardy@gmail.com>
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Tue, 08 Apr 2008 15:52:21 +0100 |
parents | 611f7b9063c6 |
children | f985c28c0ec9 |
rev | line source |
---|---|
20 | 1 /* LICENSE BLOCK |
2 Part of mde: a Modular D game-oriented Engine | |
3 Copyright © 2007-2008 Diggory Hardy | |
4 | |
26
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify it under the terms |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
6 of the GNU General Public License as published by the Free Software Foundation, either |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
7 version 2 of the License, or (at your option) any later version. |
20 | 8 |
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | |
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
11 See the GNU General Public License for more details. | |
12 | |
26
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
13 You should have received a copy of the GNU General Public License |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
20 | 15 |
16 /************************************************************************************************** | |
17 * Initialisation setup and exit cleanup module. | |
18 * | |
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. | |
22 *************************************************************************************************/ | |
23 module mde.scheduler.Init; | |
24 | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
25 import mde.scheduler.InitFunctions; |
20 | 26 import mde.scheduler.exception; |
27 | |
28 import mde.options; | |
29 import paths = mde.resource.paths; | |
30 import mde.exception; | |
31 | |
32 // tango imports | |
33 import tango.core.Thread; | |
34 import tango.core.Exception; | |
35 import tango.stdc.stringz : fromStringz; | |
36 import tango.util.log.Log : Log, Logger; | |
37 import tango.util.log.ConsoleAppender : ConsoleAppender; | |
38 | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
39 //version = SwitchAppender; |
20 | 40 version (SwitchAppender) { // My own variation, currently just a test |
41 import tango.util.log.SwitchingFileAppender : SwitchingFileAppender; | |
42 } else { | |
43 import tango.util.log.RollingFileAppender : RollingFileAppender; | |
44 } | |
45 | |
46 /** | |
47 * Static CTOR | |
48 * | |
49 * This should handle a minimal amount of functionality where useful. For instance, configuring the | |
50 * logger here and not in Init allows unittests to use the logger. | |
51 */ | |
52 static this() | |
53 { | |
54 // Find/create paths: | |
55 try { | |
56 paths.resolvePaths(); | |
57 } catch (Exception e) { | |
28
b5fadd8d930b
Small addition to GUI, paths work-around for Windows.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
26
diff
changeset
|
58 // NOTE: an exception thrown here cannot be caught by main()! |
20 | 59 throw new InitException ("Resolving paths failed: " ~ e.msg); |
60 } | |
61 | |
62 // Set up the logger: | |
63 { | |
64 // Where logging is done to is determined at compile-time, currently just via static ifs. | |
65 Logger root = Log.getRootLogger(); | |
26
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
66 |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
67 static if (true ) { // Log to files (first appender so root seperator messages don't show on console) |
20 | 68 version (SwitchAppender) { |
69 root.addAppender (new SwitchingFileAppender (paths.logDir~"/log-.txt", 5)); | |
70 } else { | |
71 // Use 2 log files with a maximum size of 1 MB: | |
72 root.addAppender (new RollingFileAppender (paths.logDir~"/log-.txt", 2, 1024*1024)); | |
26
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
73 root.info (""); // some kind of separation between runs |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
74 root.info (""); |
20 | 75 } |
76 } | |
26
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
77 static if (true ) { // Log to the console |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
78 root.addAppender(new ConsoleAppender); |
611f7b9063c6
Changed the licensing and removed a few dead files.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
25
diff
changeset
|
79 } |
20 | 80 |
81 // Set the level here, but set it again once options have been loaded: | |
82 debug root.setLevel(root.Level.Trace); | |
83 else root.setLevel(root.Level.Info); | |
84 } | |
85 } | |
86 static ~this() | |
87 { | |
88 } | |
89 | |
90 /** | |
91 * Init class | |
92 * | |
93 * A scope class created at beginning of the program and destroyed at the end; thus the CTOR | |
94 * handles program initialisation and the DTOR handles program cleanup. | |
95 */ | |
96 scope class Init | |
97 { | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
98 //private bool failure = false; // set true on failure during init, so that clean |
20 | 99 private static Logger logger; |
100 static this() { | |
101 logger = Log.getLogger ("mde.scheduler.Init.Init"); | |
102 } | |
103 | |
104 /** CTOR − initialisation | |
105 * | |
106 * Runs general initialisation code, in a threaded manner where this isn't difficult. | |
107 * | |
108 * If any init fails, it must run necessary cleanup first since the DTOR cannot(?) be run. */ | |
109 /* In a single-threaded function this could be done with: | |
110 * scope(failure) cleanup; | |
111 * This won't work with a threaded init function since any threads completing succesfully will | |
112 * not clean-up, and a fixed list of clean-up functions cannot be used since clean-up functions | |
113 * must not run where the initialisation functions have failed. | |
114 * Hence a list of clean-up functions is built similarly to scope(failure) --- see addCleanupFct. | |
115 */ | |
116 this() | |
117 { | |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
118 debug logger.trace ("Init: starting"); |
20 | 119 |
120 //BEGIN Pre-init (stage init0) | |
121 /* Load options now. Don't load in a thread since: | |
122 * Loading should be fast | |
123 * Most work is probably disk access | |
124 * It's a really good idea to let the options apply to all other loading. */ | |
125 try { | |
126 Options.load(); | |
127 } catch (optionsLoadException e) { | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
128 throw new InitException ("Loading options failed: " ~ e.msg); |
20 | 129 } |
130 | |
131 // Now re-set the logging level: | |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
132 Log.getRootLogger.setLevel (cast(Log.Level) miscOpts.logLevel, true); // set the stored log level |
20 | 133 //END Pre-init |
134 | |
135 | |
136 //BEGIN Init (stages init2, init4) | |
137 /* Call init functions. | |
138 * | |
139 * Current method is to try using threads, and on failure assume no threads were actually | |
140 * created and run functions in a non-threaded manner. */ | |
141 | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
142 try { |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
143 if (runStageThreaded (init)) runStageForward (init); |
20 | 144 } |
145 catch (InitStageException) { // This init stage failed. | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
146 // FIXME: check DTOR still runs |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
147 throw new InitException ("An init function failed (see above message(s))"); |
20 | 148 } |
149 //END Init | |
150 | |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
151 debug logger.trace ("Init: done"); |
20 | 152 } |
153 | |
154 /** DTOR - runs cleanup functions. */ | |
155 ~this() | |
156 { | |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
157 debug logger.trace ("Cleanup: starting"); |
20 | 158 |
159 Options.save(); // save options... do so here for now | |
160 | |
25
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
161 // General cleanup: |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
162 try { |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
163 if (runStageThreaded (cleanup)) runStageReverse (cleanup); |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
164 } |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
165 catch (InitStageException) { |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
166 // Nothing else to do but report: |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
167 logger.error ("One or more cleanup functions failed!"); |
2c28ee04a4ed
Some minor and some futile efforts.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
24
diff
changeset
|
168 } |
20 | 169 |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
170 debug logger.trace ("Cleanup: done"); |
20 | 171 } |
172 | |
173 | |
174 //BEGIN runStage... | |
175 private static { | |
176 /* The following three functions, runStage*, each run all functions in a stage in some order, | |
177 * catching any exceptions thrown by the functions (although this isn't guaranteed for threads), | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
178 * and throw an InitStageException on initFailure. */ |
20 | 179 |
180 const UFE = "Unhandled exception from Init function:"; | |
181 /* Runs all functions consecutively, first-to-last. | |
182 * If any function fails, halts immediately. */ | |
183 void runStageForward (InitStage s) { | |
184 foreach (func; s.funcs) { | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
185 if (initFailure) break; |
20 | 186 try { |
187 func(); | |
188 } catch (Exception e) { | |
189 logger.fatal (UFE); | |
190 logger.fatal (e.msg); | |
191 | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
192 setInitFailure(); |
20 | 193 } |
194 } | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
195 |
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
196 if (initFailure) throw new InitStageException; // Problem running; abort and cleanup from here. |
20 | 197 } |
198 /* Runs all functions consecutively, last-to-first. | |
199 * If any function fails, continue until all have been run. */ | |
200 void runStageReverse (InitStage s) { | |
201 foreach_reverse (func; s.funcs) { | |
202 try { | |
203 func(); | |
204 } catch (Exception e) { | |
205 logger.fatal (UFE); | |
206 logger.fatal (e.msg); | |
207 | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
208 setInitFailure(); |
20 | 209 } |
210 } | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
211 if (initFailure) throw new InitStageException; // Problem running; abort and cleanup from here. |
20 | 212 } |
213 /* Tries running functions in a threaded way. Returns false if successful, true if not but | |
214 * functions should be run without threads. */ | |
215 bool runStageThreaded (InitStage s) { | |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
216 if (!miscOpts.useThreads) return true; // Use unthreaded route instead |
20 | 217 |
218 ThreadGroup tg; | |
219 try { // creating/starting threads could fail | |
220 tg = new ThreadGroup; | |
221 foreach (func; s.funcs) tg.create(func); // Start all threads | |
222 } catch (ThreadException e) { // Problem with threading; try without threads | |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
223 logger.error ("Caught ThreadException while trying to create threads:"); |
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
224 logger.error (e.msg); |
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
225 logger.info ("Will disable threads and continue."); |
20 | 226 |
24
32eff0e01c05
Only locally-changed options are stored in user-config now. Log levels revised.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
21
diff
changeset
|
227 Options.setBool("misc", "useThreads", false); // Disable threads entirely |
20 | 228 return true; // Try again without threads |
229 } | |
230 | |
231 /* Wait for all threads to complete. | |
232 * | |
233 * If something went wrong, we still need to do this before cleaning up. | |
234 */ | |
235 foreach (t; tg) { | |
236 try { | |
237 t.join (true); | |
238 } catch (Exception e) { | |
239 // Relying on catching exceptions thrown by other threads is a bad idea. | |
240 // Hence all threads should catch their own exceptions and return safely. | |
241 | |
242 logger.fatal ("Unhandled exception from Init function:"); | |
243 logger.fatal (e.msg); | |
244 | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
245 setInitFailure (); // abort (but join other threads first) |
20 | 246 } |
247 } | |
21
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
248 |
a60cbb7359dd
Window settings now come from options, and may use OpenGL (enabled/disabled at compile time).
Diggory Hardy <diggory.hardy@gmail.com>
parents:
20
diff
changeset
|
249 if (initFailure) throw new InitStageException; // Problem running; abort and cleanup from here. |
20 | 250 return false; // Done successfully |
251 } | |
252 } | |
253 //END runStage... | |
254 | |
255 debug (mdeUnitTest) unittest { | |
256 /* Fake init and cleanup. Use unittest-specific init and cleanup InitStages to avoid | |
257 * messing other init/cleanup up. */ | |
258 static InitStage initUT, cleanupUT; | |
259 | |
260 static bool initialised = false; | |
261 static void cleanupFunc1 () { | |
262 initialised = false; | |
263 } | |
264 static void cleanupFunc2 () { | |
265 assert (initialised == true); | |
266 } | |
267 | |
268 static void initFunc () { | |
269 initialised = true; | |
270 cleanupUT.addFunc (&cleanupFunc1); | |
271 cleanupUT.addFunc (&cleanupFunc2); | |
272 } | |
273 | |
274 initUT.addFunc (&initFunc); | |
275 | |
276 runStageForward (initUT); | |
277 assert (initialised); | |
278 | |
279 runStageReverse (cleanupUT); | |
280 assert (!initialised); | |
281 | |
282 logger.info ("Unittest complete."); | |
283 } | |
284 } |