Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use structured logging #105

Merged
merged 55 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
1de95b1
Trying to utilize adafruit_logging package. Ran into memory allocatio…
blakejameson Jan 5, 2025
e6e8918
deleting adafruit_logging.mpy file
blakejameson Jan 5, 2025
4b943bf
added adafruit_logging to the lib requirements.txt
blakejameson Jan 5, 2025
f5e8b45
started working on Logger class
blakejameson Jan 6, 2025
990f8fd
created base functions for the Logger
blakejameson Jan 7, 2025
0d8f2f5
modifying Logger class implementation
blakejameson Jan 7, 2025
b8ccb55
removing FC_Board from repo
blakejameson Jan 7, 2025
2d21edf
instantiated Logger in main.py
blakejameson Jan 8, 2025
686d4fb
testing out different formatting for log output. Extra code remains t…
blakejameson Jan 8, 2025
f2b6deb
continuing to develop Logger class. still very early on in process
Jan 10, 2025
e03d93c
adding a debug vs print mode to the Logger class
Jan 14, 2025
95501e6
removing unneccesary output previously in logger.py, incorporated a f…
blakejameson Jan 14, 2025
f9f8c48
converting more debug print and print statements into logging calls
blakejameson Jan 15, 2025
8312998
changed debug_print and print statements in functions.py to be logger…
blakejameson Jan 15, 2025
2cab8c1
defined a Logger as a parameter for functions in cdh.py. This way, a …
blakejameson Jan 15, 2025
a5c9352
changed print statements in Field.py to be logger function calls
blakejameson Jan 15, 2025
57efb43
replace print statements with logger function calls in packet_manager
blakejameson Jan 15, 2025
528c8c7
replace print statements with logger function calls in packet_sender.py
blakejameson Jan 15, 2025
81ac401
changed debug_print calls in pysquared.py to use the logger instead. …
blakejameson Jan 16, 2025
e6e0b2b
refactored Face and AllFaces class constructor to take a logger as pa…
blakejameson Jan 16, 2025
30c77c3
had once missed a line of code I previously forgot to remove
blakejameson Jan 16, 2025
b9ef668
moved super secret code print statement from cdh.py to functions.py a…
blakejameson Jan 16, 2025
e8cefe2
needed to change function call, as I had updated the logger function …
blakejameson Jan 16, 2025
12e783f
changed hardware initialiation to use logger instead of printing the …
blakejameson Jan 16, 2025
786ff55
fixing a function call that forgot an argument
blakejameson Jan 16, 2025
f5f2545
defined the critical function in the Logger
blakejameson Jan 17, 2025
6d84457
changed print statements to Logger function calls in battery_helper.py
blakejameson Jan 17, 2025
bdb30a3
added logger as 3rd argument to Field constructor calls. added logger…
blakejameson Jan 17, 2025
aaeb9a7
added member variable to Logger class pertaining to the logger loggin…
blakejameson Jan 19, 2025
70e72bc
removed lines commented out with print and debug_print, as they were …
blakejameson Jan 21, 2025
acb31c3
refactored _scan_single_channel method to use logger methods instead …
blakejameson Jan 21, 2025
000bd26
removed enum that will no longer be used in the Logger class
blakejameson Jan 21, 2025
4b436cc
removed comments no longer needed
blakejameson Jan 21, 2025
abaa7a9
Finished rebasing then cleaned up a few resulting lint errors
blakejameson Jan 22, 2025
0079c5b
changed leftover debug_print function calls to Logger function calls.…
blakejameson Jan 22, 2025
6673f16
Structured logging: DRYing and adding comments (#109)
nateinaction Jan 24, 2025
898fb5a
removing the filename argument from the Logger function calls
blakejameson Jan 24, 2025
ae762ce
Update lib/pysquared/battery_helper.py
blakejameson Jan 24, 2025
bf0a94a
Update main.py
blakejameson Jan 24, 2025
4a447aa
Update lib/pysquared/battery_helper.py
blakejameson Jan 24, 2025
57a8cbc
Update lib/pysquared/battery_helper.py
blakejameson Jan 24, 2025
ff73609
removed the 'message=' from the function calls
blakejameson Jan 24, 2025
a8713d8
Merge branch 'main' into use-structured-logging
Jan 24, 2025
59b7d7d
replacing message strings with variables injected to be kwargs instead
blakejameson Jan 25, 2025
1776292
replacing more message strings with variables injected to be kwargs i…
blakejameson Jan 25, 2025
c25feef
continuing to remove variables from message strings, moving them into…
blakejameson Jan 25, 2025
8371060
removed more variables from message strings. created key/value pairs …
blakejameson Jan 25, 2025
ce37974
changed name of kwarg to be something other than msg, as that keyword…
blakejameson Jan 25, 2025
bcf3685
added tests to the other 4 log methods
blakejameson Jan 25, 2025
a3dfd8f
fixing unintended typo that somehow made its way in
blakejameson Jan 25, 2025
246c828
Applying suggestions from Nate's code review
blakejameson Jan 25, 2025
664022f
removing debug_print function definitions from many files. changed pa…
blakejameson Jan 25, 2025
b75082d
fixed PacketSender constructor function call to include Logger
blakejameson Jan 25, 2025
210c122
Apply suggestions from Nate's code review
blakejameson Jan 25, 2025
c53d83d
fixed typo in the word 'initializing' and type hinted in the Logger c…
blakejameson Jan 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/pysquared/Big_Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@


class Face:
def __init__(self, Add, Pos, debug_state, tca):
def __init__(self, Add, Pos, debug_state, tca, logger):
self.tca = tca
self.address = Add
self.position = Pos
self.debug = debug_state
self.logger = logger

# Use tuple instead of list for immutable data
self.senlist = ()
Expand Down Expand Up @@ -44,7 +45,7 @@ def Sensorinit(self, senlist, address):
self.mcp = adafruit_mcp9808.MCP9808(self.tca[address], address=27)
self.sensors["MCP"] = True
except Exception as e:
self.debug_print("[ERROR][Temperature Sensor]" + str(e))
self.logger.error(message="[Temperature Sensor]" + str(e))

if "VEML" in senlist:
try:
Expand All @@ -53,7 +54,7 @@ def Sensorinit(self, senlist, address):
self.veml = adafruit_veml7700.VEML7700(self.tca[address])
self.sensors["VEML"] = True
except Exception as e:
self.debug_print("[ERROR][Light Sensor]" + str(e))
self.logger.error(message="[Light Sensor]" + str(e))

if "DRV" in senlist:
try:
Expand All @@ -62,21 +63,22 @@ def Sensorinit(self, senlist, address):
self.drv = adafruit_drv2605.DRV2605(self.tca[address])
self.sensors["DRV"] = True
except Exception as e:
self.debug_print("[ERROR][Motor Driver]" + str(e))
self.logger.error(messagge="[Motor Driver]" + str(e))

gc.collect() # Clean up after initialization


class AllFaces:
def __init__(self, debug, tca):
def __init__(self, debug, tca, logger):
self.tca = tca
self.debug = debug
self.faces = []
self.logger = logger

# Create faces using a loop instead of individual variables
positions = [("y+", 0), ("y-", 1), ("x+", 2), ("x-", 3), ("z-", 4)]
for pos, addr in positions:
face = Face(addr, pos, debug, tca)
face = Face(addr, pos, debug, tca, self.logger)
face.Sensorinit(face.senlist, face.address)
self.faces.append(face)
gc.collect() # Clean up after each face initialization
Expand Down
24 changes: 13 additions & 11 deletions lib/pysquared/Field.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,41 @@
import traceback
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting for the future that we talked about removing traceback and we couldn't find a reason to keep it. We tried both in repl and found that reporting back the exception var e gave the same output but maybe we missed something. 🤷


from lib.pysquared.debugcolor import co
from lib.pysquared.logger import Logger


class Field:
def debug_print(self, statement):
if self.debug:
print(co("[Field]" + statement, "pink", "bold"))
nateinaction marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, cubesat, debug):
def __init__(self, cubesat, debug, logger: Logger):
self.debug = debug
self.cubesat = cubesat
self.logger = logger

def Beacon(self, msg):
try:
if self.cubesat.is_licensed:
self.debug_print("I am beaconing: " + str(msg))
print(
"Message Success: "
+ str(self.cubesat.radio1.send(bytes(msg, "UTF-8")))
self.logger.info(message="I am beaconing: " + str(msg))
self.logger.info(
message="Message Success: "
+ str(self.cubesat.radio1.send(bytes(msg, "UTF-8"))),
)
else:
self.debug_print(
"Please toggle licensed variable in code once you obtain an amateur radio license"
self.logger.debug(
message="Please toggle licensed variable in code once you obtain an amateur radio license",
)
except Exception as e:
self.debug_print(
"Tried Beaconing but encountered error: ".join(
self.logger.error(
message="Tried Beaconing but encountered error: ".join(
traceback.format_exception(e)
)
),
)

def troubleshooting(self):
# this is for troubleshooting comms
pass

def __del__(self):
self.debug_print("Object Destroyed!")
self.logger.debug(message="Object Destroyed!")
34 changes: 18 additions & 16 deletions lib/pysquared/battery_helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import time

from lib.pysquared.logger import Logger

# Written with Claude 3.5
# Author: Michael Pham
# Date: 2024-11-05
Expand All @@ -21,7 +23,7 @@ class BatteryHelper:
CMD_RESET_MCU = "11" # Reset microcontroller
CMD_ERROR = "208"

def __init__(self, pysquared):
def __init__(self, pysquared, logger: Logger):
"""
Initialize UART helper with existing Pysquared object

Expand All @@ -31,6 +33,7 @@ def __init__(self, pysquared):
self.uart = pysquared.uart
self.last_command_time = 0
self.debug_mode = True
self.logger = logger

def _flush_input(self):
"""Flush the input buffer"""
Expand All @@ -52,7 +55,7 @@ def _wait_for_ack(self):
if self.uart.in_waiting:
byte = self.uart.read(1)
if self.debug_mode:
print(f"ACK byte received: {byte}")
self.logger.info(message=f"ACK byte received: {byte}")
blakejameson marked this conversation as resolved.
Show resolved Hide resolved
if byte == b"A":
return True
time.sleep(0.001)
Expand Down Expand Up @@ -80,7 +83,7 @@ def _read_message(self, timeout_ms=150):
try:
text = response.decode("utf-8")
if self.debug_mode:
print(f"Buffer: {text}")
self.logger.info(message=f"Buffer: {text}")

# Check for complete message
if "AA<" in text and ">" in text:
Expand All @@ -89,8 +92,7 @@ def _read_message(self, timeout_ms=150):
if start_idx < end_idx:
return text[start_idx + 1 : end_idx]
except Exception as e:
print(f"Error decoding message: {e}")
pass
self.logger.error(message=f"Error decoding message: {e}")
blakejameson marked this conversation as resolved.
Show resolved Hide resolved

return ""

Expand All @@ -109,7 +111,7 @@ def _send_command(self, cmd):
return self._read_message()

except Exception as e:
print(f"UART error: {e}")
self.logger.error(message=f"UART error: {e}")
return ""

def _is_valid_message(self, msg):
Expand Down Expand Up @@ -155,10 +157,10 @@ def get_power_metrics(self):

except Exception as e:
if self.debug_mode:
print(f"Error parsing metrics: {e}")
self.logger.error(message=f"Error parsing metrics: {e}")

if self.debug_mode:
print("Failed to get valid power metrics")
self.logger.warning(message="Failed to get valid power metrics")
return (0.0, 0.0, 0.0, 0.0, False, 0.0)

def get_error_metrics(self):
Expand Down Expand Up @@ -255,8 +257,6 @@ def get_battery_percentage(self, pack_voltage, is_charging=False):
def debug_timing(self):
"""Measure and print timing of each step"""

print("\nTiming analysis:")

# Measure command send time
start = time.monotonic()
self.uart.write(bytes(self.CMD_GET_POWER.encode()))
Expand All @@ -275,14 +275,16 @@ def debug_timing(self):
values = [float(x) for x in parts[:4]]
values.append(bool(int(parts[4])))
except Exception as e:
print(f"Parse error: {e}")
self.logger.error(message=f"Parse error: {e}")
parse_time = (time.monotonic() - parse_start) * 1000

# Total time
total_time = (time.monotonic() - start) * 1000

print(f"Send time: {send_time:.2f}ms")
print(f"Read time: {read_time:.2f}ms")
print(f"Parse time: {parse_time:.2f}ms")
print(f"Total time: {total_time:.2f}ms")
print(f"Response: {response}")
self.logger.info(
send_time=f"{send_time:.2f}ms",
read_time=f"{read_time:.2f}ms",
parse_time=f"{parse_time:.2f}ms",
total_time=f"{total_time:.2f}ms",
response=f"{response}",
)
blakejameson marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading