Reference documentation for deal.II version 9.4.1
|
#include <deal.II/base/logstream.h>
Classes | |
class | Prefix |
Public Member Functions | |
LogStream () | |
~LogStream () override | |
void | attach (std::ostream &o, const bool print_job_id=true, const std::ios_base::fmtflags flags=std::ios::showpoint|std::ios::left) |
void | detach () |
std::ostream & | get_console () |
std::ostream & | get_file_stream () |
bool | has_file () const |
const std::string & | get_prefix () const |
void | push (const std::string &text) |
void | pop () |
unsigned int | depth_console (const unsigned int n) |
unsigned int | depth_file (const unsigned int n) |
bool | log_thread_id (const bool flag) |
std::streamsize | precision (const std::streamsize prec) |
std::streamsize | width (const std::streamsize wide) |
std::ios::fmtflags | flags (const std::ios::fmtflags f) |
LogStream & | operator<< (std::ostream &(*p)(std::ostream &)) |
std::size_t | memory_consumption () const |
Private Member Functions | |
std::stack< std::string > & | get_prefixes () const |
void | print_line_head () |
std::ostringstream & | get_stream () |
Private Attributes | |
Threads::ThreadLocalStorage< std::stack< std::string > > | prefixes |
std::thread::id | parent_thread |
std::ostream * | std_out |
std::ostream * | file |
unsigned int | std_depth |
unsigned int | file_depth |
bool | print_thread_id |
bool | at_newline |
Threads::ThreadLocalStorage< std::shared_ptr< std::ostringstream > > | outstreams |
Friends | |
template<typename T > | |
LogStream & | operator<< (LogStream &log, const T &t) |
Subscriptor functionality | |
Classes derived from Subscriptor provide a facility to subscribe to this object. This is mostly used by the SmartPointer class. | |
std::atomic< unsigned int > | counter |
std::map< std::string, unsigned int > | counter_map |
std::vector< std::atomic< bool > * > | validity_pointers |
const std::type_info * | object_info |
void | subscribe (std::atomic< bool > *const validity, const std::string &identifier="") const |
void | unsubscribe (std::atomic< bool > *const validity, const std::string &identifier="") const |
unsigned int | n_subscriptions () const |
template<typename StreamType > | |
void | list_subscribers (StreamType &stream) const |
void | list_subscribers () const |
template<class Archive > | |
void | serialize (Archive &ar, const unsigned int version) |
void | check_no_subscribers () const noexcept |
using | map_value_type = decltype(counter_map)::value_type |
using | map_iterator = decltype(counter_map)::iterator |
static std::mutex | mutex |
static ::ExceptionBase & | ExcInUse (int arg1, std::string arg2, std::string arg3) |
static ::ExceptionBase & | ExcNoSubscriber (std::string arg1, std::string arg2) |
A class that simplifies the process of execution logging. It does so by providing
The usual usage of this class is through the pregenerated object deallog
. Typical setup steps are:
deallog.depth_console(n)
: restrict output on screen to outer loops. deallog.attach(std::ostream)
: write logging information into a file. deallog.depth_file(n)
: restrict output to file to outer loops. Before entering a new phase of your program, e.g. a new loop, a new prefix can be set via LogStream::Prefix p("loopname");
. The destructor of the prefix will pop the prefix text from the stack.
Write via the <<
operator, deallog << "This is a log
notice";
will be buffered thread locally until a std::flush
or std::endl
is encountered, which will trigger a writeout to the console and, if set up, the log file.
In the vicinity of concurrent threads, LogStream behaves in the following manner:
<<
(or with one of the special member functions) is buffered in a thread-local storage. std::flush
or std::endl
will trigger a writeout to the console and (if attached) to the file stream. This writeout is sequentialized so that output from concurrent threads don't interleave. Definition at line 82 of file logstream.h.
LogStream::LogStream | ( | ) |
Standard constructor. The constructor sets the output stream to std::cout
and the depth to zero. (Use attach() and depth_console() to change this.)
Definition at line 75 of file logstream.cc.
|
override |
Destructor.
Definition at line 89 of file logstream.cc.
void LogStream::attach | ( | std::ostream & | o, |
const bool | print_job_id = true , |
||
const std::ios_base::fmtflags | flags = std::ios::showpoint | std::ios::left |
||
) |
Enable output to a second stream o
.
o | Attach this output stream. | |
[in] | print_job_id | Whether or not the JobIdentifier for the current process should be printed to the stream. |
[in] | flags | Format flags to set on the output stream o . |
Definition at line 219 of file logstream.cc.
void LogStream::detach | ( | ) |
Disable output to the second stream. You may want to call close
on the stream that was previously attached to this object.
Definition at line 232 of file logstream.cc.
std::ostream & LogStream::get_console | ( | ) |
Return the default stream (std_out
).
Definition at line 240 of file logstream.cc.
std::ostream & LogStream::get_file_stream | ( | ) |
Return the file stream.
Definition at line 271 of file logstream.cc.
bool LogStream::has_file | ( | ) | const |
Return true
if file stream has already been attached, false
otherwise.
Definition at line 282 of file logstream.cc.
const std::string & LogStream::get_prefix | ( | ) | const |
Return the prefix string.
Definition at line 290 of file logstream.cc.
void LogStream::push | ( | const std::string & | text | ) |
Push another prefix on the stack. Prefixes are automatically separated by a colon and there is a double colon after the last prefix.
A simpler way to add a prefix (without the manual need to add the corresponding pop()) is to use the LogStream::Prefix class. Using that class has the advantage that the corresponding pop() call is issued whenever the Prefix object goes out of scope – either at the end of the code block, at the nearest return
statement, or because an intermediate function call results in an exception that is not immediately caught.
Definition at line 303 of file logstream.cc.
void LogStream::pop | ( | ) |
Remove the last prefix added with push().
Definition at line 317 of file logstream.cc.
Maximum number of levels to be printed on the console. The default is 0, which will not generate any output. This function allows one to restrict console output to the highest levels of iterations. Only output with less than n
prefixes is printed. By calling this function with n=0
, no console output will be written. See step-3 for an example usage of this method.
The previous value of this parameter is returned.
Definition at line 350 of file logstream.cc.
Maximum number of levels to be written to the log file. The functionality is the same as depth_console
, nevertheless, this function should be used with care, since it may spoil the value of a log file.
The previous value of this parameter is returned.
Definition at line 361 of file logstream.cc.
Log the thread id.
Definition at line 372 of file logstream.cc.
std::streamsize LogStream::precision | ( | const std::streamsize | prec | ) |
set the precision for the underlying stream and returns the previous stream precision. This function mimics http://www.cplusplus.com/reference/ios/ios_base/precision/
Definition at line 334 of file logstream.cc.
std::streamsize LogStream::width | ( | const std::streamsize | wide | ) |
set the width for the underlying stream and returns the previous stream width. This function mimics http://www.cplusplus.com/reference/ios/ios_base/width/
Definition at line 342 of file logstream.cc.
std::ios::fmtflags LogStream::flags | ( | const std::ios::fmtflags | f | ) |
set the flags for the underlying stream and returns the previous stream flags. This function mimics http://www.cplusplus.com/reference/ios/ios_base/flags/
Definition at line 326 of file logstream.cc.
LogStream & LogStream::operator<< | ( | std::ostream &(*)(std::ostream &) | p | ) |
Treat ostream manipulators. This passes on the whole thing to the template function with the exception of the std::endl
manipulator, for which special action is performed: write the temporary stream buffer including a header to the file and std::cout
and empty the buffer.
An overload of this function is needed anyway, since the compiler can't bind manipulators like std::endl
directly to template arguments T
like in the previous general template. This is due to the fact that std::endl
is actually an overloaded set of functions for std::ostream
, std::wostream
, and potentially more of this kind. This function is therefore necessary to pick one element from this overload set.
Definition at line 125 of file logstream.cc.
std::size_t LogStream::memory_consumption | ( | ) | const |
Return an estimate for the memory consumption, in bytes, of this object. This is not exact (but will usually be close) because calculating the memory usage of trees (e.g., std::map
) is difficult.
|
private |
Internal wrapper around thread-local prefixes. This private function will return the correct internal prefix stack. More important, a new thread- local stack will be copied from the current stack of the "blessed" thread that created this LogStream instance (usually, in the case of deallog, the "main" thread).
Definition at line 383 of file logstream.cc.
|
private |
Print head of line.
Definition at line 403 of file logstream.cc.
|
private |
Internal wrapper around "thread local" outstreams. This private function will return the correct internal ostringstream buffer for operator<<.
Definition at line 248 of file logstream.cc.
Output a constant something through LogStream:
Definition at line 406 of file logstream.h.
|
mutableprivate |
Stack of strings which are printed at the beginning of each line to allow identification where the output was generated.
Definition at line 322 of file logstream.h.
|
private |
We record the thread id of the thread creating this object. We need this information to "steal" the current prefix from this "parent" thread on first use of deallog on a new thread.
Definition at line 329 of file logstream.h.
|
private |
Default stream, where the output is to go to. This stream defaults to std::cout
, but can be set to another stream through the constructor.
Definition at line 336 of file logstream.h.
|
private |
Pointer to a stream, where a copy of the output is to go to. Usually, this will be a file stream.
You can set and reset this stream by the attach
function.
Definition at line 344 of file logstream.h.
|
private |
Value denoting the number of prefixes to be printed to the standard output. If more than this number of prefixes is pushed to the stack, then no output will be generated until the number of prefixes shrinks back below this number.
Definition at line 352 of file logstream.h.
|
private |
Same for the maximum depth of prefixes for output to a file.
Definition at line 357 of file logstream.h.
|
private |
Flag for printing thread id.
Definition at line 362 of file logstream.h.
|
private |
A flag indicating whether output is currently at a new line
Definition at line 367 of file logstream.h.
|
private |
We use our thread local storage facility to generate a stringstream for every thread that sends log messages.
Definition at line 386 of file logstream.h.