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_BASE_CODE_CONTEXT_H
10#define SQUID_BASE_CODE_CONTEXT_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
114template <typename Fun>
115inline void
116CallBack(const CodeContext::Pointer &callbackContext, Fun &&callback)
117{
118 // TODO: Consider catching exceptions and letting CodeContext handle them.
119 const auto savedCodeContext(CodeContext::Current());
120 CodeContext::Reset(callbackContext);
121 callback();
122 CodeContext::Reset(savedCodeContext);
123}
124
131template <typename Fun>
132inline void
133CallService(const CodeContext::Pointer &serviceContext, Fun &&service)
134{
135 // TODO: Consider catching exceptions and letting CodeContext handle them.
136 CodeContextGuard guard(serviceContext);
137 service();
138}
139
146template <typename Fun>
147inline void
148CallContextCreator(Fun &&creator)
149{
150 const auto savedCodeContext(CodeContext::Current());
151 creator();
152 CodeContext::Reset(savedCodeContext);
153}
154
156
157#endif
158
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)
Definition: CodeContext.h:148
std::ostream & CurrentCodeContextBrief(std::ostream &os)
void CallService(const CodeContext::Pointer &serviceContext, Fun &&service)
Definition: CodeContext.h:133
void CallBack(const CodeContext::Pointer &callbackContext, Fun &&callback)
Definition: CodeContext.h:116
std::ostream & CurrentCodeContextDetail(std::ostream &os)
Definition: CodeContext.cc:96
#define RefCountable
The locking interface for use on Reference-Counted classes.
Definition: Lock.h:66
CodeContext::Pointer savedCodeContext
Definition: CodeContext.h:106
CodeContextGuard(const CodeContext::Pointer &newContext)
Definition: CodeContext.h:100
CodeContextGuard(CodeContextGuard &&)=delete
Stopwatch busyTime
time spent in this context (see also: busy_time)
Definition: CodeContext.h:76
static const Pointer & Current()
Definition: CodeContext.cc:33
RefCount< CodeContext > Pointer
Definition: CodeContext.h:55
static void Leaving()
Definition: CodeContext.cc:68
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)
Definition: CodeContext.cc:55
static void ForgetCurrent()
Definition: CodeContext.cc:42
~CodeContext() override
Definition: CodeContext.h:66
static void Reset()
forgets the current context, setting it to nil/unknown
Definition: CodeContext.cc:77

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors