DocWire SDK
DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, document analysis. Offline processing possible for security and confidentiality
log_scope.h
1 /*********************************************************************************************************************************************/
2 /* DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. */
3 /* Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, */
4 /* document analysis. Offline processing possible for security and confidentiality */
5 /* */
6 /* Copyright (c) SILVERCODERS Ltd, http://silvercoders.com */
7 /* Project homepage: https://github.com/docwire/docwire */
8 /* */
9 /* SPDX-License-Identifier: GPL-2.0-only OR LicenseRef-DocWire-Commercial */
10 /*********************************************************************************************************************************************/
11 
12 #ifndef DOCWIRE_LOG_SCOPE_H
13 #define DOCWIRE_LOG_SCOPE_H
14 
15 #include "log_entry.h"
16 
17 namespace docwire::log {
18 
19 namespace detail {
20 
24 template <typename... Args>
25 class scope {
26 public:
27  scope(const Args&... args, const source_location& location = source_location::current()) noexcept
28  : m_location(location), m_args_tuple(args...)
29  {
30  if (detail::is_logging_enabled())
31  {
32  docwire::log::entry(m_location, std::tuple_cat(std::make_tuple(log::scope_enter{}), m_args_tuple));
33  }
34  }
35 
36  ~scope() noexcept
37  {
38  if (detail::is_logging_enabled())
39  {
40  try {
41  docwire::log::entry(m_location, std::tuple_cat(std::make_tuple(log::scope_exit{}), m_args_tuple));
42  } catch(...) {}
43  }
44  }
45 
46 private:
47  source_location m_location;
48  std::tuple<Args...> m_args_tuple;
49 };
50 
51 // An empty struct to be used when the log scope should be completely compiled out.
55 struct empty_scope {
56  // A constructor that accepts any arguments and does nothing.
57  // This is designed to be a 'sink' for any arguments passed from the log_scope macro
58  // when logging is disabled for a release build, ensuring compilation succeeds
59  // without generating any code.
60  template <typename... T>
61  [[maybe_unused]] explicit empty_scope(T&&...) noexcept
62  {}
63 };
64 
65 #ifdef NDEBUG
67 constexpr bool is_debug_build = false;
68 #else
70 constexpr bool is_debug_build = true;
71 #endif
72 
73 } // namespace detail
74 
75 // The public-facing `scope` class template.
76 // It inherits from the real implementation or an empty struct based on build mode.
83 template <typename... Args>
84 class scope : public std::conditional_t< // Note: This is now the public `scope`
85  detail::is_debug_build || detail::should_log_in_release<Args...>(),
86  detail::scope<Args...>,
87  detail::empty_scope
88 >
89 {
90 public:
91  using base = std::conditional_t<detail::is_debug_build || detail::should_log_in_release<Args...>(), detail::scope<Args...>, detail::empty_scope>;
92  using base::base;
93  // Constructor that takes a tuple of arguments and forwards it to the base.
94  [[maybe_unused]] explicit scope(const Args&... args, const source_location& location = source_location::current()) noexcept
95  : base(args..., location)
96  {}
97 };
98 
99 // Deduction guide to allow creating a scope object without explicitly specifying template arguments.
100 template<typename... Args>
101 scope(const Args&...) -> scope<Args...>;
102 
103 } // namespace docwire::log
104 
105 #define DOCWIRE_LOG_SCOPE_CONCAT_IMPL(a, b) a##b
106 #define DOCWIRE_LOG_SCOPE_CONCAT(a, b) DOCWIRE_LOG_SCOPE_CONCAT_IMPL(a, b)
107 
108 #define DOCWIRE_LOG_SCOPE(...) \
109  [[maybe_unused]] docwire::log::scope \
110  DOCWIRE_LOG_SCOPE_CONCAT(docwire_log_scope_object_at_line_, __LINE__) {DOCWIRE_DIAGNOSTIC_CONTEXT_MAKE_TUPLE(__VA_ARGS__)}
111 
112 #ifdef DOCWIRE_ENABLE_SHORT_MACRO_NAMES
113  #define log_scope(...) DOCWIRE_LOG_SCOPE(__VA_ARGS__)
114 #endif
115 
116 #endif // DOCWIRE_LOG_SCOPE_H
RAII class for logging scope entry and exit.
Definition: log_scope.h:25
Represents a logging scope.
Definition: log_scope.h:89
Provides a modern, high-performance, and structured logging framework.
A fallback implementation of source_location for compilers that do not support std::source_location.
A no-op scope class used when logging is disabled or in release builds.
Definition: log_scope.h:55
Tag automatically added to a log entry when a log_scope is entered.
Definition: log_tags.h:35
Tag automatically added to a log entry when a log_scope is exited.
Definition: log_tags.h:38