Flow123d  master-f44eb46
logger_options.cc
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.cc
15  * @brief
16  */
17 
18 
19 #include "system/logger_options.hh"
20 #include "system/logger.hh"
21 #include "system/global_defs.h"
22 #include "system/file_path.hh"
23 #include "system/time_point.hh"
24 #include <time.h>
25 #include <random>
26 
27 /*******************************************************************
28  * implementation of LoggerOptions
29  */
30 
32  return *instance_;
33 }
34 
35 
36 
38  TimePoint t = TimePoint();
39  double seconds = t-LoggerOptions::start_time;
40  ASSERT(seconds > -numeric_limits<double>::epsilon())(seconds).error("Formating of negative time.");
41 
42  unsigned int h,m,s,ms;
43  unsigned int full_time = (int)(seconds * 1000); // in first step in miliseconds
44 
45  ms = full_time % 1000;
46  full_time /= 1000;
47  s = full_time % 60;
48  full_time /= 60;
49  m = full_time % 60;
50  h = full_time / 60;
51 
52  stringstream ss;
53  if (h<10) ss << "0";
54  ss << h << ":";
55  if (m<10) ss << "0";
56  ss << m << ":";
57  if (s<10) ss << "0";
58  ss << s << ".";
59  if (ms<100) ss << "0";
60  if (ms<10) ss << "0";
61  ss << ms;
62 
63  return ss.str();
64 }
65 
66 
68 
69 
71 
72 
74 : mpi_rank_(-1), no_log_(false), init_(false) {}
75 
76 
78  if (file_stream_.is_open()) {
79  file_stream_ << std::flush;
80  file_stream_.close();
81  }
82 }
83 
84 
86  return mpi_rank_;
87 }
88 
89 
91  ASSERT(!init_).error("Setup MPI must be performed before setting logger file.");
92 
93  return MPI_Comm_rank(comm, &mpi_rank_);
94 }
95 
96 
97 void LoggerOptions::set_log_file(std::string log_file_base) {
98  ASSERT(!init_).error("Recurrent initialization of logger file stream.");
99 
100  if (log_file_base.size() == 0) { // empty string > no_log
101  no_log_ = true;
102  } else {
103  int mpi_rank = LoggerOptions::get_mpi_rank();
104  if (mpi_rank == -1) { // MPI is not set, random value is used
105  std::random_device rd;
106  std::mt19937 gen(rd());
107  std::uniform_int_distribution<int> dis(0, 999999);
108  mpi_rank = dis(gen);
109  WarningOut() << "Unset MPI rank, random value '" << mpi_rank << "' of rank will be used.\n";
110  }
111  std::stringstream file_name;
112  file_name << log_file_base << "." << mpi_rank << ".log";
114  }
115  init_ = true;
116 }
117 
118 
120  mpi_rank_ = -1;
121  no_log_ = false;
122  init_ = false;
123  if (file_stream_.is_open()) {
124  file_stream_ << std::flush;
125  file_stream_.close();
126  }
127 }
#define ASSERT(expr)
Definition: asserts.hh:351
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
@ output_file
Definition: file_path.hh:69
void open_stream(Stream &stream) const
Definition: file_path.cc:211
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'.
~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.
Global macros to enhance readability and debugging, general constants.
#define WarningOut()
Macro defining 'warning' record of log.
Definition: logger.hh:278
int MPI_Comm
Definition: mpi.h:141
#define MPI_Comm_rank
Definition: mpi.h:236