blob: 5bb622975cf72fbb5b826624c2794f975c2b30a4 [file] [log] [blame] [edit]
# Copyright 2023 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import json
import typing
import event
async def writer(
recorder: event.EventRecorder,
out_stream: typing.TextIO,
):
"""Asynchronously serialize events to the given stream.
Args:
recorder (event.EventRecorder): The source of events to
drain. Continues until all events are written.
out_stream (typing.TextIO): Output text stream.
"""
value: event.Event
async for value in recorder.iter():
try:
json.dump(value.to_dict(), out_stream) # type:ignore
out_stream.write("\n")
# Eagerly flush after each line. This task may terminate at any time,
# including from an interrupt, so this ensures we at least see
# the most recently written lines.
out_stream.flush()
except TypeError as e:
print(f"LOG ERROR: {e} {value}")