-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogger.cpp
More file actions
94 lines (76 loc) · 2.03 KB
/
logger.cpp
File metadata and controls
94 lines (76 loc) · 2.03 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
88
89
90
91
92
93
94
#include "logger.h"
#include <fstream>
#include "system.h"
#include <sstream>
#include <execinfo.h> // backtrace
#include <iostream>
using namespace std;
void log(string severity, string message)
{
ofstream f;
f.open("car-log.txt", ofstream::out | ofstream::app );
f << time_string() << "," << severity << "," << message << endl;
cout << time_string() << "," << severity << "," << message << endl;
}
void log_error(string message)
{
static string severity = "ERROR";
log(severity, message);
}
void log_warning(string message)
{
static string severity = "WARNING";
log(severity, message);
}
void log_info(string message)
{
static string severity = "INFO";
log(severity, message);
}
void log_trace(string message)
{
static string severity = "TRACE";
log(severity, message);
}
log_warning_if_duration_exceeded::log_warning_if_duration_exceeded(string label, chrono::duration<double> max_time)
{
start_time = std::chrono::high_resolution_clock::now();
this->label = label;
this->max_time = max_time;
}
log_warning_if_duration_exceeded::~log_warning_if_duration_exceeded()
{
std::chrono::duration<double> duration = std::chrono::high_resolution_clock::now() - start_time;
if(duration > max_time) {
stringstream ss;
ss << "time exceeded for " << label
<< ". expected less than " << max_time.count()
<< ", was " << duration.count();
log_warning(ss.str());
}
}
log_entry_exit::log_entry_exit(string scope_label)
{
this->scope_label = scope_label;
log_info("entering " + scope_label);
}
log_entry_exit::~log_entry_exit()
{
log_info("exiting " + scope_label);
}
void log_backtrace() {
size_t max_count = 25;
void * array[max_count];
// get void*'s for all entries on the stack
size_t count = backtrace(array, max_count);
char **strings = backtrace_symbols(array, count);
// print out all the frames to stderr
for(int i = 0; i < count; ++i) {
log_error(strings[i]);
}
free(strings);
}
void throw_and_log(string error) {
log_error((string)"Throwing: " + error);
log_backtrace();
}