commore  1.0.6-SNAPSHOT
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LogHookManager.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2006-2014 Raphael David / CANTOR
3 //
4 
5 #ifndef CMR_LOG_HOOK_MANAGER_INCLUDED
6 #define CMR_LOG_HOOK_MANAGER_INCLUDED
7 
8 #include "commore/AutoRef.h"
9 #include "commore/Error.h"
10 #include "commore/LogHook.h"
11 
12 
13 namespace commore
14 {
15  //
16  // The unique log hook manager (internal commore class)
17  //
19  {
20  private:
22  LogHookManager& operator = (const LogHookManager&);
23  // LogHookManager is a singleton
24  LogHookManager() : ok_(true), busy_(false)
25  {
26  }
27 
28  public:
29  //
30  bool ok_;
31  bool busy_;
34 
36  {
37  ok_ = false;
38  }
40  {
41  static LogHookManager log_hook_manager_;
42  return log_hook_manager_;
43  }
44 
45  //
46  // Reentrance protection
47  //
48  bool check_free(bool busy)
49  {
50  bool r = false;
51  Lock lock(cs_);
52  if (busy)
53  {
54  r = !busy_;
55  if (!busy_)
56  {
57  busy_ = true;
58  }
59  }
60  else
61  {
62  if (busy_)
63  {
64  busy_ = false;
65  }
66  }
67  return r;
68  }
69 
70  bool add(LogHook* hook)
71  {
72  bool r = false;
73  if (ok_)
74  {
75  //
76  // Add once
77  //
78  Lock lock(cs_);
79  if (!hook->hooked_)
80  {
81  hook->hooked_ = true;
82  hook_list_.push_back(hook);
83  r = true;
84  }
85  }
86  return r;
87  }
88  bool remove(LogHook* hook)
89  {
90  bool r = false;
91  if (ok_)
92  {
93  Lock lock(cs_);
94  r = hook->hooked_;
95  hook->hooked_ = false;
96  hook_list_.remove(hook);
97  }
98  return r;
99  }
100  //
101  // Return true if list is not empty
102  //
103  bool get_hook_list(List<PLogHook>& hook_list)
104  {
105  int r = 0;
106  Lock lock(cs_);
107  for (List<LogHook*>::iterator i = hook_list_.begin(); i; i++)
108  {
109  hook_list.add(*i);
110  r++;
111  }
112  return r > 0;
113  }
114  void dispatch(Tuple& message);
115 
116  };
117 
118 }
119 
120 
121 #endif
122 
bool busy_
Definition: LogHookManager.h:31
Definition: Tuple.h:29
Definition: List.h:23
Definition: LogHookManager.h:18
bool get_hook_list(List< PLogHook > &hook_list)
Definition: LogHookManager.h:103
Definition: List.h:29
static LogHookManager & get_instance()
Definition: LogHookManager.h:39
bool ok_
Definition: LogHookManager.h:30
Definition: LogHook.h:21
T & add()
Definition: List.h:319
Definition: CriticalSection.h:119
bool check_free(bool busy)
Definition: LogHookManager.h:48
Definition: CriticalSection.h:15
List< LogHook * > hook_list_
Definition: LogHookManager.h:32
bool add(LogHook *hook)
Definition: LogHookManager.h:70
~LogHookManager()
Definition: LogHookManager.h:35
void dispatch(Tuple &message)
Definition: LogHook.cpp:176
static CriticalSection cs_
Definition: LogHookManager.h:33