mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
SCRIPT: wpiformat
This commit is contained in:
committed by
Peter Johnson
parent
ae6bdc9d25
commit
2109161534
@@ -23,7 +23,7 @@ int main(int argc, const char** argv) {
|
||||
auto fileBuffer = wpi::util::MemoryBuffer::GetFile(argv[1]);
|
||||
if (!fileBuffer) {
|
||||
wpi::util::print(stderr, "could not open file: {}\n",
|
||||
fileBuffer.error().message());
|
||||
fileBuffer.error().message());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
wpi::log::DataLogReader reader{std::move(*fileBuffer)};
|
||||
@@ -37,9 +37,10 @@ int main(int argc, const char** argv) {
|
||||
if (record.IsStart()) {
|
||||
wpi::log::StartRecordData data;
|
||||
if (record.GetStartData(&data)) {
|
||||
wpi::util::print("Start({}, name='{}', type='{}', metadata='{}') [{}]\n",
|
||||
data.entry, data.name, data.type, data.metadata,
|
||||
record.GetTimestamp() / 1000000.0);
|
||||
wpi::util::print(
|
||||
"Start({}, name='{}', type='{}', metadata='{}') [{}]\n", data.entry,
|
||||
data.name, data.type, data.metadata,
|
||||
record.GetTimestamp() / 1000000.0);
|
||||
if (entries.find(data.entry) != entries.end()) {
|
||||
wpi::util::print("...DUPLICATE entry ID, overriding\n");
|
||||
}
|
||||
@@ -51,7 +52,7 @@ int main(int argc, const char** argv) {
|
||||
int entry;
|
||||
if (record.GetFinishEntry(&entry)) {
|
||||
wpi::util::print("Finish({}) [{}]\n", entry,
|
||||
record.GetTimestamp() / 1000000.0);
|
||||
record.GetTimestamp() / 1000000.0);
|
||||
auto it = entries.find(entry);
|
||||
if (it == entries.end()) {
|
||||
wpi::util::print("...ID not found\n");
|
||||
@@ -64,8 +65,8 @@ int main(int argc, const char** argv) {
|
||||
} else if (record.IsSetMetadata()) {
|
||||
wpi::log::MetadataRecordData data;
|
||||
if (record.GetSetMetadataData(&data)) {
|
||||
wpi::util::print("SetMetadata({}, '{}') [{}]\n", data.entry, data.metadata,
|
||||
record.GetTimestamp() / 1000000.0);
|
||||
wpi::util::print("SetMetadata({}, '{}') [{}]\n", data.entry,
|
||||
data.metadata, record.GetTimestamp() / 1000000.0);
|
||||
auto it = entries.find(data.entry);
|
||||
if (it == entries.end()) {
|
||||
wpi::util::print("...ID not found\n");
|
||||
@@ -78,14 +79,15 @@ int main(int argc, const char** argv) {
|
||||
} else if (record.IsControl()) {
|
||||
wpi::util::print("Unrecognized control record\n");
|
||||
} else {
|
||||
wpi::util::print("Data({}, size={}) ", record.GetEntry(), record.GetSize());
|
||||
wpi::util::print("Data({}, size={}) ", record.GetEntry(),
|
||||
record.GetSize());
|
||||
auto entry = entries.find(record.GetEntry());
|
||||
if (entry == entries.end()) {
|
||||
wpi::util::print("<ID not found>\n");
|
||||
continue;
|
||||
}
|
||||
wpi::util::print("<name='{}', type='{}'> [{}]\n", entry->second.name,
|
||||
entry->second.type, record.GetTimestamp() / 1000000.0);
|
||||
entry->second.type, record.GetTimestamp() / 1000000.0);
|
||||
|
||||
// handle systemTime specially
|
||||
if (entry->second.name == "systemTime" && entry->second.type == "int64") {
|
||||
@@ -93,7 +95,7 @@ int main(int argc, const char** argv) {
|
||||
if (record.GetInteger(&val)) {
|
||||
std::time_t timeval = val / 1000000;
|
||||
wpi::util::print(" {:%Y-%m-%d %H:%M:%S}.{:06}\n",
|
||||
*std::localtime(&timeval), val % 1000000);
|
||||
*std::localtime(&timeval), val % 1000000);
|
||||
} else {
|
||||
wpi::util::print(" invalid\n");
|
||||
}
|
||||
|
||||
@@ -74,8 +74,8 @@ int main(int argc, char** argv) {
|
||||
resVec.push_back(duration_cast<microseconds>(stop - start).count());
|
||||
}
|
||||
|
||||
wpi::util::print("{}us\n",
|
||||
std::accumulate(resVec.begin(), resVec.end(), 0) / kNumRuns);
|
||||
wpi::util::print(
|
||||
"{}us\n", std::accumulate(resVec.begin(), resVec.end(), 0) / kNumRuns);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
@@ -509,7 +509,8 @@ void DataLog::AppendFloatArray(int entry, std::span<const float> arr,
|
||||
while ((arr.size() * 4) > kBlockSize) {
|
||||
buf = Reserve(kBlockSize);
|
||||
for (auto val : arr.subspan(0, kBlockSize / 4)) {
|
||||
wpi::util::support::endian::write32le(buf, std::bit_cast<uint32_t>(val));
|
||||
wpi::util::support::endian::write32le(buf,
|
||||
std::bit_cast<uint32_t>(val));
|
||||
buf += 4;
|
||||
}
|
||||
arr = arr.subspan(kBlockSize / 4);
|
||||
@@ -541,7 +542,8 @@ void DataLog::AppendDoubleArray(int entry, std::span<const double> arr,
|
||||
while ((arr.size() * 8) > kBlockSize) {
|
||||
buf = Reserve(kBlockSize);
|
||||
for (auto val : arr.subspan(0, kBlockSize / 8)) {
|
||||
wpi::util::support::endian::write64le(buf, std::bit_cast<uint64_t>(val));
|
||||
wpi::util::support::endian::write64le(buf,
|
||||
std::bit_cast<uint64_t>(val));
|
||||
buf += 8;
|
||||
}
|
||||
arr = arr.subspan(kBlockSize / 8);
|
||||
|
||||
@@ -469,8 +469,8 @@ struct WPI_DataLog* WPI_DataLog_CreateBackgroundWriter(
|
||||
const struct WPI_String* dir, const struct WPI_String* filename,
|
||||
double period, const struct WPI_String* extraHeader) {
|
||||
return reinterpret_cast<WPI_DataLog*>(new DataLogBackgroundWriter{
|
||||
wpi::util::to_string_view(dir), wpi::util::to_string_view(filename), period,
|
||||
wpi::util::to_string_view(extraHeader)});
|
||||
wpi::util::to_string_view(dir), wpi::util::to_string_view(filename),
|
||||
period, wpi::util::to_string_view(extraHeader)});
|
||||
}
|
||||
|
||||
struct WPI_DataLog* WPI_DataLog_CreateBackgroundWriter_Func(
|
||||
|
||||
@@ -97,7 +97,8 @@ bool DataLogRecord::GetFloat(float* value) const {
|
||||
if (m_data.size() != 4) {
|
||||
return false;
|
||||
}
|
||||
*value = std::bit_cast<float>(wpi::util::support::endian::read32le(m_data.data()));
|
||||
*value =
|
||||
std::bit_cast<float>(wpi::util::support::endian::read32le(m_data.data()));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -105,7 +106,8 @@ bool DataLogRecord::GetDouble(double* value) const {
|
||||
if (m_data.size() != 8) {
|
||||
return false;
|
||||
}
|
||||
*value = std::bit_cast<double>(wpi::util::support::endian::read64le(m_data.data()));
|
||||
*value = std::bit_cast<double>(
|
||||
wpi::util::support::endian::read64le(m_data.data()));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -142,8 +144,8 @@ bool DataLogRecord::GetFloatArray(std::vector<float>* arr) const {
|
||||
}
|
||||
arr->reserve(m_data.size() / 4);
|
||||
for (size_t pos = 0; pos < m_data.size(); pos += 4) {
|
||||
arr->push_back(
|
||||
std::bit_cast<float>(wpi::util::support::endian::read32le(&m_data[pos])));
|
||||
arr->push_back(std::bit_cast<float>(
|
||||
wpi::util::support::endian::read32le(&m_data[pos])));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -155,8 +157,8 @@ bool DataLogRecord::GetDoubleArray(std::vector<double>* arr) const {
|
||||
}
|
||||
arr->reserve(m_data.size() / 8);
|
||||
for (size_t pos = 0; pos < m_data.size(); pos += 8) {
|
||||
arr->push_back(
|
||||
std::bit_cast<double>(wpi::util::support::endian::read64le(&m_data[pos])));
|
||||
arr->push_back(std::bit_cast<double>(
|
||||
wpi::util::support::endian::read64le(&m_data[pos])));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -107,9 +107,10 @@ void DataLogReaderThread::ReadMain() {
|
||||
auto desc = m_structDb.Add(*typeStr, schema, &err);
|
||||
if (!desc) {
|
||||
wpi::util::print("could not decode struct '{}' schema '{}': {}\n", name,
|
||||
schema, err);
|
||||
schema, err);
|
||||
}
|
||||
} else if (auto filename = wpi::util::remove_prefix(name, "/.schema/proto:")) {
|
||||
} else if (auto filename =
|
||||
wpi::util::remove_prefix(name, "/.schema/proto:")) {
|
||||
// protobuf descriptor handling
|
||||
upb_Status status;
|
||||
status.ok = true;
|
||||
@@ -120,7 +121,7 @@ void DataLogReaderThread::ReadMain() {
|
||||
&status);
|
||||
if (!status.ok) {
|
||||
wpi::util::print("could not decode protobuf '{}' filename '{}'\n", name,
|
||||
*filename);
|
||||
*filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
|
||||
using namespace wpi::log;
|
||||
|
||||
static std::unique_ptr<wpi::util::raw_ostream> CheckOpen(std::string_view filename,
|
||||
std::error_code& ec) {
|
||||
static std::unique_ptr<wpi::util::raw_ostream> CheckOpen(
|
||||
std::string_view filename, std::error_code& ec) {
|
||||
auto rv = std::make_unique<wpi::util::raw_fd_ostream>(filename, ec);
|
||||
if (ec) {
|
||||
return nullptr;
|
||||
@@ -25,8 +25,9 @@ DataLogWriter::DataLogWriter(std::string_view filename, std::error_code& ec,
|
||||
std::string_view extraHeader)
|
||||
: DataLogWriter{s_defaultMessageLog, filename, ec, extraHeader} {}
|
||||
|
||||
DataLogWriter::DataLogWriter(wpi::util::Logger& msglog, std::string_view filename,
|
||||
std::error_code& ec, std::string_view extraHeader)
|
||||
DataLogWriter::DataLogWriter(wpi::util::Logger& msglog,
|
||||
std::string_view filename, std::error_code& ec,
|
||||
std::string_view extraHeader)
|
||||
: DataLogWriter{msglog, CheckOpen(filename, ec), extraHeader} {
|
||||
if (ec) {
|
||||
Stop();
|
||||
@@ -79,8 +80,9 @@ struct WPI_DataLog* WPI_DataLog_CreateWriter(
|
||||
const struct WPI_String* filename, int* errorCode,
|
||||
const struct WPI_String* extraHeader) {
|
||||
std::error_code ec;
|
||||
auto rv = reinterpret_cast<WPI_DataLog*>(new DataLogWriter{
|
||||
wpi::util::to_string_view(filename), ec, wpi::util::to_string_view(extraHeader)});
|
||||
auto rv = reinterpret_cast<WPI_DataLog*>(
|
||||
new DataLogWriter{wpi::util::to_string_view(filename), ec,
|
||||
wpi::util::to_string_view(extraHeader)});
|
||||
*errorCode = ec.value();
|
||||
return rv;
|
||||
}
|
||||
|
||||
@@ -87,8 +87,8 @@ FileLogger::~FileLogger() {
|
||||
|
||||
std::function<void(std::string_view)> FileLogger::Buffer(
|
||||
std::function<void(std::string_view)> callback) {
|
||||
return [callback,
|
||||
buf = wpi::util::SmallVector<char, 64>{}](std::string_view data) mutable {
|
||||
return [callback, buf = wpi::util::SmallVector<char, 64>{}](
|
||||
std::string_view data) mutable {
|
||||
buf.append(data.begin(), data.end());
|
||||
if (!wpi::util::contains({data.data(), data.size()}, "\n")) {
|
||||
return;
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
|
||||
namespace wpi::util {
|
||||
class Logger;
|
||||
} // namespace wpi
|
||||
} // namespace wpi::util
|
||||
|
||||
namespace wpi::log {
|
||||
|
||||
@@ -159,7 +159,8 @@ class DataLog {
|
||||
* @param timestamp Time stamp (0 to indicate now)
|
||||
*/
|
||||
template <wpi::util::ProtobufSerializable T>
|
||||
void AddProtobufSchema(wpi::util::ProtobufMessage<T>& msg, int64_t timestamp = 0) {
|
||||
void AddProtobufSchema(wpi::util::ProtobufMessage<T>& msg,
|
||||
int64_t timestamp = 0) {
|
||||
if (timestamp == 0) {
|
||||
timestamp = wpi::util::Now();
|
||||
}
|
||||
@@ -1313,8 +1314,8 @@ class StructLogEntry : public DataLogEntry {
|
||||
: m_info{std::move(info)...} {
|
||||
m_log = &log;
|
||||
log.AddStructSchema<T, I...>(info..., timestamp);
|
||||
m_entry =
|
||||
log.Start(name, wpi::util::GetStructTypeString<T>(info...), metadata, timestamp);
|
||||
m_entry = log.Start(name, wpi::util::GetStructTypeString<T>(info...),
|
||||
metadata, timestamp);
|
||||
}
|
||||
|
||||
StructLogEntry(StructLogEntry&& rhs)
|
||||
@@ -1443,7 +1444,8 @@ class StructArrayLogEntry : public DataLogEntry {
|
||||
m_log = &log;
|
||||
log.AddStructSchema<T, I...>(info..., timestamp);
|
||||
m_entry = log.Start(
|
||||
name, wpi::util::MakeStructArrayTypeString<T, std::dynamic_extent>(info...),
|
||||
name,
|
||||
wpi::util::MakeStructArrayTypeString<T, std::dynamic_extent>(info...),
|
||||
metadata, timestamp);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
namespace wpi::util {
|
||||
class Logger;
|
||||
} // namespace wpi
|
||||
} // namespace wpi::util
|
||||
|
||||
namespace wpi::log {
|
||||
|
||||
|
||||
@@ -85,7 +85,9 @@ class DataLogReaderThread {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
wpi::util::StructDescriptorDatabase& GetStructDatabase() { return m_structDb; }
|
||||
wpi::util::StructDescriptorDatabase& GetStructDatabase() {
|
||||
return m_structDb;
|
||||
}
|
||||
upb_DefPool* GetProtobufDatabase() { return m_protoPool; }
|
||||
upb_Arena* GetProtobufArena() { return m_arena; }
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
namespace wpi::util {
|
||||
class raw_ostream;
|
||||
class Logger;
|
||||
} // namespace wpi
|
||||
} // namespace wpi::util
|
||||
|
||||
namespace wpi::log {
|
||||
|
||||
@@ -62,7 +62,8 @@ class DataLogWriter final : public DataLog {
|
||||
* @param os output stream
|
||||
* @param extraHeader extra header data
|
||||
*/
|
||||
DataLogWriter(wpi::util::Logger& msglog, std::unique_ptr<wpi::util::raw_ostream> os,
|
||||
DataLogWriter(wpi::util::Logger& msglog,
|
||||
std::unique_ptr<wpi::util::raw_ostream> os,
|
||||
std::string_view extraHeader = "");
|
||||
|
||||
~DataLogWriter() final;
|
||||
|
||||
@@ -131,8 +131,8 @@ class DataLogTest : public ::testing::Test {
|
||||
public:
|
||||
wpi::util::Logger msglog;
|
||||
std::vector<uint8_t> data;
|
||||
wpi::log::DataLogWriter log{msglog,
|
||||
std::make_unique<wpi::util::raw_uvector_ostream>(data)};
|
||||
wpi::log::DataLogWriter log{
|
||||
msglog, std::make_unique<wpi::util::raw_uvector_ostream>(data)};
|
||||
};
|
||||
|
||||
TEST_F(DataLogTest, SimpleInt) {
|
||||
|
||||
Reference in New Issue
Block a user