Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
d763696f
Commit
d763696f
authored
Apr 26, 2014
by
Davis King
Browse files
Added an example program showing how to setup a logger hook
parent
ff55b029
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
0 deletions
+74
-0
examples/CMakeLists.txt
examples/CMakeLists.txt
+1
-0
examples/logger_custom_output_ex.cpp
examples/logger_custom_output_ex.cpp
+73
-0
No files found.
examples/CMakeLists.txt
View file @
d763696f
...
...
@@ -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
)
...
...
examples/logger_custom_output_ex.cpp
0 → 100644
View file @
d763696f
// 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 <dlib/logger.h>
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
<<
" ["
<<
thread_id
<<
"] "
<<
logger_name
<<
": "
<<
message_to_log
<<
endl
;
// But only log messages that are of LINFO priority or higher to the console.
if
(
ll
>=
LINFO
)
cout
<<
ll
<<
" ["
<<
thread_id
<<
"] "
<<
logger_name
<<
": "
<<
message_to_log
<<
endl
;
}
private:
ofstream
fout
;
};
int
main
()
{
my_hook
hook
;
// This tells all dlib loggers to send their logging events to the hook object. That
// is, any time a logger generates a message it will call hook.log() with the message
// contents. Additionally, hook.log() will also only be called from one thread at a
// time so it is safe to use this kind of hook in a multi-threaded program with many
// loggers in many threads.
set_all_logging_output_hooks
(
hook
);
// It should also be noted that the hook object must not be destructed while the
// loggers are still in use. So it is a good idea to declare the hook object
// somewhere where it will live the entire lifetime of the program, as we do here.
logger
dlog
(
"main"
);
// Tell the dlog logger to emit a message for all logging events rather than its
// default behavior of only logging LERROR or above.
dlog
.
set_level
(
LALL
);
// All these message go to my_log_file.txt, but only the last two go to the console.
dlog
<<
LDEBUG
<<
"This is a debugging message."
;
dlog
<<
LINFO
<<
"This is an informational message."
;
dlog
<<
LERROR
<<
"An error message!"
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment