Flow123d  3.9.0-be1f591da
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 }
LoggerOptions::start_time
static TimePoint start_time
Start time of program, allows you to specify the actual time of program (see format_hh_mm_ss method)
Definition: logger_options.hh:89
LoggerOptions::LoggerOptions
LoggerOptions()
Forbidden constructor.
Definition: logger_options.cc:73
ASSERT
#define ASSERT(expr)
Definition: asserts.hh:351
LoggerOptions::no_log_
bool no_log_
Turn off logger file output.
Definition: logger_options.hh:105
file_path.hh
FilePath
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
LoggerOptions::~LoggerOptions
~LoggerOptions()
Destructor.
Definition: logger_options.cc:77
FilePath::open_stream
void open_stream(Stream &stream) const
Definition: file_path.cc:211
MPI_Comm_rank
#define MPI_Comm_rank
Definition: mpi.h:236
LoggerOptions
Definition: logger_options.hh:55
LoggerOptions::get_instance
static LoggerOptions & get_instance()
Getter of singleton instance object.
Definition: logger_options.cc:31
LoggerOptions::get_mpi_rank
int get_mpi_rank()
Returns number of actual process, if MPI is not supported returns -1.
Definition: logger_options.cc:85
TimePoint
Definition: time_point.hh:69
LoggerOptions::file_stream_
std::ofstream file_stream_
Stream for storing logger messages to file.
Definition: logger_options.hh:95
LoggerOptions::init_
bool init_
Flag sign if logger is initialized by set_log_file method.
Definition: logger_options.hh:108
logger.hh
LoggerOptions::set_log_file
void set_log_file(std::string log_file_base)
Initialize instance object in format 'log_file_base.process.log'.
Definition: logger_options.cc:97
LoggerOptions::instance_
static LoggerOptions * instance_
Singleton instance.
Definition: logger_options.hh:92
MPI_Comm
int MPI_Comm
Definition: mpi.h:141
LoggerOptions::reset
void reset()
Reset MPI rank and log file name.
Definition: logger_options.cc:119
time_point.hh
global_defs.h
Global macros to enhance readability and debugging, general constants.
WarningOut
#define WarningOut()
Macro defining 'warning' record of log.
Definition: logger.hh:278
LoggerOptions::setup_mpi
int setup_mpi(MPI_Comm comm)
Set rank of actual process by MPI communicator.
Definition: logger_options.cc:90
LoggerOptions::mpi_rank_
int mpi_rank_
Actual process number.
Definition: logger_options.hh:102
FilePath::output_file
@ output_file
Definition: file_path.hh:69
LoggerOptions::format_hh_mm_ss
static std::string format_hh_mm_ss()
Definition: logger_options.cc:37
logger_options.hh