Flow123d  JB_release_tests-c4abd42
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/time_point.hh"
23 #include <time.h>
24 #include <random>
25 
26 using namespace std;
27 
28 /*******************************************************************
29  * implementation of LoggerOptions
30  */
31 
33  return *instance_;
34 }
35 
36 
37 
39  TimePoint t = TimePoint();
40  double seconds = t-LoggerOptions::start_time;
41  ASSERT(seconds > -numeric_limits<double>::epsilon())(seconds).error("Formating of negative time.");
42 
43  unsigned int h,m,s,ms;
44  unsigned int full_time = (int)(seconds * 1000); // in first step in miliseconds
45 
46  ms = full_time % 1000;
47  full_time /= 1000;
48  s = full_time % 60;
49  full_time /= 60;
50  m = full_time % 60;
51  h = full_time / 60;
52 
53  stringstream ss;
54  if (h<10) ss << "0";
55  ss << h << ":";
56  if (m<10) ss << "0";
57  ss << m << ":";
58  if (s<10) ss << "0";
59  ss << s << ".";
60  if (ms<100) ss << "0";
61  if (ms<10) ss << "0";
62  ss << ms;
63 
64  return ss.str();
65 }
66 
67 
69 
70 
72 
73 
75 : mpi_rank_(-1), init_flag_(InitFlag::uninitialize) {}
76 
77 
79  if (file_stream_.is_open()) {
80  file_stream_ << std::flush;
81  file_stream_.close();
82  }
83 }
84 
85 
86 void LoggerOptions::set_mpi_rank(int mpi_rank) {
87  ASSERT(init_flag_ == InitFlag::uninitialize).error("Setup MPI must be performed before setting logger file.");
88 
89  this->mpi_rank_ = mpi_rank;
90 }
91 
92 
93 std::string LoggerOptions::log_file_name(std::string log_file_base) {
94  ASSERT(init_flag_ == InitFlag::uninitialize).error("Recurrent initialization of logger file stream.");
95 
96  if ( (log_file_base.size()) == 0 || (log_file_base == "//") ) {
97  init_flag_ = InitFlag::no_log;
98  return "";
99  }
100 
101  int mpi_rank = this->mpi_rank_;
102  if (mpi_rank == -1) { // MPI is not set, random value is used
103  std::random_device rd;
104  std::mt19937 gen(rd());
105  std::uniform_int_distribution<int> dis(0, 999999);
106  mpi_rank = dis(gen);
107  WarningOut() << "Unset MPI rank, random value '" << mpi_rank << "' of rank will be used.\n";
108  }
109  std::stringstream file_name;
110  file_name << log_file_base << "." << mpi_rank << ".log";
111  return file_name.str();
112 }
113 
114 
115 void LoggerOptions::set_stream(std::string abs_path) {
116  ASSERT(init_flag_ == InitFlag::uninitialize).error("Recurrent initialization of logger file stream.");
117 
118  file_stream_.open(abs_path, ios_base::out);
119  if (! file_stream_.is_open())
120  THROW( ExcMessage() << EI_Message("Can not open logger file: '" + abs_path + "'!") );
121 
122  init_flag_ = InitFlag::initialize;
123 }
124 
125 
127  mpi_rank_ = -1;
128  init_flag_ = InitFlag::uninitialize;
129  if (file_stream_.is_open()) {
130  file_stream_ << std::flush;
131  file_stream_.close();
132  }
133 }
#define ASSERT(expr)
Definition: asserts.hh:351
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()
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.
std::ofstream file_stream_
Stream for storing logger messages to file.
InitFlag
Initialization flag of Logger.
Global macros to enhance readability and debugging, general constants.
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
#define WarningOut()
Macro defining 'warning' record of log.
Definition: logger.hh:278