Flow123d  build_with_4.0.3-86a16ad
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 <string> // for string
24 class TimePoint;
25 
26 
27 
28 /**
29  * Helper class defined logger output file and flags for setting of logger.
30  *
31  * Use singleton design pattern.
32  *
33  * Setting of logger is ensured by two methods: setup_mpi and set_log_file. Both methods are
34  * optional.
35  *
36  * - setup_mpi sets actual rank of procces according to given MPI communicator. If setting
37  * is not performed, rank has default value -1. For correct functioning this method must
38  * be called before set_log_file.
39  *
40  * - set_log_file allows set base name of logger output file (prefix). Name of logger file
41  * is created in format '<log_file_base>.<MPI_rank>.log'. If MPI rank is not set, it's
42  * generated random value (this option is not recommended). Method allows turn off logging
43  * if parameter log_file_base is set to empty string. If set_log_file method is not called,
44  * all logger messages are redirected to screen output.
45  *
46  * Example of complete initialization of logger:
47  *
48  @code
49  std::string log_file_prefix;
50  // ... set value of log_file_prefix
51  LoggerOptions::get_instance().setup_mpi(MPI_COMM_WORLD);
52  LoggerOptions::get_instance().set_log_file(log_file_prefix);
53  @endcode
54  */
56 {
57 public:
58  /// Getter of singleton instance object
59  static LoggerOptions& get_instance();
60 
61  /**
62  * Return actual time from the beginning of application runtime in format HH:MM:SS.SSS
63  */
64  static std::string format_hh_mm_ss();
65 
66  /// Returns number of actual process, if MPI is not supported returns -1.
67  int get_mpi_rank();
68 
69  /// Set rank of actual process by MPI communicator.
70  int setup_mpi(MPI_Comm comm);
71 
72  /// Initialize instance object in format 'log_file_base.process.log'.
73  void set_log_file(std::string log_file_base);
74 
75  /// Reset MPI rank and log file name
76  void reset();
77 
78  /// Check if singleton instance object is initialize.
79  inline bool is_init()
80  { return init_; }
81 
82  /// Destructor
84 private:
85  /// Forbidden constructor
86  LoggerOptions();
87 
88  /// Start time of program, allows you to specify the actual time of program (see \p format_hh_mm_ss method)
90 
91  /// Singleton instance
93 
94  /// Stream for storing logger messages to file.
95  std::ofstream file_stream_;
96 
97  /**
98  * @brief Actual process number
99  *
100  * Default value is set to -1 and indicates that MPI is not set
101  */
103 
104  /// Turn off logger file output
105  bool no_log_;
106 
107  /// Flag sign if logger is initialized by set_log_file method
108  bool init_;
109 
110  friend class Logger;
111 };
112 
113 
114 #endif /* LOGGER_OPTIONS_HH_ */
int get_mpi_rank()
Returns number of actual process, if MPI is not supported returns -1.
bool no_log_
Turn off logger file output.
static TimePoint start_time
Start time of program, allows you to specify the actual time of program (see format_hh_mm_ss method)
LoggerOptions()
Forbidden constructor.
static LoggerOptions * instance_
Singleton instance.
int mpi_rank_
Actual process number.
static LoggerOptions & get_instance()
Getter of singleton instance object.
int setup_mpi(MPI_Comm comm)
Set rank of actual process by MPI communicator.
void set_log_file(std::string log_file_base)
Initialize instance object in format 'log_file_base.process.log'.
bool is_init()
Check if singleton instance object is initialize.
~LoggerOptions()
Destructor.
void reset()
Reset MPI rank and log file name.
static std::string format_hh_mm_ss()
bool init_
Flag sign if logger is initialized by set_log_file method.
std::ofstream file_stream_
Stream for storing logger messages to file.
Class for storing logger messages.
Definition: logger.hh:135
int MPI_Comm
Definition: mpi.h:141