 |
Changeset 2596
- Timestamp:
- 10/02/07 02:12:48
(1 year ago)
- Author:
- kris
- Message:
updated to use log.format()
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r2465 |
r2596 |
|
| 8 | 8 | tango.util.log.Configurator; |
|---|
| 9 | 9 | |
|---|
| 10 | | private import tango.text.convert.Sprint; |
|---|
| | 10 | /******************************************************************************* |
|---|
| 11 | 11 | |
|---|
| 12 | | /******************************************************************************* |
|---|
| | 12 | Search for a set of prime numbers |
|---|
| 13 | 13 | |
|---|
| 14 | 14 | *******************************************************************************/ |
|---|
| 15 | 15 | |
|---|
| 16 | | private class Sieve |
|---|
| 17 | | { |
|---|
| 18 | | private Logger logger; |
|---|
| 19 | | |
|---|
| 20 | | /*********************************************************************** |
|---|
| 21 | | |
|---|
| 22 | | Initialize the Sieve class |
|---|
| 23 | | |
|---|
| 24 | | ***********************************************************************/ |
|---|
| 25 | | |
|---|
| 26 | | this() |
|---|
| 27 | | { |
|---|
| 28 | | // get a logger for this object. Could make this static instead |
|---|
| 29 | | logger = Log.getLogger ("example.logging.Sieve"); |
|---|
| 30 | | } |
|---|
| 31 | | |
|---|
| 32 | | /*********************************************************************** |
|---|
| 33 | | |
|---|
| 34 | | Search for a set of prime numbers |
|---|
| 35 | | |
|---|
| 36 | | ***********************************************************************/ |
|---|
| 37 | | |
|---|
| 38 | | void compute (uint max) |
|---|
| | 16 | void compute (Logger log, uint max) |
|---|
| 39 | 17 | { |
|---|
| 40 | 18 | byte* feld; |
|---|
| … | … | |
| 45 | 23 | e = 1; |
|---|
| 46 | 24 | int count; |
|---|
| 47 | | auto sprint = new Sprint!(char); |
|---|
| | 25 | char tmp[128] = void; |
|---|
| 48 | 26 | |
|---|
| 49 | 27 | void set (byte* f, uint x) |
|---|
| … | … | |
| 58 | 36 | |
|---|
| 59 | 37 | // information level |
|---|
| 60 | | logger.info (sprint ("Searching prime numbers to : {0}", max)); |
|---|
| | 38 | log.info (log.format (tmp, "Searching prime numbers up to {}", max)); |
|---|
| 61 | 39 | |
|---|
| 62 | 40 | feld = (new byte[max / 16 + 1]).ptr; |
|---|
| 63 | 41 | |
|---|
| 64 | 42 | // get milliseconds since application began |
|---|
| 65 | | auto begin = logger.runtime(); |
|---|
| | 43 | auto begin = log.runtime; |
|---|
| 66 | 44 | |
|---|
| 67 | 45 | while ((teste += 2) < max) |
|---|
| … | … | |
| 70 | 48 | if ((++hits & 0x0f) == 0) |
|---|
| 71 | 49 | // more information level |
|---|
| 72 | | logger.info (sprint ("found {0}", hits)); |
|---|
| | 50 | log.info (log.format (tmp, "found {}", hits)); |
|---|
| 73 | 51 | |
|---|
| 74 | 52 | for (mom=3*teste; mom < max; mom += teste<<1) |
|---|
| … | … | |
| 77 | 55 | |
|---|
| 78 | 56 | // get number of milliseconds we took to compute |
|---|
| 79 | | auto period = logger.runtime() - begin; |
|---|
| | 57 | auto period = log.runtime - begin; |
|---|
| 80 | 58 | |
|---|
| 81 | 59 | if (hits) |
|---|
| 82 | 60 | // more information |
|---|
| 83 | | logger.info (sprint ("{0} prime numbers found in {1} millsecs", hits, period)); |
|---|
| | 61 | log.info (log.format (tmp, "{} prime numbers found in {} millsecs", hits, period)); |
|---|
| 84 | 62 | else |
|---|
| 85 | 63 | // a warning level |
|---|
| 86 | | logger.warn ("no prime numbers found"); |
|---|
| | 64 | log.warn ("no prime numbers found"); |
|---|
| 87 | 65 | |
|---|
| 88 | 66 | // check to see if we're enabled for |
|---|
| 89 | 67 | // tracing before we expend a lot of effort |
|---|
| 90 | | if (logger.isEnabled (logger.Level.Trace)) |
|---|
| | 68 | if (log.isEnabled (log.Level.Trace)) |
|---|
| 91 | 69 | { |
|---|
| 92 | 70 | e = max; |
|---|
| … | … | |
| 98 | 76 | // log trace information |
|---|
| 99 | 77 | if (! test (feld, count)) |
|---|
| 100 | | logger.trace (sprint ("prime found: {0}", count)); |
|---|
| | 78 | log.trace (log.format (tmp, "prime found: {}", count)); |
|---|
| 101 | 79 | } |
|---|
| 102 | | } |
|---|
| 103 | 80 | } |
|---|
| 104 | 81 | |
|---|
| … | … | |
| 106 | 83 | /******************************************************************************* |
|---|
| 107 | 84 | |
|---|
| 108 | | Create a Sieve and have it compute a bunch of prime numbers. |
|---|
| | 85 | Compute a bunch of prime numbers |
|---|
| 109 | 86 | |
|---|
| 110 | 87 | *******************************************************************************/ |
|---|
| … | … | |
| 114 | 91 | // get a logger to represent this module. We could just as |
|---|
| 115 | 92 | // easily share a name with some other module(s) |
|---|
| 116 | | auto logger = Log.getLogger ("example.logging"); |
|---|
| 117 | | |
|---|
| | 93 | auto log = Log.getLogger ("example.logging"); |
|---|
| 118 | 94 | try { |
|---|
| 119 | | Sieve sieve = new Sieve; |
|---|
| 120 | | |
|---|
| 121 | | sieve.compute (1000); |
|---|
| | 95 | compute (log, 1000); |
|---|
| 122 | 96 | |
|---|
| 123 | 97 | } catch (Exception x) |
|---|
| 124 | 98 | { |
|---|
| 125 | 99 | // log the exception as an error |
|---|
| 126 | | logger.error ("Exception: " ~ x.toUtf8); |
|---|
| | 100 | log.error ("Exception: " ~ x.toUtf8); |
|---|
| 127 | 101 | } |
|---|
| 128 | 102 | } |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic