xtd 0.2.0
Loading...
Searching...
No Matches
xtd::diagnostics::default_trace_listener Class Reference
Inheritance diagram for xtd::diagnostics::default_trace_listener:
xtd::diagnostics::trace_listener xtd::abstract_object xtd::object

Definition

Provides the default output methods and behavior for tracing.

default_trace_listener()
Initializes a new instance of the default_trace_listener class with "default" as its xtd::diagnostics...
Provides the abstract base class for the listeners who monitor trace and debug output.
Definition trace_listener.hpp:42
#define core_export_
Define shared library export.
Definition core_export.hpp:13
Inheritance
xtd::objectxtd::abstract_objectxtd::diagnostics::trace_listenerxtd::diagnostics::default_trace_listener
Header
#include <xtd/diagnostics/default_trace_listener>
Namespace
xtd::diagnostics
Library
xtd.core
Examples
The following code example calculates binomial coefficients, which are values used in probability and statistics. This example uses a xtd::diagnostics::default_trace_listener to trace results and log errors. It creates a new xtd::diagnostics::default_trace_listener, adds it to the xtd::diagnostics::trace::listeners collection, and sets the xtd::diagnostics::default_trace_listener::log_file_name property to the log file specified in the command-line arguments.

If an error is detected while processing the input parameter, or if the calc_binomial function throws an exception, the xtd::diagnostics::default_trace_listener::fail method logs and displays an error message. If the xtd::diagnostics::default_trace_listener::assert_ui_enabled property is false, the error message is also written to the console. When the result is calculated successfully, the xtd::diagnostics::default_trace_listener::write and xtd::diagnostics::default_trace_listener::write_line methods write the results to the log file.

The xtd::diagnostics::default_trace_listener::fail, xtd::diagnostics::default_trace_listener::write, and xtd::diagnostics::default_trace_listener::write_line methods cause trace information to be written only to the xtd::diagnostics::default_trace_listener. To write trace information to all listeners in the xtd::diagnostics::trace::listeners collection, use the xtd::diagnostics::trace::fail, xtd::diagnostics::trace::write, and xtd::diagnostics::trace::write_line methods of the xtd::diagnostics::trace class.
#include <xtd/xtd>
class binomial {
public:
// args(0) is the number of possibilities for binomial coefficients.
// args(1) is the file specification for the trace log file.
static auto main(const argument_collection& args) {
auto possibilities = .0l;
auto iter = .0l;
// Remove the original default trace listener.
// Create and add a new default trace listener.
diagnostics::trace::listeners().add(default_listener);
// Assign the log file specification from the command line, if entered.
if (args.length() >= 2)
default_listener->log_file_name(args[1]);
// Validate the number of possibilities argument.
if (args.length() >= 1) {
// Verify that the argument is a number within the correct range.
try {
const auto MAX_POSSIBILITIES = 99.0l;
possibilities = parse<decimal>(args[0]);
if (possibilities < 0 || possibilities > MAX_POSSIBILITIES)
throw new system_exception(string::format("The number of possibilities must be in the range 0..{}.", MAX_POSSIBILITIES));
} catch (const system_exception& ex) {
auto fail_message = string::format("\"{}\" is not a valid number of possibilities.", args[0]);
default_listener->fail(fail_message, ex.message());
if (!default_listener->assert_ui_enabled())
console::write_line(fail_message + "\n" + ex.message());
return;
}
} else {
// Report that the required argument is not present.
const auto ENTER_PARAM = "Enter the number of possibilities as a command line argument.";
default_listener->fail(ENTER_PARAM);
if (!default_listener->assert_ui_enabled())
console::write_line(ENTER_PARAM);
return;
}
for (iter = 0; iter <= possibilities; iter++) {
auto result = .0l;
auto binomial = string::empty_string;
// Compute the next binomial coefficient and handle all exceptions.
try {
result = calc_binomial(possibilities, iter);
} catch (const system_exception& ex) {
auto fail_message = string::format("An exception was raised when calculating Binomial( {}, {} ).", possibilities, iter);
default_listener->fail(fail_message, ex.message());
if (!default_listener->assert_ui_enabled())
console::write_line(fail_message + "\n" + ex.message());
return;
}
// Format the trace and console output.
binomial = string::format("Binomial( {0}, {1} ) = ", possibilities, iter);
default_listener->write(binomial);
default_listener->write_line(string::format("{}", result));
console::write_line("{0} {1}", binomial, result);
}
}
static decimal calc_binomial(decimal possibilities, decimal outcomes) {
// Calculate a binomial coefficient, and minimize the chance of overflow.
auto result = 1.0l;
auto iter = .0l;
for (iter = 1.0l; iter <= possibilities - outcomes; iter++) {
result *= outcomes + iter;
result /= iter;
}
return result;
}
};
startup_(binomial::main);
// This code produces the following output :
//
// Binomial( 20, 0 ) = 1
// Binomial( 20, 1 ) = 20
// Binomial( 20, 2 ) = 190
// Binomial( 20, 3 ) = 1140
// Binomial( 20, 4 ) = 4845
// Binomial( 20, 5 ) = 15504
// Binomial( 20, 6 ) = 38760
// Binomial( 20, 7 ) = 77520
// Binomial( 20, 8 ) = 125970
// Binomial( 20, 9 ) = 167960
// Binomial( 20, 10 ) = 184756
// Binomial( 20, 11 ) = 167960
// Binomial( 20, 12 ) = 125970
// Binomial( 20, 13 ) = 77520
// Binomial( 20, 14 ) = 38760
// Binomial( 20, 15 ) = 15504
// Binomial( 20, 16 ) = 4845
// Binomial( 20, 17 ) = 1140
// Binomial( 20, 18 ) = 190
// Binomial( 20, 19 ) = 20
// Binomial( 20, 20 ) = 1
virtual size_type length() const noexcept
Gets a size that represents the total number of elements in all the dimensions of the array.
Definition basic_array.hpp:183
void add(const type_t &item) override
Adds an object to the end of the xtd::collections::generic::list <type_t>.
Definition list.hpp:312
void remove_at(size_type index) override
Removes the element at the specified index of the xtd::collections::generic::list <type_t>.
Definition list.hpp:848
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static listener_collection & listeners()
Gets the collection of listeners that is monitoring the trace output.
virtual const xtd::string & message() const noexcept
Gets message associate to the exception.
The exception that is thrown when a method call is invalid for the object's current state.
Definition system_exception.hpp:18
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:168
long double decimal
Represents a decimal-precision floating-point number.
Definition decimal.hpp:23
ptr< type_t > new_ptr(args_t &&... args)
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
xtd::array< xtd::string > argument_collection
Represents the collection of arguments passed to the main entry point method.
Definition argument_collection.hpp:19
value_t parse(const std::string &str)
Convert a string into a type.
Definition parse.hpp:34

Public Constructors

 default_trace_listener ()
 Initializes a new instance of the default_trace_listener class with "default" as its xtd::diagnostics::default_trace_listener::name property value.
 
 default_trace_listener (const xtd::string log_file_name)
 Initializes a new instance of the default_trace_listener class with "default" as its xtd::diagnostics::default_trace_listener::name property value.
 

Public Properties

bool assert_ui_enabled () const noexcept
 Gets a value indicating whether the application is running in user-interface mode.
 
void assert_ui_enabled (bool assert_ui_enabled) noexcept
 Sets a value indicating whether the application is running in user-interface mode.
 
xtd::string log_file_name () const noexcept
 Gets the name of a log file to write trace or debug messages to.
 
void log_file_name (const xtd::string log_file_name) noexcept
 Sets the name of a log file to write trace or debug messages to.
 

Public Methods

void close () override
 When overridden in a derived class, closes the output stream so it no longer receives tracing or debugging output.
 
void flush () override
 When overridden in a derived class, flushes the output buffer.
 
void write (const xtd::string &message) override
 Writes the message to the listener you create when you implement the trace_listener class.
 
void write_line (const xtd::string &message) override
 Writes the message to the listener you create when you implement the trace_listener class followed by a line terminator.followed by a line terminator.
 
template<class object>
void write (const object &o)
 Writes the value of the object's ToString method to the listener you create when you implement the TraceListener class.
 
template<class object>
void write (const object &o, const xtd::string &category)
 Writes a category name and the value of the object's ToString method to the listener you create when you implement the TraceListener class.
 
template<class object>
void write_line (const object &o)
 Writes the value of the object's ToString method to the listener you create when you implement the TraceListener class.
 
template<class object>
void write_line (const object &o, const xtd::string &category)
 Writes a category name and the value of the object's ToString method to the listener you create when you implement the TraceListener class.
 

Additional Inherited Members

 trace_listener ()=default
 Initializes a new instance of the trace_listener class.
 
 trace_listener (const xtd::string &name)
 Initializes a new instance of the trace_listener class using the specified name as the listener.
 
uint32 indent_level () const noexcept
 Gets the indent level.
 
void indent_level (uint32 indent_level) noexcept
 Sets the indent level.
 
uint32 indent_size () const noexcept
 Gets the number of spaces in an indent.
 
void indent_size (uint32 indent_size) noexcept
 Sets the number of spaces in an indent.
 
virtual bool is_thread_safe () const noexcept
 Gets a value indicating whether the trace listener is thread safe.
 
const xtd::stringname () const noexcept
 Gets or sets a name for this TraceListener.
 
void name (const xtd::string &name) noexcept
 Sets a name for this TraceListener.
 
trace_options trace_output_options () const noexcept
 Gets the trace output options.
 
void trace_output_options (trace_options trace_output_options) noexcept
 Sets the trace output options.
 
virtual void fail (const xtd::string &message)
 Emits an error message to the listener you create when you implement the TraceListener class.
 
virtual void fail (const xtd::string &message, const xtd::string &detail_message)
 Emits the specified error message.
 
template<class objelassct>
void trace_data (const xtd::diagnostics::trace_event_cache &event_cache, const xtd::string &source, const xtd::diagnostics::trace_event_type &event_type, int32 id, const object &data)
 Writes trace information, a data object and event information to the listener specific output.
 
template<class objelassct>
void trace_data (const xtd::diagnostics::trace_event_cache &event_cache, const xtd::string &source, const xtd::diagnostics::trace_event_type &event_type, int32 id, const xtd::array< object > &data)
 Writes trace information, a data object and event information to the listener specific output.
 
template<class ... objects>
void trace_data (const xtd::diagnostics::trace_event_cache &event_cache, const xtd::string &source, const xtd::diagnostics::trace_event_type &event_type, int32 id, objects &&... data)
 Writes trace information, an array of data objects and event information to the listener specific output.
 
virtual void trace_event (const xtd::diagnostics::trace_event_cache &event_cache, const xtd::string &source, const xtd::diagnostics::trace_event_type &event_type, int32 id)
 Writes trace and event information to the listener specific output.
 
virtual void trace_event (const xtd::diagnostics::trace_event_cache &event_cache, const xtd::string &source, const xtd::diagnostics::trace_event_type &event_type, int32 id, const xtd::string &message)
 Writes trace information, a message, and event information to the listener specific output.
 
template<class ... objects>
void trace_event (const xtd::diagnostics::trace_event_cache &event_cache, const xtd::string &source, const xtd::diagnostics::trace_event_type &event_type, int32 id, const xtd::string &format, const objects &... args)
 Writes trace information, a formatted array of objects and event information to the listener specific output.
 
template<class activity_id_type>
void trace_transfer (const xtd::diagnostics::trace_event_cache &event_cache, const xtd::string &source, int32 id, const xtd::string &message, const activity_id_type &related_activity_id)
 Writes trace information, a message, a related activity identity and event information to the listener specific output.
 
template<class object>
void write (const object &o)
 Writes the value of the object's ToString method to the listener you create when you implement the TraceListener class.
 
template<class object>
void write (const object &o, const xtd::string &category)
 Writes a category name and the value of the object's ToString method to the listener you create when you implement the TraceListener class.
 
template<class object>
void write_line (const object &o)
 Writes the value of the object's ToString method to the listener you create when you implement the TraceListener class.
 
template<class object>
void write_line (const object &o, const xtd::string &category)
 Writes a category name and the value of the object's ToString method to the listener you create when you implement the TraceListener class.
 
 object ()=default
 Create a new instance of the ultimate base class object.
 
virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object.
 
virtual xtd::size get_hash_code () const noexcept
 Serves as a hash function for a particular type.
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
 
template<class object_t>
xtd::unique_ptr_object< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
virtual xtd::string to_string () const noexcept
 Returns a xtd::string that represents the current object.
 
template<class object_a_t, class object_b_t>
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 
template<class object_a_t, class object_b_t>
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are the same instance.
 
bool need_indent () const noexcept
 Gets a value indicating whether to indent the output.
 
void need_indent (bool need_indent) noexcept
 Sets a value indicating whether to indent the output.
 
void thread_safe (bool thread_safe) noexcept
 Sets a value indicating whether the trace listener is thread safe.
 
virtual void write_indent ()
 Writes the indent to the listener you create when you implement this class, and resets the NeedIndent property to false.
 
 abstract_object ()=default
 Initializes a new instance of the xtd::abstract_object class.
 

Constructor & Destructor Documentation

◆ default_trace_listener() [1/2]

xtd::diagnostics::default_trace_listener::default_trace_listener ( )

Initializes a new instance of the default_trace_listener class with "default" as its xtd::diagnostics::default_trace_listener::name property value.

Examples
The following code example removes the xtd::diagnostics::default_trace_listener provided by the application from the xtd::diagnostics::trace::listeners collection and then creates a new xtd::diagnostics::default_trace_listener and adds it to the xtd::diagnostics::trace::listeners collection.
// Remove the original default trace listener.
// Create and add a new default trace listener.
trace::listeners().push_back(default_listener);
// Assign the log file specification from the command line, if entered.
if (args.size() >= 2)
default_listener->log_file_name(args[1]);
xtd::shared_ptr_object< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.hpp:25
sptr< type_t > new_sptr(args_t &&... args)
xtd::new_sptr operator creates a xtd::sptr object.
Definition new_sptr.hpp:24
const_iterator begin() const
Returns an iterator to the beginning.
Definition read_only_span.hpp:183

◆ default_trace_listener() [2/2]

xtd::diagnostics::default_trace_listener::default_trace_listener ( const xtd::string log_file_name)

Initializes a new instance of the default_trace_listener class with "default" as its xtd::diagnostics::default_trace_listener::name property value.

Parameters
log_file_nameThe name of a log file to write trace or debug messages to.

Member Function Documentation

◆ assert_ui_enabled() [1/2]

bool xtd::diagnostics::default_trace_listener::assert_ui_enabled ( ) const
noexcept

Gets a value indicating whether the application is running in user-interface mode.

Returns
true if user-interface mode is enabled; otherwise, false.
Examples
The following code example calls a function that calls the xtd::diagnostics::default_trace_listener::fail method to log an error message if the function throws an exception. If the xtd::diagnostics::default_trace_listener::assert_ui_enabled property is false, the method also writes the error message to the console.
// Compute the next binomial coefficient and handle all exceptions.
result = calc_binomial(possibilities, iter);
} catch (const exception& ex) {
string fail_message = string::format("An exception was raised when calculating Binomial( {}, {} ).", possibilities, iter);
default_listener->fail(fail_message, ex.message());
if (!default_listener->assert_ui_enabled())
console::write_line(fail_message + "\n" + ex.message());
return;
}
Defines the base class for predefined exceptions in the xtd namespace.
Definition exception.hpp:29

◆ assert_ui_enabled() [2/2]

void xtd::diagnostics::default_trace_listener::assert_ui_enabled ( bool assert_ui_enabled)
noexcept

Sets a value indicating whether the application is running in user-interface mode.

Parameters
assert_ui_enabledtrue if user-interface mode is enabled; otherwise, false.
Examples
The following code example calls a function that calls the xtd::diagnostics::default_trace_listener::fail method to log an error message if the function throws an exception. If the xtd::diagnostics::default_trace_listener::assert_ui_enabled property is false, the method also writes the error message to the console.
// Compute the next binomial coefficient and handle all exceptions.
result = calc_binomial(possibilities, iter);
} catch (const exception& ex) {
string fail_message = string::format("An exception was raised when calculating Binomial( {}, {} ).", possibilities, iter);
default_listener->fail(fail_message, ex.message());
if (!default_listener->assert_ui_enabled())
console::write_line(fail_message + "\n" + ex.message());
return;
}

◆ log_file_name() [1/2]

xtd::string xtd::diagnostics::default_trace_listener::log_file_name ( ) const
noexcept

Gets the name of a log file to write trace or debug messages to.

Returns
The name of a log file to write trace or debug messages to.
Examples
The following code example creates a new xtd::diagnostics::default_trace_listener, adds it to the xtd::diagnostics::trace::listeners collection, and sets the xtd::diagnostics::default_trace_listener::log_file_name property to the log file specified in the command-line arguments.
// Create and add a new default trace listener.
trace::listeners().push_back(default_listener);
// Assign the log file specification from the command line, if entered.
if (args.size() >= 2)
default_listener->log_file_name(args[1]);

◆ log_file_name() [2/2]

void xtd::diagnostics::default_trace_listener::log_file_name ( const xtd::string log_file_name)
noexcept

Sets the name of a log file to write trace or debug messages to.

Parameters
log_file_nameThe name of a log file to write trace or debug messages to.
Examples
The following code example creates a new xtd::diagnostics::default_trace_listener, adds it to the xtd::diagnostics::trace::listeners collection, and sets the xtd::diagnostics::default_trace_listener::log_file_name property to the log file specified in the command-line arguments.
// Create and add a new default trace listener.
trace::listeners().push_back(default_listener);
// Assign the log file specification from the command line, if entered.
if (args.size() >= 2)
default_listener->log_file_name(args[1]);

◆ close()

void xtd::diagnostics::default_trace_listener::close ( )
overridevirtual

When overridden in a derived class, closes the output stream so it no longer receives tracing or debugging output.

Remarks
Use this method when the output is going to a file, such as to the TextWriterTraceListener. After a call to this method, you must reinitialize the object.

Reimplemented from xtd::diagnostics::trace_listener.

◆ flush()

void xtd::diagnostics::default_trace_listener::flush ( )
overridevirtual

When overridden in a derived class, flushes the output buffer.

Reimplemented from xtd::diagnostics::trace_listener.

◆ write() [1/3]

void xtd::diagnostics::default_trace_listener::write ( const xtd::string & message)
overridevirtual

Writes the message to the listener you create when you implement the trace_listener class.

Parameters
messageA string you want to write.

Implements xtd::diagnostics::trace_listener.

◆ write_line() [1/3]

void xtd::diagnostics::default_trace_listener::write_line ( const xtd::string & message)
overridevirtual

Writes the message to the listener you create when you implement the trace_listener class followed by a line terminator.followed by a line terminator.

Parameters
messageA string you want to write.

Implements xtd::diagnostics::trace_listener.

◆ write() [2/3]

template<class object>
void xtd::diagnostics::trace_listener::write ( const object & o)
inline

Writes the value of the object's ToString method to the listener you create when you implement the TraceListener class.

Parameters
oAn Object whose fully qualified class name you want to write.

◆ write() [3/3]

template<class object>
void xtd::diagnostics::trace_listener::write ( const object & o,
const xtd::string & category )
inline

Writes a category name and the value of the object's ToString method to the listener you create when you implement the TraceListener class.

Parameters
oAn Object whose fully qualified class name you want to write.
categoryA category name used to organize the output.

◆ write_line() [2/3]

template<class object>
void xtd::diagnostics::trace_listener::write_line ( const object & o)
inline

Writes the value of the object's ToString method to the listener you create when you implement the TraceListener class.

Parameters
oAn Object whose fully qualified class name you want to write.

◆ write_line() [3/3]

template<class object>
void xtd::diagnostics::trace_listener::write_line ( const object & o,
const xtd::string & category )
inline

Writes a category name and the value of the object's ToString method to the listener you create when you implement the TraceListener class.

Parameters
oAn Object whose fully qualified class name you want to write.
categoryA category name used to organize the output.

The documentation for this class was generated from the following file: