Skip to content

Commit

Permalink
better debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
yktoo committed Aug 18, 2024
1 parent c92cb48 commit bb6005e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
11 changes: 11 additions & 0 deletions lib/indicator_sound_switcher/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def __init__(self, name: str, description: str, num_sinks: int, num_sources: int
self.priority = priority
self.is_active = is_active

def get_id_text(self):
"""Return a descriptive identification text for the profile."""
return '`{}` ({})'.format(self.name, self.description)


class Card(GObject.GObject):
"""Card class."""
Expand Down Expand Up @@ -65,6 +69,13 @@ def find_stream_port(self, card_port, sources: dict, sinks: dict):
# No luck
return None, None

def get_active_profile(self) -> CardProfile | None:
"""Returns the currently active profile on the card, if any, otherwise None."""
for profile in self.profiles.values():
if profile.is_active:
return profile
return None

def get_property_str(self, name: str) -> str:
"""Returns value of a property by its name as a string."""
v = lib_pulseaudio.pa_proplist_gets(self.proplist, name.encode())
Expand Down
46 changes: 25 additions & 21 deletions lib/indicator_sound_switcher/indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def on_port_keyboard_shortcut(self, shortcut, data: list):
if idx_active < 0 and port.is_active:
idx_active = len(card_ports)-1
logging.debug(
' * card `%s`, port `%s` is currently active (index %d)', card.name, port.name, idx_active)
' * card `%s`, port %s is currently active (index %d)', card.name, port.get_id_text(), idx_active)

# If there's any port on the list
if card_ports:
Expand Down Expand Up @@ -436,14 +436,18 @@ def card_info(self, data):

# If card already exists, fetch it
if index in self.cards:
logging.debug(' * Card[%d] updated', index)
card = self.cards[index]
logging.debug(' * Card[%d] `%s` updated', index, card.name)

# Update active profile
cur_profile = card.get_active_profile()
for profile in card.profiles.values():
profile.is_active = profile.name == act_prof_name
if profile.is_active:
logging.debug(' * Activated profile: `%s` (`%s`)', profile.name, profile.description)
if profile.is_active and profile != cur_profile:
logging.debug(
' * Switched active profile: %s ⇒ %s',
cur_profile.get_id_text() if cur_profile else 'None',
profile.get_id_text())

# Update port availability
for new_port in card_ports.values():
Expand All @@ -452,8 +456,8 @@ def card_info(self, data):
if port.is_available != new_port.is_available:
port.is_available = new_port.is_available
logging.debug(
' * Port is made %savailable: `%s` (`%s`)',
'' if port.is_available else 'un', port.name, port.description)
' * Port is made %savailable: %s',
'' if port.is_available else 'un', port.get_id_text())

# Otherwise, register a new card object
else:
Expand All @@ -464,14 +468,14 @@ def card_info(self, data):
# Log profiles
for profile in card_profiles.values():
logging.debug(
' + Card profile added: `%s` (`%s`), %d sinks, %d sources, priority: %d%s',
profile.name, profile.description, profile.num_sinks, profile.num_sources, profile.priority,
' + Card profile added: %s, %d sinks, %d sources, priority: %d%s',
profile.get_id_text(), profile.num_sinks, profile.num_sources, profile.priority,
' -- Active' if profile.is_active else '')
# Log ports
for port in card_ports.values():
logging.debug(
' + Card port added: `%s` (`%s`); priority: %d; direction: %d; available: %s',
port.name, port.description, port.priority, port.direction, YESNO[port.is_available])
' + Card port added: %s; priority: %d; direction: %d; available: %s',
port.get_id_text(), port.priority, port.direction, YESNO[port.is_available])
if port.profiles:
for port_profile_name in port.profiles:
logging.debug(' . Supported profile: `%s`', port_profile_name)
Expand Down Expand Up @@ -536,11 +540,11 @@ def card_switch_profile(self, port, can_keep_current: bool) -> bool:
if port.pref_profile:
if port.pref_profile in profiles:
selected_profile = profiles[port.pref_profile]
logging.debug('* Preferred profile `%s` is specified for port `%s`', port.pref_profile, port.name)
logging.debug('* Preferred profile `%s` is specified for port %s', port.pref_profile, port.get_id_text())
else:
logging.warning(
'! Cannot activate preferred profile `%s` for port `%s` as this port doesn\'t support it',
port.pref_profile, port.name)
'! Cannot activate preferred profile `%s` for port %s as this port doesn\'t support it',
port.pref_profile, port.get_id_text())

# If no preferred profile given and the current one is fine, do nothing
if not selected_profile and can_keep_current:
Expand All @@ -552,13 +556,13 @@ def card_switch_profile(self, port, can_keep_current: bool) -> bool:

# Don't bother if the profile is already active (it won't help anyway)
if selected_profile.is_active:
logging.debug('* Profile `%s` is already active on card[%d]', selected_profile.name, card.index)
logging.debug('* Profile %s is already active on card[%d]', selected_profile.get_id_text(), card.index)
return False

# Switch the profile
logging.debug(
'* Switching card[%d] to profile `%s` with priority %d',
card.index, selected_profile.name, selected_profile.priority)
'* Switching card[%d] to profile %s with priority %d',
card.index, selected_profile.get_id_text(), selected_profile.priority)
self.synchronise_op(
'pa_context_set_card_profile_by_index()',
pa_context_set_card_profile_by_index(
Expand Down Expand Up @@ -630,8 +634,8 @@ def sink_info(self, data):
False)
sink_ports[port.name] = port
logging.debug(
' + Sink port added: `%s` (`%s`); priority: %d; available: %s',
port.name, port.description, port.priority, YESNO[port.is_available])
' + Sink port added: %s; priority: %d; available: %s',
port.get_id_text(), port.priority, YESNO[port.is_available])
idx_port += 1

# Create and register a new instance of Sink object (this will also set owner_stream in each port)
Expand Down Expand Up @@ -762,8 +766,8 @@ def source_info(self, data):
False)
source_ports[port.name] = port
logging.debug(
' + Source port added: `%s` (`%s`); priority: %d; available: %s',
port.name, port.description, port.priority, YESNO[port.is_available])
' + Source port added: %s; priority: %d; available: %s',
port.get_id_text(), port.priority, YESNO[port.is_available])
idx_port += 1

# Create and register a new instance of Source object (this will also set owner_stream in each port)
Expand Down Expand Up @@ -885,7 +889,7 @@ def activate_port(self, idx_card: int, stream_or_port):

port = card.ports[port_name]
is_output = port.is_output
logging.info('# Card[%d], port `%s` selected', idx_card, port.name)
logging.info('# Card[%d], port %s selected', idx_card, port.get_id_text())

# Try to find a matching stream
stream = card.find_stream_port(port, self.sources, self.sinks)[0]
Expand Down
4 changes: 4 additions & 0 deletions lib/indicator_sound_switcher/port.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def get_display_name(self) -> str:
"""Returns display name for the port."""
return self.display_name or self.description

def get_id_text(self):
"""Return a descriptive identification text for the port."""
return '`{}` ({})'.format(self.name, self.description)

def get_menu_item_title(self) -> str:
"""Returns the title to be used with menu item."""
# Port on a physical device
Expand Down

0 comments on commit bb6005e

Please sign in to comment.