Skip to content

Commit 0970666

Browse files
committed
♻️ Logging improvements & fixes
1 parent 64383bb commit 0970666

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

octoprint_ws281x_led_status/runner/__init__.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __init__(
5454

5555
self._logger = logging.getLogger("octoprint.plugins.ws281x_led_status.runner")
5656
self.setup_custom_logger(log_path, debug)
57+
self._logger.debug("Starting WS281x LED Status Effect runner")
5758

5859
self.segment_manager = None # type Optional[segments.SegmentManager]
5960

@@ -63,7 +64,6 @@ def __init__(
6364
self.features_settings = features_settings
6465
self.active_times_settings = features_settings["active_times"]
6566
self.transition_settings = features_settings["transitions"]
66-
self.reverse = strip_settings["reverse"]
6767
self.max_brightness = int(
6868
round((float(strip_settings["brightness"]) / 100) * 255)
6969
)
@@ -87,10 +87,11 @@ def __init__(
8787
self.segment_settings.append(default_segment)
8888

8989
if int(self.strip_settings["count"]) < 6:
90-
self._logger.info("Applying < 6 LED bug workaround")
90+
self._logger.info("Applying < 6 LED flickering bug workaround")
9191
# rpi_ws281x will think we want 6 LEDs, but we will only use those configured
9292
# this works around issues where LEDs would show the wrong colour, flicker and more
9393
# when used with less than 6 LEDs.
94+
# See #132 for details
9495
self.strip_settings["count"] = 6
9596

9697
# State holders
@@ -103,12 +104,13 @@ def __init__(
103104
self.queue = queue # type: multiprocessing.Queue
104105
try:
105106
self.strip = self.start_strip() # type: PixelStrip
106-
except StripFailedError:
107-
self._logger.info("No strip initialised, exiting the effect process.")
107+
except (StripFailedError, segments.InvalidSegmentError):
108+
self._logger.error("Exiting the effect process")
108109
return
109110
except Exception as e:
110-
self._logger.error("Unknown exception, abort abort abort")
111111
self._logger.exception(e)
112+
self._logger.error("Exiting the effect process")
113+
return
112114

113115
self.effect_queue = Queue()
114116
self.effect_thread = None # type: Optional[threading.Thread]
@@ -136,14 +138,19 @@ def __init__(
136138
self.previous_state["type"] == "standard"
137139
and self.previous_state["effect"] == "blank"
138140
):
141+
self._logger.debug(
142+
"Returning to previous state: {}".format(self.previous_state)
143+
)
139144
self.parse_q_msg(self.previous_state)
140145

146+
self._logger.info("Startup Complete!")
141147
self.main_loop()
142148

143149
def main_loop(self):
144150
try:
145151
while True:
146152
msg = self.queue.get()
153+
self._logger.debug("New message: {}".format(msg))
147154
if msg:
148155
if msg == constants.KILL_MSG:
149156
self.kill()
@@ -160,16 +167,18 @@ def main_loop(self):
160167
raise
161168

162169
def kill(self):
170+
self._logger.debug("Kill message received, shutting down...")
163171
self.blank_leds()
164172
self.stop_effect()
165-
self._logger.info("Kill message recieved, all effects stopped. Bye!")
173+
self.active_times_timer.end_timer()
174+
self._logger.info("Effect runner shutdown. Bye!")
166175

167176
def parse_q_msg(self, msg):
168177
if msg["type"] == "lights":
169178
if msg["action"] == "on":
170-
self.turn_lights_on()
179+
self.switch_lights(True)
171180
if msg["action"] == "off":
172-
self.turn_lights_off()
181+
self.switch_lights(False)
173182

174183
elif msg["type"] == "progress":
175184
self.progress_msg(msg["effect"], msg["value"])
@@ -184,10 +193,12 @@ def parse_q_msg(self, msg):
184193

185194
def switch_lights(self, state):
186195
# state: target state for lights
187-
# Only call when current state must change, since it will interrupt the currently running effect
196+
# Only run when current state must change, since it will interrupt the currently running effect
188197
if state == self.lights_on:
189198
return
190199

200+
self._logger.info("Switching lights {}".format("on" if state else "off"))
201+
191202
if state:
192203
self.turn_lights_on()
193204
else:
@@ -196,20 +207,19 @@ def switch_lights(self, state):
196207
def turn_lights_on(self):
197208
if not self.active_times_timer.active:
198209
# Active times are not now, don't do anything
210+
self._logger.debug("LED switch on blocked by active times")
199211
self.parse_q_msg(self.previous_state)
200212
return
201213

202214
if self.turn_off_timer and self.turn_off_timer.is_alive():
203215
self.turn_off_timer.cancel()
204216

205217
self.lights_on = True
218+
206219
if self.transition_settings["fade"]["enabled"]:
207220
start_daemon_thread(
208-
target=self.brightness_manager.do_fade_in, name="Fade in thread"
221+
target=self.brightness_manager.do_fade_in, name="Fade in"
209222
)
210-
self._logger.info(
211-
"On message received, turning on LEDs to {}".format(self.previous_state)
212-
)
213223
self.parse_q_msg(self.previous_state)
214224

215225
def turn_lights_off(self):
@@ -226,12 +236,6 @@ def turn_lights_off(self):
226236
else:
227237
self.lights_off()
228238

229-
self._logger.info(
230-
"Off message received, turning off LEDs (fade: {})".format(
231-
self.transition_settings["fade"]["enabled"]
232-
)
233-
)
234-
235239
def lights_off(self):
236240
self.standard_effect("blank")
237241
self.lights_on = False
@@ -278,7 +282,7 @@ def parse_m150(self, msg):
278282
"command": "M150", # Chop the parameters, so it is not parsed again
279283
}
280284
self._logger.debug(
281-
"Parsed new M150: M150 R{red} G{green} B{blue} (brightness: {brightness}".format(
285+
"Parsed new M150: M150 R{red} G{green} B{blue} (brightness: {brightness})".format(
282286
**locals()
283287
)
284288
)
@@ -326,7 +330,7 @@ def progress_effect(self, mode, value):
326330
"base_color": apply_color_correction(
327331
self.color_correction, *hex_to_rgb(effect_settings["base"])
328332
),
329-
"reverse": self.reverse,
333+
"reverse": self.strip_settings["reverse"],
330334
},
331335
name=mode,
332336
)
@@ -335,8 +339,7 @@ def progress_effect(self, mode, value):
335339

336340
def standard_effect(self, mode):
337341
# Log if the effect is changing
338-
if self.previous_state != mode:
339-
self._logger.debug("Changing effect to {}".format(mode))
342+
self._logger.debug("Changing effect to {}".format(mode))
340343

341344
if self.lights_on and not mode == "blank":
342345
effect_settings = self.effect_settings[mode]
@@ -384,6 +387,8 @@ def blank_leds(self, whole_strip=True):
384387
# Use a segment, not whole strip
385388
strip = self.segment_manager.get_segment(1)
386389

390+
self._logger.debug("Blanking LEDs")
391+
387392
self.run_effect(
388393
target=constants.EFFECTS["Solid Color"],
389394
kwargs={
@@ -403,7 +408,7 @@ def start_strip(self):
403408
Start PixelStrip and SegmentManager object
404409
:returns strip: (rpi_ws281x.PixelStrip) The initialised strip object
405410
"""
406-
self._logger.info("Initialising LED strip")
411+
self._logger.info("Starting up LED strip")
407412
try:
408413
strip = PixelStrip(
409414
num=int(self.strip_settings["count"]),
@@ -416,18 +421,18 @@ def start_strip(self):
416421
strip_type=constants.STRIP_TYPES[self.strip_settings["type"]],
417422
)
418423
strip.begin()
419-
self._logger.info("Strip successfully initialised")
424+
self._logger.info("Strip startup complete!")
420425
except Exception as e: # Probably wrong settings...
421426
self._logger.error(repr(e))
422-
self._logger.error("Strip failed to initialize, no effects will be run.")
423-
raise StripFailedError("Error intitializing strip")
427+
self._logger.error("Strip failed to startup")
428+
raise StripFailedError("Error initializing strip")
424429

425430
# Create segments & segment manager
426431
try:
427432
self.segment_manager = segments.SegmentManager(strip, self.segment_settings)
428433
self.segment_manager.create_segments()
429434
except segments.InvalidSegmentError:
430-
self._logger.error("You did something wrong...")
435+
self._logger.error("Segment configuration error. Please report this issue!")
431436
raise
432437
return strip
433438

0 commit comments

Comments
 (0)