library(stringi)
library(purrr)

warning_log <- list()

capture_warning <- function(code) {
  warnings <- character()
  wHandler <- function(w) {
    warnings <<- c(warnings, w$message)
    invokeRestart("muffleWarning")
  }
  
  messages <- character()
  mHandler <- function(m) {
    messages <<- c(messages, m$message)
    invokeRestart("muffleMessage")
  }
  
  result <- withCallingHandlers(
    code,
    warning = wHandler,
    message = mHandler
  )
  
  warning_log <<- append(warning_log, 
                         list(list(warnings = warnings,
                                   messages = messages)))
  
  result
  
}

catchingly <- function(.f) {
  .f <- as_function(.f)
  function(...) capture_warning(.f(...))
}

bad_function <- function() {
  warning(stri_rand_lipsum(1, FALSE))
  message(sprintf("SHOULD NOT DISPLAY: %s", Sys.time()))
  return(Sys.time())
}

W_bad_function <- catchingly(bad_function)

set.seed(1492)

for (i in 1:10) {
  print(W_bad_function())
}
## [1] "2016-03-21 12:38:37 EDT"
## [1] "2016-03-21 12:38:37 EDT"
## [1] "2016-03-21 12:38:37 EDT"
## [1] "2016-03-21 12:38:37 EDT"
## [1] "2016-03-21 12:38:37 EDT"
## [1] "2016-03-21 12:38:37 EDT"
## [1] "2016-03-21 12:38:37 EDT"
## [1] "2016-03-21 12:38:37 EDT"
## [1] "2016-03-21 12:38:37 EDT"
## [1] "2016-03-21 12:38:37 EDT"
print(str(warning_log))
## List of 10
##  $ :List of 2
##   ..$ warnings: chr "Ex, ante eu interdum, et. Torquent massa felis. Turpis penatibus non dictumst sagittis mauris cubilia, elit magna ac sed. Tinci"| __truncated__
##   ..$ messages: chr "SHOULD NOT DISPLAY: 2016-03-21 12:38:37\n"
##  $ :List of 2
##   ..$ warnings: chr "Semper tortor, blandit nisl condimentum accumsan felis. Quam vel in, eros lorem at fermentum ac, dui ut dignissim. Vivamus metu"| __truncated__
##   ..$ messages: chr "SHOULD NOT DISPLAY: 2016-03-21 12:38:37\n"
##  $ :List of 2
##   ..$ warnings: chr "Ut lorem sit ornare mauris libero leo. Aenean et ullamcorper, sed montes. Montes ante posuere ac ligula hendrerit velit mi just"| __truncated__
##   ..$ messages: chr "SHOULD NOT DISPLAY: 2016-03-21 12:38:37\n"
##  $ :List of 2
##   ..$ warnings: chr "Malesuada, ut diam porta iaculis. A, vitae. Dolor, tempus nibh ac. Urna sit iaculis mauris euismod. In mauris, massa integer er"| __truncated__
##   ..$ messages: chr "SHOULD NOT DISPLAY: 2016-03-21 12:38:37\n"
##  $ :List of 2
##   ..$ warnings: chr "Ac ex vel habitasse ridiculus, sed sociosqu faucibus, in risus! Nulla natoque turpis sed nisi elementum tincidunt leo pellentes"| __truncated__
##   ..$ messages: chr "SHOULD NOT DISPLAY: 2016-03-21 12:38:37\n"
##  $ :List of 2
##   ..$ warnings: chr "Leo ut venenatis interdum, congue consectetur non magnis erat vehicula erat mauris cras. Non tempus nisi aenean etiam. Litora h"| __truncated__
##   ..$ messages: chr "SHOULD NOT DISPLAY: 2016-03-21 12:38:37\n"
##  $ :List of 2
##   ..$ warnings: chr "Imperdiet a, neque nec orci luctus eu quam feugiat nullam, sit diam. Tellus vehicula sem lacus phasellus est integer, vulputate"| __truncated__
##   ..$ messages: chr "SHOULD NOT DISPLAY: 2016-03-21 12:38:37\n"
##  $ :List of 2
##   ..$ warnings: chr "Justo in condimentum torquent et sed accumsan, curae, urna venenatis magna a ligula sapien consectetur? Orci et at ad dolor ame"| __truncated__
##   ..$ messages: chr "SHOULD NOT DISPLAY: 2016-03-21 12:38:37\n"
##  $ :List of 2
##   ..$ warnings: chr "Pulvinar amet placerat dignissim pharetra eu ipsum, tortor. Eros magna tempor pulvinar ut posuere elementum id at magna. Turpis"| __truncated__
##   ..$ messages: chr "SHOULD NOT DISPLAY: 2016-03-21 12:38:37\n"
##  $ :List of 2
##   ..$ warnings: chr "Purus, habitasse vestibulum erat. Lobortis aliquam, vel mollis? Odio sed sociosqu lacus faucibus in consectetur finibus. Proin "| __truncated__
##   ..$ messages: chr "SHOULD NOT DISPLAY: 2016-03-21 12:38:37\n"
## NULL