OpenVPN 3 Core Library
Loading...
Searching...
No Matches
logthread_class.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#pragma once
13
14// Define this parameter before including this header:
15// OPENVPN_LOG_CLASS -- client class that exposes a log() method
16#ifndef OPENVPN_LOG_CLASS
17#error OPENVPN_LOG_CLASS must be defined
18#endif
19
20namespace openvpn::Log {
21
22#ifdef OPENVPN_LOG_GLOBAL
23// OPENVPN_LOG uses global object pointer
24inline OPENVPN_LOG_CLASS *global_log = nullptr; // GLOBAL
25#else
26// OPENVPN_LOG uses thread-local object pointer
27inline thread_local OPENVPN_LOG_CLASS *global_log = nullptr; // GLOBAL
28#endif
29
37struct Context
38{
45 class Wrapper
46 {
47#ifndef OPENVPN_LOG_GLOBAL
48 public:
50 : log(obj())
51 {
52 }
53
54 private:
55 friend struct Context;
57#endif
58 };
59
65 Context(const Wrapper &wrap)
66 {
67#ifndef OPENVPN_LOG_GLOBAL
68 global_log = wrap.log;
69#endif
70 }
71
78 {
79 global_log = cli;
80 }
81
83 {
84 global_log = nullptr;
85 }
86
87 static bool defined()
88 {
89 return global_log != nullptr;
90 }
91
93 {
94 return global_log;
95 }
96};
97} // namespace openvpn::Log
thread_local OPENVPN_LOG_CLASS * global_log
#define OPENVPN_LOG_CLASS
Definition ovpncli.cpp:66
Argument to construct a Context in a different thread.
Scoped RAII for the global_log pointer.
Context(const Wrapper &wrap)
construct a Context in a different thread
static OPENVPN_LOG_CLASS * obj()
Context(OPENVPN_LOG_CLASS *cli)
construct a Context setting the global_log pointer