Skip to content

Commit 8ab16ef

Browse files
Debug messages are now prepended with a timestamp
1 parent 13784e4 commit 8ab16ef

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

NEWS

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
Package: future.batchtools
22
==========================
33

4-
Version: 0.7.2-9000 [2019-01-22]
4+
Version: 0.7.2-9000 [2019-03-15]
5+
6+
NEW FEATURES:
7+
8+
* Debug messages are now prepended with a timestamp.
59

610
BUG FIXES:
711

R/BatchtoolsFuture-class.R

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,6 @@ run.BatchtoolsFuture <- function(future, ...) {
321321
stop(sprintf("A future ('%s') can only be launched once.", label))
322322
}
323323

324-
mdebug <- import_future("mdebug")
325-
326324
## Assert that the process that created the future is
327325
## also the one that evaluates/resolves/queries it.
328326
assertOwner <- import_future("assertOwner")
@@ -350,7 +348,7 @@ run.BatchtoolsFuture <- function(future, ...) {
350348
## (ii) Attach packages that needs to be attached
351349
packages <- future$packages
352350
if (length(packages) > 0) {
353-
mdebug("Attaching %d packages (%s) ...",
351+
mdebugf("Attaching %d packages (%s) ...",
354352
length(packages), hpaste(sQuote(packages)))
355353

356354
## Record which packages in 'pkgs' that are loaded and
@@ -364,7 +362,7 @@ run.BatchtoolsFuture <- function(future, ...) {
364362
reg$packages <- packages
365363
saveRegistry(reg = reg)
366364

367-
mdebug("Attaching %d packages (%s) ... DONE",
365+
mdebugf("Attaching %d packages (%s) ... DONE",
368366
length(packages), hpaste(sQuote(packages)))
369367
}
370368
## Not needed anymore
@@ -388,7 +386,7 @@ run.BatchtoolsFuture <- function(future, ...) {
388386

389387
## 2. Update
390388
future$config$jobid <- jobid
391-
mdebug("Created %s future #%d", class(future)[1], jobid$job.id)
389+
mdebugf("Created %s future #%d", class(future)[1], jobid$job.id)
392390

393391
## WORKAROUND: (For multicore and macOS only)
394392
if (reg$cluster.functions$name == "Multicore") {
@@ -419,7 +417,7 @@ run.BatchtoolsFuture <- function(future, ...) {
419417

420418
batchtools::submitJobs(reg = reg, ids = jobid, resources = resources)
421419

422-
mdebug("Launched future #%d", jobid$job.id)
420+
mdebugf("Launched future #%d", jobid$job.id)
423421

424422
invisible(future)
425423
} ## run()
@@ -458,7 +456,6 @@ await.BatchtoolsFuture <- function(future, cleanup = TRUE,
458456
1.0),
459457
alpha = getOption("future.wait.alpha", 1.01),
460458
...) {
461-
mdebug <- import_future("mdebug")
462459
stop_if_not(is.finite(timeout), timeout >= 0)
463460
stop_if_not(is.finite(alpha), alpha > 0)
464461

@@ -480,9 +477,9 @@ await.BatchtoolsFuture <- function(future, cleanup = TRUE,
480477

481478
res <- waitForJobs(ids = jobid, timeout = timeout, sleep = sleep_fcn,
482479
stop.on.error = FALSE, reg = reg)
483-
mdebug("- batchtools::waitForJobs(): %s", res)
480+
mdebugf("- batchtools::waitForJobs(): %s", res)
484481
stat <- status(future)
485-
mdebug("- status(): %s", paste(sQuote(stat), collapse = ", "))
482+
mdebugf("- status(): %s", paste(sQuote(stat), collapse = ", "))
486483
mdebug("batchtools::waitForJobs() ... done")
487484

488485
finished <- is_na(stat) || any(c("finished", "error", "expired") %in% stat)
@@ -576,8 +573,6 @@ delete.BatchtoolsFuture <- function(future,
576573
delta = getOption("future.wait.interval", 1.0),
577574
alpha = getOption("future.wait.alpha", 1.01),
578575
...) {
579-
mdebug <- import_future("mdebug")
580-
581576
onRunning <- match.arg(onRunning)
582577
onMissing <- match.arg(onMissing)
583578
onFailure <- match.arg(onFailure)
@@ -593,7 +588,7 @@ delete.BatchtoolsFuture <- function(future,
593588
if (is.null(path) || !file_test("-d", path)) {
594589
if (onMissing %in% c("warning", "error")) {
595590
msg <- sprintf("Cannot remove batchtools registry, because directory does not exist: %s", sQuote(path)) #nolint
596-
mdebug("delete(): %s", msg)
591+
mdebugf("delete(): %s", msg)
597592
if (onMissing == "warning") {
598593
warning(msg)
599594
} else if (onMissing == "error") {
@@ -611,7 +606,7 @@ delete.BatchtoolsFuture <- function(future,
611606
label <- future$label
612607
if (is.null(label)) label <- "<none>"
613608
msg <- sprintf("Will not remove batchtools registry, because is appears to hold a non-resolved future (%s; state = %s; batchtools status = %s): %s", sQuote(label), sQuote(future$state), paste(sQuote(status), collapse = ", "), sQuote(path)) #nolint
614-
mdebug("delete(): %s", msg)
609+
mdebugf("delete(): %s", msg)
615610
if (onRunning == "warning") {
616611
warning(msg)
617612
return(invisible(TRUE))
@@ -627,19 +622,19 @@ delete.BatchtoolsFuture <- function(future,
627622

628623
## To simplify post mortem troubleshooting in non-interactive sessions,
629624
## should the batchtools registry files be removed or not?
630-
mdebug("delete(): Option 'future.delete = %s",
625+
mdebugf("delete(): Option 'future.delete = %s",
631626
sQuote(getOption("future.delete", "<NULL>")))
632627
if (!getOption("future.delete", interactive())) {
633628
status <- status(future)
634629
res <- future$result
635630
if (inherits(res, "FutureResult")) {
636631
if (result_has_errors(res)) status <- unique(c("error", status))
637632
}
638-
mdebug("delete(): status(<future>) = %s",
633+
mdebugf("delete(): status(<future>) = %s",
639634
paste(sQuote(status), collapse = ", "))
640635
if (any(c("error", "expired") %in% status)) {
641636
msg <- sprintf("Will not remove batchtools registry, because the status of the batchtools was %s and option 'future.delete' is FALSE or running in an interactive session: %s", paste(sQuote(status), collapse = ", "), sQuote(path)) #nolint
642-
mdebug("delete(): %s", msg)
637+
mdebugf("delete(): %s", msg)
643638
warning(msg)
644639
return(invisible(FALSE))
645640
}
@@ -664,7 +659,7 @@ delete.BatchtoolsFuture <- function(future,
664659
if (file_test("-d", path)) {
665660
if (onFailure %in% c("warning", "error")) {
666661
msg <- sprintf("Failed to remove batchtools registry: %s", sQuote(path))
667-
mdebug("delete(): %s", msg)
662+
mdebugf("delete(): %s", msg)
668663
if (onMissing == "warning") {
669664
warning(msg)
670665
} else if (onMissing == "error") {
@@ -674,7 +669,7 @@ delete.BatchtoolsFuture <- function(future,
674669
return(invisible(FALSE))
675670
}
676671

677-
mdebug("delete(): batchtools registry deleted: %s", sQuote(path))
672+
mdebugf("delete(): batchtools registry deleted: %s", sQuote(path))
678673

679674
invisible(TRUE)
680675
} # delete()

R/utils.R

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,35 @@ capture_output <- function(expr, envir = parent.frame(), ...) {
4747

4848
printf <- function(...) cat(sprintf(...))
4949

50-
mcat <- function(...) message(..., appendLF = FALSE)
50+
now <- function(x = Sys.time(), format = "[%H:%M:%OS3] ") {
51+
## format(x, format = format) ## slower
52+
format(as.POSIXlt(x, tz = ""), format = format)
53+
}
54+
55+
mdebug <- function(..., debug = getOption("future.debug", FALSE)) {
56+
if (!debug) return()
57+
message(now(), ...)
58+
}
5159

52-
mprintf <- function(...) message(sprintf(...), appendLF = FALSE)
60+
mdebugf <- function(..., appendLF = TRUE,
61+
debug = getOption("future.debug", FALSE)) {
62+
if (!debug) return()
63+
message(now(), sprintf(...), appendLF = appendLF)
64+
}
5365

54-
mprint <- function(...) {
55-
bfr <- capture_output(print(...))
56-
bfr <- paste(c(bfr, ""), collapse = "\n")
57-
message(bfr, appendLF = FALSE)
66+
#' @importFrom utils capture.output
67+
mprint <- function(..., appendLF = TRUE, debug = getOption("future.debug", FALSE)) {
68+
if (!debug) return()
69+
message(paste(now(), capture.output(print(...)), sep = "", collapse = "\n"), appendLF = appendLF)
5870
}
5971

60-
#' @importFrom utils str
61-
mstr <- function(...) {
62-
bfr <- capture_output(str(...))
63-
bfr <- paste(c(bfr, ""), collapse = "\n")
64-
message(bfr, appendLF = FALSE)
72+
#' @importFrom utils capture.output str
73+
mstr <- function(..., appendLF = TRUE, debug = getOption("future.debug", FALSE)) {
74+
if (!debug) return()
75+
message(paste(now(), capture.output(str(...)), sep = "", collapse = "\n"), appendLF = appendLF)
6576
}
6677

78+
6779
## From R.utils 2.0.2 (2015-05-23)
6880
hpaste <- function(..., sep="", collapse=", ", last_collapse=NULL,
6981
max_head=if (missing(last_collapse)) 3 else Inf,

tests/incl/start,load-only.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ is_false <- future.batchtools:::is_false
3737
is_na <- future.batchtools:::is_na
3838
is_os <- future.batchtools:::is_os
3939
hpaste <- future.batchtools:::hpaste
40-
mcat <- future.batchtools:::mcat
41-
mprintf <- future.batchtools:::mprintf
40+
mcat <- function(...) message(..., appendLF = FALSE)
41+
mprintf <- function(...) message(sprintf(...), appendLF = FALSE)
4242
mprint <- future.batchtools:::mprint
4343
mstr <- future.batchtools:::mstr
4444
printf <- future.batchtools:::printf

0 commit comments

Comments
 (0)