From d763696f6804a348e37b53e525868baf9b883cc9 Mon Sep 17 00:00:00 2001 From: Davis King Date: Sat, 26 Apr 2014 17:32:06 -0400 Subject: [PATCH] Added an example program showing how to setup a logger hook --- examples/CMakeLists.txt | 1 + examples/logger_custom_output_ex.cpp | 73 ++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 examples/logger_custom_output_ex.cpp 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 << " ["<