add: log messages woth python module 'logging' instead of print

This commit is contained in:
Bernhard Stampfer
2016-05-07 20:51:06 +02:00
parent d124755c88
commit 54ce22c0c1
6 changed files with 36 additions and 28 deletions

View File

@@ -3,7 +3,7 @@ from flask import session, render_template
from user import User
from database import *
from functools import wraps
import logging
def requires_baron(fn):
@wraps(fn)

View File

@@ -8,6 +8,7 @@ from deposit import Deposit
import random as rand
import datetime
from settings import settings
import logging
DATABASE = 'test/database.db'
@@ -28,13 +29,11 @@ def query_db(query, args=(), one=False):
try:
db = get_db()
except RuntimeError:
print "GUI DB acces"
logging.info("GUI DB access")
db = sqlite3.connect(DATABASE)
closeflag = True
print query
print args
#print "Sqlite: " + query % args
logging.info("Database query: " + str(query) + " args: " + str(args))
cur = db.execute(query, args)
rows = cur.fetchall()
cur.close()
@@ -58,7 +57,7 @@ def get_user(u):
u.isbaron=row[7]
u.isshown=row[8]
u.autoblack=row[9]
print u
logging.info(u)
return u
def get_user_by_name(name):
@@ -76,7 +75,7 @@ def get_user_by_name(name):
u.isbaron=row[7]
u.isshown=row[8]
u.autoblack=row[9]
print u
logging.debug(u)
return u
def get_user_by_rfid(rfidid):
@@ -94,7 +93,7 @@ def get_user_by_rfid(rfidid):
u.isbaron=row[7]
u.isshown=row[8]
u.autoblack=row[9]
print u
logging.debug(u)
return u
def get_users():
@@ -196,8 +195,7 @@ def get_consumed(user=None, startdate=None, enddate=None):
def add_consume(username, productid):
consumerid = query_db("SELECT ID FROM USERS WHERE NAME = ?", [username], one=True)
print "consumerid = "
print consumerid
logging.info("consumerid = " + str(consumerid))
consumerid = int(consumerid[0])
product = get_product_by_id(productid)
#INSERT INTO USERS (NAME, PASSWORD, LONGNAME, EMAIL, RFID_ID) VALUES (? ,? ,?, ?, ?)", (u.name, u.password, u.longname, u.email, u.rfid_id))
@@ -209,7 +207,7 @@ def add_consume(username, productid):
u = get_user_by_name(username)
u.isblack = True
update_user(u)
print "consumed"
logging.info("consumed")
return
@@ -256,7 +254,7 @@ def add_deposit(username, amount):
u = get_user_by_name(username)
u.isblack = False
update_user(u)
print "deposit"
logging.info("deposit")
return
@@ -286,7 +284,7 @@ def generate_test_consumptions():
p = 1 + int(rand.random() * (1.0*num_p))
daysa = int(rand.random() * (30.0))
add_test_consume(u,p,daysa)
print 'trying to add ' + str(p) + ' to ' + str(u)
logging.info('trying to add ' + str(p) + ' to ' + str(u))
return
def add_test_consume(consumerid, productid, daysago):

View File

@@ -72,13 +72,13 @@ class MainWindow(wx.Frame):
self.active = 4 #Sorry Bro
else:
self.active = 1 #Drinks
print "switching panels", self.active
logging.info("switching panels: " + str(self.active))
#has to be called from main thread!!
wx.CallAfter(self.switchPanels)
def onProduct(self, e):
self.active = 3
print self.user.longname + ' consumes'
logging.info(self.user.longname + ' consumes')
self.drinkl = e.GetEventObject().GetLabelText()
drink = get_product_by_name(self.drinkl.split('\n')[0]).id
with app.app_context():
@@ -112,7 +112,7 @@ class MainWindow(wx.Frame):
try:
self.panelThanks.bitmap_2.SetBitmap(wx.Bitmap("./app/static/product_%s.png" % self.drinkl.split('\n')[0], wx.BITMAP_TYPE_ANY))
except:
print "no picture for drink:", self.drinkl.split('\n')
logging.error("no picture for drink: " + self.drinkl.split('\n')[0])
self.panelThanks.Show()
self.delayExit()
elif active == 4:

View File

@@ -1,16 +1,18 @@
try:
import MFRC522
except:
print "ERROR: Need MFRC522 Library to read RFID tags, disable RFID if no reader is present!"
logging.critical("Need MFRC522 Library to read RFID tags, disable RFID if no reader is present!")
exit()
import signal
import thread
import time
import logging
class RFID:
def __init__(self, callbackf):
logging.info("RFID Reader initialized!")
self.reader = MFRC522.MFRC522()
signal.signal(signal.SIGINT, self.stop)
self.callback = callbackf
@@ -21,21 +23,21 @@ class RFID:
while True:
while self.loop:
(status, tagtype) = self.reader.MFRC522_Request(self.reader.PICC_REQIDL)
print "RFID Status:", status
logging.debug("RFID Status: " + str(status))
if status == self.reader.MI_OK:
(status, uid) = self.reader.MFRC522_Anticoll()
if status == self.reader.MI_OK:
uids = "0x" + "".join(format(x, '02x') for x in uid)
print "RFID Detect:", uids
logging.info("RFID Detect: " + uids)
self.stop()
self.callback(uids)
while not self.loop:
time.sleep(0.1)
def start(self):
print "RFID reader started"
logging.info("RFID reader started")
self.loop = True
def stop(self):
print "RFID reader stopped"
logging.info("RFID reader stopped")
self.loop = False

View File

@@ -4,6 +4,7 @@ import database
# from email.mime.text import MIMEText
# from email.MIMEText import MIMEText
import user
import logging
def send_email(recipient, subject, body):
gmail_user = 'bier1baroness'
@@ -17,7 +18,7 @@ def send_email(recipient, subject, body):
message = message.encode('utf-8')
# message = msg.as_string()
print message
logging.info(message)
try:
server = smtplib.SMTP("smtp.gmail.com:587")
#server.set_debuglevel(1)
@@ -25,10 +26,10 @@ def send_email(recipient, subject, body):
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, recipient, message)
server.quit()
print 'Mail was sent to %s' % recipient
logging.info('Mail was sent to %s' % recipient)
except:
print "Failed to send mail to %s" %recipient
logging.error("Failed to send mail to %s" %recipient)
def send_emails(body, subject, users):
FROM = 'bier1baroness@gmail.com'
@@ -39,9 +40,6 @@ def send_emails(body, subject, users):
body_parsed = parse_email(body, user, debt)
send_email(user.email, subject_parsed, body_parsed)
def parse_email(text, u, dept):
text = text.replace('%%longname%%', u.longname)

12
run.py
View File

@@ -4,8 +4,18 @@ from app import app
from os import urandom
from app import gui
import thread
import logging
if __name__ == '__main__':
#logging!
logfile = "baroness.log"
#logging.basicConfig(filename=logfile, level=logging.WARNING)
logging.basicConfig(level=logging.DEBUG)
logging.info("Baroness started!")
print "Baroness started: logging to ", logfile
#start gui
wx = wx.App()
gui.MainWindow(None)
@@ -13,6 +23,6 @@ if __name__ == '__main__':
# start flask
app.secret_key = urandom(24)
app.run(debug=True)
app.run(host="0.0.0.0")
#app.run()