Improved logging | TODO: debug

This commit is contained in:
Torsten Kurbad 2017-09-07 17:11:56 +02:00
parent 48de4faf90
commit 75c8a13170
1 changed files with 16 additions and 1 deletions

View File

@ -1,13 +1,17 @@
#!/usr/bin/python3 #!/usr/bin/python3
# #
# Depends on pyyaml! # Depends on:
# - python-systemd
# - pyyaml
import logging
import shlex import shlex
import subprocess import subprocess
import sys import sys
import yaml import yaml
from serial import Serial from serial import Serial
from systemd.journal import JournalHandler
from threading import Timer from threading import Timer
from time import sleep from time import sleep
@ -189,24 +193,35 @@ class ConvertUPS(Serial):
if __name__ == "__main__": if __name__ == "__main__":
log = logging.getLogger('pyupsmon')
log.addHandler(JournalHandler())
log.setLevel(logging.INFO)
try: try:
ups = ConvertUPS() ups = ConvertUPS()
except: except:
log.error('Error: Could not open UPS serial connection.')
log.error('\tCheck the config file!')
print('Error: Could not open UPS serial connection.') print('Error: Could not open UPS serial connection.')
print('\tCheck the config file!') print('\tCheck the config file!')
sys.exit(1) sys.exit(1)
if not ups.upsInit(): if not ups.upsInit():
log.error('PyUPSMon successfully started.')
print('Error: Could not initialize UPS.') print('Error: Could not initialize UPS.')
sys.exit(1) sys.exit(1)
log.info('PyUPSMon successfully started.')
while True: while True:
(toggled, onBatt) = ups.powerToggled() (toggled, onBatt) = ups.powerToggled()
if toggled: if toggled:
if onBatt: if onBatt:
log.critical('UPS AC power loss. Starting shutdown timer.')
print('UPS AC power loss. Starting shutdown timer.') print('UPS AC power loss. Starting shutdown timer.')
ups.startShutdownTimer() ups.startShutdownTimer()
if not onBatt: if not onBatt:
log.critical('UPS AC power restored. Cancelling shutdown timer.')
print('UPS AC power restored. Cancelling shutdown timer.') print('UPS AC power restored. Cancelling shutdown timer.')
ups.cancelShutdownTimer() ups.cancelShutdownTimer()
sleep(ups.interval) sleep(ups.interval)