Mercurial > projects > ldc
comparison tango/example/logging/logging.d @ 132:1700239cab2e trunk
[svn r136] MAJOR UNSTABLE UPDATE!!!
Initial commit after moving to Tango instead of Phobos.
Lots of bugfixes...
This build is not suitable for most things.
author | lindquist |
---|---|
date | Fri, 11 Jan 2008 17:57:40 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
131:5825d48b27d1 | 132:1700239cab2e |
---|---|
1 /******************************************************************************* | |
2 | |
3 Shows how the basic functionality of Logger operates. | |
4 | |
5 *******************************************************************************/ | |
6 | |
7 private import tango.util.log.Log, | |
8 tango.util.log.Configurator; | |
9 | |
10 /******************************************************************************* | |
11 | |
12 Search for a set of prime numbers | |
13 | |
14 *******************************************************************************/ | |
15 | |
16 void compute (Logger log, uint max) | |
17 { | |
18 byte* feld; | |
19 int teste=1, | |
20 mom, | |
21 hits=0, | |
22 s=0, | |
23 e = 1; | |
24 int count; | |
25 char tmp[128] = void; | |
26 | |
27 void set (byte* f, uint x) | |
28 { | |
29 *(f+(x)/16) |= 1 << (((x)%16)/2); | |
30 } | |
31 | |
32 byte test (byte* f, uint x) | |
33 { | |
34 return cast(byte) (*(f+(x)/16) & (1 << (((x)%16)/2))); | |
35 } | |
36 | |
37 // information level | |
38 log.info (log.format (tmp, "Searching prime numbers up to {}", max)); | |
39 | |
40 feld = (new byte[max / 16 + 1]).ptr; | |
41 | |
42 // get milliseconds since application began | |
43 auto begin = log.runtime; | |
44 | |
45 while ((teste += 2) < max) | |
46 if (! test (feld, teste)) | |
47 { | |
48 if ((++hits & 0x0f) == 0) | |
49 // more information level | |
50 log.info (log.format (tmp, "found {}", hits)); | |
51 | |
52 for (mom=3*teste; mom < max; mom += teste<<1) | |
53 set (feld, mom); | |
54 } | |
55 | |
56 // get number of milliseconds we took to compute | |
57 auto period = log.runtime - begin; | |
58 | |
59 if (hits) | |
60 // more information | |
61 log.info (log.format (tmp, "{} prime numbers found in {} millsecs", hits, period)); | |
62 else | |
63 // a warning level | |
64 log.warn ("no prime numbers found"); | |
65 | |
66 // check to see if we're enabled for | |
67 // tracing before we expend a lot of effort | |
68 if (log.isEnabled (log.Level.Trace)) | |
69 { | |
70 e = max; | |
71 count = 0 - 2; | |
72 if (s % 2 is 0) | |
73 count++; | |
74 | |
75 while ((count+=2) < e) | |
76 // log trace information | |
77 if (! test (feld, count)) | |
78 log.trace (log.format (tmp, "prime found: {}", count)); | |
79 } | |
80 } | |
81 | |
82 | |
83 /******************************************************************************* | |
84 | |
85 Compute a bunch of prime numbers | |
86 | |
87 *******************************************************************************/ | |
88 | |
89 void main() | |
90 { | |
91 // get a logger to represent this module. We could just as | |
92 // easily share a name with some other module(s) | |
93 auto log = Log.getLogger ("example.logging"); | |
94 try { | |
95 compute (log, 1000); | |
96 | |
97 } catch (Exception x) | |
98 { | |
99 // log the exception as an error | |
100 log.error ("Exception: " ~ x.toString); | |
101 } | |
102 } |