Skip to content

Commit

Permalink
added file and lines as metadata for loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
markvanderloo committed May 8, 2020
1 parent 6236f41 commit e776441
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
7 changes: 7 additions & 0 deletions pkg/NEWS
@@ -1,3 +1,10 @@
version 1.1.5
- The 'meta' argument that loggers receive now contains two new
elements: file and lines, referring to the file beign logged
and the line numbers where the expression was read. (thanks
to anonymous reviewer B for suggesting).
- Fixed typos in documentation (thanks to anonymous reviewer B)

version 1.1.4
- When the $add() method of a loggger is called by lumberjack it now passes
a 'meta' argument containing an 'src' string that is taken from the 'srcref'
Expand Down
14 changes: 11 additions & 3 deletions pkg/R/run.R
Expand Up @@ -90,10 +90,12 @@ dump_capture <- function(store){
}
}

update_loggers <- function(store, envir, expr, src){
update_loggers <- function(store, envir, expr, src, file, lines){
datasets <- ls(store)
meta <- list(expr = expr
, src = src)
, src = src
, file = file
, line = lines)

for ( dataset in datasets ){
old <- store[[dataset]]$data
Expand Down Expand Up @@ -170,7 +172,13 @@ run_file <- function(file, auto_dump=TRUE, envir=NULL){

for ( i in seq_along(prog) ){
eval(prog[[i]], envir=envir)
update_loggers(store, envir, prog[[i]], paste(as.character(src[[i]]),collapse="\n") )
lines <- c(first = src[[i]][1], last = src[[i]][3])
update_loggers(store = store
, envir = envir
, expr = prog[[i]]
, src = paste(as.character(src[[i]]),collapse="\n")
, file = fname
, lines = lines )
}
# dump everything not dumped yet.
if (auto_dump) eval(envir$dump_log(), envir=envir)
Expand Down
2 changes: 1 addition & 1 deletion pkg/inst/tinytest/test_logging_infra.R
Expand Up @@ -6,7 +6,7 @@ expect_false( is.null(get_log(start_log(1:3))) )
expect_true( is.null(get_log(stop_log(start_log(1:3), dump=FALSE))) )


## Logging does not depend on functins keeping attributes
## Logging does not depend on functions keeping attributes
naughty_function <- function(x){
attr(x, lumberjack:::LOGNAME) <- NULL
x
Expand Down
14 changes: 10 additions & 4 deletions pkg/vignettes/using_lumberjack.Rnw
Expand Up @@ -369,20 +369,26 @@ The main differences with `magrittr` are that
\end{itemize}

\section{Extending lumberjack}
There are many ways to register changes in data. That is why
\pkg{lumberjack} is extensible with new loggers.
There are many ways to register changes in data. That is why \pkg{lumberjack}
is extensible with new loggers.

\subsection{The lumberjack logging API}

In short, a logger is a \emph{reference object} with the following \emph{mandatory} elements:
In short, a logger is a \emph{reference object} with the following
\emph{mandatory} elements:

\begin{enumerate}
\item A method \code{\$add(meta, input, output)}. This is a function that
computes the difference between \code{input} and \code{output} and adds it to a
log. The \code{meta} argument is a \code{list} with two elements:
log. The \code{meta} argument is a \code{list} with the following elements:
\begin{itemize}
\item \code{expr} The expression used to turn \code{input} into \code{output}.
\item \code{src} The same expression, but turned into a string.
\item \code{file} The name of the file that was run. This element is only
available when code is run from a script.
\item \code{lines} A named \code{integer} vector containing the first and last
line of the expression in the source file. This element is only available
when code is run from a script.
\end{itemize}
\item A method \code{\$dump(...)} this function writes the current logging info
somewhere. Often this will be a file, but it really can be any place where R
Expand Down

0 comments on commit e776441

Please sign in to comment.