Skip to content

Commit 3b475f4

Browse files
Merge pull request #214 from uorocketry/fernando_n.logger_timestamp
Add Custom logging formatter
2 parents 94cbfda + 1a71f18 commit 3b475f4

File tree

12 files changed

+66
-28
lines changed

12 files changed

+66
-28
lines changed

src/IO/ArduinoEncoder.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
#pragma once
22

33
#include "cobs.h"
4-
#include "helpers/Helper.h"
54
#include <memory>
65
#include <spdlog/spdlog.h>
76

87
class ArduinoEncoder
98
{
109
public:
1110
template <typename T>
12-
static helper::SharedArray<char> encode(const T &dataOut);
11+
static SharedArray<char> encode(const T &dataOut);
1312

1413
template <typename T>
1514
static T decode(const char *buffer, int length);
@@ -20,7 +19,7 @@ class ArduinoEncoder
2019
* applied to it, and INCLUDES the ending 0x0 byte, so it can be sent as is.
2120
*/
2221
template <typename T>
23-
helper::SharedArray<char> ArduinoEncoder::encode(const T &dataOut)
22+
SharedArray<char> ArduinoEncoder::encode(const T &dataOut)
2423
{
2524
// Convert from Protobuf to byte array
2625
size_t protoSize = dataOut.ByteSizeLong();
@@ -37,7 +36,7 @@ helper::SharedArray<char> ArduinoEncoder::encode(const T &dataOut)
3736
cobs_encode(&cobsData[0], cobsSize, &protoData[0], protoSize);
3837
cobsData[cobsSize - 1] = 0x0;
3938

40-
return helper::SharedArray<char>{cobsData, static_cast<size_t>(cobsSize)};
39+
return SharedArray<char>{cobsData, static_cast<size_t>(cobsSize)};
4140
}
4241

4342
/**

src/IO/ArduinoProxy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void ArduinoProxy::send(const RocketryProto::ArduinoIn &data)
120120
{
121121
std::lock_guard<std::mutex> lockGuard(serialMutex);
122122

123-
helper::SharedArray<char> encodedData = ArduinoEncoder::encode(data);
123+
SharedArray<char> encodedData = ArduinoEncoder::encode(data);
124124

125125
serialPutchar(fd, 0);
126126
for (int i = 0; i < encodedData.length; i++)

src/IO/InterfaceImpl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "IO/TestingSensors.h"
66
#include "InterfaceImpl.h"
77
#include "common/pch.h"
8+
#include "common/utils.h"
89
#include "data/UOSMData.h"
910
#include <chrono>
1011
#include <iostream>
@@ -88,8 +89,7 @@ bool InterfaceImpl::updateInputs()
8889
{
8990
latestState = std::make_shared<StateData>();
9091

91-
latestState->timeStamp =
92-
std::chrono::duration_cast<time_point::duration>(std::chrono::steady_clock::now().time_since_epoch()).count();
92+
latestState->timeStamp = utils::getMonotonicTimeStamp();
9393

9494
#if USE_SBG == 1
9595
latestState->sbg = mySbgSensor.getData();

src/IO/SensorLogger.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
#include "SensorLogger.h"
55

6-
#include "helpers/Helper.h"
7-
86
#include "boost/filesystem.hpp"
7+
#include "common/utils.h"
98
#include <iostream>
109
#include <mutex>
1110
#include <spdlog/spdlog.h>
@@ -28,7 +27,7 @@ bool SensorLogger::isInitialized()
2827

2928
void SensorLogger::run()
3029
{
31-
auto path = helper::getEnvOrDefault<std::string>("LOG_PATH", "./sensor-data");
30+
auto path = utils::getEnvOrDefault<std::string>("LOG_PATH", "./sensor-data");
3231
std::string ext = ".uorocketlog";
3332
if (path.back() != '/')
3433
path += "/";

src/IO/TestingSensors.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#if TESTING == 1
33

44
#include "TestingSensors.h"
5+
#include "common/utils.h"
56
#include "data/StateData.h"
6-
#include "helpers/Helper.h"
77

88
#include <boost/algorithm/string.hpp>
99
#include <boost/lexical_cast.hpp>
@@ -16,7 +16,7 @@ void TestingSensors::run()
1616
void TestingSensors::initialize()
1717
{
1818
createThread = false;
19-
auto fileName = helper::getEnvOrDefault<std::string>("TESTING_INPUT_FILE", "./data/test-data.csv");
19+
auto fileName = utils::getEnvOrDefault<std::string>("TESTING_INPUT_FILE", "./data/test-data.csv");
2020
std::ifstream logFile(fileName.c_str());
2121

2222
std::string line;

src/IO/gpio/Gpio.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include "Gpio.h"
55

6-
#include "helpers/Helper.h"
76
#include <iostream>
87
#include <string>
98

src/common/FileLogFormatter.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
#include "pch.h"
3+
#include "spdlog/pattern_formatter.h"
4+
#include "utils.h"
5+
6+
/**
7+
* Custom formatter for spdlog. Same as default but using the getTimeStamp() function
8+
*/
9+
class FileLogFormatter : public spdlog::formatter
10+
{
11+
private:
12+
spdlog::pattern_formatter f; // Separate formatter for rest of the log messages
13+
public:
14+
FileLogFormatter()
15+
{
16+
auto format = "[%n] [%^%l%$] [%s:%#] %v";
17+
f.set_pattern(format);
18+
}
19+
void format(const spdlog::details::log_msg &msg, spdlog::memory_buf_t &dest) override
20+
{
21+
auto timeStamp = utils::getMonotonicTimeStamp();
22+
dest.append("[" + std::to_string(timeStamp) + "] "); // The only thing we need to change is the time stamp
23+
f.format(msg, dest);
24+
}
25+
26+
std::unique_ptr<formatter> clone() const override
27+
{
28+
return std::make_unique<FileLogFormatter>();
29+
}
30+
};

src/common/types.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,11 @@ using time_point = std::chrono::time_point<std::chrono::steady_clock>;
55
using duration_ns = std::chrono::duration<int64_t, std::nano>;
66
using duration_ms = std::chrono::duration<int64_t, std::milli>;
77

8-
using eventType = long;
8+
using eventType = long;
9+
10+
template <class T>
11+
struct SharedArray
12+
{
13+
std::shared_ptr<T[]> data;
14+
size_t length;
15+
};

src/common/utils.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "common/utils.h"
2+
3+
uint64_t utils::getMonotonicTimeStamp()
4+
{
5+
return std::chrono::duration_cast<time_point::duration>(std::chrono::steady_clock::now().time_since_epoch())
6+
.count();
7+
}

src/helpers/Helper.h renamed to src/common/utils.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22
#include "common/pch.h"
33
#include <boost/lexical_cast.hpp>
44

5-
namespace helper
5+
namespace utils
66
{
77

8+
uint64_t getMonotonicTimeStamp();
9+
810
template <typename T>
911
T getEnvOrDefault(const std::string &envName, T defaultValue)
1012
{
1113
const char *value = std::getenv(envName.c_str());
1214
return value ? boost::lexical_cast<T>(value) : defaultValue;
1315
}
1416

15-
template <class T>
16-
struct SharedArray
17-
{
18-
std::shared_ptr<T[]> data;
19-
size_t length;
20-
};
21-
22-
} // namespace helper
17+
} // namespace utils

0 commit comments

Comments
 (0)