Merge pull request #911 from MihailRis/log-param

--log command line argument
This commit is contained in:
MihailRis 2026-08-03 21:00:44 +03:00 • committed by GitHub
commit 07b59e6882
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 25 additions and 8 deletions

View file

@ -12,6 +12,7 @@ struct CoreParameters {
std::filesystem::path userFolder = ".";
std::filesystem::path scriptFile;
std::filesystem::path projectFolder;
std::filesystem::path logFile;
std::string debugServerString;
int tps = 20;
int subProcessDepth = 0;

View file

@ -403,6 +403,7 @@ static int l_start_background_instance(lua::State* L) {
"--script", io::resolve(scriptPath).u8string(),
"--sub-depth", std::to_string(engine->getCoreParameters()
.subProcessDepth + 1),
"--log", io::resolve(outputPath).u8string(),
};
args.emplace_back("--project");
args.emplace_back(io::resolve(engine->getProject().path).u8string());
@ -417,11 +418,9 @@ static int l_start_background_instance(lua::State* L) {
if (handle == -1) {
throw std::runtime_error("sub-processes limit exceeded");
}
::processes[handle] = platform::new_engine_instance(
std::move(args),
outputPath.empty() ? "" : io::resolve(outputPath),
true
);
::processes[handle] =
platform::new_engine_instance(std::move(args), "", true);
return lua::pushinteger(L, handle);
}

View file

@ -7,6 +7,7 @@
#include <iostream>
#include <stdexcept>
using namespace std::literals;
static debug::Logger logger("main");
static void sigterm_handler(int signum) {
@ -36,7 +37,14 @@ int main(int argc, char** argv) {
#ifdef NDEBUG
std::signal(SIGINT, sigterm_handler);
#endif
debug::Logger::init(coreParameters.userFolder.string() + "/latest.log");
std::filesystem::path logFile = coreParameters.logFile;
if (logFile.empty()) {
logFile = coreParameters.userFolder.string() + "/latest"s +
(coreParameters.subProcessDepth > 0
? ".sub" + std::to_string(coreParameters.subProcessDepth)
: "") + ".log"s;
}
debug::Logger::init(logFile.u8string());
platform::configure_encoding();
auto& engine = Engine::getInstance();

View file

@ -82,6 +82,10 @@ static bool perform_keyword(
params.subProcessDepth = reader.nextInt();
return true;
}, "<depth>", "sub-process depth"),
ArgC("--log", [](auto& params, auto& reader) -> bool {
params.logFile = reader.next();
return true;
}, "<file>", "target log file"),
ArgC("--help", [](auto&, auto&) -> bool {
std::cout << "VoxelCore v" << ENGINE_VERSION_STRING << "\n\n";
std::cout << "Command-line arguments:\n";

View file

@ -28,6 +28,8 @@
#include <sys/wait.h>
#endif
using namespace std::literals;
namespace platform::internal {
std::filesystem::path get_executable_path();
}
@ -356,7 +358,7 @@ std::unique_ptr<Process> platform::new_engine_instance(
if (!outputFile.empty()) {
si.dwFlags |= STARTF_USESTDHANDLES;
si.hStdOutput = CreateFileW(
toWString(outputFile.u8string()).data(),
toWString(outputFile.empty() ? outputFile.u8string() : "NUL"s).data(),
GENERIC_WRITE,
FILE_SHARE_WRITE | FILE_SHARE_READ,
nullptr,
@ -434,9 +436,12 @@ std::unique_ptr<Process> platform::new_engine_instance(
if (subProcess) {
pid_t pid = fork();
if (outputFile.empty()) {
outputFile = "/dev/null";
}
int fd = open(
outputFile.string().c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644
);
);
if (pid == 0) {
prctl(PR_SET_PDEATHSIG, SIGTERM);