groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Merlin Beedell <MBeed...@cryoserver.com>
Subject Use of SystemOutputInterceptor with different groovy versions
Date Thu, 09 Nov 2017 18:15:16 GMT
I also noted that the following code would work just fine in recent Groovy version, but not
in older.  The idea is to intercept the stdout (System.out) so any output can be logged to
a text file as well as to the console [really rather handy!].


import groovy.ui.SystemOutputInterceptor

dateString = new Date().format("yyyyMMMdd-HHmmss")

logOutput = new PrintWriter(new File("./","scriptLog_" + dateString +".log"))  //log into
the current directory

myLoger = new SystemOutputInterceptor(

   {Integer i, String s -> logOutput << s; logOutput.flush(); true }

)

myLoger.start()

In older groovy versions, it seems that the Integer parameter is not passed to the closure
(and the 'start' method is not required either):


myLoger = new SystemOutputInterceptor(

{Integer i, String s -> logOutput << s; logOutput.flush(); true }

)

myLoger.start()

But I can't think how I can specify this code so it works for all versions of Groovy.  This
is the error if it is wrong (here under Groovy 2.0.5):


Caught: groovy.lang.MissingMethodException: No signature of method: test$_run_closure2.doCall()
is applicable for argument types: (java.lang.Integer, java.lang.String) values: [0,  Hello
world]

Possible solutions: doCall(java.lang.String), findAll(), findAll()

It is as though I need to be able to provide two closure definitions - one with and one without
the Integer parameter.
Any ideas?

Merlin

Mime
View raw message