Skip to content

Commit

Permalink
logging
Browse files Browse the repository at this point in the history
  • Loading branch information
beasteers committed Oct 31, 2023
1 parent f42b955 commit 1e4f892
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
7 changes: 6 additions & 1 deletion redis_record/record/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,14 @@ def record(
cmd.stop_recording(r)



def cli():
import logging
from tqdm.contrib.logging import logging_redirect_tqdm
import fire
fire.Fire(record)
logging.basicConfig(level=logging.DEBUG)
with logging_redirect_tqdm():
fire.Fire(record)

if __name__ == '__main__':
from pyinstrument import Profiler
Expand Down
6 changes: 6 additions & 0 deletions redis_record/storage/recorder/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

import logging

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)



class BaseRecorder:
def __init__(self, out_dir='.', schema=None):
Expand Down
4 changes: 4 additions & 0 deletions redis_record/storage/recorder/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from .base import BaseRecorder
from ...util import move_with_suffix

import logging

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)


class JsonRecorder(BaseRecorder):
Expand Down
13 changes: 9 additions & 4 deletions redis_record/storage/recorder/mcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import os
import time
import json
import tqdm
import base64
from mcap.writer import Writer
from .base import BaseRecorder
from ...config import DEFAULT_CHANNEL
from ...util import move_with_suffix

import logging

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)


class MCAPRecorder(BaseRecorder):
def __init__(self, schema=None, out_dir='.'):
self.out_dir = out_dir
Expand All @@ -35,7 +40,7 @@ def ensure_writer(self, record_name, force=False):
self.fhandle = fhandle = open(fname, "wb")
self.writer = writer = Writer(fhandle)
writer.start()
tqdm.tqdm.write(f"Opening writer {fname}")
log.info("Created Recorder: %s", fname)

# https://mcap.dev/docs/python/mcap-apidoc/mcap.well_known#mcap.well_known.MessageEncoding
kw = {"properties": self.schema} if self.schema else {}
Expand All @@ -48,7 +53,7 @@ def ensure_writer(self, record_name, force=False):

def ensure_channel(self, channel=DEFAULT_CHANNEL):
if channel not in self.channel_ids:
tqdm.tqdm.write(f"opening channel {channel}")
log.debug("Opening Recorder Channel: %s %s", self.fname, channel)
self.channel_ids[channel] = self.writer.register_channel(
schema_id=self.schema_id,
topic=channel,
Expand All @@ -67,11 +72,11 @@ def write(self, stream_id, timestamp, data):

def close(self):
if self.writer is not None:
tqdm.tqdm.write(f"closing {self.fname}")
self.writer.finish()
self.fhandle.close()
self.writer = self.fhandle = None
self.channel_ids.clear()
log.info("Closed Recorder: %s", self.fname)



Expand Down
8 changes: 8 additions & 0 deletions redis_record/storage/recorder/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
from .base import BaseRecorder
from ...util import format_epoch_time, move_with_suffix

import logging

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)


MB = 1024 * 1024

class ZipRecorder(BaseRecorder):
Expand All @@ -24,6 +30,7 @@ def close(self):
if self.writer:
for _, w in self.writer.items():
w.close()
log.info("Closed Recorder: %s", self.recording_dir)
self.writer = None

def ensure_writer(self, record_name, force=False):
Expand All @@ -34,6 +41,7 @@ def ensure_writer(self, record_name, force=False):
self.recording_dir = os.path.join(self.out_dir, record_name)
move_with_suffix(self.recording_dir)
os.makedirs(self.recording_dir, exist_ok=True)
log.info("Created Recorder: %s", self.recording_dir)
return self

def ensure_channel(self, channel):
Expand Down
10 changes: 10 additions & 0 deletions redis_record/storage/replay/mcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
from mcap.reader import make_reader
from redis_record.config import RECORDING_DIR

import logging

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)


class MCAPPlayer:
def __init__(self, name, recording_dir=RECORDING_DIR, subset=None) -> None:
self.path = name if os.path.isfile(name) else os.path.join(recording_dir, f'{name}.mcap')
self.subset = subset
log.info("Created Player: %s - streams: %s", self.path, self.subset or 'all')

def __enter__(self):
self.fh = open(self.path, "rb")
Expand All @@ -17,7 +23,11 @@ def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, trace):
self.close()

def close(self):
self.fh.close()
log.info("Closed Player: %s - streams: %s", self.path, self.subset or 'all')

def __iter__(self):
yield from self.iter_messages()
Expand Down
4 changes: 3 additions & 1 deletion redis_record/storage/replay/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import logging

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
log.setLevel(logging.INFO)


class ZipPlayer:
Expand All @@ -24,6 +24,7 @@ def __init__(self, path, recording_dir=RECORDING_DIR, subset=None, raw_timestamp
self.file_end_timestamps = {}
self.queue = queue.PriorityQueue()
self.raw_timestamp = raw_timestamp
log.info("Created Player: %s - streams: %s", self.recording_dir, self.subset or 'all')

self._load_file_index()
for stream_id in self.file_index:
Expand Down Expand Up @@ -82,6 +83,7 @@ def close(self):
for zf in self.zipfh.values():
zf.close()
self.zipfh.clear()
log.info("Closed Player: %s - streams: %s", self.recording_dir, self.subset or 'all')

def _get_time_range_from_file(self, fname):
t0, t1 = fname.split(os.sep)[-1].removesuffix('.zip').split('_')
Expand Down

0 comments on commit 1e4f892

Please sign in to comment.