1 /// Allows adding verbosity to sync objects
2 ///
3 /// Intended for use only by sync object developers
4 module mecca.reactor.sync.verbose;
5 
6 // Licensed under the Boost license. Full copyright information in the AUTHORS file
7 
8 import mecca.log;
9 
10 enum SyncVerbosityEventType {
11     HazardOn, HazardOff, Contention, Wakeup
12 }
13 
14 alias EventReporter = void delegate(SyncVerbosityEventType evt) nothrow @safe @nogc;
15 
16 struct SyncVerbosity(SyncType, string Name, ExtraParam = void) {
17     SyncType syncer;
18 
19 private:
20     enum WithExtraParam = !is(ExtraParam == void);
21 
22     static if( WithExtraParam ) {
23         ExtraParam param;
24     }
25 
26 public:
27     static if( WithExtraParam ) {
28         this(ExtraParam param) nothrow @safe @nogc {
29             this.param = param;
30             this.syncer.setVerbosityCallback(&reportEvent);
31         }
32 
33         void open( ExtraParam param ) nothrow @safe @nogc {
34             this.param = param;
35             this.syncer.setVerbosityCallback(&reportEvent);
36         }
37     } else {
38         void open() nothrow @safe @nogc {
39             this.syncer.setVerbosityCallback(&reportEvent);
40         }
41     }
42 
43     alias syncer this;
44 
45 private:
46     void reportEvent(SyncVerbosityEventType event) nothrow @safe @nogc {
47         static if( WithExtraParam ) {
48             enum NameFormat = Name ~ "(%s)";
49         } else {
50             enum NameFormat = Name;
51         }
52 
53         with(SyncVerbosityEventType) {
54             final switch(event) {
55             case HazardOn:
56                 static if( WithExtraParam ) {
57                     WARN!(NameFormat ~ " became unavailable")(param);
58                 } else {
59                     WARN!(NameFormat ~ " became unavailable")();
60                 }
61                 break;
62             case HazardOff:
63                 static if( WithExtraParam ) {
64                     INFO!(NameFormat ~ " became available again")(param);
65                 } else {
66                     INFO!(NameFormat ~ " became available again")();
67                 }
68                 break;
69             case Contention:
70                 static if( WithExtraParam ) {
71                     WARN!("Blocking in wait for " ~ NameFormat)(param);
72                 } else {
73                     WARN!("Blocking in wait for " ~ NameFormat)();
74                 }
75                 break;
76             case Wakeup:
77                 static if( WithExtraParam ) {
78                     DEBUG!("Woke up after waiting for " ~ NameFormat)(param);
79                 } else {
80                     DEBUG!("Woke up after waiting for " ~ NameFormat)();
81                 }
82                 break;
83             }
84         }
85     }
86 }