-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cpp
91 lines (74 loc) · 3.14 KB
/
Logger.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
//
// Created by André Schnabel on 10.10.17.
//
#include <iostream>
#include <cmath>
#include "Logger.h"
#include "Utils.h"
using namespace std;
namespace Utils {
Logger *Logger::instance = nullptr;
Logger::Logger(const string &_logName, LogMode _mode) : logName(_logName), f(logName + "Log.txt"), mode(_mode) {
}
void Logger::log(LogLevel level, const string &message) {
if (mode == LogMode::QUIET || (level == LogLevel::INFO && mode == LogMode::MEDIUM)) return;
string timestr = formattedNow();
f << "[" << logName << ", " << timestr << "]: " << message << endl;
f.flush();
cout << "[" << logName << ", " << timestr << "]: " << message << endl;
}
Logger *Logger::getInstance() {
if (!instance)
instance = new Logger("MainLogger", Utils::Logger::LogMode::VERBOSE);
return instance;
}
Tracer::Tracer(const string &filePrefix, TraceMode _traceMode) : f(filePrefix + ".txt"), traceMode(_traceMode), lastNumSchedules(0) {
sw.start();
lupdate = chrono::system_clock::now();
last_slvtime = 0.0;
if(!f.is_open())
throw runtime_error("Unable to create " + filePrefix + ".txt!");
f << "slvtime,bks_objval,nschedules,nindividuals\n";
trace(0.0, 0.0f, 0, 0);
}
Tracer::~Tracer() {
f.close();
}
void Tracer::trace(double slvtime, float bks_objval, int nschedules, int nindividuals, bool trunc_secs) {
double insecs = (slvtime / 1000.0);
if (trunc_secs) insecs = trunc(insecs);
// FIXME: Specify number of decimal places!
// FIXME?: Write in stringbuffer and only write out to disk at the end?
f << (boost::format("%.2f") % insecs) << "," << bks_objval << "," << nschedules << "," << nindividuals << endl;
}
#define FIRST_EXCEED(curVal, lastVal, threshold) (curVal >= threshold && lastVal < threshold)
void Tracer::countTrace(float bks_objval, int nschedules, int nindividuals) {
if(traceMode == TraceMode::ONLY_INTERVAL) return;
if(FIRST_EXCEED(nschedules, lastNumSchedules, 1000)
|| FIRST_EXCEED(nschedules, lastNumSchedules, 5000)
|| FIRST_EXCEED(nschedules, lastNumSchedules, 50000)) {
trace(sw.look(), bks_objval, nschedules, nindividuals);
}
lastNumSchedules = nschedules;
}
void Tracer::intervalTrace(float bks_objval, int nschedules, int nindividuals) {
if (traceMode == TraceMode::ONLY_COUNT) return;
double slvtime = sw.look();
double deltat = chrono::duration<double, milli>(chrono::system_clock::now() - lupdate).count();
if(slvtime < 1000.0 && deltat >= MSECS_BETWEEN_TRACES_SHORT) {
lupdate = chrono::system_clock::now();
trace(slvtime, bks_objval, nschedules, nindividuals);
} else if(slvtime >= 1000.0 && last_slvtime < 1000.0) {
lupdate = chrono::system_clock::now();
trace(slvtime, bks_objval, nschedules, nindividuals, true);
} else if(slvtime >= 1000.0 && deltat >= MSECS_BETWEEN_TRACES_LONG) {
//cout << "Nodes visited = " << nodeCtr << ", Boundings = " << boundCtr << ", Opt = " << lb << ", Time = " << (boost::format("%.2f") % (sw.look() / 1000.0)) << endl;
lupdate = chrono::system_clock::now();
trace(slvtime, bks_objval, nschedules, nindividuals, true);
}
last_slvtime = slvtime;
}
void Tracer::setTraceMode(TraceMode _traceMode) {
traceMode = _traceMode;
}
}