0

I'm writing a file logging utility, using Rx buffer, for my Android app.

        val logBuffer = PublishSubject.create<String>()

        logBuffer
            .observeOn(Schedulers.computation())
            .buffer(10)
            .subscribeOn(Schedulers.io())
            .subscribe {
                // write to file
            }

It works well in normal use cases. Now, I want the ability to write/flush logs, on some specific event e.g. app kill or a specific error occurrence, even when buffer is not filled. Needed help in implementing the force emission of buffer. In my search, I was unable to find any related thing and additionally I'm new to Rx.

Asim Panra
  • 51
  • 2
  • If the app is "killed" how do you expect the process to complete this task? An application is not gracefully shutdown in this circumstance. – Mark Mar 17 '21 at 08:21
  • @MarkKeen I think we can do some quick operations in onDestroy event. – Asim Panra Mar 17 '21 at 08:22
  • You'll need to clarify what you mean by "app kill" - there is no guarantee `onDestroy` is called https://stackoverflow.com/a/11473952/4252352 – Mark Mar 17 '21 at 08:23
  • @MarkKeen App kill event is just an example. There can be any other event like a specific error occurrence in app code. The actual question is how to explicitly invoke a buffer emit. – Asim Panra Mar 17 '21 at 08:24
  • 1
    " kill event is just an example." - Yes, the only one you mentioned in your question - so you don't care in this circumstance? Do any flushing, if attached to some lifecycle, in `onPause` – Mark Mar 17 '21 at 08:26
  • @MarkKeen another use case is when user logs out – Asim Panra Mar 17 '21 at 08:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230013/discussion-between-asim-panra-and-mark-keen). – Asim Panra Mar 17 '21 at 08:31
  • just don't bother with the buffer - write line by line, appending a cyclic rolling log, or log files etc .. Can you provide evidence why the buffer is even required or adds value? – Mark Mar 17 '21 at 08:32
  • @MarkKeen I'm thinking of buffer to avoid writing to file on every log and instead do a number of logs at once. This is to have a smaller number of IO operations and better app performance. – Asim Panra Mar 17 '21 at 08:40
  • @MarkKeen I'm thinking of buffer to avoid writing to file on every log and instead do a number of logs at once. This is to have a smaller number of IO operations and better app performance. – Asim Panra Mar 17 '21 at 08:40
  • Well again, evidence, how chatty are your logs? How does it offer better app performance? Are these simply assumptions? Having "pending" writes in memory is bound to lead to issues, especially as the buffer has to be "full" to emit - probably will have less issues using `window` and filtering empty lists.? – Mark Mar 17 '21 at 09:03

0 Answers0