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