-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_stats_logger.cpp
122 lines (107 loc) · 2.82 KB
/
global_stats_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
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
#include <boost/format.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/trim_all.hpp>
#include "global_stats_logger.hpp"
using namespace std;
using namespace boost;
using namespace boost::algorithm;
const vector<string> EMPTY_VECTOR;
global_stats_logger::global_stats_logger(
const std::string& procfn, const std::string& logfn_fmt,
const int _start_row, const int _num_rows,
const int _start_col, const int _num_cols,
const std::string& header,
const std::vector<std::string>& names,
const bool _incremental,
const enum procfile_direction _dir, const std::string& _delim,
const bool multilog)
throw(num_cols_too_large, num_rows_too_large) :
procfile(procfn),
start_row(_start_row), num_rows(_num_rows),
start_col(_start_col), num_cols(_num_cols),
incremental(_incremental), dir(_dir), delim(_delim),
started(false),
logfiles(dir == PF_DIRECTION_ROW ? num_rows : num_cols),
prev(num_rows)
{
int x;
if (incremental) {
for (int i = 0; i < num_rows; i++)
prev[i].reserve(num_cols);
}
x = dir == PF_DIRECTION_ROW ? num_rows : num_cols;
if (x > 1 && !multilog) {
if (dir == PF_DIRECTION_ROW)
throw num_rows_too_large();
else
throw num_cols_too_large();
}
if (!multilog) {
logfiles[0].open(logfn_fmt);
logfiles[0] << header << endl;
}else{
for (int i = 0; i < x; i++) {
string filename;
if (names == EMPTY_VECTOR)
filename = (format(logfn_fmt) % i).str();
else
filename = (format(logfn_fmt) % names[i]).str();
logfiles[i].open(filename);
logfiles[i] << header << endl;
}
}
}
global_stats_logger::~global_stats_logger()
{
for (auto& l : logfiles)
l.close();
}
void global_stats_logger::update() throw(num_cols_too_large, num_rows_too_large)
{
int x;
if (!incremental && !started)
return;
procfile.seekg(0, ios::beg);
for (int i = 0; i < start_row; i++) {
string line;
getline(procfile, line);
}
for (int i = 0; i < num_rows; i++) {
string line;
vector<string> cols;
getline(procfile, line);
trim_all(line);
split(cols, line, is_any_of(delim));
if (cols.size() < (start_col + num_cols - 1)) {
throw num_cols_too_large();
}
for (int j = 0; j < num_cols; j++) {
long val = stol(cols[start_col + j]);
if (started) {
if (incremental) {
if (dir == PF_DIRECTION_ROW)
logfiles[i] << val - prev[i][j] << ",";
else
logfiles[j] << val - prev[i][j] << ",";
}else{
if (dir == PF_DIRECTION_ROW)
logfiles[i] << val << ",";
else
logfiles[j] << val << ",";
}
}
if (incremental)
prev[i][j] = val;
}
if (started && dir == PF_DIRECTION_ROW)
logfiles[i] << endl;
}
if (started && dir == PF_DIRECTION_COL)
for (int j = 0; j < num_cols; j++)
logfiles[j] << endl;
}
void global_stats_logger::start()
{
started = true;
}