CodeContext.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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
15#include <iosfwd>
16
52{
53public:
55
57 static const Pointer &Current();
58
60 static void Reset();
61
63 static void Reset(const Pointer);
64
65 virtual ~CodeContext() {}
66
69 virtual ScopedId codeContextGist() const = 0;
70
72 virtual std::ostream &detailCodeContext(std::ostream &os) const = 0;
73
74private:
75 static void ForgetCurrent();
76 static void Entering(const Pointer &codeCtx);
77 static void Leaving();
78};
79
81inline
82std::ostream &operator <<(std::ostream &os, const CodeContext &ctx)
83{
84 return os << ctx.codeContextGist();
85}
86
87/* convenience context-reporting wrappers that also reduce linking problems */
88std::ostream &CurrentCodeContextBrief(std::ostream &os);
89std::ostream &CurrentCodeContextDetail(std::ostream &os);
90
94{
95public:
98
99 // no copying of any kind (for simplicity and to prevent accidental copies)
101
103};
104
110template <typename Fun>
111inline void
112CallBack(const CodeContext::Pointer &callbackContext, Fun &&callback)
113{
114 // TODO: Consider catching exceptions and letting CodeContext handle them.
115 const auto savedCodeContext(CodeContext::Current());
116 CodeContext::Reset(callbackContext);
117 callback();
118 CodeContext::Reset(savedCodeContext);
119}
120
127template <typename Fun>
128inline void
129CallService(const CodeContext::Pointer &serviceContext, Fun &&service)
130{
131 // TODO: Consider catching exceptions and letting CodeContext handle them.
132 CodeContextGuard guard(serviceContext);
133 service();
134}
135
142template <typename Fun>
143inline void
144CallContextCreator(Fun &&creator)
145{
146 const auto savedCodeContext(CodeContext::Current());
147 creator();
148 CodeContext::Reset(savedCodeContext);
149}
150
152
153#endif
154
std::ostream & operator<<(std::ostream &os, const CodeContext &ctx)
by default, only small context gist is printed
Definition: CodeContext.h:82
void CallContextCreator(Fun &&creator)
Definition: CodeContext.h:144
std::ostream & CurrentCodeContextBrief(std::ostream &os)
void CallService(const CodeContext::Pointer &serviceContext, Fun &&service)
Definition: CodeContext.h:129
void CallBack(const CodeContext::Pointer &callbackContext, Fun &&callback)
Definition: CodeContext.h:112
std::ostream & CurrentCodeContextDetail(std::ostream &os)
Definition: CodeContext.cc:94
#define RefCountable
The locking interface for use on Reference-Counted classes.
Definition: Lock.h:66
CodeContext::Pointer savedCodeContext
Definition: CodeContext.h:102
CodeContextGuard(const CodeContext::Pointer &newContext)
Definition: CodeContext.h:96
CodeContextGuard(CodeContextGuard &&)=delete
static const Pointer & Current()
Definition: CodeContext.cc:33
RefCount< CodeContext > Pointer
Definition: CodeContext.h:54
static void Leaving()
Definition: CodeContext.cc:66
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:54
static void ForgetCurrent()
Definition: CodeContext.cc:42
static void Reset()
forgets the current context, setting it to nil/unknown
Definition: CodeContext.cc:75
virtual ~CodeContext()
Definition: CodeContext.h:65

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors