]> git.donarmstrong.com Git - cran2deb.git/blob - branch/double_build/R/log.R
3d74bae7221123116434b59d94747fdb38bdd532
[cran2deb.git] / branch / double_build / R / log.R
1 log_messages <- list()
2
3 log_clear <- function() {
4     assign('log_messages',list(),envir=.GlobalEnv)
5 }
6
7 log_add <- function(text,print=T) {
8     if (print) {
9         message(text)
10     }
11     assign('log_messages',c(log_messages, text),envir=.GlobalEnv)
12 }
13
14 log_retrieve <- function() {
15     return(log_messages)
16 }
17
18 notice <- function(...) {
19     log_add(paste('N:',...))
20 }
21
22 warn <- function(...) {
23     log_add(paste('W:',...))
24 }
25
26 error <- function(...) {
27     log_add(paste('E:',...))
28 }
29
30 fail <- function(...) {
31     txt <- paste('E:',...)
32     log_add(txt)
33     stop(txt)
34 }
35
36 log_system <- function(...) {
37     r <- try((function() {
38         # pipe() does not appear useful here, since
39         # we want the return value!
40         # XXX: doesn't work with ; or | !
41         tmp <- tempfile('log_system')
42         on.exit(unlink(tmp))
43         cmd <- paste(...)
44         # unfortunately this destroys ret
45         #cmd <- paste(cmd,'2>&1','| tee',tmp)
46         cmd <- paste(cmd,'>',tmp,'2>&1')
47         ret <- system(cmd)
48         f <- file(tmp)
49         output <- readLines(f)
50         close(f)
51         unlink(tmp)
52         return(list(ret,output))
53     })())
54     if (inherits(r,'try-error')) {
55         fail('system failed on:',paste(...))
56     }
57     log_add(paste('C:',...))
58     for (line in r[[2]]) {
59         if (!length(grep('^[WENI]:',line))) {
60             line = paste('I:',line)
61         }
62         log_add(line) #,print=F)
63     }
64     return(r[[1]])
65 }
66