Skip to content
Ben Tupper edited this page Jun 5, 2015 · 1 revision

Logging messages and keeping time

A simple logger to allow info/warn/error messaging. Logged messages are possibly sent to a regular file, but the user can opt to echo the messages to the console. Also included is a simple StopwatchRefClass class for multi-part timing as well as a TimekeeperRefClass which will manage one or more stopwatches. Here is a rough outline of the 'is-as' and 'has-a' relationships among these classes

  • A LoggerRefClass object contains a TimekeeperRefClass object.
  • A TimekeeperRefClass object contains a list of one or more StopwatchRefClass objects referred to by 'stopwatch' name or index position
  • A StopwatchRefClass contains one or more intervals referred to by 'interval' name or index position }

Using Logger

    X <- Logger(name = 'logger-example', file = 'test.log', start_watch = TRUE)
    X$warn( "You gotta big problem here!", do_sprintf = FALSE)
    X$info("I like %s", "sailing", do_timestamp = FALSE)
    X$error("I ate all the %0.2f", pi, do_echo = FALSE)
    X$elapsed() 

Using Stopwatch

# An example with Stopwatch
SW <- Stopwatch(name = 'timex', start = FALSE)
#start it
SW$start()
SW$elapsed()
SW
Sys.sleep(2)
# When stopping you can name the interval for later retreival
SW$stop(interval = 'first')
SW
SW$elapsed(interval = "first")
SW$start()
Sys.sleep(2)
SW$stop(interval = "second")
Sys.sleep(1)
SW$elapsed(interval = 'second')
SW$elapsed(interval = 'first')
# now accumulate the elapsed times
SW$elapsed(accumulate = TRUE)

Using Stopwatch

An example that creates a Timekeeper container class, starting a stopwatch right away

timer <- Timekeeper(start_watch = TRUE, name = 'master')
Sys.sleep(3)
# add another stopwatch
timer$start("two")
timer
timer$stop("two", interval = "another")
timer$elapsed("master")
Clone this wiki locally