Rewritten src/logs.py and better comments on logs and init

This commit is contained in:
nanometer5088
2023-02-12 17:35:27 -03:00
parent a4167715e2
commit f08088111f
2 changed files with 46 additions and 31 deletions

View File

@@ -1,3 +1,14 @@
# This code is the initialization script for CLI TikTok. It performs the following tasks:
# Clears the console screen
# Welcomes the user and waits for the user to press the "ENTER" key to proceed.
# Tests for required dependencies and installs them if missing.
# Determines the operating system and Python version.
# Checks for available updates to the software.
# If an internet connection is not available, it informs the user and exits the program.
# The code also uses the log function to log important events during the execution of the script.
import os
import platform
import sys
@@ -6,14 +17,11 @@ from log import logtofile as log
def init():
# Intro for the user
os.system("cls || clear")
input(
"Welcome to CLI TikTok, an open-source TikTok archiver and viewer!\nPress ENTER to proceed"
)
# Detect and install libraries - If they aren't installed,
# the user is prompted to make the automatic installation.
log("Started dependency test")
try:
import atoma
@@ -39,7 +47,6 @@ def init():
os.system("cls || clear")
return -1
# Detect OS and Python version, and include this information in the log file
log("Started operating system and python detection")
if platform.system() == "Windows":
log(f"Operating System: Windows {platform.win32_ver()[0]}")
@@ -60,8 +67,7 @@ def init():
)
log("Operating System and Python detection finished!\n")
# Search for updates, and prompts the user in case they're found.
# If the user does not have internet access, warns him the software won't work properly and quit.
try:
import requests

View File

@@ -1,33 +1,42 @@
# This code imports the `glob`, `os`, and `time` modules
import glob
import os
import time
# The `Logging` class is defined
class Logging:
# The class constructor initializes the `logFileOpened` attribute as `False`
def __init__(self):
self.logFileOpened = False
def log(self, stringToLog: str):
# creating logs folder
try:
os.mkdir(os.getcwd() + "/logs")
except FileExistsError:
pass
filenames = []
for filename in glob.glob(r"logs/log-*.txt"):
filenames.append(int(filename[9:-4]))
if len(filenames) == 0:
filenames.append(0)
if self.logFileOpened:
with open(f"logs/log-{max(filenames)}.txt", "a") as logFile:
logFile.write(
f"[{time.strftime('%Y.%m.%d-%H.%M.%S', time.localtime(time.time()))}]"
f" {stringToLog.encode('ascii', 'replace').decode()}\n"
)
else:
with open(f"logs/log-{max(filenames) + 1}.txt", "w") as logFile:
self.logFileOpened = True
logFile.write(
f"[{time.strftime('%Y.%m.%d-%H.%M.%S', time.localtime(time.time()))}]"
f" {stringToLog.encode('ascii', 'replace').decode()}\n"
)
# The `log` method takes a string `log_string` as an argument
def log(self, log_string: str):
# The logs directory is determined
logs_directory = os.getcwd() + "/logs"
# If the logs directory does not exist, it is created
if not os.path.exists(logs_directory):
os.mkdir(logs_directory)
# A list of log files is obtained using the `glob` module
log_files = glob.glob(r"logs/log-*.txt")
# The log file numbers are extracted from the filenames
log_file_numbers = [int(file[9:-4]) for file in log_files]
# If no log files exist, a log-0.txt file is created
if not log_file_numbers:
log_file_numbers.append(0)
# The log file name is determined
log_file_name = f"logs/log-{max(log_file_numbers) + 1 if not self.logFileOpened else max(log_file_numbers)}.txt"
# The log file is opened and written to
with open(log_file_name, "a" if self.logFileOpened else "w") as log_file:
self.logFileOpened = True
# The current time is formatted
current_time = time.strftime("%Y.%m.%d-%H.%M.%S", time.localtime(time.time()))
# The log string is written to the file with the current time as a prefix
log_file.write(f"[{current_time}] {log_string.encode('ascii', 'replace').decode()}\n")