Squid Web Cache v8/master
Loading...
Searching...
No Matches
CodeContext.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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
53{
54public:
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
78private:
79 static void ForgetCurrent();
80 static void Entering(const Pointer &codeCtx);
81 static void Leaving();
82};
83
85inline
86std::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 */
92std::ostream &CurrentCodeContextBrief(std::ostream &os);
93std::ostream &CurrentCodeContextDetail(std::ostream &os);
94
98{
99public:
102
103 // no copying of any kind (for simplicity and to prevent accidental copies)
105
107};
108
112template <typename Fun>
113inline void
114CallAndRestore_(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
124template <typename Fun>
125inline void
126CallBack(const CodeContext::Pointer &callbackContext, Fun &&callback)
127{
128 // TODO: Consider catching exceptions and letting CodeContext handle them.
129 CallAndRestore_(callbackContext, callback);
130}
131
136template <typename Fun>
137inline void
138CallParser(const CodeContext::Pointer &parsingContext, Fun &&parse)
139{
140 CallAndRestore_(parsingContext, parse);
141}
142
149template <typename Fun>
150inline void
151CallService(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
164template <typename Fun>
165inline void
166CallContextCreator(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
void CallAndRestore_(const CodeContext::Pointer &context, Fun &&fun)
std::ostream & operator<<(std::ostream &os, const CodeContext &ctx)
by default, only small context gist is printed
Definition CodeContext.h:86
void CallContextCreator(Fun &&creator)
std::ostream & CurrentCodeContextBrief(std::ostream &os)
void CallParser(const CodeContext::Pointer &parsingContext, Fun &&parse)
void CallService(const CodeContext::Pointer &serviceContext, Fun &&service)
void CallBack(const CodeContext::Pointer &callbackContext, Fun &&callback)
std::ostream & CurrentCodeContextDetail(std::ostream &os)
#define RefCountable
The locking interface for use on Reference-Counted classes.
Definition Lock.h:66
CodeContext::Pointer savedCodeContext
CodeContextGuard(const CodeContext::Pointer &newContext)
CodeContextGuard(CodeContextGuard &&)=delete
Stopwatch busyTime
time spent in this context (see also: busy_time)
Definition CodeContext.h:76
static const Pointer & Current()
RefCount< CodeContext > Pointer
Definition CodeContext.h:55
static void Leaving()
virtual std::ostream & detailCodeContext(std::ostream &os) const =0
appends human-friendly context description line(s) to a cache.log record
virtual ScopedId codeContextGist() const =0
static void Entering(const Pointer &codeCtx)
static void ForgetCurrent()
~CodeContext() override
Definition CodeContext.h:66
static void Reset()
forgets the current context, setting it to nil/unknown
static struct node * parse(FILE *fp)
Definition parse.c:965