From 8e1b4ade13cb0709c0e3649091fb9960a684e9ff Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Mon, 13 Jan 2025 18:15:00 +0100 Subject: [PATCH] Added docstrings. --- wobbl_tools/text/log.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/wobbl_tools/text/log.py b/wobbl_tools/text/log.py index 3ed68ed..622af59 100644 --- a/wobbl_tools/text/log.py +++ b/wobbl_tools/text/log.py @@ -18,18 +18,39 @@ _msg_types = { # message types with colors class Log: + """ + Using this class, you can easily write to logfiles. + """ + def __init__( - self, log_path: str = None, + self, + log_path: str = None, console_out: bool = True, console_colored: bool = True, file_colored: bool = True ): + """ + :param log_path: The path to the log file. E.g. "~/cool_project/crashlog.txt" + :param console_out: Specifies whether the written messages should be also printed to the console. + :param console_colored: Specifies whether the messages should be printed in color. (e.g. error messages will be + colored red) + :param file_colored: Specifies whether the logfile should also contain color codes. + """ + self.log_path = log_path self.console_out = console_out self.console_colored = console_colored self.file_colored = file_colored def write(self, msg: str, msg_type: str = "info", prefix: str = ""): + """ + Writes a message to the logfile. + + :param msg: The message to be written. + :param msg_type: The type of the message. (e.g. "info", "error") + :param prefix: An optional prefix that gets added at the beginning of the message. + """ + logfile_msg = self.format_msg(msg, msg_type, self.file_colored, prefix) + "\n" if self.console_out: @@ -55,6 +76,12 @@ class Log: return formatted_msg def read(self): + """ + Reads the content of the logfile. + + :return: The content of the logfile as a string. + """ + if self.log_path is not None: log_file = open(self.log_path, "r") log_content = log_file.read() @@ -65,6 +92,10 @@ class Log: raise FileNotFoundError("You didn't specify a log path at the log instance definition.") def clear(self): + """ + Completely clears the logfile. + """ + logfile = open(self.log_path, "w") logfile.write("") logfile.close()