From 2fa442d9f22fb1397bfe69067455304ea130a6a5 Mon Sep 17 00:00:00 2001 From: shrey Date: Tue, 13 Apr 2021 22:05:18 +0530 Subject: [PATCH] Adding logging to the LRU Cache implementation --- LRU_Cache.py | 22 ++++++++++++++++++++-- Logger/{Logger.py => logger.py} | 7 +++++-- 2 files changed, 25 insertions(+), 4 deletions(-) rename Logger/{Logger.py => logger.py} (84%) diff --git a/LRU_Cache.py b/LRU_Cache.py index 9307c5f..2b3299f 100644 --- a/LRU_Cache.py +++ b/LRU_Cache.py @@ -13,6 +13,7 @@ # Importing the required modules from Linked_List.doubly_linked_list import doubly_linked_list from Helper.helper_functions import helper_functions +from Logger.logger import logger_wrapper # Total Pages TOTAL_PAGES = 10 @@ -20,13 +21,19 @@ # LRU Cache class implements the functionality of a LRU cache class lru_cache: - def __init__(self, size): + def __init__(self, size, logger): self.cache_size = size self.ll = doubly_linked_list() self.hashmap = [None] * TOTAL_PAGES self.faults = 0 + self.logger = logger + + self.logger.info("LRU Cache Instance created and initialized") + self.logger.debug("LRU Cache Instance parameters are: Size - {}, Total Pages = {}".format(self.cache_size, TOTAL_PAGES)) def refer(self, page): + self.logger.info("Cache refer call was made") + if self.hashmap[page] is not None: status = "Cache Hit!" self.ll.move_node_back(self.hashmap[page]) @@ -42,19 +49,28 @@ def refer(self, page): node = self.ll.add_node_back(page) self.hashmap[page] = node + self.logger.debug("The page requested was {} and the refer call result was - {}".format(page, status)) return status def display_cache(self): + self.logger.info("Display Cache call was made") + print("The total number of cache misses as far is: ", self.faults) print("The present cache state is") self.ll.display_list() + self.logger.debug("The total cache misses are - {}".format(self.faults)) + # Code execution begins here def main(): + lw = logger_wrapper() + logger = lw.new_logger() + + logger.debug("Program Execution Initiated") print("------------------------------ Welcome ------------------------------") print("Please specify the cache size...") - lru_cache_test = lru_cache(helper_functions.accept_int()) + lru_cache_test = lru_cache(helper_functions.accept_int(), logger) while True: print("\n1. Refer Cache\n2. Display Cache\nPress any other key to exit...") @@ -70,6 +86,8 @@ def main(): print("\n------------------------------ Thank You ------------------------------") break + logger.debug("Program Execution Completed") + if __name__ == '__main__': main() diff --git a/Logger/Logger.py b/Logger/logger.py similarity index 84% rename from Logger/Logger.py rename to Logger/logger.py index 940a2be..8f2317a 100644 --- a/Logger/Logger.py +++ b/Logger/logger.py @@ -35,8 +35,11 @@ def new_logger(self): def test(): log_wrapper = logger_wrapper() logger = log_wrapper.new_logger() - logger.info("Hello") - logger.debug("debug") + logger.debug("Hello world!") + logger.info("This is a test message") + logger.warning("Being printed from the logger wrapper class") + logger.error("To ensure appropriate formatting of logs") + logger.critical("And the correctness of code.") if __name__ == '__main__':