-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexecutioninformation.cpp
More file actions
87 lines (73 loc) · 2.98 KB
/
executioninformation.cpp
File metadata and controls
87 lines (73 loc) · 2.98 KB
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
#include "executioninformation.hpp"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem.hpp>
#include <cstdio>
#include <ctime>
#include <random>
#include "entropy.hpp"
using namespace std;
string reconstitute_cmdline(int argc, const char **argv) {
string cmd;
for (int i = 0; i < argc; i++)
cmd += (i != 0 ? " " : "") + string(argv[i]);
return cmd;
}
ExecutionInformation::ExecutionInformation()
: ExecutionInformation("program_name", "unknown", "unknown", "unknown", "") {}
ExecutionInformation::ExecutionInformation(const string &name,
const string &version,
const string &branch,
const string &build, int argc,
const char **argv)
: ExecutionInformation(name, version, branch, build,
reconstitute_cmdline(argc, argv)) {}
ExecutionInformation::ExecutionInformation(const string &name,
const string &version,
const string &branch,
const string &build,
const string &cmd)
: program_name(boost::filesystem::path(name).filename().string()),
program_version(version),
git_branch(branch),
build_type(build),
cmdline(cmd),
datetime(),
directory(boost::filesystem::initial_path().string()) {
time_t rawtime;
time(&rawtime);
datetime = ctime(&rawtime);
datetime = datetime.substr(0, datetime.size() - 1);
}
string ExecutionInformation::name_and_version() const {
return program_name + " " + program_version + " [" + git_branch + " branch] - " + build_type + " build";
}
string generate_random_label(const string &prefix, size_t n_rnd_char) {
random_device rng;
uniform_int_distribution<char> r_char('a', 'z');
using namespace boost::posix_time;
string label = prefix;
// TODO perhaps add the process ID -> getpid()
if (verbosity >= Verbosity::debug)
cout << "Generating random label with prefix " << prefix << " and "
<< n_rnd_char << " random characters." << endl;
try {
ptime t = microsec_clock::universal_time();
string datetime = to_iso_extended_string(t) + "Z";
label += "_" + datetime;
} catch (...) {
cout << "WARNING: An error occurred while generating the date part of a label." << endl
<< "Although this shouldn't occur (something weird is going on with your system!)," << endl
<< "you can likely circumvent this issue by using the --output command line switch)" << endl
<< "to provide you own output label." << endl;
if (n_rnd_char < 5)
n_rnd_char = 5;
}
if (n_rnd_char > 0) {
label += "_";
for (size_t i = 0; i < n_rnd_char; i++)
label += r_char(rng);
}
if (verbosity >= Verbosity::debug)
cout << "Generated random label " << label << endl;
return label;
}