From 3792fe5b1e7b5498b1d4bb3f849ec185c817c17c Mon Sep 17 00:00:00 2001 From: Green Sky Date: Mon, 10 Jun 2024 21:37:31 +0200 Subject: [PATCH] simple regex and test --- CMakeLists.txt | 7 +++++++ README.md | 3 +++ src/CMakeLists.txt | 2 ++ src/factorio.cpp | 2 ++ src/factorio.hpp | 1 - src/log_parse.cpp | 21 +++++++++++++++++++++ src/log_parse.hpp | 11 +++++++++++ test/CMakeLists.txt | 16 ++++++++++++++++ test/test1.cpp | 21 +++++++++++++++++++++ 9 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 README.md create mode 100644 src/log_parse.cpp create mode 100644 src/log_parse.hpp create mode 100644 test/CMakeLists.txt create mode 100644 test/test1.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4607f70..ed20d7d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,8 @@ endif() message("II SOLANACEAE_FACTORIO_STANDALONE " ${SOLANACEAE_FACTORIO_STANDALONE}) option(SOLANACEAE_FACTORIO_BUILD_PLUGINS "Build the factorio plugins" ${SOLANACEAE_FACTORIO_STANDALONE}) +option(SOLANACEAE_FACTORIO_BUILD_TESTING "Build the factorio tests" ${SOLANACEAE_FACTORIO_STANDALONE}) +message("II SOLANACEAE_FACTORIO_TESTING " ${SOLANACEAE_FACTORIO_BUILD_TESTING}) if (SOLANACEAE_FACTORIO_STANDALONE) set(CMAKE_POSITION_INDEPENDENT_CODE ON) @@ -66,6 +68,11 @@ endif() add_subdirectory(./src) +if (SOLANACEAE_FACTORIO_BUILD_TESTING) + include(CTest) + add_subdirectory(./test) +endif() + if (SOLANACEAE_FACTORIO_BUILD_PLUGINS) add_subdirectory(./plugins) endif() diff --git a/README.md b/README.md new file mode 100644 index 0000000..e771282 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +Parses the factorio log. + +Requires https://github.com/royvandongen/Factorio-Event-Logger-Mod diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4f98e5a..dff1bbf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.9...3.24 FATAL_ERROR) project(solanaceae) add_library(solanaceae_factorio + ./log_parse.hpp + ./log_parse.cpp ./factorio.hpp ./factorio.cpp ) diff --git a/src/factorio.cpp b/src/factorio.cpp index 84d325d..4d71c2b 100644 --- a/src/factorio.cpp +++ b/src/factorio.cpp @@ -3,6 +3,8 @@ #include #include +#include "./log_parse.hpp" + Factorio::Factorio(Contact3Registry& cr, RegistryMessageModel& rmm) : _cr(cr), _rmm(rmm) { _rmm.subscribe(this, RegistryMessageModel_Event::message_construct); } diff --git a/src/factorio.hpp b/src/factorio.hpp index b9a3937..ea7c960 100644 --- a/src/factorio.hpp +++ b/src/factorio.hpp @@ -12,6 +12,5 @@ class Factorio : public RegistryMessageModelEventI { protected: // rmm bool onEvent(const Message::Events::MessageConstruct& e) override; - }; diff --git a/src/log_parse.cpp b/src/log_parse.cpp new file mode 100644 index 0000000..f4f8e3c --- /dev/null +++ b/src/log_parse.cpp @@ -0,0 +1,21 @@ +#include "./log_parse.hpp" + +#include + +std::optional log_parse_line(std::string_view line) { + static const std::regex mod_match{".*Factorio-Event-Logger+.*\\[([A-Z ]+)\\] (.+)$"}; + + std::cmatch matches; + if (!std::regex_match(line.cbegin(), line.cend(), matches, mod_match)) { + return std::nullopt; + } + + if (matches.empty() || matches.size() != 3) { + return std::nullopt; + } + + return LPLRes{ + std::string_view{matches[1].first, static_cast(matches[1].second - matches[1].first)}, + std::string_view{matches[2].first, static_cast(matches[2].second - matches[2].first)}, + }; +} diff --git a/src/log_parse.hpp b/src/log_parse.hpp new file mode 100644 index 0000000..0b1f237 --- /dev/null +++ b/src/log_parse.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +struct LPLRes { + std::string_view event; + std::string_view info; +}; + +std::optional log_parse_line(std::string_view line); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..ba16c9f --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.9...3.24 FATAL_ERROR) + +project(solanaceae) + +add_executable(solanaceae_factorio_test1 + ./test1.cpp +) + +target_compile_features(solanaceae_factorio_test1 PUBLIC cxx_std_17) +target_link_libraries(solanaceae_factorio_test1 PUBLIC + solanaceae_factorio +) + +add_test(NAME solanaceae_factorio_test1 COMMAND solanaceae_factorio_test1) + +######################################## diff --git a/test/test1.cpp b/test/test1.cpp new file mode 100644 index 0000000..f5f4770 --- /dev/null +++ b/test/test1.cpp @@ -0,0 +1,21 @@ +#include "log_parse.hpp" + +#include + +int main(void) { + + { + const auto parse_res = log_parse_line(" 442.539 Script @__Factorio-Event-Logger__/logger.lua:65: [RESEARCH CANCELLED] worker-robots-speed"); + assert(parse_res.has_value()); + assert(parse_res.value().event == "RESEARCH CANCELLED"); + assert(parse_res.value().info == "worker-robots-speed"); + } + + { + const auto parse_res = log_parse_line(" 224.089 Info ServerMultiplayerManager.cpp:944: updateTick(38228812) received stateChanged peerID(2) oldState(ConnectedLoadingMap) newState(TryingToCatchUp)"); + assert(!parse_res.has_value()); + } + + return 0; +} +