modifié: m/log.py

modifié:         superTornado.py
This commit is contained in:
sidya82 2014-03-03 19:03:06 +01:00
parent e204ab4024
commit 9941b381d6
2 changed files with 59 additions and 54 deletions

View File

@ -1,13 +1,26 @@
import logging import logging
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
import sys
import time class bcolors:
from datetime import datetime DEBUG = '\033[94m'
INFO = '\033[95m'
SUCCESS = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
class lvl:
NOTSET = 0
DEBUG = 10
INFO = 20
SUCCESS = 25
WARNING = 30
FAIL = 40
class Log(object): class Log(object):
def __init__(self) : def __init__(self) :
logging.addLevelName(25, "SUCCESS") logging.addLevelName(lvl.SUCESS, "SUCCESS")
self.logger = logging.getLogger() self.logger = logging.getLogger()
self.logger.setLevel(logging.DEBUG) self.logger.setLevel(logging.DEBUG)
@ -18,8 +31,6 @@ class Log(object):
file_handler.setFormatter(formatter) file_handler.setFormatter(formatter)
self.logger.addHandler(file_handler) self.logger.addHandler(file_handler)
file_handler_error = RotatingFileHandler('error.log', 'a', 1000000, 1) file_handler_error = RotatingFileHandler('error.log', 'a', 1000000, 1)
file_handler_error.setLevel(logging.ERROR) file_handler_error.setLevel(logging.ERROR)
file_handler_error.setFormatter(formatter) file_handler_error.setFormatter(formatter)
@ -31,26 +42,20 @@ class Log(object):
def printL(self,pMsg,pLvl): def printL(self,pMsg,pLvl):
if pLvl == 10 : if pLvl == lvl.DEBUG :
pMsg = bcolors.DEBUG + pMsg + bcolors.ENDC pMsg = bcolors.DEBUG + pMsg + bcolors.ENDC
elif pLvl == 20 : elif pLvl == lvl.INFO :
pMsg = bcolors.INFO + pMsg + bcolors.ENDC pMsg = bcolors.INFO + pMsg + bcolors.ENDC
elif pLvl == 25 : elif pLvl == lvl.SUCESS :
pMsg = bcolors.SUCCESS + pMsg + bcolors.ENDC pMsg = bcolors.SUCCESS + pMsg + bcolors.ENDC
elif pLvl == 30 : elif pLvl == lvl.WARNING :
pMsg = bcolors.WARNING + pMsg + bcolors.ENDC pMsg = bcolors.WARNING + pMsg + bcolors.ENDC
elif pLvl == 40 : elif pLvl == lvl.FAIL :
pMsg = bcolors.FAIL + pMsg + bcolors.ENDC pMsg = bcolors.FAIL + pMsg + bcolors.ENDC
self.logger.log(pLvl,pMsg) self.logger.log(pLvl,pMsg)
class bcolors:
DEBUG = '\033[94m'
INFO = '\033[95m'
SUCCESS = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'

View File

@ -42,13 +42,13 @@ class MainHandler(BaseHandler):
login = Login() login = Login()
autorise = login.connexion(iden, mdp) autorise = login.connexion(iden, mdp)
log.printL('maison = httplib.HTTPConnection("192.168.16.150", 80)',10) log.printL('maison = httplib.HTTPConnection("192.168.16.150", 80)',lvl.DEBUG)
self.set_secure_cookie("user", iden) self.set_secure_cookie("user", iden)
if autorise == True: if autorise == True:
self.set_secure_cookie("user", iden,1) self.set_secure_cookie("user", iden,1)
self.redirect("/video") self.redirect("/video")
else: else:
log.printL("->An unauthorized user try to access : " + self.request.remote_ip,30) log.printL("->An unauthorized user try to access : " + self.request.remote_ip,lvl.WARNING)
self.redirect("/unauthorized") self.redirect("/unauthorized")
class VideoHandler(BaseHandler): class VideoHandler(BaseHandler):
@ -81,44 +81,44 @@ class WSocketHandler(BaseHandler,tornado.websocket.WebSocketHandler):
if not self.current_user : if not self.current_user :
self.close() self.close()
return return
log.printL("->Websocket opened : " + self.request.remote_ip,25) log.printL("->Websocket opened : " + self.request.remote_ip,lvl.SUCCESS)
iden = self.current_user iden = self.current_user
if iden != "IllegalUser": if iden != "IllegalUser":
log.printL("->"+iden + " : Authorized user connection : "+self.request.remote_ip,20) log.printL("->"+iden + " : Authorized user connection : "+self.request.remote_ip,lvl.INFO)
if blind == True: if blind == True:
log.printL('->Send audio alarm authorized user',20) log.printL('->Send audio alarm authorized user',lvl.INFO)
log.printL('maison.request("GET", "micom/say.php?source=toto&text=Connection%20a%20la%20camera%20autorisee")',10) log.printL('maison.request("GET", "micom/say.php?source=toto&text=Connection%20a%20la%20camera%20autorisee")',lvl.DEBUG)
else: else:
log.printL('->Send visual alarm authorized user',20) log.printL('->Send visual alarm authorized user',lvl.INFO)
log.printL('maison.request("GET", "micom/lamp.php?room=salon1&order=1")',10) log.printL('maison.request("GET", "micom/lamp.php?room=salon1&order=1")',lvl.DEBUG)
else : else :
log.printL("->"+iden + ": Unauthorized user connection : " + self.request.remote_ip,30) log.printL("->"+iden + ": Unauthorized user connection : " + self.request.remote_ip,lvl.WARNING)
if blind == True: if blind == True:
log.printL('->Send audio alarm unauthorized user',30) log.printL('->Send audio alarm unauthorized user',lvl.WARNING)
log.printL('maison.request("GET", "micom/say.php?source=toto&text=Connection%20a%20la%20camera%20non%20autorisee")',10) log.printL('maison.request("GET", "micom/say.php?source=toto&text=Connection%20a%20la%20camera%20non%20autorisee")',lvl.DEBUG)
else: else:
log.printL('->Send visual alarm unauthorized user',30) log.printL('->Send visual alarm unauthorized user',lvl.WARNING)
log.printL('maison.request("GET", "micom/lamp.php?room=salon1&order=1")',10) log.printL('maison.request("GET", "micom/lamp.php?room=salon1&order=1")',lvl.DEBUG)
self.send_image() self.send_image()
def on_message(self,mesg): def on_message(self,mesg):
log.printL("->Data receive : " + self.request.remote_ip,20) log.printL("->Data receive : " + self.request.remote_ip,lvl.INFO)
self.send_image() self.send_image()
def on_close(self): def on_close(self):
log.printL("->Websocket closed : "+self.request.remote_ip,25) log.printL("->Websocket closed : "+self.request.remote_ip,lvl.SUCCESS)
iden = self.current_user iden = self.current_user
if iden != "IllegalUser": if iden != "IllegalUser":
log.printL("->"+iden+" : Authorized user deconnection : "+self.request.remote_ip,20) log.printL("->"+iden+" : Authorized user deconnection : "+self.request.remote_ip,lvl.INFO)
else : else :
log.printL("->"+iden +" : Unauthorized user deconnection : "+self.request.remote_ip,30) log.printL("->"+iden +" : Unauthorized user deconnection : "+self.request.remote_ip,lvl.WARNING)
if blind == True: if blind == True:
log.printL('->Send audio alarm deconnection user', 20) log.printL('->Send audio alarm deconnection user', lvl.INFO)
log.printL('maison.request("GET", "micom/say.php?source=toto&text=Connection%20a%20la%20camera%20rompue")',10) log.printL('maison.request("GET", "micom/say.php?source=toto&text=Connection%20a%20la%20camera%20rompue")',lvl.DEBUG)
else: else:
log.printL('->Send visual alarm deconnection user',20) log.printL('->Send visual alarm deconnection user',lvl.INFO)
log.printL('maison.request("GET", "micom/lamp.php?room=salon1&order=0")',10) log.printL('maison.request("GET", "micom/lamp.php?room=salon1&order=0")',lvl.DEBUG)
log.printL("->"+iden+" Deconnection : " + self.request.remote_ip) log.printL("->"+iden+" Deconnection : " + self.request.remote_ip)
@ -129,9 +129,9 @@ class WSocketHandler(BaseHandler,tornado.websocket.WebSocketHandler):
data = f.read() data = f.read()
encoded = base64.b64encode(data) encoded = base64.b64encode(data)
self.write_message(encoded) self.write_message(encoded)
log.printL( "->Data send : " + self.request.remote_ip, 20) log.printL( "->Data send : " + self.request.remote_ip, lvl.INFO)
except Exception, e : except Exception, e :
log.printL(e,40) log.printL(e,lvl.FAIL)
self.write_message("error") self.write_message("error")
application = tornado.web.Application([ application = tornado.web.Application([
@ -146,7 +146,7 @@ application = tornado.web.Application([
cookie_secret="1213215656") cookie_secret="1213215656")
if __name__ == "__main__": if __name__ == "__main__":
log.printL("->Loading configuration ... ",20) log.printL("->Loading configuration ... ",lvl.INFO)
try : try :
blind = config.isBlind() blind = config.isBlind()
ipCamera = config.ipCamera() ipCamera = config.ipCamera()
@ -161,27 +161,27 @@ if __name__ == "__main__":
if portServ == "error" : if portServ == "error" :
raise ConfigError("Failed Load Port Server Configuration") raise ConfigError("Failed Load Port Server Configuration")
except ConfigError as e : except ConfigError as e :
log.printL(e.value,40) log.printL(e.value,lvl.FAIL)
log.printL("Configuration Loading Failed ! Check Configuration File !",40) log.printL("Configuration Loading Failed ! Check Configuration File !",lvl.FAIL)
sys.exit(1) sys.exit(1)
log.printL("->Configuration Server Load Successfully !",25) log.printL("->Configuration Server Load Successfully !",lvl.SUCCESS)
if blind == True: if blind == True:
log.printL(" +Blind unhabitant",20) log.printL(" +Blind unhabitant",lvl.INFO)
else : else :
log.printL(" +Not blind unhabitant",20) log.printL(" +Not blind unhabitant",lvl.INFO)
log.printL(" +Ip Camera : " + ipCamera,20) log.printL(" +Ip Camera : " + ipCamera,lvl.INFO)
log.printL(" +Port Camera : " + portCamera,20) log.printL(" +Port Camera : " + portCamera,lvl.INFO)
log.printL(" +Port Server : " + portServ,20) log.printL(" +Port Server : " + portServ,lvl.INFO)
print "" print ""
try : try :
log.printL("->Server Start ...",20) log.printL("->Server Start ...",lvl.INFO)
tornado.options.parse_command_line() tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(application) http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(portServ) http_server.listen(portServ)
log.printL("->Server Start Successfully !",25) log.printL("->Server Start Successfully !",lvl.SUCCESS)
tornado.ioloop.IOLoop.instance().start() tornado.ioloop.IOLoop.instance().start()
except Exception, e : except Exception, e :
log.printL("Server Start Failed !",40) log.printL("Server Start Failed !",lvl.FAIL)
log.printL(e,40) log.printL(e,lvl.FAIL)
sys.exit(1) sys.exit(1)