root/branches/v2.1/example/logging.d

Revision 611, 4.4 kB (checked in by kris, 3 years ago)

whoops! made an invalid cast

Line 
1 /*******************************************************************************
2
3         Shows how the basic functionality of Logger operates.
4
5 *******************************************************************************/
6
7 private import mango.log.Logger,
8                mango.log.Configurator;
9
10 private import mango.convert.Sprint;
11
12
13 /*******************************************************************************
14
15 *******************************************************************************/
16
17 private class Sieve
18 {
19         private Logger logger;
20        
21         /***********************************************************************
22
23                 Initialize the Sieve class
24
25         ***********************************************************************/
26
27         this()
28         {
29                 // get a logger for this object. Could make this static instead
30                 logger = Logger.getLogger ("example.logging.Sieve");
31         }
32
33         /***********************************************************************
34
35                 Search for a set of prime numbers
36
37         ***********************************************************************/
38
39         void compute (uint max)
40         {
41                 byte*   feld;
42                 int     teste=1,
43                         mom,
44                         hits=0,
45                         s=0,
46                         e = 1;
47                 int     count;
48                 auto    sprint = new Sprint (256);
49
50                 void set (byte* f, uint x)
51                 {
52                         *(f+(x)/16) |=1 <<(((x)%16)/2);
53                 }
54
55                 byte test (byte* f, uint x)
56                 {
57                         return cast(byte) (*(f+(x)/16) & (1<<(((x)%16)/2)));
58                 }
59
60                 // information level
61                 logger.info (sprint ("Searching prime numbers to : %d", max));
62
63                 feld = new byte[max / 16 + 1];
64
65                 // get milliseconds since application began
66                 ulong begin = logger.getRuntime();
67
68                 while ((teste += 2) < max)
69                         if (! test (feld, teste))
70                            {
71                            if  ((++hits & 0x0f) == 0)
72                                 // more information level
73                                 logger.info (sprint ("found %d", hits));
74
75                            for (mom=3*teste; mom < max; mom += teste<<1)
76                                 set (feld, mom);
77                            }
78
79                 // get number of milliseconds we took to compute
80                 ulong period = logger.getRuntime() - begin;
81
82                 if (hits)
83                     // more information
84                     logger.info (sprint ("%d prime numbers found in %d millsecs", hits, period));
85                 else
86                    // a warning level
87                    logger.warn ("no prime numbers found");
88        
89                 // check to see if we're enabled for
90                 // tracing before we expend a lot of effort
91                 if (logger.isEnabled (logger.Level.Trace))
92                    {       
93                    e = max;
94                    count = 0 - 2;
95                    if (s % 2 == 0)
96                        count++;
97            
98                    while ((count+=2) < e)
99                            // log trace information
100                            if (! test (feld, count))
101                                logger.trace (sprint ("prime found: %d", count));
102                    }
103         }
104 }
105
106
107 /*******************************************************************************
108
109         Create a Sieve and have it compute a bunch of prime numbers.
110
111 *******************************************************************************/
112
113 void main()
114 {
115         // get a logger to represent this module. We could just as
116         // easily share a name with some other module(s)
117         Logger logger = Logger.getLogger ("example.logging");
118        
119         // set up a basic logging configuration
120         BasicConfigurator.configure();
121
122         try {
123             Sieve sieve = new Sieve;
124
125             sieve.compute (1000);
126
127             } catch (Exception x)
128                     {
129                     // log the exception as an error
130                     logger.error ("Exception: " ~ x.toString);
131                     }
132 }
Note: See TracBrowser for help on using the browser.