Flow123d  JB_release_tests-c4abd42
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  int mpi_rank;
51  // ... set value of log_file_prefix
52  MPI_Comm_rank(comm, &mpi_rank);
53  LoggerOptions::get_instance().setup_mpi(mpi_rank);
54  LoggerOptions::get_instance().set_log_file(log_file_prefix);
55  @endcode
56  */
58 {
59 public:
60  /// Initialization flag of Logger.
61  enum InitFlag {
65  };
66 
67  /// Getter of singleton instance object
68  static LoggerOptions& get_instance();
69 
70  /**
71  * Return actual time from the beginning of application runtime in format HH:MM:SS.SSS
72  */
73  static std::string format_hh_mm_ss();
74 
75  /// Returns number of actual process, if MPI is not supported returns -1.
76  inline int get_mpi_rank() const {
77  return mpi_rank_;
78  }
79 
80  /// Set rank of actual process.
81  void set_mpi_rank(int mpi_rank);
82 
83  /// Reset MPI rank and log file name
84  void reset();
85 
86  /// Check if singleton instance object is initialize.
88  { return init_flag_; }
89 
90  /// Set \p init_ flag.
91  void set_stream(std::string abs_path);
92 
93  /// Create unique log file name
94  std::string log_file_name(std::string log_file_base);
95 
96  /// Destructor
98 private:
99  /// Forbidden constructor
100  LoggerOptions();
101 
102  /// Start time of program, allows you to specify the actual time of program (see \p format_hh_mm_ss method)
104 
105  /// Singleton instance
107 
108  /// Stream for storing logger messages to file.
109  std::ofstream file_stream_;
110 
111  /**
112  * @brief Actual process number
113  *
114  * Default value is set to -1 and indicates that MPI is not set
115  */
117 
118  /// Flag sign if logger is initialized
120 
121  friend class Logger;
122 };
123 
124 
125 #endif /* LOGGER_OPTIONS_HH_ */
void set_stream(std::string abs_path)
Set init_ flag.
static TimePoint start_time
Start time of program, allows you to specify the actual time of program (see format_hh_mm_ss method)
LoggerOptions::InitFlag init_flag()
Check if singleton instance object is initialize.
LoggerOptions()
Forbidden constructor.
static LoggerOptions * instance_
Singleton instance.
int mpi_rank_
Actual process number.
static LoggerOptions & get_instance()
Getter of singleton instance object.
~LoggerOptions()
Destructor.
void reset()
Reset MPI rank and log file name.
void set_mpi_rank(int mpi_rank)
Set rank of actual process.
std::string log_file_name(std::string log_file_base)
Create unique log file name.
static std::string format_hh_mm_ss()
InitFlag init_flag_
Flag sign if logger is initialized.
int get_mpi_rank() const
Returns number of actual process, if MPI is not supported returns -1.
std::ofstream file_stream_
Stream for storing logger messages to file.
InitFlag
Initialization flag of Logger.
Class for storing logger messages.
Definition: logger.hh:135