forked from amkozlov/raxml-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.cpp
127 lines (101 loc) · 2.55 KB
/
log.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "log.hpp"
#include "common.h"
using namespace std;
void LogStream::add_stream(std::ostream* stream)
{
if (stream)
{
*stream << fixed;
_streams.push_back(stream);
}
}
Logging::Logging() : _log_level(LogLevel::info), _logfile()
{
/* add file stream to the list, even though it's to attached to a file yet */
_full_stream.add_stream(&_logfile);
}
Logging& Logging::instance()
{
static Logging instance;
return instance;
}
LogStream& Logging::logstream(LogLevel level, bool worker)
{
if ((ParallelContext::master() || (ParallelContext::group_master() && worker))
&& level <= _log_level)
return _full_stream;
else
return _empty_stream;
}
void Logging::set_log_filename(const std::string& fname, ios_base::openmode mode)
{
_logfile.open(fname, mode);
if (_logfile.fail())
{
throw runtime_error("Cannot open the log file for writing: " + fname +
"\nPlease make sure directory exists and you have write permissions for it!");
}
}
void Logging::add_log_stream(std::ostream* stream)
{
if (stream)
_full_stream.add_stream(stream);
}
void Logging::log_level(LogLevel level)
{
_log_level = level;
}
LogLevel Logging::log_level() const
{
return _log_level;
}
void Logging::precision(const LogElementMap& prec)
{
_precision = prec;
}
void Logging::precision(unsigned int prec, LogElement elem)
{
_precision[elem] = prec;
}
unsigned int Logging::precision(LogElement elem) const
{
if (_precision.count(elem))
return _precision.at(elem);
else if (_precision.count(LogElement::all))
return _precision.at(LogElement::all);
else
return RAXML_DEFAULT_PRECISION;
}
Logging& logger()
{
return Logging::instance();
}
TimeStamp::TimeStamp() : secs(global_timer().elapsed_seconds())
{
};
LogStream& operator<<(LogStream& logstream, std::ostream& (*pf)(std::ostream&))
{
for (auto s: logstream.streams())
*s << pf;
return logstream;
}
LogStream& operator<<(LogStream& logstream, const time_t& t)
{
logstream << sysutil_fmt_time(t);
return logstream;
}
LogStream& operator<<(LogStream& logstream, const TimeStamp& ts)
{
const unsigned int hh = ts.secs / 3600;
const unsigned int mm = (ts.secs - hh * 3600) / 60;
const unsigned int ss = (ts.secs - hh * 3600 - mm * 60);
logstream << setfill('0') << setw(2) << hh << ":" <<
setw(2) << mm << ":" <<
setw(2) << ss;
return logstream;
}
LogStream& operator<<(LogStream& logstream, const ProgressInfo& prog)
{
logstream << "[" << TimeStamp() << " " << FMT_LH(prog.loglh) << "] ";
return logstream;
}