Flow123d  release_2.1.0-87-gfbc1563
logger_options.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file logger_options.hh
15  * @brief
16  */
17 
18 #ifndef LOGGER_OPTIONS_HH_
19 #define LOGGER_OPTIONS_HH_
20 
21 #include <fstream>
22 #include <mpi.h>
23 #include "system/time_point.hh"
24 
25 
26 
27 /**
28  * Helper class defined logger output file and flags for setting of logger.
29  *
30  * Use singleton design pattern.
31  *
32  * Setting of logger is ensured by two methods: setup_mpi and set_log_file. Both methods are
33  * optional.
34  *
35  * - setup_mpi sets actual rank of procces according to given MPI communicator. If setting
36  * is not performed, rank has default value -1. For correct functioning this method must
37  * be called before set_log_file.
38  *
39  * - set_log_file allows set base name of logger output file (prefix). Name of logger file
40  * is created in format '<log_file_base>.<MPI_rank>.log'. If MPI rank is not set, it's
41  * generated random value (this option is not recommended). Method allows turn off logging
42  * if parameter log_file_base is set to empty string. If set_log_file method is not called,
43  * all logger messages are redirected to screen output.
44  *
45  * Example of complete initialization of logger:
46  *
47  @code
48  std::string log_file_prefix;
49  // ... set value of log_file_prefix
50  LoggerOptions::get_instance().setup_mpi(MPI_COMM_WORLD);
51  LoggerOptions::get_instance().set_log_file(log_file_prefix);
52  @endcode
53  */
55 {
56 public:
57  /// Getter of singleton instance object
58  static LoggerOptions& get_instance();
59 
60  /**
61  * Return actual time from the beginning of application runtime in format HH:MM:SS.SSS
62  */
63  static std::string format_hh_mm_ss();
64 
65  /// Returns number of actual process, if MPI is not supported returns -1.
66  int get_mpi_rank();
67 
68  /// Set rank of actual process by MPI communicator.
69  int setup_mpi(MPI_Comm comm);
70 
71  /// Initialize instance object in format 'log_file_base.process.log'.
72  void set_log_file(std::string log_file_base);
73 
74  /// Reset MPI rank and log file name
75  void reset();
76 
77  /// Check if singleton instance object is initialize.
78  inline bool is_init()
79  { return init_; }
80 
81  /// Destructor
83 private:
84  /// Forbidden constructor
85  LoggerOptions();
86 
87  /// Start time of program, allows you to specify the actual time of program (see \p format_hh_mm_ss method)
89 
90  /// Singleton instance
92 
93  /// Stream for storing logger messages to file.
94  std::ofstream file_stream_;
95 
96  /**
97  * @brief Actual process number
98  *
99  * Default value is set to -1 and indicates that MPI is not set
100  */
102 
103  /// Turn off logger file output
104  bool no_log_;
105 
106  /// Flag sign if logger is initialized by set_log_file method
107  bool init_;
108 
109  friend class Logger;
110 };
111 
112 
113 #endif /* LOGGER_OPTIONS_HH_ */
Class for storing logger messages.
Definition: logger.hh:132
~LoggerOptions()
Destructor.
static TimePoint start_time
Start time of program, allows you to specify the actual time of program (see format_hh_mm_ss method) ...
bool no_log_
Turn off logger file output.
int MPI_Comm
Definition: mpi.h:141
bool is_init()
Check if singleton instance object is initialize.
static std::string format_hh_mm_ss()
static LoggerOptions * instance_
Singleton instance.
int setup_mpi(MPI_Comm comm)
Set rank of actual process by MPI communicator.
void reset()
Reset MPI rank and log file name.
bool init_
Flag sign if logger is initialized by set_log_file method.
int get_mpi_rank()
Returns number of actual process, if MPI is not supported returns -1.
void set_log_file(std::string log_file_base)
Initialize instance object in format &#39;log_file_base.process.log&#39;.
static LoggerOptions & get_instance()
Getter of singleton instance object.
LoggerOptions()
Forbidden constructor.
int mpi_rank_
Actual process number.
std::ofstream file_stream_
Stream for storing logger messages to file.