<filename>pyhacks/Logger.py
import queue
import threading
from .constants import *

def verifyKey(item, keyName):
	if type(keyName)!=str:
		raise Exception("Key type excpected to be str but got {}".format(type(keyName)))
	if keyName in item:
		return True
	else:
		raise Exception("Key {} doesn't exist in item: {}".format(keyName, item))

def verifyKeys(item, keys):
	if type(keys)!=list:
		raise Exception("Keys type excpected to be list but got {}".format(type(keys)))
	for key in keys:
		verifyKey(item, key)
	return True

class Logger:
	def __init__(self, verbose = False):
		self.q = queue.Queue()
		self.thread = self.initThread()
		self.verbose = verbose

	def initThread(self):
		def worker():
			while True:
				logObj = self.q.get()
				if logObj is None:
					self.q.task_done()
					break
				verifyKeys(logObj,["fileName","content"])
				self._write(logObj["fileName"],logObj["content"])
				self.q.task_done()
		t = threading.Thread(target=worker)
		t.start()
		return t

	def _write(self, fileName, content):
		with open(fileName, "a") as myfile:
			myfile.write("{}\n".format(content))

	def green(self, content, fileName = LOG_FILE_NAME, verbose = True):
		if verbose:
			print('\033[32m', content, '\033[0m', sep='')
		self.q.put({"fileName":fileName, "content":content})

	def red(self, content, fileName = LOG_FILE_NAME, verbose = True):
		if verbose:
		    print('\033[31m', content, '\033[0m', sep='')

		self.q.put({"fileName":fileName, "content":content})

	def yellow(self, content, fileName = LOG_FILE_NAME, verbose = True):
		if verbose:
			    print('\033[33m', content, '\033[0m', sep='')
		self.q.put({"fileName":fileName, "content":content})

	def log(self, content, fileName = LOG_FILE_NAME, verbose = True):
		if verbose:
			print(content)
		self.q.put({"fileName":fileName, "content":content})
	
	def debug(self,content):
		self.log(content,verbose=self.verbose)

	def success(self, item):
		item.verifyKey("counter")
		self.q.put({"fileName":SUCESS_FILE_NAME, "content":item.get("counter")})

	def put(self, item):
		self.q.put(item)

	def finish(self):
		self.q.put(None)
		self.thread.join()
		self.q.join()
<filename>pyhacks/Logger.py
import queue
import threading
from .constants import *

def verifyKey(item, keyName):
	if type(keyName)!=str:
		raise Exception("Key type excpected to be str but got {}".format(type(keyName)))
	if keyName in item:
		return True
	else:
		raise Exception("Key {} doesn't exist in item: {}".format(keyName, item))

def verifyKeys(item, keys):
	if type(keys)!=list:
		raise Exception("Keys type excpected to be list but got {}".format(type(keys)))
	for key in keys:
		verifyKey(item, key)
	return True

class Logger:
	def __init__(self, verbose = False):
		self.q = queue.Queue()
		self.thread = self.initThread()
		self.verbose = verbose

	def initThread(self):
		def worker():
			while True:
				logObj = self.q.get()
				if logObj is None:
					self.q.task_done()
					break
				verifyKeys(logObj,["fileName","content"])
				self._write(logObj["fileName"],logObj["content"])
				self.q.task_done()
		t = threading.Thread(target=worker)
		t.start()
		return t

	def _write(self, fileName, content):
		with open(fileName, "a") as myfile:
			myfile.write("{}\n".format(content))

	def green(self, content, fileName = LOG_FILE_NAME, verbose = True):
		if verbose:
			print('\033[32m', content, '\033[0m', sep='')
		self.q.put({"fileName":fileName, "content":content})

	def red(self, content, fileName = LOG_FILE_NAME, verbose = True):
		if verbose:
		    print('\033[31m', content, '\033[0m', sep='')

		self.q.put({"fileName":fileName, "content":content})

	def yellow(self, content, fileName = LOG_FILE_NAME, verbose = True):
		if verbose:
			    print('\033[33m', content, '\033[0m', sep='')
		self.q.put({"fileName":fileName, "content":content})

	def log(self, content, fileName = LOG_FILE_NAME, verbose = True):
		if verbose:
			print(content)
		self.q.put({"fileName":fileName, "content":content})
	
	def debug(self,content):
		self.log(content,verbose=self.verbose)

	def success(self, item):
		item.verifyKey("counter")
		self.q.put({"fileName":SUCESS_FILE_NAME, "content":item.get("counter")})

	def put(self, item):
		self.q.put(item)

	def finish(self):
		self.q.put(None)
		self.thread.join()
		self.q.join()