-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
138 lines (100 loc) · 4.51 KB
/
main.cpp
File metadata and controls
138 lines (100 loc) · 4.51 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <cstdlib>
#include "LoggingPch.h"
#include "SimpleLogger.h"
using namespace core;
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
const char* lines[] = {
"10",
"-3",
"37",
};
class Reader {
public:
void ReadFile(const char* path, unsigned short*& output, unsigned int& outputCount) {
sinfo("Reader", "Reading from file %s", path);
outputCount = COUNT_OF(lines);
sdebug("Reader", "File %s has %d lines", path, outputCount);
strace("Reader", "Creating output array of length %d", outputCount);
output = new unsigned short[outputCount];
for(int i = 0; i < outputCount; ++i)
{
strace("Reader", "Reading line %d", i);
sdebug("Reader", "Line %d is %s", i, lines[i]);
auto asInt = atoi(lines[i]);
strace("Reader", "Converted %s to %d", lines[i], asInt);
output[i] = asInt;
strace("Reader", "Stored %d in array as %u", asInt, output[i]);
}
}
};
class AverageAction {
public:
unsigned short* inputs;
unsigned int inputCount;
unsigned int CalculateAverageFromFile(const char* path) {
Reader reader;
sinfo("Average", "Reading inputs from file");
reader.ReadFile("/usr/tmp/file.txt", inputs, inputCount);
sinfo("Average", "Read %d inputs", inputCount);
return Execute();
}
unsigned int Execute() {
sinfo("Average", "Averaging %u inputs", inputCount);
int sum = 0;
for (int i = 0; i < inputCount; ++i) {
sdebug("Average", "Inputs %d = %u", i, inputs[i]);
sum += inputs[i];
}
strace("Average", "Sum is %u", sum);
int average = sum / inputCount;
strace("Average", "Average is %u", sum);
return average;
}
};
void Test() {
AverageAction averageAction;
auto average = averageAction.CalculateAverageFromFile("/usr/tmp/file.txt");
if(average > 100) {
swarn("Average", "Average is unusually high!");
}
sinfo("Average", "Average is: %d -------------\n", average);
printf("\n");
}
int main() {
SimpleLogger::Create();
int testCount = 3;
sforce("Example", "By default, the logger is not subscribed to any categories");
sforce("Example", "But force logs are always shown");
sforce("Example", "Lets subscribe to 'Example' so we can see logs without using force");
SimpleLogger::Instance().SubscribeTo("Example");
sinfo("Example", "Ah that's better! Lets run a quick averaging test");
Test();
sinfo("Example", "Hmm, looks like there is something wrong with the average.");
sinfo("Example", "By default warnings and above are shown regardless of subscriton.");
sinfo("Example", "But there is no output for the averager, so lets subscribe to that channel");
SimpleLogger::Instance().SubscribeTo("Average");
Test();
sinfo("Example", "Hmm, not much there, so lets increase the verbosity of the log");
SimpleLogger::Instance().hideBelow = Logger::kDebug;
Test();
sinfo("Example", "Ah looks like the input is wrong.");
sinfo("Example", "At this point its not clear what is causing the error, so lets subscribe to all channels.");
SimpleLogger::Instance().SubscribeToAll();
Test();
sinfo("Example", "Looks like there's an issue with the reader.");
sinfo("Example", "Seems like the reader has a unsigned/signed issue.");
sinfo("Example", "Lets increase the verbosity of the log once more.");
sinfo("Example", "In addition, since we know that the issue isn't with the Averager,");
sinfo("Example", "We don't need to see it's logs anymore. We can unsubscribe from the channel");
sinfo("Example", "but since we're subscribe to all channels it will still output.");
sinfo("Example", "Instead, let's ignore the channel, which will also mute the initial warning.");
SimpleLogger::Instance().hideBelow = Logger::kTrace;
SimpleLogger::Instance().ignore("Average");
Test();
sinfo("Example", "Bingo! Now we know that there's a signing issue, it's storing -3 as 65533. Bug found!\n");
sinfo("Example", "This example isn't exactly as you would see it in the real world, but an illustrative example. You would likely\n"
"have a Logger that outputs to the TTY for what's important to the local programmer, and a Logger\n"
"that saves to a file with all log statements, and a Logger that displays frequenly changed values to the\n"
"screen");
return 0;
}