xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
trace_source.h
Go to the documentation of this file.
1 #pragma once
5 #include <map>
6 #include "../object.h"
8 #include "source_levels.h"
9 #include "source_switch.h"
10 #include "trace_event_cache.h"
12 
14 namespace xtd {
16  namespace diagnostics {
32  class trace_source : public object {
33  public:
36  explicit trace_source(const xtd::ustring& name);
40  explicit trace_source(const xtd::ustring& name, xtd::diagnostics::source_levels default_levels);
41 
46 
51 
54  const xtd::ustring& name() const;
55 
65  void source_switch(const xtd::diagnostics::source_switch& source_switch);
66 
69  void close();
70 
73  void flush();
74 
81  template<typename object_t>
82  void trace_data(const xtd::diagnostics::trace_event_type& event_type, int32_t id, const object_t& data) {
83 #if defined(TRACE)
84  if (source_switch_.should_trace(event_type))
85  for (auto listener : listeners_)
86  listener->trace_data(trace_event_cache(), name_, event_type, id, data);
87 #endif
88  }
89 
96  template<typename object_t>
97  void trace_data(const xtd::diagnostics::trace_event_type& event_type, int32_t id, const std::vector<object_t>& data) {
98 #if defined(TRACE)
99  if (source_switch_.should_trace(event_type))
100  for (auto listener : listeners_)
101  listener->trace_data(trace_event_cache(), name_, event_type, id, data);
102 #endif
103  }
104 
109  void trace_event(const xtd::diagnostics::trace_event_type& event_type, int32_t id) {
110 #if defined(TRACE)
111  if (source_switch_.should_trace(event_type))
112  for (auto listener : listeners_)
113  listener->trace_event(trace_event_cache(), name_, event_type, id);
114 #endif
115  }
116 
121  void trace_event(const xtd::diagnostics::trace_event_type& event_type, int32_t id, const xtd::ustring& message) {
122 #if defined(TRACE)
123  if (source_switch_.should_trace(event_type))
124  for (auto listener : listeners_)
125  listener->trace_event(trace_event_cache(), name_, event_type, id, message);
126 #endif
127  }
128 
134  template<typename ...objects>
135  void trace_event(const xtd::diagnostics::trace_event_type& event_type, int32_t id, const xtd::ustring& format, const objects& ... args) {
136 #if defined(TRACE)
137  if (source_switch_.should_trace(event_type))
138  for (auto listener : listeners_)
139  listener->trace_event(trace_event_cache(), name_, event_type, id, xtd::ustring::format(format, args...));
140 #endif
141  }
142 
147  void trace_information(const xtd::ustring& message);
148 
154  template<typename ...objects_t>
155  void trace_information(const xtd::ustring& format, const objects_t& ... args) {trace_event(trace_event_type::information, 0, format, args...);}
156 
163  template<typename guid_t>
164  void trace_transfer (int32_t id, const xtd::ustring& message,const guid_t& related_activity_id) {
165 #if defined(TRACE)
166  for (auto listener : listeners_)
167  listener->trace_transfer(trace_event_cache(), name_, id, message, related_activity_id);
168 #endif
169  }
170 
171  private:
172  std::map<xtd::ustring, xtd::ustring> attributes_;
173  xtd::ustring name_;
176  xtd::diagnostics::trace_listener_collection listeners_ {std::make_shared<xtd::diagnostics::default_trace_listener>()};
177  xtd::diagnostics::source_switch source_switch_ {""};
178  };
179  }
180 }
Provides a multilevel switch to control tracing and debug output without recompiling your code...
Definition: source_switch.h:22
void trace_transfer(int32_t id, const xtd::ustring &message, const guid_t &related_activity_id)
Writes a trace transfer message to the trace listeners in the listeners collection using the specifie...
Definition: trace_source.h:164
void trace_event(const xtd::diagnostics::trace_event_type &event_type, int32_t id)
Writes a trace event message to the trace listeners in the listeners collection using the specified e...
Definition: trace_source.h:109
xtd::diagnostics::trace_listener_collection & listeners()
Gets the collection of trace listeners for the trace source.
void trace_event(const xtd::diagnostics::trace_event_type &event_type, int32_t id, const xtd::ustring &format, const objects &... args)
Writes a trace event message to the trace listeners in the listeners collection using the specified e...
Definition: trace_source.h:135
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
void flush()
Flushes all the trace listeners in the trace listener collection.
source_levels
Specifies the levels of trace messages filtered by the source switch and event type filter...
Definition: source_levels.h:18
Provides trace event data specific to a thread and a process.
Definition: trace_event_cache.h:22
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
void trace_information(const xtd::ustring &message)
Writes an informational message to the trace listeners in the listeners collection using the specifie...
void trace_information(const xtd::ustring &format, const objects_t &... args)
Writes an informational message to the trace listeners in the listeners collection using the specifie...
Definition: trace_source.h:155
Provides a set of methods and properties that enable applications to trace the execution of code and ...
Definition: trace_source.h:32
void trace_data(const xtd::diagnostics::trace_event_type &event_type, int32_t id, const object_t &data)
Writes trace data to the trace listeners in the Listeners collection using the specified event type...
Definition: trace_source.h:82
Contains xtd::diagnostics::default_trace_listener class.
void trace_data(const xtd::diagnostics::trace_event_type &event_type, int32_t id, const std::vector< object_t > &data)
Writes trace data to the trace listeners in the Listeners collection using the specified event type...
Definition: trace_source.h:97
Represents a collection of xtd::diagnostics::trace_listener.
Definition: trace_listener_collection.h:19
Contains xtd::diagnostics::trace_listener_collection class.
Contains xtd::diagnostics::source_levels enum class.
Contains xtd::diagnostics::source_switch class.
void close()
Closes all the trace listeners in the trace listener collection.
const xtd::diagnostics::source_switch & source_switch() const
Gets the source switch value.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:26
static ustring format(const ustring &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition: ustring.h:689
bool should_trace(xtd::diagnostics::trace_event_type event_type)
Determines if trace listeners should be called, based on the trace event type.
Contains xtd::diagnostics::trace_event_cache class.
trace_source(const xtd::ustring &name)
Initializes a new instance of the Trace_Source class, using the specified name for the source...
Does not allow any events through.
void trace_event(const xtd::diagnostics::trace_event_type &event_type, int32_t id, const xtd::ustring &message)
Writes a trace event message to the trace listeners in the listeners collection using the specified e...
Definition: trace_source.h:121
trace_event_type
Identifies the type of event that has caused the trace.
Definition: trace_event_type.h:18
const xtd::ustring & name() const
Gets the name of the trace source.