RTI Routing Service  Version 6.0.1
 All Data Structures Files Functions Typedefs Enumerations Enumerator Groups Pages
AdvLogContext.hpp
1 /*
2  (c) Copyright, Real-Time Innovations, 2017-
3  All rights reserved.
4 
5  No duplications, whole or partial, manual or electronic, may be made
6  without express written permission. Any such copies, or
7  revisions thereof, must display this notice unaltered.
8  This code contains trade secrets of Real-Time Innovations, Inc.
9 */
10 
11 #ifndef HPP_SERVICE_LOG_ADVLOG_CONTEXT_CONTEXT_HPP_
12 #define HPP_SERVICE_LOG_ADVLOG_CONTEXT_CONTEXT_HPP_
13 
14 #include <cstdio>
15 #include <cstdarg>
16 
17 #include "advlog/advlog_context.h"
18 #include "dds_c/dds_c_domain_impl.h"
19 #include "routingservice/routingservice_log.h"
20 
21 #include <rti/service/log/LogConfig.hpp>
22 
23 namespace rti { namespace service { namespace log {
24 
25 class AdvLogContextGuard {
26 
27 public:
28  AdvLogContextGuard(
29  const struct ADVLOGContext *context,
30  int32_t intParam,
31  const void *pointerParam) :
32  context_(context),
33  worker_(DDS_DomainParticipantFactory_get_workerI(DDS_TheParticipantFactory))
34  {
35  ADVLOGContext_enter(
36  worker_,
37  context,
38  intParam,
39  pointerParam);
40  }
41 
42  AdvLogContextGuard(
43  const struct ADVLOGContext *context,
44  const void *pointerParam = NULL) :
45  context_(context),
46  worker_(DDS_DomainParticipantFactory_get_workerI(DDS_TheParticipantFactory))
47  {
48  ADVLOGContext_enter(
49  worker_,
50  context,
51  0,
52  pointerParam);
53  }
54 
55  void leave()
56  {
57  if (context_ != NULL) {
58  ADVLOGContext_leave(worker_, context_);
59  context_ = NULL;
60  }
61  }
62 
63 
64  ~AdvLogContextGuard()
65  {
66  leave();
67  }
68 
69 private:
70  const struct ADVLOGContext *context_;
71  struct REDAWorker *worker_;
72 };
73 
74 class AdvLogContextStackGuard {
75 public:
76 
77  AdvLogContextStackGuard(
78  const struct ADVLOGContext *context,
79  const char *string_param)
80  : worker_(DDS_DomainParticipantFactory_get_workerI(DDS_TheParticipantFactory))
81  {
82  push(context, string_param);
83  }
84 
85  virtual ~AdvLogContextStackGuard()
86  {
87  leave();
88  }
89 
90  AdvLogContextStackGuard& push(
91  const struct ADVLOGContext *context,
92  const char *string_param = NULL)
93  {
94  ADVLOGContext_enter(
95  worker_,
96  context,
97  0,
98  string_param);
99  context_stack_.push_back(context);
100 
101  return *this;
102  }
103 
104  AdvLogContextStackGuard& pop()
105  {
106  if (!context_stack_.empty()) {
107  ADVLOGContext_leave(worker_, context_stack_.back());
108  context_stack_.pop_back();
109  }
110 
111  return *this;
112  }
113 
114  void leave()
115  {
116  std::vector<const ADVLOGContext*>::reverse_iterator it = context_stack_.rbegin();
117  for (; it != context_stack_.rend(); ++it) {
118  ADVLOGContext_leave(worker_, *it);
119  }
120  }
121 
122 private:
123  std::vector<const ADVLOGContext*> context_stack_;
124  struct REDAWorker *worker_;
125 };
126 
127 class AdvlogContextStackLeaveGuard
128 {
129 public:
130  AdvlogContextStackLeaveGuard(const ADVLOGContext *context)
131  {
132  push(context);
133  }
134 
135  ~AdvlogContextStackLeaveGuard()
136  {
137  std::vector<const ADVLOGContext*>::iterator it = context_stack_.begin();
138  for (; it != context_stack_.end(); ++it) {
139  ADVLOGContext_leave(NULL, *it);
140  }
141  }
142 
143  void push(const struct ADVLOGContext *context)
144  {
145  context_stack_.push_back(context);
146  }
147 
148  void leave()
149  {
150  if (!context_stack_.empty()) {
151  ADVLOGContext_leave(NULL, context_stack_.front());
152  }
153  }
154 
155 private:
156  std::vector<const ADVLOGContext*> context_stack_;
157 };
158 
159 #define RTI_ACTIVITY_CONTEXT_LEAVE() \
160  context_guard.leave()
161 
162 #define RTI_ACTIVITY_CONTEXT_ENTER(...) \
163  rti::service::log::AdvLogContextStackGuard context_guard(__VA_ARGS__);
164 
165 #define RTI_LOG_ACTIVITY_CONTEXT_ENTER(...) \
166  RTI_ACTIVITY_CONTEXT_ENTER(__VA_ARGS__); \
167  SERVICELog_flushActivityContext(RTI_LOG_BIT_LOCAL)
168 
169 #define RTI_ACTIVITY_CONTEXT_RESOURCE_ENTER(CONTEXT, RESOURCE_ID) \
170  rti::service::log::AdvLogContextStackGuard context_guard(\
171  (CONTEXT), \
172  (RESOURCE_ID))
173 
174 #define RTI_ACTIVITY_CONTEXT_RESOURCE_METHOD_ENTER(\
175  RESOURCE_ID, \
176  ...) \
177  rti::service::log::AdvLogContextStackGuard context_guard(\
178  (&RTI_ADVLOG_CONTEXT_RESOURCE_ID_s), \
179  (RESOURCE_ID)); \
180  context_guard.push(__VA_ARGS__)
181 
182 
183 #define RTI_LOG_ACTIVITY_CONTEXT_RESOURCE_METHOD_ENTER(\
184  RESOURCE_ID, \
185  ...) \
186  RTI_ACTIVITY_CONTEXT_RESOURCE_METHOD_ENTER(\
187  RESOURCE_ID, \
188  __VA_ARGS__);\
189  SERVICELog_flushActivityContext(RTI_LOG_BIT_LOCAL)
190 
191 #define RTI_LOG_ACTIVITY_RESOURCE_SCOPED_LEAVE(CONTEXT) \
192  rti::service::log::AdvlogContextStackLeaveGuard leave_guard((CONTEXT)); \
193  leave_guard.push((&RTI_ADVLOG_CONTEXT_RESOURCE_ID_s))
194 
195 } } } // rti::service::log
196 
197 #endif /* HPP_SERVICE_LOG_ADVLOG_CONTEXT_CONTEXT_HPP_ */

RTI Routing Service Version 6.0.1 Copyright © Sun Nov 17 2019 Real-Time Innovations, Inc