Adam Cameron
SystemOutput
(https://docs.lucee.org/reference/functions/systemoutput.html) to write to stdout and stderr. Seems to be working well.
I was hoping to write an test for the code, so wanted to listen to stdout and stderr as part of that. Anyone done this? I looked on S/O for a Java approach to it, and thusfar the answers have all been along the lines of "ooooh... that isn't easy, so do it this other way instead". The other ways are not helpful to me in this case (although it's good knowledge to know)./usr/local/lib/serverHome/logs/server.out.txt
it seems. That'll do.
Now I just need to work out how to tail a file in Lucee... 😄zackster
03/23/2022, 2:28 PMsystemOutput(getApplicationSettings())
bdw429s
03/23/2022, 7:21 PMSystem.out
print reader, but it's certainly a painAdam Cameron
import test.BaseSpec
component extends=BaseSpec {
function run() {
describe("Trying to capture stdout", () => {
var fixtures = {}
aroundEach((spec) => {
try {
fixtures.outFilePath = getTempDirectory() & createUuid() & ".out"
fixtures.system = createObject("java", "java.lang.System")
var originalOut = fixtures.system.out
var fos = createObject("java", "java.io.FileOutputStream").init(fixtures.outFilePath)
var ps = createObject("java", "java.io.PrintStream").init(fos, true)
fixtures.system.setOut(ps)
spec.body()
} finally {
fixtures.system.setOut(originalOut)
}
})
it("captures stuff Java puts there", () => {
var testString = "find this (Java)"
fixtures.system.out.println(testString)
var contents = fileRead(fixtures.outFilePath)
expect(contents).toBe(testString & chr(10))
})
it("doesn't capture stuff Lucee puts there", () => {
systemOutput("find this (Lucee)")
var contents = fileRead(fixtures.outFilePath)
expect(contents).toBeEmpty() // except I wish this *wasn't* the case
})
})
}
}
https://i2.paste.pics/0fb1c7acd3335bfe9ee7c18e0f363eea.png▾
bdw429s
03/23/2022, 9:31 PMsystemOutput()
function doesn't just internally do this
System.out.println( yourText )
Instead, it does this
PrintStream stream = CFMLEngineImpl.CONSOLE_OUT;
stream.println(string);
And that CONSOLE_OUT
is a static variable set when the CFMLEngine is constructed like so
public static final PrintStream CONSOLE_ERR = System.err;
public static final PrintStream CONSOLE_OUT = System.out;
This means Lucee is going to print your string to whatever the print stream WAS back when Lucee started. Which, for the sake of discussion is a wrapper, but that's mostly beside the point. What Micha never considered was the possibility that someone would try to do their own swap in CFML at runtime.public
and final
so while you can access them, you probably can't change them at runtime.Adam Cameron
bdw429s
03/23/2022, 9:33 PMByteArrayOutputStream
instead of a file ouput stream. It will buffer the input internally and you can just grab its contents when your test is done.Adam Cameron
bdw429s
03/23/2022, 9:34 PMBAOS.toString( "UTF-8" )
to get the bytes back out as a stringAdam Cameron
bdw429s
03/23/2022, 9:34 PMAdam Cameron