xref: /aosp_15_r20/external/federated-compute/fcp/tracing/scoped_tracing_recorder.h (revision 14675a029014e728ec732f129a32e299b2da0601)
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FCP_TRACING_SCOPED_TRACING_RECORDER_H_
16 #define FCP_TRACING_SCOPED_TRACING_RECORDER_H_
17 
18 #include "fcp/tracing/tracing_recorder.h"
19 
20 namespace fcp {
21 
22 // This is an utility class that installs a specified tracing recorder as
23 // thread local and uninstalls it automatically when going out of scope.
24 class ScopedTracingRecorder {
25  public:
ScopedTracingRecorder(TracingRecorder * tracing_recorder)26   explicit ScopedTracingRecorder(TracingRecorder* tracing_recorder)
27       : tracing_recorder_(tracing_recorder) {
28     tracing_recorder_->InstallAsThreadLocal();
29   }
30 
~ScopedTracingRecorder()31   ~ScopedTracingRecorder() { tracing_recorder_->UninstallAsThreadLocal(); }
32 
33   // This class isn't copyable or moveable and can't be created via
34   // new operator.
35   ScopedTracingRecorder(const ScopedTracingRecorder& other) = delete;
36   ScopedTracingRecorder& operator=(const ScopedTracingRecorder& other) = delete;
37   void* operator new(std::size_t) = delete;
38   void* operator new[](std::size_t) = delete;
39 
40  private:
41   TracingRecorder* tracing_recorder_;
42 };
43 
44 }  // namespace fcp
45 
46 #endif  // FCP_TRACING_SCOPED_TRACING_RECORDER_H_
47