1 /**
2 *   Sometimes logging is needed only if something goes wrong. This module
3 *   describes a class-wrapper to handle delayed logging. 
4 *
5 *   Example:
6 *   ----------
7 *   auto delayed = new shared BufferedLogger(logger); // wrapping a logger
8 *	scope(exit) delayed.finalize(); // write down information in wrapped logger
9 *   scope(failure) delayed.minOutputLevel = LoggingLevel.Notice; // if failed, spam in console
10 *   delayed.logNotice("Hello!");
11 *
12 *   // do something that can fail
13 *
14 *   ----------
15 *
16 *   Copyright: © 2013-2014 Anton Gushcha
17 *   License: Subject to the terms of the MIT license, as written in the included LICENSE file.
18 *   Authors: NCrashed <ncrashed@gmail.com>
19 */
20 module dlogg.buffered;
21 
22 import std.array;
23 import std.stdio;
24 import dlogg.strict;
25 
26 /**
27 *   Class-wrapper around strict logger. All strings are written down
28 *   only after finalizing the wrapper.
29 */
30 synchronized class BufferedLogger : StrictLogger
31 {
32     this(shared ILogger delayLogger)
33     {
34         this.delayLogger = delayLogger;
35         minOutputLevel = LoggingLevel.Muted;
36     }
37     
38     override void rawInput(string message) @trusted
39     {
40         buffer ~= message;
41     }
42     
43     override void finalize() @trusted
44     {
45         if(finalized) return;
46         scope(exit) finalized = true;
47         
48         foreach(msg; buffer)
49         {
50             scope(failure) {}
51             
52             if(minOutputLevel != LoggingLevel.Muted)
53                 writeln(msg);
54                 
55             delayLogger.rawInput(msg);
56         }
57     }
58     
59     private
60     {
61         shared ILogger delayLogger;
62         string[] buffer;
63         bool finalized;
64     }
65 }