-
Notifications
You must be signed in to change notification settings - Fork 348
/
Copy pathmain.cc
108 lines (85 loc) · 4.03 KB
/
main.cc
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
/* Copyright (c) 2018 Stanford University
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/**
* This file demonstrates the usage of the NanoLog API through the
* implementation of simple benchmarking application that reports the
* average latency and throughput of the NanoLog system.
*/
#include <chrono>
// Required to use the NanoLog system
#include "NanoLogCpp17.h"
void runBenchmark();
// Optional: Import the NanoLog log levels into the current namespace; this
// allows the log levels (DEBUG, NOTICE, WARNING, ERROR) to be used without
// using the NanoLog namespace (i.e. NanoLog::DEBUG).
using namespace NanoLog::LogLevels;
int main(int argc, char** argv) {
// Optional: Set the output location for the NanoLog system. By default
// the log will be output to ./compressedLog
NanoLog::setLogFile("/tmp/logFile");
// Optional optimization: pre-allocates thread-local data structures
// needed by NanoLog. This can be invoked once per new
// thread that will use the NanoLog system.
NanoLog::preallocate();
// Optional: Set the minimum LogLevel that log messages must have to be
// persisted. Valid from least to greatest values are
// DEBUG, NOTICE, WARNING, ERROR
NanoLog::setLogLevel(NOTICE);
NANO_LOG(DEBUG, "This message wont be logged since it is lower "
"than the current log level.");
NANO_LOG(DEBUG, "Another message.");
// All the standard printf specifiers (except %n) can be used
char randomString[] = "Hello World";
NANO_LOG(NOTICE, "A string, pointer, number, and float: '%s', %p, %d, %f",
randomString,
&randomString,
512,
3.14159);
// Even with width and length specifiers
NANO_LOG(NOTICE, "Shortend String: '%5s' and shortend float %0.2lf",
randomString,
3.14159);
runBenchmark();
// Optional: Flush all pending log messages to disk
NanoLog::sync();
// Optional: Gather statics generated by NanoLog
std::string stats = NanoLog::getStats();
printf("%s", stats.c_str());
// Optional: Prints NanoLog configuration parameters
NanoLog::printConfig();
}
void runBenchmark() {
const uint64_t RECORDS = 1000;
std::chrono::high_resolution_clock::time_point start, stop;
double time_span;
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < RECORDS; ++i) {
NANO_LOG(NOTICE, "Simple log message with 0 parameters");
}
stop = std::chrono::high_resolution_clock::now();
time_span = std::chrono::duration_cast<std::chrono::duration<double>>(
stop - start).count();
printf("The total time spent invoking NANO_LOG with no parameters %lu "
"times took %0.2lf seconds (%0.2lf ns/message average)\r\n",
RECORDS, time_span, (time_span/RECORDS)*1e9);
start = std::chrono::high_resolution_clock::now();
// Flush all pending log messages to disk
NanoLog::sync();
stop = std::chrono::high_resolution_clock::now();
time_span = std::chrono::duration_cast<std::chrono::duration<double>>(
stop - start).count();
printf("Flushing the log statements to disk took an additional "
"%0.2lf secs\r\n", time_span);
}