CodeContext.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS code_contexts for details.
7  */
8 
9 #ifndef SQUID_SRC_BASE_CODECONTEXT_H
10 #define SQUID_SRC_BASE_CODECONTEXT_H
11 
12 #include "base/InstanceId.h"
13 #include "base/RefCount.h"
14 #include "base/Stopwatch.h"
15 
16 #include <iosfwd>
17 
49 class CodeContext: public RefCountable
53 {
54 public:
56 
58  static const Pointer &Current();
59 
61  static void Reset();
62 
64  static void Reset(const Pointer);
65 
66  ~CodeContext() override {}
67 
70  virtual ScopedId codeContextGist() const = 0;
71 
73  virtual std::ostream &detailCodeContext(std::ostream &os) const = 0;
74 
77 
78 private:
79  static void ForgetCurrent();
80  static void Entering(const Pointer &codeCtx);
81  static void Leaving();
82 };
83 
85 inline
86 std::ostream &operator <<(std::ostream &os, const CodeContext &ctx)
87 {
88  return os << ctx.codeContextGist();
89 }
90 
91 /* convenience context-reporting wrappers that also reduce linking problems */
92 std::ostream &CurrentCodeContextBrief(std::ostream &os);
93 std::ostream &CurrentCodeContextDetail(std::ostream &os);
94 
98 {
99 public:
102 
103  // no copying of any kind (for simplicity and to prevent accidental copies)
104  CodeContextGuard(CodeContextGuard &&) = delete;
105 
107 };
108 
112 template <typename Fun>
113 inline void
114 CallAndRestore_(const CodeContext::Pointer &context, Fun &&fun)
115 {
116  const auto savedCodeContext(CodeContext::Current());
117  CodeContext::Reset(context);
118  fun();
119  CodeContext::Reset(savedCodeContext);
120 }
121 
124 template <typename Fun>
125 inline void
126 CallBack(const CodeContext::Pointer &callbackContext, Fun &&callback)
127 {
128  // TODO: Consider catching exceptions and letting CodeContext handle them.
129  CallAndRestore_(callbackContext, callback);
130 }
131 
136 template <typename Fun>
137 inline void
138 CallParser(const CodeContext::Pointer &parsingContext, Fun &&parse)
139 {
140  CallAndRestore_(parsingContext, parse);
141 }
142 
149 template <typename Fun>
150 inline void
151 CallService(const CodeContext::Pointer &serviceContext, Fun &&service)
152 {
153  // TODO: Consider catching exceptions and letting CodeContext handle them.
154  CodeContextGuard guard(serviceContext);
155  service();
156 }
157 
164 template <typename Fun>
165 inline void
166 CallContextCreator(Fun &&creator)
167 {
168  const auto savedCodeContext(CodeContext::Current());
169  creator();
170  CodeContext::Reset(savedCodeContext);
171 }
172 
174 
175 #endif /* SQUID_SRC_BASE_CODECONTEXT_H */
176 
static void Leaving()
Definition: CodeContext.cc:68
CodeContext::Pointer savedCodeContext
Definition: CodeContext.h:106
std::ostream & operator<<(std::ostream &os, const CodeContext &ctx)
by default, only small context gist is printed
Definition: CodeContext.h:86
~CodeContext() override
Definition: CodeContext.h:66
static void Entering(const Pointer &codeCtx)
Definition: CodeContext.cc:55
virtual std::ostream & detailCodeContext(std::ostream &os) const =0
appends human-friendly context description line(s) to a cache.log record
static struct node * parse(FILE *fp)
Definition: parse.c:965
void CallBack(const CodeContext::Pointer &callbackContext, Fun &&callback)
Definition: CodeContext.h:126
virtual ScopedId codeContextGist() const =0
RefCount< CodeContext > Pointer
Definition: CodeContext.h:55
static void Reset()
forgets the current context, setting it to nil/unknown
Definition: CodeContext.cc:77
void CallParser(const CodeContext::Pointer &parsingContext, Fun &&parse)
Definition: CodeContext.h:138
std::ostream & CurrentCodeContextBrief(std::ostream &os)
CodeContextGuard(const CodeContext::Pointer &newContext)
Definition: CodeContext.h:100
void CallAndRestore_(const CodeContext::Pointer &context, Fun &&fun)
Definition: CodeContext.h:114
static const Pointer & Current()
Definition: CodeContext.cc:33
Stopwatch busyTime
time spent in this context (see also: busy_time)
Definition: CodeContext.h:76
std::ostream & CurrentCodeContextDetail(std::ostream &os)
Definition: CodeContext.cc:96
void CallContextCreator(Fun &&creator)
Definition: CodeContext.h:166
void CallService(const CodeContext::Pointer &serviceContext, Fun &&service)
Definition: CodeContext.h:151
static void ForgetCurrent()
Definition: CodeContext.cc:42

 

Introduction

Documentation

Support

Miscellaneous