Skip to content
Snippets Groups Projects
Commit 1a24f7fe authored by Ruben Heinrich's avatar Ruben Heinrich
Browse files

[feature] Improved logging

parent b33eab90
No related branches found
No related tags found
1 merge request!6Merge branch 7 fixed functionality into master
...@@ -6,12 +6,12 @@ ...@@ -6,12 +6,12 @@
#'@description Calls OGS6 object validator functions, exports all necessary #'@description Calls OGS6 object validator functions, exports all necessary
#' files and starts OpenGeoSys6 #' files and starts OpenGeoSys6
#'@param ogs6_obj OGS6: Simulation object #'@param ogs6_obj OGS6: Simulation object
#'@param output_to_log_file flag: Should output be written to a log file? #'@param write_logfile flag: Should output be written to a logfile?
#'@export #'@export
run_simulation <- function(ogs6_obj, output_to_log_file = TRUE) { run_simulation <- function(ogs6_obj, write_logfile = TRUE) {
assertthat::assert_that(inherits(ogs6_obj), "OGS6") assertthat::assert_that(inherits(ogs6_obj, "OGS6"))
assertthat::assert_that(assertthat::is.flag(output_to_log_file)) assertthat::assert_that(assertthat::is.flag(write_logfile))
# Call all validators # Call all validators
validate_all(ogs6_obj) validate_all(ogs6_obj)
...@@ -20,7 +20,7 @@ run_simulation <- function(ogs6_obj, output_to_log_file = TRUE) { ...@@ -20,7 +20,7 @@ run_simulation <- function(ogs6_obj, output_to_log_file = TRUE) {
if (!dir.exists(ogs6_obj$sim_path)) { if (!dir.exists(ogs6_obj$sim_path)) {
dir.create(ogs6_obj$sim_path) dir.create(ogs6_obj$sim_path)
} else{ } else{
if (length(dir(ogs6_obj$sim_path, all.files = TRUE)) != 0) { if (length(list.files(ogs6_obj$sim_path, all.files = TRUE)) != 0) {
warning( warning(
paste0( paste0(
"The defined sim_path directory '", "The defined sim_path directory '",
...@@ -36,71 +36,44 @@ run_simulation <- function(ogs6_obj, output_to_log_file = TRUE) { ...@@ -36,71 +36,44 @@ run_simulation <- function(ogs6_obj, output_to_log_file = TRUE) {
export_gml(ogs6_obj) export_gml(ogs6_obj)
export_prj(ogs6_obj) export_prj(ogs6_obj)
# Construct the system call # Construct the call
ogs6_call <- paste0( ogs6_command_str <- paste0(ogs6_obj$ogs_bin_path, "ogs.exe")
ogs6_obj$ogs_bin_path, sim_path_full <- paste0(ogs6_obj$sim_path,
"ogs.exe ", ogs6_obj$sim_name,
ogs6_obj$sim_path, ".prj")
ogs6_obj$sim_name, ogs6_args <- c(sim_path_full, "-o", ogs6_obj$sim_path)
".prj -o ",
ogs6_obj$sim_path
)
# Finally, make the system call to start the simulation exit_code <- 0
if (output_to_log_file) {
system(command = setup_logging(ogs6_obj$sim_name,
ogs6_obj$sim_path,
ogs6_call))
} else{
system(command = ogs6_call)
}
closeAllConnections()
}
#===== LOGGING UTILITY =====
# Finally, make the system call to start the simulation
if (write_logfile) {
#'setup_logging # Create logfile directory
#'@description Sets up logging. logfile_dir <- paste0(ogs6_obj$sim_path, "logfiles/")
#'@param sim_name string: Simulation name
#'@param sim_path string: Simulation path
#'@param ogs6_call string: Corresponding OGS6 call
setup_logging <- function(sim_name, sim_path, ogs6_call){
assertthat::assert_that(assertthat::is.string(sim_name))
assertthat::assert_that(assertthat::is.string(sim_path))
sim_path <- validate_is_dir_path(sim_path)
assertthat::assert_that(assertthat::is.string(ogs6_call))
# Create logfile directory # Set logfile parameter of simulation object
logfile_dir <- paste0(sim_path, "logfiles/") ogs6_obj$logfile <- paste0(logfile_dir, ogs6_obj$sim_name, "_log.txt")
if(!dir.exists(logfile_dir)){ if(!dir.exists(logfile_dir)){
dir.create(logfile_dir) dir.create(logfile_dir)
} }
# Create initialization script assertthat::assert_that(!file.exists(ogs6_obj$logfile))
script_path <- paste0(logfile_dir, "sim_init.R") file.create(ogs6_obj$logfile)
if(!file.exists(script_path)){ exit_code <- system2(command = ogs6_command_str,
file.create(script_path) args = ogs6_args,
stdout = ogs6_obj$logfile)
} else{
exit_code <- system2(command = ogs6_command_str,
args = ogs6_args)
} }
cmd_str <- paste0("system(command = \"", ogs6_call, "\")") closeAllConnections()
file_conn <- file(script_path)
writeLines(c(cmd_str), file_conn)
close(file_conn)
# Return string for calling R CMD BATCH cat("\nCaught exit code", exit_code, "\n")
batch_call <- paste0("R CMD BATCH --no-echo ",
script_path, " ",
sim_name, "_log.txt")
return(invisible(batch_call)) return(invisible(exit_code))
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment