Skip to content

Commit d2a6d44

Browse files
committed
added logfile (clears at 300 lines)
1 parent 8ff126c commit d2a6d44

File tree

6 files changed

+65
-16
lines changed

6 files changed

+65
-16
lines changed

smct/config.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
import requests
55

66
from smct import paths, registry, ui
7+
from smct.logger import log
78

89
# config.ini structure
9-
_ENCODING = "utf-8"
10+
ENCODING = "utf-8"
1011

1112
_SETTINGS_SECTION = "Settings"
1213

@@ -22,22 +23,26 @@
2223
def _check_for_missing_files():
2324
if not os.path.exists(paths.ASSETS_DIR_PATH):
2425
os.mkdir(paths.ASSETS_DIR_PATH)
26+
log(f"Creating assets directory: {paths.ASSETS_DIR_PATH}")
2527

2628
# Check for Icons
27-
# ! maybe send error message here?
2829
if not os.path.exists(paths.ASSETS_ICO_PATH):
30+
log(f"{paths.ASSETS_ICO_PATH} not found! Downloading...")
2931
download_assets_file(os.path.basename(paths.ASSETS_ICO_PATH))
3032
# sys.exit(1)
3133
if not os.path.exists(paths.ASSETS_ICON_ENABLED_PATH):
34+
log(f"{paths.ASSETS_ICON_ENABLED_PATH} not found! Downloading...")
3235
download_assets_file(os.path.basename(paths.ASSETS_ICON_ENABLED_PATH))
3336
# sys.exit(1)
3437
if not os.path.exists(paths.ASSETS_ICON_DISABLED_PATH):
38+
log(f"{paths.ASSETS_ICON_DISABLED_PATH} not found! Downloading...")
3539
download_assets_file(os.path.basename(paths.ASSETS_ICON_DISABLED_PATH))
3640
# sys.exit(1)
3741

3842
# Check for temp folder
3943
if not os.path.exists(paths.MMT_DIR_PATH):
4044
os.makedirs(paths.MMT_DIR_PATH)
45+
log(f"Creating {paths.MMT_DIR_PATH}")
4146

4247

4348
def download_assets_file(image_name):
@@ -50,10 +55,13 @@ def download_assets_file(image_name):
5055
with open(os.path.join(paths.ASSETS_DIR_PATH, filename), "wb") as f:
5156
for chunk in response.iter_content(1024):
5257
f.write(chunk)
58+
else:
59+
log(f"Error occurred while downloading {image_name}: {response.status_code}")
5360

5461

5562
def init_config():
5663
if not os.path.exists(paths.CONFIG_PATH):
64+
log("config.ini not found, creating default config")
5765
_create_default_config_file()
5866

5967
_check_for_missing_files()
@@ -74,6 +82,7 @@ def get_mmt_path_value():
7482

7583

7684
def set_mmt_path_value(_value):
85+
log(f"Config.ini - Setting mmt_path to {_value}")
7786
_configparser[_SETTINGS_SECTION][_MMT_PATH_KEY] = _value
7887
_write_to_config()
7988

@@ -84,6 +93,7 @@ def get_monitor_name_value():
8493

8594

8695
def set_monitor_name_value(_value):
96+
log(f"Config.ini - Setting monitor_name to {_value}")
8797
_configparser[_SETTINGS_SECTION][_MONITOR_NAME_KEY] = _value
8898
_write_to_config()
8999

@@ -94,6 +104,7 @@ def get_monitor_serial_value():
94104

95105

96106
def set_monitor_serial_value(_value):
107+
log(f"Config.ini - Setting monitor_serial to {_value}")
97108
_configparser[_SETTINGS_SECTION][_MONITOR_SERIAL_KEY] = _value
98109
_write_to_config()
99110

@@ -105,6 +116,7 @@ def get_start_with_windows_value():
105116

106117
def set_start_with_windows_value(_value):
107118
value_str = "yes" if _value else "no"
119+
log(f"Config.ini - Setting start_with_windows to {value_str}")
108120
_configparser[_SETTINGS_SECTION][_START_WITH_WINDOWS_KEY] = value_str
109121
_write_to_config()
110122

@@ -116,16 +128,17 @@ def get_first_start_value():
116128

117129
def set_first_start_value(_value):
118130
value_str = "yes" if _value else "no"
131+
log(f"Config.ini - Setting first_start to {value_str}")
119132
_configparser[_SETTINGS_SECTION][_FIRST_START_KEY] = value_str
120133
_write_to_config()
121134

122135

123136
def _read_from_config():
124-
_configparser.read(paths.CONFIG_PATH, encoding=_ENCODING)
137+
_configparser.read(paths.CONFIG_PATH, encoding=ENCODING)
125138

126139

127140
def _write_to_config():
128-
with open(paths.CONFIG_PATH, "w", encoding=_ENCODING) as configfile:
141+
with open(paths.CONFIG_PATH, "w", encoding=ENCODING) as configfile:
129142
_configparser.write(configfile)
130143

131144

smct/logger.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import logging
2+
import os
3+
from smct import paths
4+
5+
LOGFILENAME = paths.LOG_PATH
6+
ENCODING = "utf-8"
7+
8+
LOG = None
9+
STARTUP = True
10+
11+
LOG_LINE_LIMIT = 300
12+
13+
14+
def log(text):
15+
LOG.info(text)
16+
17+
18+
def clear_log_if_needed():
19+
if os.path.exists(LOGFILENAME):
20+
with open(LOGFILENAME, "r", encoding=ENCODING) as file:
21+
lines = file.readlines()
22+
if len(lines) > LOG_LINE_LIMIT:
23+
with open(LOGFILENAME, "w", encoding=ENCODING) as file:
24+
file.write("")
25+
26+
27+
if STARTUP:
28+
clear_log_if_needed()
29+
logging.basicConfig(
30+
filename=LOGFILENAME,
31+
level=logging.INFO,
32+
format="%(asctime)s - %(levelname)s - %(message)s",
33+
)
34+
LOG = logging.getLogger(__name__)
35+
LOG.critical("Start")
36+
STARTUP = False

smct/multimonitortool.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import subprocess
22
import pandas as pd
33
from smct import config, paths
4+
from smct.logger import log
45

56
MMT_CSV_MONITOR_NAME = "Monitor Name"
67
MMT_CSV_SERIAL_NUMBER = "Monitor Serial Number"
@@ -21,6 +22,7 @@ def get_monitor_selection_list():
2122
_monitor_selection_list.append(
2223
f"{_monitor_id} | {_monitor_name} | {_monitor_serial}"
2324
)
25+
log(f"Monitor detected: {_monitor_id} | {_monitor_name} | {_monitor_serial}")
2426
_monitor_selection_list.sort()
2527
return _monitor_selection_list
2628

@@ -41,8 +43,9 @@ def _run_mmt_command(command, destination):
4143
],
4244
check=True,
4345
)
46+
log(f"MultiMonitorTool.exe {command} {destination}")
4447
except subprocess.CalledProcessError as error:
45-
print(f"{config.get_mmt_path_value} {command} {destination} failed: {error}")
48+
log(f"MultiMonitorTool.exe {command} {destination} failed: {error}")
4649

4750

4851
def save_mmt_config():

smct/notification.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

smct/paths.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def strip_package_name_from_path(path):
2525
EXE_PATH = BASE_PATH + "\\" + ui_strings.APP_NAME + ".exe"
2626

2727
CONFIG_PATH = BASE_PATH + "\\config.ini"
28+
LOG_PATH = BASE_PATH + "\\smct.log"
2829
ASSETS_DIR_PATH = BASE_PATH + "\\assets"
2930
MMT_DIR_PATH = BASE_PATH + "\\mmt"
3031

smct/registry.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import winreg
22
from smct import paths, ui_strings
3+
from smct.logger import log
34

45
KEY_NAME = ui_strings.APP_NAME
56
REGISTRY_KEY = r"Software\Microsoft\Windows\CurrentVersion\Run"
@@ -23,9 +24,9 @@ def add_to_autostart():
2324
exe_path = paths.EXE_PATH[0].upper() + paths.EXE_PATH[1:]
2425
winreg.SetValueEx(key, KEY_NAME, 0, winreg.REG_SZ, exe_path)
2526
winreg.CloseKey(key)
26-
except PermissionError:
27-
# print(f"Error occurred while adding to autostart: {e}")
28-
pass
27+
log(f"Enabled autostart, wrote {KEY_NAME} to {REGISTRY_KEY}")
28+
except PermissionError as e:
29+
log(f"Failed to enable autostart: {e} in {REGISTRY_KEY}")
2930

3031

3132
def remove_from_autostart():
@@ -35,6 +36,6 @@ def remove_from_autostart():
3536
)
3637
winreg.DeleteValue(key, KEY_NAME)
3738
winreg.CloseKey(key)
38-
except (FileNotFoundError, PermissionError):
39-
# print(f"Error occurred while removing from autostart: {e}")
40-
pass
39+
log(f"Disabled autostart, removed {KEY_NAME} from {REGISTRY_KEY}")
40+
except (FileNotFoundError, PermissionError) as e:
41+
log(f"Failed to disable autostart: {e} in {REGISTRY_KEY}")

0 commit comments

Comments
 (0)