diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 6c077380c..0a51053b1 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -50,6 +50,7 @@ add_example(krr_regression_ex) add_example(learning_to_track_ex) add_example(least_squares_ex) add_example(linear_manifold_regularizer_ex) +add_example(logger_custom_output_ex) add_example(logger_ex) add_example(logger_ex_2) add_example(matrix_ex) diff --git a/examples/logger_custom_output_ex.cpp b/examples/logger_custom_output_ex.cpp new file mode 100644 index 000000000..6916e43de --- /dev/null +++ b/examples/logger_custom_output_ex.cpp @@ -0,0 +1,73 @@ +// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt + +/* + + This is an example showing how to control where the dlib::logger sends its messages. + This is done by creating a "hook" class that is called whenever any of the loggers want + to log a message. The hook class then outputs the messages using any method you like. + + + Prior to reading this example, you should understand the basics of the dlib::logger. + So you should have already read the logger_ex.cpp and logger_ex_2.cpp example programs. + +*/ + + +#include + +using namespace dlib; +using namespace std; + +class my_hook +{ +public: + my_hook( + ) + { + fout.open("my_log_file.txt"); + } + + void log ( + const string& logger_name, + const log_level& ll, + const uint64 thread_id, + const char* message_to_log + ) + { + // Log all messages from any logger to our log file. + fout << ll << " ["<= LINFO) + cout << ll << " ["<