Improved log and log test.
This commit is contained in:
parent
2655e2c65e
commit
7d38dab998
3 changed files with 51 additions and 20 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
__pycache__
|
__pycache__
|
||||||
build
|
build
|
||||||
wobbl_tools.egg-info
|
wobbl_tools.egg-info
|
||||||
|
test_log.txt
|
|
@ -1,17 +1,30 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import pytest
|
||||||
from wobbl_tools.text.log import Log
|
from wobbl_tools.text.log import Log
|
||||||
|
|
||||||
print("\n==== wobbl_tools.text.log =====")
|
print("\n==== wobbl_tools.text.log =====")
|
||||||
|
|
||||||
test_log = Log("test_log.txt")
|
test_log = Log("test_log.txt")
|
||||||
|
test_log.clear()
|
||||||
|
|
||||||
test_log.write("Testing the log module...")
|
test_log.write("Testing the log module...")
|
||||||
test_log.write("This should be fine...", "ok")
|
test_log.write("This should be fine...", "ok")
|
||||||
test_log.write("Something happened...", "warning")
|
test_log.write("Something happened...", "warning")
|
||||||
test_log.write("Oh shit!", "error")
|
test_log.write("Oh shit!", "error")
|
||||||
|
|
||||||
|
|
||||||
print("Logfile contents:")
|
print("Logfile contents:")
|
||||||
print(test_log.read())
|
print(test_log.read())
|
||||||
|
|
||||||
|
expected_content = \
|
||||||
|
"[info]: Testing the log module...[0m[0m\n" \
|
||||||
|
"[[38;5;2m[1mOK[0m]: [0mThis should be fine...[0m[0m\n" \
|
||||||
|
"[38;5;11m[1m[WARNING]: Something happened...[0m[0m\n" \
|
||||||
|
"[38;5;9m[1m[ERROR]: Oh shit![0m[0m\n"
|
||||||
|
|
||||||
|
assert test_log.read() == expected_content
|
||||||
|
|
||||||
|
without_file = Log()
|
||||||
|
|
||||||
|
with pytest.raises(FileNotFoundError, match="You didn't specify a log path at the log instance definition."):
|
||||||
|
without_file.read()
|
||||||
|
|
|
@ -2,14 +2,14 @@
|
||||||
|
|
||||||
from .format import format_string
|
from .format import format_string
|
||||||
|
|
||||||
msg_types_ncl = { # message types without colors
|
_msg_types_ncl = { # message types without colors
|
||||||
"info": "[info]: ",
|
"info": "[info]: ",
|
||||||
"ok": "[OK]: ",
|
"ok": "[OK]: ",
|
||||||
"warning": "[WARNING]: ",
|
"warning": "[WARNING]: ",
|
||||||
"error": "[ERROR]: "
|
"error": "[ERROR]: "
|
||||||
}
|
}
|
||||||
|
|
||||||
msg_types = {
|
_msg_types = { # message types with colors
|
||||||
"info": "[info]: ",
|
"info": "[info]: ",
|
||||||
"ok": format_string("[§dark_green§boldOK§rs]: "),
|
"ok": format_string("[§dark_green§boldOK§rs]: "),
|
||||||
"warning": format_string("§yellow§bold[WARNING]: ", auto_rs=False),
|
"warning": format_string("§yellow§bold[WARNING]: ", auto_rs=False),
|
||||||
|
@ -18,29 +18,41 @@ msg_types = {
|
||||||
|
|
||||||
|
|
||||||
class Log:
|
class Log:
|
||||||
def __init__(self, log_path: str = None, no_console: bool = False, no_colors: bool = False):
|
def __init__(
|
||||||
|
self, log_path: str = None,
|
||||||
|
console_out: bool = True,
|
||||||
|
console_colored: bool = True,
|
||||||
|
file_colored: bool = True
|
||||||
|
):
|
||||||
self.log_path = log_path
|
self.log_path = log_path
|
||||||
self.no_console = no_console
|
self.console_out = console_out
|
||||||
self.no_colors = no_colors
|
self.console_colored = console_colored
|
||||||
self.log = ""
|
self.file_colored = file_colored
|
||||||
|
|
||||||
def write(self, msg: str, msg_type: str = "info", prefix: str = ""):
|
def write(self, msg: str, msg_type: str = "info", prefix: str = ""):
|
||||||
if self.no_colors:
|
logfile_msg = self.format_msg(msg, msg_type, self.file_colored, prefix) + "\n"
|
||||||
msg = prefix + msg_types_ncl[msg_type] + msg
|
|
||||||
|
|
||||||
else:
|
if self.console_out:
|
||||||
msg = prefix + msg_types[msg_type] + msg
|
console_msg = self.format_msg(msg, msg_type, self.console_colored, prefix)
|
||||||
msg += format_string("§rs")
|
|
||||||
|
|
||||||
if not self.no_console:
|
print(console_msg)
|
||||||
print(msg)
|
|
||||||
|
|
||||||
if self.log_path is not None:
|
if self.log_path is not None:
|
||||||
log_file = open(self.log_path, "a")
|
logfile = open(self.log_path, "a")
|
||||||
log_file.write(msg + "\n")
|
logfile.write(logfile_msg)
|
||||||
log_file.close()
|
logfile.close()
|
||||||
|
|
||||||
self.log += msg + "\n"
|
def format_msg(self, msg, msg_type: str="info", colored: bool=True, prefix: str=""):
|
||||||
|
if colored:
|
||||||
|
formatted_msg = _msg_types[msg_type] + msg
|
||||||
|
formatted_msg += format_string("§rs")
|
||||||
|
|
||||||
|
else:
|
||||||
|
formatted_msg = _msg_types_ncl[msg_type] + msg
|
||||||
|
|
||||||
|
formatted_msg = prefix + formatted_msg
|
||||||
|
|
||||||
|
return formatted_msg
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
if self.log_path is not None:
|
if self.log_path is not None:
|
||||||
|
@ -50,4 +62,9 @@ class Log:
|
||||||
return log_content
|
return log_content
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return self.log
|
raise FileNotFoundError("You didn't specify a log path at the log instance definition.")
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
logfile = open(self.log_path, "w")
|
||||||
|
logfile.write("")
|
||||||
|
logfile.close()
|
||||||
|
|
Loading…
Reference in a new issue