Skip to content

Commit

Permalink
add logger for proper logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Saskia Kohn committed Feb 26, 2024
1 parent 76bf821 commit fed62ea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
13 changes: 7 additions & 6 deletions unisens/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def __init__(self, attrib=None, parent='.', **kwargs):
for key in kwargs:
self.set_attrib(key, kwargs[key])
self._autosave()
self.log = logging.getLogger('unisens_logger')

def __contains__(self, item):
if item in self.__dict__: return True
Expand Down Expand Up @@ -215,15 +216,15 @@ def _set_channels(self, ch_names: List[str], n_data: int):
# this means new channel names are indicated and will overwrite.
assert len(ch_names) == n_data, f'len {ch_names}!={n_data}'
if hasattr(self, 'channel'):
logging.warning('Channels present will be overwritten')
self.log.warning('Channels present will be overwritten')
self.remove_entry('channel')
for name in ch_names:
channel = MiscEntry('channel', key='name', value=name)
self.add_entry(channel)
elif not hasattr(self, 'channel'):
# this means no channel names are indicated and none exist
# we create new generic names for the channels
logging.info('No channel names indicated, will use generic names')
self.log.info('No channel names indicated, will use generic names')
for i in range(n_data):
channel = MiscEntry('channel', key='name', value=f'ch_{i}')
self.add_entry(channel)
Expand Down Expand Up @@ -370,7 +371,7 @@ def remove_attr(self, name: str):
del self.attrib[name]
del self.__dict__[name]
else:
logging.error('{} not in attrib'.format(name))
self.log.error('{} not in attrib'.format(name))
self._autosave()
return self

Expand Down Expand Up @@ -435,12 +436,12 @@ def __init__(self, id, attrib=None, parent='.', **kwargs):
valid_filename(self.id)
self._filename = os.path.join(self._folder, self.id)
if not os.access(self._filename, os.F_OK):
logging.error('File {} does not exist'.format(self.id))
self.log.error('File {} does not exist'.format(self.id))
elif id:
# writing entry
valid_filename(id)
if os.path.splitext(str(id))[-1] == '':
logging.warning('id should be a filename with extension ie. .bin')
self.log.warning('id should be a filename with extension ie. .bin')
self._filename = os.path.join(self._folder, id)
self.set_attrib('id', id)
# ensure subdirectories exist to write data
Expand Down Expand Up @@ -624,7 +625,7 @@ def __init__(self, id=None, attrib=None, parent='.',
assert decimalSeparator and separator, 'Must supply separators'

if not self.id.endswith('csv'):
logging.warning(f'id "{id}" does not end in .csv')
self.log.warning(f'id "{id}" does not end in .csv')

csvFileFormat = MiscEntry('csvFileFormat', parent=self)
csvFileFormat.set_attrib('decimalSeparator', decimalSeparator)
Expand Down
7 changes: 4 additions & 3 deletions unisens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def __init__(self, folder: str, makenew=False, autosave=False, readonly=False,
:param attrib: The attribute
:param convert_nums: try to convert numbers from attribs automatically
"""
self.log = logging.getLogger('unisens_logger')
assert autosave != readonly or not autosave and not readonly, \
'either read-only or autosave can be enabled'
assert isinstance(folder, str), f'folder must be string, is {folder}'
Expand All @@ -79,12 +80,12 @@ def __init__(self, folder: str, makenew=False, autosave=False, readonly=False,
self._convert_nums = convert_nums

if os.path.isfile(self._file) and not makenew:
logging.debug('loading unisens.xml from {}'.format(self._file))
self.log.debug('loading unisens.xml from {}'.format(self._file))
with warnings.catch_warnings():
warnings.simplefilter("ignore")
self.read_unisens()
else:
logging.debug('New unisens.xml will be created at {}'.format(self._file))
self.log.debug('New unisens.xml will be created at {}'.format(self._file))
if not timestampStart:
now = datetime.datetime.now()
timestampStart = now.strftime('%Y-%m-%dT%H:%M:%S')
Expand Down Expand Up @@ -189,7 +190,7 @@ def unpack_element(self, element: (Element, ET)) -> Entry:
entry = MiscEntry(name=name, attrib=attrib, parent=self._folder)
else:
if not 'Entry' in element.tag:
logging.warning('Unknown entry type: {}'.format(entryType))
self.log.warning('Unknown entry type: {}'.format(entryType))
name = element.tag
entry = MiscEntry(name=name, attrib=attrib, parent=self._folder)

Expand Down

0 comments on commit fed62ea

Please sign in to comment.