[Logging-commits] r17 - in pkg: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Apr 8 08:04:25 CEST 2010


Author: mariotomo
Date: 2010-04-08 08:04:24 +0200 (Thu, 08 Apr 2010)
New Revision: 17

Modified:
   pkg/NAMESPACE
   pkg/R/logger.R
   pkg/man/addHandler.Rd
   pkg/man/logging-internal.Rd
   pkg/man/logging-package.Rd
   pkg/man/removeHandler.Rd
   pkg/man/setLevel.Rd
Log:
debugging, making true what is presented in the tutorial.



Modified: pkg/NAMESPACE
===================================================================
--- pkg/NAMESPACE	2010-04-07 15:11:04 UTC (rev 16)
+++ pkg/NAMESPACE	2010-04-08 06:04:24 UTC (rev 17)
@@ -10,9 +10,22 @@
        levellog,
        basicConfig,
        getLogger,
+       removeHandler,
+       removeHandler.default,
+       removeHandler.character,
        addHandler,
-       removeHandler,
+       addHandler.default,
+       addHandler.character,
+       getHandler,
+       getHandler.default,
+       getHandler.character,
        setLevel,
+       setLevel.numeric,
+       setLevel.character,
+       setLevel.default,
        writeToFile,
-       writeToConsole
+       writeToConsole,
+       updateOptions.environment,
+       updateOptions
+       
        )

Modified: pkg/R/logger.R
===================================================================
--- pkg/R/logger.R	2010-04-07 15:11:04 UTC (rev 16)
+++ pkg/R/logger.R	2010-04-08 06:04:24 UTC (rev 17)
@@ -53,9 +53,11 @@
     record$levelname <- paste("NumericLevel(", level, ")", sep='')
 
   ## invoke the action of all handlers associated to logger
-  for (handler in config$handlers) {
-    if (level >= handler$level) {
-      handler$action(handler$formatter(record), handler)
+  for (handler in config[['handlers']]) {
+    if (level >= with(handler, level)) {
+      action <- with(handler, action)
+      formatter <- with(handler, formatter)
+      action(formatter(record), handler)
     }
   }
 
@@ -66,7 +68,7 @@
     parent <- paste(removed, collapse='.')
     levellog(level, msg, ..., logger=parent)
   }
-  
+
   invisible()
 }
 
@@ -117,22 +119,22 @@
 }
 
 ## set properties of a logger or a handler
-updateOptions <- function(name, ...) {
-  if(name=='')
-    name <- 'logging.ROOT'
-  else
-    name <- paste('logging.ROOT', name, sep='.')
+updateOptions <- function(container, ...)
+  UseMethod('updateOptions')
 
-  config <- list(...)
+updateOptions.character <- function(container, ...) {
+  ## container is really just the name of the container
+  updateOptions.environment(getLogger(container), ...)
+}
+
+updateOptions.environment <- function(container, ...) {
+  ## the container is a logger
+  config <- list(...);
   if (! 'level' %in% names(config))
     config$level = loglevels['INFO']
-
-  # is there a logger by this name already?
-  if (! exists(name, logging.options))
-    logging.options[[name]] <- new.env()
-
   for (key in names(config))
-    logging.options[[name]][[key]] <- config[[key]]
+    container[[key]] <- config[[key]]
+  invisible()
 }
 
 ## Get a specific logger configuration
@@ -143,21 +145,30 @@
   else
     fullname <- paste('logging.ROOT', name, sep='.')
 
-  if (! exists(fullname, envir=logging.options)){
-    updateOptions(name, ...)
+  if(!exists(fullname, envir=logging.options)) {
+    logging.options[[fullname]] <- new.env()
+    logging.options[[fullname]][['handlers']] <- NULL
+    updateOptions.environment(logging.options[[fullname]], ...)
   }
-
   logging.options[[fullname]]
 }
 
 ## set the level of a handler or a logger
-setLevel <- function(name, level)
-{
-  if (level %in% names(loglevels))
-    level <- loglevels[level]
-  updateOptions(name, level=level)
+setLevel <- function(level, container='')
+  UseMethod('setLevel')
+
+setLevel.character <- function(level, container='') {
+  updateOptions(container, level=loglevels[level])
 }
 
+setLevel.numeric <- function(level, container='') {
+  updateOptions(container, level=level)
+}
+
+setLevel.default <- function(level, container='') {
+  NA
+}
+
 #################################################################################
 
 ## sample actions for handlers
@@ -200,7 +211,7 @@
 
 basicConfig <- function() {
   updateOptions('', level=loglevels['INFO'])
-  addHandler(writeToConsole, name='basic.stdout')
+  addHandler('basic.stdout', writeToConsole)
   invisible()
 }
 
@@ -211,25 +222,62 @@
 ##   action - the implementation for the handler. Either a function or a name of
 ##     a function
 ##   ... options to be stored as fields of new handler
-addHandler <- function(name, action, ..., level=20, logger='', formatter=defaultFormat)
+addHandler <- function(handler, ..., level=20, logger='', formatter=defaultFormat)
+  UseMethod('addHandler')
+
+addHandler.default <- function(handler, ..., level=20, logger='', formatter=defaultFormat) {
+  ## action <- handler # parameter 'handler' identifies the action
+  ## user did not provide a name for this handler, extract it from action.
+  addHandler.character(deparse(substitute(handler)), handler, ..., level=level, logger=logger, formatter=formatter)
+}
+
+addHandler.character <- function(handler, action, ..., level=20, logger='', formatter=defaultFormat)
 {
-  handlers <- getLogger(logger)[['handlers']]
+  name <- handler # parameter 'handler' identifies the name
+  handler <- new.env()
+  assign('level', level, handler)
+  assign('action', action, handler)
+  assign('formatter', formatter, handler)
+  handlers <- with(getLogger(logger), handlers)
+  handlers[[name]] <- handler
+  assign('handlers', handlers, envir=getLogger(logger))
 
-  handler <- list(level=level, action=action, formatter=formatter, ...)
-  handlers[name] <- list(handler) # this does not alter the original list
-
-  updateOptions(logger, handlers=handlers) # this replaces the original list
-  
   invisible()
 }
 
-removeHandler <- function(name, logger='') {
-  handlers <- getLogger(logger)[['handlers']]
-  to.keep <- !(names(handlers) == name)
-  updateOptions(logger, handlers=handlers[to.keep])
+removeHandler <- function(handler, logger='')
+  UseMethod('removeHandler')
+
+removeHandler.default <- function(handler, logger='') {
+  ## action <- handler # parameter 'handler' identifies the action
+  removeHandler.character(deparse(substitute(handler)), logger)
+}
+
+removeHandler.character <- function(handler, logger='') {
+  # parameter 'handler' identifies the name
+  handlers <- with(getLogger(logger), handlers)
+  to.keep <- !(names(handlers) == handler)
+  assign('handlers', handlers[to.keep], envir=getLogger(logger))
   invisible()
 }
 
+## retrieve a specific handler out of a logger.  loggers are separated
+## environments and handlers with the same name may be associated to
+## different loggers.
+
+getHandler <- function(handler, logger='')
+  UseMethod('getHandler')
+
+getHandler.default <- function(handler, logger='') {
+  ## action <- handler # assume we got the handler by action
+  getHandler.character(deparse(substitute(handler)), logger)
+}
+
+getHandler.character <- function(handler, logger='') {
+  ## name <- handler # we got the handler by name
+  with(getLogger(logger), handlers)[[handler]]
+}
+
 #################################################################################
 
 ## initialize the module

Modified: pkg/man/addHandler.Rd
===================================================================
--- pkg/man/addHandler.Rd	2010-04-07 15:11:04 UTC (rev 16)
+++ pkg/man/addHandler.Rd	2010-04-08 06:04:24 UTC (rev 17)
@@ -3,6 +3,8 @@
 
 \name{addHandler}
 \alias{addHandler}
+\alias{addHandler.default}
+\alias{addHandler.character}
 \title{add a handler to a logger}
 \description{
   
@@ -26,12 +28,15 @@
   
 }
 \usage{
-addHandler(name, action, ..., level=20, logger='',
+addHandler(handler, ..., level = 20, logger = "", formatter = defaultFormat)
+\method{addHandler}{default}(handler, ..., level=20, logger='',
                    formatter=defaultFormat)
+\method{addHandler}{character}(handler, action, ..., level=20, logger='',
+                   formatter=defaultFormat)
 }
 \arguments{
-  \item{name}{the name of the handler}
-  \item{action}{the implementation of the handler}
+  \item{handler}{the name of the handler, or its action}
+  \item{action}{the action of the handler, if you provided its name}
   \item{...}{extra parameters for the action, to be stored in the handler list}
   \item{level}{the level of the handler, defaults to 20 (INFO)}
   \item{logger}{the name of the logger to which to attach the new

Modified: pkg/man/logging-internal.Rd
===================================================================
--- pkg/man/logging-internal.Rd	2010-04-07 15:11:04 UTC (rev 16)
+++ pkg/man/logging-internal.Rd	2010-04-08 06:04:24 UTC (rev 17)
@@ -9,3 +9,5 @@
 }
 \alias{SVNVERSION}
 \alias{SVNDATE}
+\alias{updateOptions}
+\alias{updateOptions.environment}
\ No newline at end of file

Modified: pkg/man/logging-package.Rd
===================================================================
--- pkg/man/logging-package.Rd	2010-04-07 15:11:04 UTC (rev 16)
+++ pkg/man/logging-package.Rd	2010-04-08 06:04:24 UTC (rev 17)
@@ -61,8 +61,6 @@
 }
 \author{
   Mario Frasca <mariotomo at gmail.com>
-  
-  Brian Lee Yung Rowe <r at nurometic.com>
 
 Maintainer: Mario Frasca <mario.frasca at nelen-schuurmans.nl>
 }
@@ -71,5 +69,7 @@
 }
 \keyword{ package }
 \seealso{
-\code{\link[futile:futile-package]{http://cran.r-project.org/web/packages/futile/index.html}}
+  \code{\link[logging @ r-forge]{http://logging.r-forge.r-project.org/}}
+  
+  \code{\link[futile:futile-package]{http://cran.r-project.org/web/packages/futile/index.html}}
 }

Modified: pkg/man/removeHandler.Rd
===================================================================
--- pkg/man/removeHandler.Rd	2010-04-07 15:11:04 UTC (rev 16)
+++ pkg/man/removeHandler.Rd	2010-04-08 06:04:24 UTC (rev 17)
@@ -3,17 +3,21 @@
 
 \name{removeHandler}
 \alias{removeHandler}
+\alias{removeHandler.default}
+\alias{removeHandler.character}
 \title{remove a handler from a logger}
 \description{
   
-  removes the handler \var{name} from the logger.
+  removes the \var{handler} from the \var{logger}.
   
 }
 \usage{
-removeHandler(name, logger='')
+removeHandler(handler, logger='')
+\method{removeHandler}{default}(handler, logger='')
+\method{removeHandler}{character}(handler, logger='')
 }
 \arguments{
-  \item{name}{the name of the handler}
+  \item{handler}{the name or the action of the handler}
   \item{logger}{the name of the logger to which to attach the new
     handler, defaults to the root logger}
 }

Modified: pkg/man/setLevel.Rd
===================================================================
--- pkg/man/setLevel.Rd	2010-04-07 15:11:04 UTC (rev 16)
+++ pkg/man/setLevel.Rd	2010-04-08 06:04:24 UTC (rev 17)
@@ -3,16 +3,21 @@
 
 \name{setLevel}
 \alias{setLevel}
-\title{set logging.level for the named logger}
+\alias{setLevel.character}
+\alias{setLevel.numeric}
+\alias{setLevel.default}
+\title{set logging.level for the object}
 \description{
 
-  alter an existing logger, setting its logging.level to a new value.
+  alter an existing logger or handler, setting its logging.level to a
+  new value.  you can access loggers by name, while you must use
+  getHandler to get hold of a handler.
 
 }
 \usage{
-setLevel(name, level)
+setLevel(level, container)
 }
 \arguments{
-  \item{name}{the name of the logger.}
-  \item{level}{the new level for this logger.}
+  \item{level}{the new level for this object.}
+  \item{container}{a logger or a handler.}
 }



More information about the Logging-commits mailing list