Add logging

This commit is contained in:
Tanner Collin 2022-09-03 14:46:11 -06:00
parent 06c43bf3a2
commit 36c5ba8f66

37
main.py
View File

@ -1,5 +1,15 @@
#!/home/tanner/protovac/env/bin/python #!/home/tanner/protovac/env/bin/python
import os, logging
DEBUG = os.environ.get('DEBUG')
logging.basicConfig(
filename='protovax.log', encoding='utf-8',
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
level=logging.DEBUG if DEBUG else logging.INFO)
logging.info('')
logging.info('Boot up')
import curses import curses
import requests import requests
import pytz import pytz
@ -25,35 +35,43 @@ def format_date(datestr):
def sign_send(to_send): def sign_send(to_send):
try: try:
logging.info('Sending to sign: %s', to_send)
data = dict(sign=to_send, on_behalf_of='protovac') data = dict(sign=to_send, on_behalf_of='protovac')
r = requests.post('https://api.my.protospace.ca/stats/sign/', data=data, timeout=5) r = requests.post('https://api.my.protospace.ca/stats/sign/', data=data, timeout=5)
r.raise_for_status() r.raise_for_status()
return 'Success!' return 'Success!'
except: except BaseException as e:
logging.exception(e)
return 'Error' return 'Error'
def fetch_stats(): def fetch_stats():
try: try:
logging.info('Fetching status...')
r = requests.get('https://api.my.protospace.ca/stats/', timeout=5) r = requests.get('https://api.my.protospace.ca/stats/', timeout=5)
r.raise_for_status() r.raise_for_status()
return r.json() return r.json()
except: except BaseException as e:
logging.exception(e)
return 'Error' return 'Error'
def fetch_classes(): def fetch_classes():
try: try:
logging.info('Fetching classes...')
r = requests.get('https://api.my.protospace.ca/sessions/', timeout=5) r = requests.get('https://api.my.protospace.ca/sessions/', timeout=5)
r.raise_for_status() r.raise_for_status()
return r.json() return r.json()
except: except BaseException as e:
logging.exception(e)
return 'Error' return 'Error'
def fetch_protocoin(): def fetch_protocoin():
try: try:
logging.info('Fetching protocoin...')
r = requests.get('https://api.my.protospace.ca/protocoin/transactions/', timeout=5) r = requests.get('https://api.my.protospace.ca/protocoin/transactions/', timeout=5)
r.raise_for_status() r.raise_for_status()
return r.json() return r.json()
except: except BaseException as e:
logging.exception(e)
return 'Error' return 'Error'
if wa_api_key: if wa_api_key:
@ -154,6 +172,8 @@ classes_start = 0
protocoin = {} protocoin = {}
protocoin_line = 0 protocoin_line = 0
logging.info('Starting main loop...')
while True: while True:
if current_screen == 'home': if current_screen == 'home':
stdscr.addstr(0, 1, ' _______ _______ ___ _________ ___ ____ ____ _ ______ ') stdscr.addstr(0, 1, ' _______ _______ ___ _________ ___ ____ ____ _ ______ ')
@ -456,7 +476,12 @@ while True:
stdscr.addstr(9, 4, 'Thinking...') stdscr.addstr(9, 4, 'Thinking...')
stdscr.clrtoeol() stdscr.clrtoeol()
stdscr.refresh() stdscr.refresh()
think_result = think_send(think_to_send[:-1])
query = think_to_send[:-1]
logging.info('Thinking about: %s', query)
think_result = think_send(query)
logging.info('Think result: %s', think_result)
stdscr.erase() stdscr.erase()
think_to_send = '' think_to_send = ''
else: else:
@ -470,6 +495,7 @@ while True:
if current_screen != prev_screen: if current_screen != prev_screen:
prev_screen = current_screen prev_screen = current_screen
logging.info('Switching to screen: %s', current_screen)
stdscr.erase() stdscr.erase()
@ -477,3 +503,4 @@ curses.nocbreak()
stdscr.keypad(False) stdscr.keypad(False)
curses.echo() curses.echo()
curses.endwin() curses.endwin()
loging.info('Exiting.')