Reference documentation for deal.II version GIT relicensing-891-g13379fae33 2024-06-22 22:40:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
No Matches
Classes | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
LogStream Class Reference

#include <deal.II/base/logstream.h>

Inheritance diagram for LogStream:
Inheritance graph


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)
LogStreamoperator<< (std::ostream &(*p)(std::ostream &))
std::size_t memory_consumption () const
template<class Archive >
void serialize (Archive &ar, const unsigned int version)
Subscriptor functionality

Classes derived from Subscriptor provide a facility to subscribe to this object. This is mostly used by the SmartPointer class.

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

Static Public Member Functions

static ::ExceptionBaseExcInUse (int arg1, std::string arg2, std::string arg3)
static ::ExceptionBaseExcNoSubscriber (std::string arg1, std::string arg2)

Private Types

using map_value_type = decltype(counter_map)::value_type
using map_iterator = decltype(counter_map)::iterator

Private Member Functions

std::stack< std::string > & get_prefixes () const
void print_line_head ()
std::ostringstream & get_stream ()
void check_no_subscribers () const noexcept

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
std::atomic< unsigned intcounter
std::map< std::string, unsigned intcounter_map
std::vector< std::atomic< bool > * > validity_pointers
const std::type_info * object_info

Static Private Attributes

static std::mutex mutex


template<typename T >
LogStreamoperator<< (LogStream &log, const T &t)

Detailed Description

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:

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.

LogStream and thread safety

In the vicinity of concurrent threads, LogStream behaves in the following manner:

Definition at line 80 of file logstream.h.

Member Typedef Documentation

◆ map_value_type

using Subscriptor::map_value_type = decltype(counter_map)::value_type

The data type used in counter_map.

Definition at line 229 of file subscriptor.h.

◆ map_iterator

using Subscriptor::map_iterator = decltype(counter_map)::iterator

The iterator type used in counter_map.

Definition at line 234 of file subscriptor.h.

Constructor & Destructor Documentation

◆ LogStream()

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 74 of file

◆ ~LogStream()

LogStream::~LogStream ( )


Definition at line 88 of file

Member Function Documentation

◆ attach()

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.

oAttach this output stream.
[in]print_job_idWhether or not the JobIdentifier for the current process should be printed to the stream.
[in]flagsFormat flags to set on the output stream o.

Definition at line 218 of file

◆ detach()

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 231 of file

◆ get_console()

std::ostream & LogStream::get_console ( )

Return the default stream (std_out).

Definition at line 239 of file

◆ get_file_stream()

std::ostream & LogStream::get_file_stream ( )

Return the file stream.

Definition at line 270 of file

◆ has_file()

bool LogStream::has_file ( ) const

Return true if file stream has already been attached, false otherwise.

Definition at line 281 of file

◆ get_prefix()

const std::string & LogStream::get_prefix ( ) const

Return the prefix string.

Definition at line 289 of file

◆ push()

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 302 of file

◆ pop()

void LogStream::pop ( )

Remove the last prefix added with push().

Definition at line 316 of file

◆ depth_console()

unsigned int LogStream::depth_console ( const unsigned int  n)

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 349 of file

◆ depth_file()

unsigned int LogStream::depth_file ( const unsigned int  n)

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 360 of file

◆ log_thread_id()

bool LogStream::log_thread_id ( const bool  flag)

Log the thread id.

Definition at line 371 of file

◆ precision()

std::streamsize LogStream::precision ( const std::streamsize  prec)

set the precision for the underlying stream and returns the previous stream precision. This function mimics

Definition at line 333 of file

◆ width()

std::streamsize LogStream::width ( const std::streamsize  wide)

set the width for the underlying stream and returns the previous stream width. This function mimics

Definition at line 341 of file

◆ flags()

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

Definition at line 325 of file

◆ operator<<()

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 124 of file

◆ memory_consumption()

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.

◆ get_prefixes()

std::stack< std::string > & LogStream::get_prefixes ( ) const

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 382 of file

◆ print_line_head()

void LogStream::print_line_head ( )

Print head of line.

Definition at line 402 of file

◆ get_stream()

std::ostringstream & LogStream::get_stream ( )

Internal wrapper around "thread local" outstreams. This private function will return the correct internal ostringstream buffer for operator<<.

Definition at line 247 of file

◆ subscribe()

void Subscriptor::subscribe ( std::atomic< bool > *const  validity,
const std::string &  identifier = "" 
) const

Subscribes a user of the object by storing the pointer validity. The subscriber may be identified by text supplied as identifier.

Definition at line 135 of file

◆ unsubscribe()

void Subscriptor::unsubscribe ( std::atomic< bool > *const  validity,
const std::string &  identifier = "" 
) const

Unsubscribes a user from the object.

The identifier and the validity pointer must be the same as the one supplied to subscribe().

Definition at line 155 of file

◆ n_subscriptions()

unsigned int Subscriptor::n_subscriptions ( ) const

Return the present number of subscriptions to this object. This allows to use this class for reference counted lifetime determination where the last one to unsubscribe also deletes the object.

Definition at line 300 of file subscriptor.h.

◆ list_subscribers() [1/2]

template<typename StreamType >
void Subscriptor::list_subscribers ( StreamType &  stream) const

List the subscribers to the input stream.

Definition at line 317 of file subscriptor.h.

◆ list_subscribers() [2/2]

void Subscriptor::list_subscribers ( ) const

List the subscribers to deallog.

Definition at line 203 of file

◆ serialize()

template<class Archive >
void Subscriptor::serialize ( Archive &  ar,
const unsigned int  version 

Read or write the data of this object to or from a stream for the purpose of serialization using the BOOST serialization library.

This function does not actually serialize any of the member variables of this class. The reason is that what this class stores is only who subscribes to this object, but who does so at the time of storing the contents of this object does not necessarily have anything to do with who subscribes to the object when it is restored. Consequently, we do not want to overwrite the subscribers at the time of restoring, and then there is no reason to write the subscribers out in the first place.

Definition at line 309 of file subscriptor.h.

◆ check_no_subscribers()

void Subscriptor::check_no_subscribers ( ) const

Check that there are no objects subscribing to this object. If this check passes then it is safe to destroy the current object. It this check fails then this function will either abort or print an error message to deallog (by using the AssertNothrow mechanism), but will not throw an exception.

Since this function is just a consistency check it does nothing in release mode.
If this function is called when there is an uncaught exception then, rather than aborting, this function prints an error message to the standard error stream and returns.

Definition at line 52 of file

Friends And Related Symbol Documentation

◆ operator<<

template<typename T >
LogStream & operator<< ( LogStream log,
const T &  t 

Output a constant something through LogStream:

We declare this operator as a non-member function so that it is possible to overload it with more specialized templated versions under C++11 overload resolution rules

Definition at line 404 of file logstream.h.

Member Data Documentation

◆ prefixes

Threads::ThreadLocalStorage<std::stack<std::string> > LogStream::prefixes

Stack of strings which are printed at the beginning of each line to allow identification where the output was generated.

Definition at line 320 of file logstream.h.

◆ parent_thread

std::thread::id LogStream::parent_thread

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 327 of file logstream.h.

◆ std_out

std::ostream* LogStream::std_out

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 334 of file logstream.h.

◆ file

std::ostream* LogStream::file

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 342 of file logstream.h.

◆ std_depth

unsigned int LogStream::std_depth

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 350 of file logstream.h.

◆ file_depth

unsigned int LogStream::file_depth

Same for the maximum depth of prefixes for output to a file.

Definition at line 355 of file logstream.h.

◆ print_thread_id

bool LogStream::print_thread_id

Flag for printing thread id.

Definition at line 360 of file logstream.h.

◆ at_newline

bool LogStream::at_newline

A flag indicating whether output is currently at a new line

Definition at line 365 of file logstream.h.

◆ outstreams

Threads::ThreadLocalStorage<std::shared_ptr<std::ostringstream> > LogStream::outstreams

We use our thread local storage facility to generate a stringstream for every thread that sends log messages.

Definition at line 384 of file logstream.h.

◆ counter

std::atomic<unsigned int> Subscriptor::counter

Store the number of objects which subscribed to this object. Initially, this number is zero, and upon destruction it shall be zero again (i.e. all objects which subscribed should have unsubscribed again).

The creator (and owner) of an object is counted in the map below if HE manages to supply identification.

We use the mutable keyword in order to allow subscription to constant objects also.

This counter may be read from and written to concurrently in multithreaded code: hence we use the std::atomic class template.

Definition at line 218 of file subscriptor.h.

◆ counter_map

std::map<std::string, unsigned int> Subscriptor::counter_map

In this map, we count subscriptions for each different identification string supplied to subscribe().

Definition at line 224 of file subscriptor.h.

◆ validity_pointers

std::vector<std::atomic<bool> *> Subscriptor::validity_pointers

In this vector, we store pointers to the validity bool in the SmartPointer objects that subscribe to this class.

Definition at line 240 of file subscriptor.h.

◆ object_info

const std::type_info* Subscriptor::object_info

Pointer to the typeinfo object of this object, from which we can later deduce the class name. Since this information on the derived class is neither available in the destructor, nor in the constructor, we obtain it in between and store it here.

Definition at line 248 of file subscriptor.h.

◆ mutex

std::mutex Subscriptor::mutex

A mutex used to ensure data consistency when accessing the mutable members of this class. This lock is used in the subscribe() and unsubscribe() functions, as well as in list_subscribers().

Definition at line 271 of file subscriptor.h.

The documentation for this class was generated from the following files: