Skip to content

Commit 72436c7

Browse files
committed
feat(logging): add control of logging, to log_to_console and log_to_file control whether logs are shown and where, and the log message is appended with the parameters used
1 parent d045809 commit 72436c7

File tree

3 files changed

+213
-146
lines changed

3 files changed

+213
-146
lines changed

R/model.R

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#' may not wish to do if being set elsewhere - such as done in \code{runner()}).
77
#' Default is TRUE.
88
#'
9-
#' @importFrom simmer get_attribute get_mon_arrivals get_mon_resources
10-
#' @importFrom simmer set_attribute simmer timeout wrap
9+
#' @importFrom simmer get_mon_arrivals get_mon_resources simmer wrap
10+
#' @importFrom utils capture.output
1111
#'
1212
#' @return TBC
1313
#' @export
@@ -19,12 +19,15 @@ model <- function(run_number, param, set_seed = TRUE) {
1919
set.seed(run_number)
2020
}
2121

22+
# Determine whether to get verbose activity logs
23+
verbose <- any(c(param[["log_to_console"]], param[["log_to_file"]]))
24+
2225
# Transform LOS parameters to lognormal scale
2326
param[["asu_los_lnorm"]] <- transform_to_lnorm(param[["asu_los"]])
2427
param[["rehab_los_lnorm"]] <- transform_to_lnorm(param[["rehab_los"]])
2528

2629
# Create simmer environment
27-
env <- simmer("simulation", verbose = TRUE)
30+
env <- simmer("simulation", verbose = verbose)
2831

2932
# Add ASU and rehab direct admission patient generators
3033
for (unit in c("asu", "rehab")) {
@@ -38,20 +41,39 @@ model <- function(run_number, param, set_seed = TRUE) {
3841
}
3942

4043
# Add patient generator using the created trajectory
41-
env <- add_patient_generator(
42-
env = env,
43-
trajectory = traj,
44-
unit = unit,
45-
patient_type = patient_type,
46-
param = param
44+
sim_log <- capture.output(
45+
env <- add_patient_generator(
46+
env = env,
47+
trajectory = traj,
48+
unit = unit,
49+
patient_type = patient_type,
50+
param = param
51+
)
4752
)
4853
}
4954
}
5055

5156
# Run the model
52-
env <- env |>
53-
simmer::run(20L) |>
54-
wrap()
57+
sim_log <- capture.output(
58+
env <- env |>
59+
simmer::run(20L) |>
60+
wrap()
61+
)
62+
63+
# Save and/or display the log
64+
if (isTRUE(verbose)) {
65+
# Create full log message by adding parameters
66+
param_string <- paste(names(param), param, sep = "=", collapse = ";\n ")
67+
full_log <- append(c("Parameters:", param_string, "Log:"), sim_log)
68+
# Print to console
69+
if (isTRUE(param[["log_to_console"]])) {
70+
print(full_log)
71+
}
72+
# Save to file
73+
if (isTRUE(param[["log_to_file"]])) {
74+
writeLines(full_log, param[["file_path"]])
75+
}
76+
}
5577

5678
# Extract the monitored arrivals and resources information from the simmer
5779
# environment object

rmarkdown/analysis.Rmd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ library(simulation)
1818

1919

2020
```{r}
21-
model(run_number = 1L, param = create_parameters(), set_seed = TRUE)
21+
param <- create_parameters(log_to_console = TRUE)
22+
model(run_number = 1L, param = param, set_seed = TRUE)
2223
```

0 commit comments

Comments
 (0)