Provides the default output methods and behavior for tracing.
- 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>
using namespace std;
class binomial {
public:
static void main(const vector<ustring>& args) {
trace::listeners().erase(trace::listeners().begin());
shared_ptr<default_trace_listener> default_listener = make_shared<default_trace_listener>();
trace::listeners().push_back(default_listener);
if (args.size() >= 2)
default_listener->log_file_name(args[1]);
if (args.size() >= 1) {
try {
const decimal MAX_POSSIBILITIES = 99;
possibilities = parse<decimal>(args[0]);
if (possibilities < 0 || possibilities > MAX_POSSIBILITIES)
throw new system_exception(ustring::format(
"The number of possibilities must be in the range 0..{}.", MAX_POSSIBILITIES));
ustring fail_message = ustring::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 {
const ustring 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++) {
try {
result = calc_binomial(possibilities, iter);
ustring fail_message = ustring::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;
}
binomial = ustring::format("Binomial( {0}, {1} ) = ", possibilities, iter);
default_listener->write(binomial);
default_listener->write_line(ustring::format("{}", result));
console::write_line("{0} {1}", binomial, result);
}
}
for (iter = 1; iter <= possibilities - outcomes; iter++) {
result *= outcomes + iter;
result /= iter;
}
return result;
}
};
Defines the base class for predefined exceptions in the xtd namespace.
Definition: system_exception.h:24
virtual const xtd::ustring & message() const noexcept
Get message associate to the exception.
Definition: system_exception.h:118
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
#define startup_(main_class)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition: startup.h:49
decimal_t decimal
Represents a decimal-precision floating-point number.
Definition: types.h:113
The xtd::diagnostics namespace provides classes that allow you to interact with system processes,...
Definition: assert_dialog_result.h:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
|
| 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.
|
|
bool | assert_ui_enabled () |
| Gets a value indicating whether the application is running in user-interface mode.
|
|
void | assert_ui_enabled (bool assert_ui_enabled) |
| Sets a value indicating whether the application is running in user-interface mode.
|
|
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.
|
|
xtd::ustring | log_file_name () const |
| Gets the name of a log file to write trace or debug messages to.
|
|
void | log_file_name (const xtd::ustring log_file_name) |
| Sets the name of a log file to write trace or debug messages to.
|
|
void | write (const xtd::ustring &message) override |
| Writes the message to the listener you create when you implement the trace_listener class.
|
|
void | write_line (const xtd::ustring &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.
|
|
| trace_listener ()=default |
| Initializes a new instance of the trace_listener class.
|
|
| trace_listener (const xtd::ustring &name) |
| Initializes a new instance of the trace_listener class using the specified name as the listener.
|
|
virtual void | close () |
| When overridden in a derived class, closes the output stream so it no longer receives tracing or debugging output.
|
|
virtual void | fail (const xtd::ustring &message) |
| Emits an error message to the listener you create when you implement the TraceListener class.
|
|
virtual void | fail (const xtd::ustring &message, const xtd::ustring &detail_message) |
| Emits the specified error message.
|
|
virtual void | flush () |
| When overridden in a derived class, flushes the output buffer.
|
|
unsigned int | indent_level () const |
| Gets the indent level.
|
|
void | indent_level (unsigned int indent_level) |
| Sets the indent level.
|
|
unsigned int | indent_size () const |
| Gets the number of spaces in an indent.
|
|
void | indent_size (unsigned int indent_size) |
| Sets the number of spaces in an indent.
|
|
virtual bool | is_thread_safe () const |
| Gets a value indicating whether the trace listener is thread safe.
|
|
const xtd::ustring & | name () const |
| Gets or sets a name for this TraceListener.
|
|
void | name (const xtd::ustring &name) |
| Sets a name for this TraceListener.
|
|
template<typename object > |
void | trace_data (const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int id, const object &data) |
| Writes trace information, a data object and event information to the listener specific output.
|
|
template<typename ... objects> |
void | trace_data (const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int id, const objects &... data) |
| Writes trace information, an array of data objects and event information to the listener specific output.
|
|
template<typename object > |
void | trace_data (const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int id, const std::vector< object > &data) |
| Writes trace information, a data object and event information to the listener specific output.
|
|
virtual void | trace_event (const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int id) |
| Writes trace and event information to the listener specific output.
|
|
template<typename ... objects> |
void | trace_event (const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int id, const xtd::ustring &format, const objects &... args) |
| Writes trace information, a formatted array of objects and event information to the listener specific output.
|
|
virtual void | trace_event (const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int id, const xtd::ustring &message) |
| Writes trace information, a message, and event information to the listener specific output.
|
|
const trace_options & | trace_output_options () const |
| Gets the trace output options.
|
|
void | trace_output_options (const trace_options &trace_output_options) |
| Sets the trace output options.
|
|
template<typename activity_id_type > |
void | trace_transfer (const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, int id, const xtd::ustring &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<typename 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<typename object > |
void | write (const object &o, const xtd::ustring &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.
|
|
virtual void | write (const xtd::ustring &message)=0 |
| Writes the message to the listener you create when you implement the trace_listener class.
|
|
template<typename 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<typename object > |
void | write_line (const object &o, const xtd::ustring &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.
|
|
virtual void | write_line (const xtd::ustring &message)=0 |
| 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.
|
|
| 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 size_t | get_hash_code () const noexcept |
| Serves as a hash function for a particular type.
|
|
template<typename object_t > |
std::unique_ptr< object_t > | memberwise_clone () const |
| Gets the type of the current instance.
|
|
virtual xtd::ustring | to_string () const noexcept |
| Returns a std::string that represents the current object.
|
|