diff options
| author | Claudius "keldu" Holeksa <mail@keldu.de> | 2025-10-27 21:06:54 +0100 |
|---|---|---|
| committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2025-10-27 21:06:54 +0100 |
| commit | e0bbafdc39d13209a5e09a7be3c4fe30185b6b80 (patch) | |
| tree | 1b5ef8b33af4fcc8d5bf9ca420b7d7549c83b2ff | |
| parent | a3338b55da55f467d984ac36e7c3f158b88245b9 (diff) | |
| download | apps-science_tools-e0bbafdc39d13209a5e09a7be3c4fe30185b6b80.tar.gz | |
More tool work
| -rw-r--r-- | run_and_record/c++/main.cpp | 97 | ||||
| -rw-r--r-- | run_and_record/todo.txt | 11 |
2 files changed, 93 insertions, 15 deletions
diff --git a/run_and_record/c++/main.cpp b/run_and_record/c++/main.cpp index 7df7cb5..5295a2f 100644 --- a/run_and_record/c++/main.cpp +++ b/run_and_record/c++/main.cpp @@ -6,6 +6,8 @@ #include <unistd.h> #include <sys/wait.h> #include <iostream> +#include <chrono> +#include <iomanip> namespace kel { namespace sch { @@ -24,8 +26,16 @@ using RarArgs = Args< RarArgsTuple >; -using RarDataFile = Struct< - Member<Array<String>,"command">, +using RarFileData = Struct< + Member< + Member< + Array< + Struct< + Member<String, "program">, + Member<Array<String>, "arguments"> + > + >, + >, "commands">, Member< Array< Struct< @@ -38,7 +48,15 @@ using RarDataFile = Struct< Member<String, "commit">, Member<String, "origin"> >, - "git"> + "git">, + Member< + Array< + Struct< + Member<Array<String>, "command">, + Member<String, "stdout">, + Member<String, "stderr"> + > + >, "meta"> >; } @@ -70,13 +88,19 @@ saw::error_or<void> run_program(int argc, char** argv){ return saw::make_error<saw::err::critical>("execvp failed. Usually the path doesn't exist or permissions are wrong."); } - // Every other case + // This is still the parent + int child_exit_code = 0; { int status = 0; waitpid(pid,&status,0); + + if(WIFEXITED(status)){ + child_exit_code = WEXITSTATUS(status); + }else{ + return saw::make_error<saw::err::critical>("Program existed unexpectedly"); + } } - /// @todo check status recoverd and write based on that. /// /// @todo record ENV used in run. @@ -84,6 +108,50 @@ saw::error_or<void> run_program(int argc, char** argv){ return saw::make_void(); } +saw::error_or<std::string> generate_data_dir(const std::string_view& bin_name) { + using namespace std::chrono; + auto now = system_clock::now(); + auto now_time_t = system_clock::to_time_t(now); + auto now_ms = duration_cast<milliseconds>(no.time_since_epoch()) % 1000u; + + // Convert to UTC broken-down time + std::tm utc_tm{}; + gmtime_r(&now_time_t, &utc_tm); + + std::ostringstream oss; + oss << std::put_time(&utc_tm, "%Y-%m-%d-%H-%M-%S-") + << std::setw(3) << std::setfill('0') << ms_part.count() << "-" + << std::setw(9) << std::setfill('0') << ns_part.count() << "-" + << bin_name; + + return oss.str(); +} + +extern char** environ; + +/** + * Record the environment + */ +saw::error_or<void> record_environment(saw::data<sch::RarFileData>& df){ + saw::data<sch::UInt64> env_size{0u}; + + for(char** env = environ; *env != nullptr; ++env){ + ++env_size; + } + + auto& df_env = df.template get<"env">(); + + df_env = {env_size}; + + saw::data<sch::UInt64> i{0u}; + for(char** env = environ; *env != nullptr; ++env){ + df_env.at(i).set(std::to_string(env)); + ++i; + } + + return saw::make_void(); +} + /** * I want to wrap this since I want to transport error messages down */ @@ -112,7 +180,7 @@ saw::error_or<void> kel_main(int argc, char** argv){ // => sep_pos = 1, argc = 3 // args til 1 // Run program with argc - (sep_pos+1) = 1 arg_count => argc_prog = 1 starting at sep_pos+1 - + std::array<size_t, 64u> next_arg; // Grab home variable Check the default config location and try to decode the file saw::data<sch::RarArgs> args; @@ -126,10 +194,16 @@ saw::error_or<void> kel_main(int argc, char** argv){ auto data_dir = home / ".local" / "kel" / "run_and_record"; // - { + // + auto eo_data_child_dir = generate_data_dir(""); + if(eo_data_child_dir.is_error()){ + return std::move(eo_data_child_dir.get_error()); + } + auto& data_child_dir = eo_data_child_dir.get_value(); + auto data_full_dir = data_dir / data_child_dir; + { auto conf_file = home / ".config" / "kel" / "run_and_record.json"; - auto eo_file = simple_read_file<sch::RarConfig,saw::encode::Json>(conf_file); if(eo_file.is_error()){ @@ -148,9 +222,13 @@ saw::error_or<void> kel_main(int argc, char** argv){ return eov; } } - saw::data<sch::RarDataFile> data_for_file; + saw::data<sch::RarFileData> data_for_file; { auto& env = data_for_file.template get<"env">(); + auto eov = record_environment(env); + if(eov.is_error()){ + return eov; + } } // sep_pos+1 til end int argc_prog = argc - (sep_pos+1); @@ -161,7 +239,6 @@ saw::error_or<void> kel_main(int argc, char** argv){ } } { - } diff --git a/run_and_record/todo.txt b/run_and_record/todo.txt index 1817c2e..1512e7f 100644 --- a/run_and_record/todo.txt +++ b/run_and_record/todo.txt @@ -2,8 +2,9 @@ * read config.json from ~/.config/kel/run_and_record/config.json * Create folder based on time and random id or maybe hash content? - Record env - - Record git hash if exists - - Record command used - - Record additional files used -* Possibly record crash? -* Maybe record std::cout? + - Record git hash if exists + - Record command used + - Record additional files used + - Record stdout and stdcerr +* Possibly record crash? +* Maybe record std::cout? |
