File: log.R

package info (click to toggle)
r-bioc-biocparallel 1.40.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,768 kB
  • sloc: cpp: 139; sh: 14; makefile: 8
file content (93 lines) | stat: -rw-r--r-- 2,395 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
.log_data <- local({
    env <- new.env(parent=emptyenv())
    env[["buffer"]] <- character()
    env
})

.log_load <- function(log, threshold)
{
    if (!log) {
        if (isNamespaceLoaded("futile.logger")) {
            futile.logger::flog.appender(
                futile.logger::appender.console(),
                'ROOT'
                )
        }
        return(invisible(NULL))
    }

    ## log == TRUE
    if (!isNamespaceLoaded("futile.logger"))
        tryCatch({
            loadNamespace("futile.logger")
        }, error=function(err) {
            msg <- "logging requires the 'futile.logger' package"
            stop(conditionMessage(err), msg)
        })
    futile.logger::flog.appender(.log_buffer_append, 'ROOT')
    futile.logger::flog.threshold(threshold)
    futile.logger::flog.info("loading futile.logger package")
}

.log_warn <- function(log, fmt, ...)
{
    if (log)
        futile.logger::flog.warn(fmt, ...)
}

.log_error <- function(log, fmt, ...)
{
    if (log)
        futile.logger::flog.error(fmt, ...)
}

## logging buffer

.log_buffer_init <- function()
{
    .log_data[["buffer"]] <- character()
}

.log_buffer_append <- function(line)
{
    .log_data[["buffer"]] <- c(.log_data[["buffer"]], line)
}

.log_buffer_get <- function()
{
    .log_data[["buffer"]]
}


### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### logs and results printed in the manager process
###

.bpwriteLog <- function(con, d) {
    .log_internal <- function() {
        message(
            "############### LOG OUTPUT ###############\n",
            "Task: ", d$value$tag,
            "\nNode: ", d$node,
            "\nTimestamp: ", Sys.time(),
            "\nSuccess: ", d$value$success,
            "\n\nTask duration:\n",
            paste(capture.output(d$value$time), collapse="\n"),
            "\n\nMemory used:\n", paste(capture.output(gc()), collapse="\n"),
            "\n\nLog messages:\n",
            paste(trimws(d$value$log), collapse="\n"),
            "\n\nstderr and stdout:\n",
            if (!is.null(d$value$sout))
                paste(noquote(d$value$sout), collapse="\n")
        )
    }
    if (!is.null(con)) {
        on.exit({
            sink(NULL, type = "message")
            sink(NULL, type = "output")
        })
        sink(con, type = "message")
        sink(con, type = "output")
    }
    .log_internal()
}