add a simple logger

This commit is contained in:
randomuser 2021-07-20 02:55:32 -05:00
parent 08588b967f
commit c9f0dce189
1 changed files with 27 additions and 0 deletions

27
esgd.py
View File

@ -1,9 +1,11 @@
import socketserver
import subprocess
import os
import time
host = "localhost"
port = 77
log = Logger()
class GopherError(BaseException): pass
class RequestError(GopherError): pass
@ -52,6 +54,31 @@ class GopherLine:
self.port
)
class Logger:
def __init__(self, file=None):
if file != None: self.fd = open(file, "a+")
else: self.fd = False
def time(self)
return int(time.time())
def write(self, msg):
if self.fd: self.fd.write("{}\n")
def log(self, msg):
ct = self.time()
self.write("[{}] {}".format(str(ct), msg))
print("[{}] {}".format(str(ct), msg))
def warn(self, msg):
ct = self.time()
self.write("! [{}] {}".format(str(ct), msg))
print("! [{}] {}".format(str(ct), msg))
def error(self, msg):
ct = self.time()
self.write("!! [{}] {}".format(str(ct), msg))
print("!! [{}] {}".format(str(ct), msg))
self.close()
raise SystemExit
def close(self):
self.fd.close()
class GopherServerLogic:
def recieveRequest(self):
data = b""