Reputation: 134
I'm calling an AVFoundation
method that is logging a lot of data to stdout
and there's no option to turn this logging off.
Is it possible to write something like a middleware to process the data before sending it to stdout
?
Something like this pseudocode:
Process.beforePrint( data => {
if (data !== blackListedData) {
print(data);
}
});
Upvotes: 3
Views: 372
Reputation: 66252
There are a number of options you can employ using the stdio
functions. Here's a simple one that redirects stdout to a log file:
class StdoutFilter {
let path: String = {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
return (paths[0] as NSString).stringByAppendingPathComponent("swift.log")
}()
func startFilter() {
freopen(path, "w", stdout)
}
func stopFilter() {
fclose(stdout)
}
}
You can use this trivial implementation like this:
let filter = StdoutFilter()
print("This goes to stdout…")
filter.startFilter()
print("This gets logged to the file…")
print("Put your AVFoundation stuff here.")
filter.stopFilter()
print("And we're back to normal!")
This could be modified in a few obvious ways. You could redirect the output to /dev/null
, for example. You could also try monitoring the file and applying a predicate to determine whether the filter should be started or stopped, but I think that's more complex than I have time for at the moment.
One final note: "w"
in the startFilter()
function will overwrite the file if it exists. Use "a"
if you prefer to append an existing file.
Upvotes: 3