Skip to content

Commit

Permalink
Improve logging within Flapi server
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Apr 13, 2024
1 parent 86a8399 commit c93a251
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion flapi/_consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
from enum import IntEnum

VERSION = (1, 0, 0)
VERSION = (1, 0, 1)
"""
The version of Flapi in the format (major, minor, revision)
"""
Expand Down
2 changes: 1 addition & 1 deletion flapi/server/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
from enum import IntEnum

VERSION = (1, 0, 0)
VERSION = (1, 0, 1)
"""
The version of Flapi in the format (major, minor, revision)
"""
Expand Down
20 changes: 14 additions & 6 deletions flapi/server/device_flapi_receive.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def send_stdout(text: str):

def OnInit():
print("\n".join([
"Flapi server",
"Flapi request server",
f"Server version: {'.'.join(str(n) for n in consts.VERSION)}",
f"Device name: {device.getName()}",
f"Device assigned: {bool(device.isAssigned())}",
Expand Down Expand Up @@ -97,7 +97,6 @@ def fl_exec(res: FlapiResponse, data: bytes):
statement = b64decode(data)
try:
# Exec in global scope so that the imports are remembered
# TODO: Give each client separate global and local scopes
exec(statement, connected_clients[res.client_id])
except Exception as e:
# Something went wrong, give the error
Expand All @@ -111,7 +110,6 @@ def fl_eval(res: FlapiResponse, data: bytes):
expression = b64decode(data)
try:
# Exec in global scope so that the imports are remembered
# TODO: Give each client separate global and local scopes
result = eval(expression, connected_clients[res.client_id])
except Exception as e:
# Something went wrong, give the error
Expand Down Expand Up @@ -161,10 +159,20 @@ def OnSysEx(event: 'FlMidiMsg'):
handler = message_handlers.get(message_type)

if handler is None:
return res.fail(message_type, f"Unknown message type {message_type}")
log.error(f"Unknown handler for message type {message_type}")
return res \
.fail(message_type, f"Unknown message type {message_type}") \
.send()

# Capture stdout for the duration of the operation
with capout(client_id):
handler(res, data)
try:
with capout(client_id):
handler(res, data)
except Exception as e:
log.error(f"Unhandled error for handler {handler}", exc_info=e)
res.fail(
message_type,
f"Unhandled exception in {handler}: {type(e).__name__}: {e}",
)

res.send()
13 changes: 11 additions & 2 deletions flapi/server/device_flapi_respond.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,29 @@
Flapi client.
"""
import device
from consts import MessageOrigin, MessageType, SYSEX_HEADER
from consts import MessageOrigin, MessageType, SYSEX_HEADER, VERSION

try:
from fl_classes import FlMidiMsg
except ImportError:
pass


def OnInit():
print("\n".join([
"Flapi response server",
f"Server version: {'.'.join(str(n) for n in VERSION)}",
f"Device name: {device.getName()}",
f"Device assigned: {bool(device.isAssigned())}",
f"FL Studio port number: {device.getPortNumber()}",
]))


# def print_msg(name: str, msg: bytes):
# print(f"{name}: {[hex(b) for b in msg]}")


def OnSysEx(event: 'FlMidiMsg'):

header = event.sysex[1:len(SYSEX_HEADER)+1] # Sysex header
# print_msg("Header", header)
# Remaining sysex data
Expand Down

0 comments on commit c93a251

Please sign in to comment.