simple regex and test

This commit is contained in:
Green Sky 2024-06-10 21:37:31 +02:00
parent bf91297e83
commit 3792fe5b1e
No known key found for this signature in database
9 changed files with 83 additions and 1 deletions

View File

@ -11,6 +11,8 @@ endif()
message("II SOLANACEAE_FACTORIO_STANDALONE " ${SOLANACEAE_FACTORIO_STANDALONE}) message("II SOLANACEAE_FACTORIO_STANDALONE " ${SOLANACEAE_FACTORIO_STANDALONE})
option(SOLANACEAE_FACTORIO_BUILD_PLUGINS "Build the factorio plugins" ${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) if (SOLANACEAE_FACTORIO_STANDALONE)
set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON)
@ -66,6 +68,11 @@ endif()
add_subdirectory(./src) add_subdirectory(./src)
if (SOLANACEAE_FACTORIO_BUILD_TESTING)
include(CTest)
add_subdirectory(./test)
endif()
if (SOLANACEAE_FACTORIO_BUILD_PLUGINS) if (SOLANACEAE_FACTORIO_BUILD_PLUGINS)
add_subdirectory(./plugins) add_subdirectory(./plugins)
endif() endif()

3
README.md Normal file
View File

@ -0,0 +1,3 @@
Parses the factorio log.
Requires https://github.com/royvandongen/Factorio-Event-Logger-Mod

View File

@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.9...3.24 FATAL_ERROR)
project(solanaceae) project(solanaceae)
add_library(solanaceae_factorio add_library(solanaceae_factorio
./log_parse.hpp
./log_parse.cpp
./factorio.hpp ./factorio.hpp
./factorio.cpp ./factorio.cpp
) )

View File

@ -3,6 +3,8 @@
#include <solanaceae/message3/components.hpp> #include <solanaceae/message3/components.hpp>
#include <solanaceae/contact/components.hpp> #include <solanaceae/contact/components.hpp>
#include "./log_parse.hpp"
Factorio::Factorio(Contact3Registry& cr, RegistryMessageModel& rmm) : _cr(cr), _rmm(rmm) { Factorio::Factorio(Contact3Registry& cr, RegistryMessageModel& rmm) : _cr(cr), _rmm(rmm) {
_rmm.subscribe(this, RegistryMessageModel_Event::message_construct); _rmm.subscribe(this, RegistryMessageModel_Event::message_construct);
} }

View File

@ -12,6 +12,5 @@ class Factorio : public RegistryMessageModelEventI {
protected: // rmm protected: // rmm
bool onEvent(const Message::Events::MessageConstruct& e) override; bool onEvent(const Message::Events::MessageConstruct& e) override;
}; };

21
src/log_parse.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "./log_parse.hpp"
#include <regex>
std::optional<LPLRes> 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<size_t>(matches[1].second - matches[1].first)},
std::string_view{matches[2].first, static_cast<size_t>(matches[2].second - matches[2].first)},
};
}

11
src/log_parse.hpp Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <string_view>
#include <optional>
struct LPLRes {
std::string_view event;
std::string_view info;
};
std::optional<LPLRes> log_parse_line(std::string_view line);

16
test/CMakeLists.txt Normal file
View File

@ -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)
########################################

21
test/test1.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "log_parse.hpp"
#include <cassert>
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;
}