2021-01-20 04:16:12 +08:00
|
|
|
#pragma once
|
2020-01-18 03:20:17 +08:00
|
|
|
|
2023-08-24 04:25:17 +08:00
|
|
|
#include <cassert>
|
2021-02-02 13:00:42 +08:00
|
|
|
#include <memory>
|
2023-08-24 04:25:17 +08:00
|
|
|
#include <string>
|
2021-05-09 13:15:17 +08:00
|
|
|
|
2021-08-31 08:12:09 +08:00
|
|
|
#include "cereal/messaging/messaging.h"
|
2022-05-19 05:11:57 +08:00
|
|
|
#include "common/util.h"
|
2022-06-12 07:38:24 +08:00
|
|
|
#include "system/hardware/hw.h"
|
2020-01-18 03:20:17 +08:00
|
|
|
|
2022-05-03 20:09:17 +08:00
|
|
|
class RawFile {
|
2021-02-02 13:00:42 +08:00
|
|
|
public:
|
2023-11-19 14:11:13 +08:00
|
|
|
RawFile(const std::string &path) {
|
|
|
|
file = util::safe_fopen(path.c_str(), "wb");
|
2021-02-02 13:00:42 +08:00
|
|
|
assert(file != nullptr);
|
|
|
|
}
|
2022-05-03 20:09:17 +08:00
|
|
|
~RawFile() {
|
2021-10-29 19:23:31 +08:00
|
|
|
util::safe_fflush(file);
|
2021-02-02 13:00:42 +08:00
|
|
|
int err = fclose(file);
|
|
|
|
assert(err == 0);
|
|
|
|
}
|
|
|
|
inline void write(void* data, size_t size) {
|
2022-05-03 20:09:17 +08:00
|
|
|
int written = util::safe_fwrite(data, 1, size, file);
|
|
|
|
assert(written == size);
|
2021-02-02 13:00:42 +08:00
|
|
|
}
|
|
|
|
inline void write(kj::ArrayPtr<capnp::byte> array) { write(array.begin(), array.size()); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
FILE* file = nullptr;
|
|
|
|
};
|
|
|
|
|
2021-08-31 08:12:09 +08:00
|
|
|
typedef cereal::Sentinel::SentinelType SentinelType;
|
|
|
|
|
2020-01-18 03:20:17 +08:00
|
|
|
|
2023-11-19 14:11:13 +08:00
|
|
|
class LoggerState {
|
|
|
|
public:
|
|
|
|
LoggerState(const std::string& log_root = Path::log_root());
|
|
|
|
~LoggerState();
|
|
|
|
bool next();
|
|
|
|
void write(uint8_t* data, size_t size, bool in_qlog);
|
|
|
|
inline int segment() const { return part; }
|
|
|
|
inline const std::string& segmentPath() const { return segment_path; }
|
|
|
|
inline const std::string& routeName() const { return route_name; }
|
|
|
|
inline void write(kj::ArrayPtr<kj::byte> bytes, bool in_qlog) { write(bytes.begin(), bytes.size(), in_qlog); }
|
|
|
|
inline void setExitSignal(int signal) { exit_signal = signal; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int part = -1, exit_signal = 0;
|
|
|
|
std::string route_path, route_name, segment_path, lock_file;
|
2021-01-28 20:34:07 +08:00
|
|
|
kj::Array<capnp::word> init_data;
|
2023-11-19 14:11:13 +08:00
|
|
|
std::unique_ptr<RawFile> rlog, qlog;
|
|
|
|
};
|
2020-01-18 03:20:17 +08:00
|
|
|
|
2021-01-28 20:34:07 +08:00
|
|
|
kj::Array<capnp::word> logger_build_init_data();
|
2024-01-27 11:17:38 +08:00
|
|
|
std::string logger_get_identifier(std::string key);
|