-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.cpp
More file actions
118 lines (102 loc) · 3.34 KB
/
Log.cpp
File metadata and controls
118 lines (102 loc) · 3.34 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// General-purpose timestamp-based log utility
#include <ctime>
#include <ostream>
#include <sstream>
#include <string>
#include <sys/time.h>
#include "Log.hpp"
//==============================================================================
// Log constructor; initializes output_stream
//==============================================================================
Log::Log(std::ostream& output_stream,
bool flush_after_write) :
output_stream(&output_stream),
current_time_format(GMT),
flush_after_write(flush_after_write)
{
}
//==============================================================================
// Log destructor; does nothing
//==============================================================================
Log::~Log()
{
}
//==============================================================================
// Writes a standard log message
//==============================================================================
void Log::write(const std::string& message)
{
// Generate a timestamp
std::string timestamp;
generateTimestamp(timestamp);
// Write the log message
*output_stream << timestamp + " " + message + "\n";
if (flush_after_write)
{
flush();
}
}
//==============================================================================
// Writes a warning log message
//==============================================================================
void Log::writeWarning(const std::string& message)
{
// Write a normal log message but with WARNING prepended
write("WARNING - " + message);
}
//==============================================================================
// Writes an error log message
//==============================================================================
void Log::writeError(const std::string& message)
{
// Write a normal log message but with ERROR prepended
write("ERROR - " + message);
}
//==============================================================================
// Flushes the current output stream
//==============================================================================
void Log::flush()
{
output_stream->flush();
}
//==============================================================================
// Generates a timestamp to prepend to a log message
//==============================================================================
void Log::generateTimestamp(std::string& timestamp) const
{
// Get numeric timestamp
timeval ts;
if (gettimeofday(&ts, 0) == -1)
{
// Could not generate timestamp
timestamp = "[???]";
return;
}
// Convert ts into string timestamp
tm ts_local;
if (current_time_format == LOCAL)
{
localtime_r(&ts.tv_sec, &ts_local);
}
else
{
gmtime_r(&ts.tv_sec, &ts_local);
}
// Format ts_local
unsigned int max_ts_len = 30;
char ts_local_temp[max_ts_len];
strftime(ts_local_temp, max_ts_len, "%a %b %d %Y %X:", &ts_local);
// Save into string timestamp
timestamp = "[";
timestamp += ts_local_temp;
// Add microseconds
std::ostringstream out_stream;
out_stream.width(6);
out_stream.fill('0');
out_stream << ts.tv_usec;
timestamp += out_stream.str();
// Add timezone
strftime(ts_local_temp, max_ts_len, " %Z", &ts_local);
timestamp += ts_local_temp;
timestamp += "]";
}