Improve logging (#148)

Now auto-cpufreq will write directly to the log file or stdout.
The file will automatically be cleared when on each refresh and on
startup.
This commit is contained in:
Technical27 2021-01-24 07:14:11 -05:00 committed by GitHub
parent 7169561054
commit f3f652d69e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 33 deletions

View File

@ -12,7 +12,7 @@ import warnings
from math import isclose from math import isclose
from pathlib import Path from pathlib import Path
from pprint import pformat from pprint import pformat
from subprocess import getoutput, call, run, check_output from subprocess import getoutput, call, run, check_output, DEVNULL
import psutil import psutil
import distro import distro
@ -38,13 +38,23 @@ CPUS = os.cpu_count()
powersave_load_threshold = (75*CPUS)/100 powersave_load_threshold = (75*CPUS)/100
performance_load_threshold = (50*CPUS)/100 performance_load_threshold = (50*CPUS)/100
# auto-cpufreq log file # auto-cpufreq log file path
auto_cpufreq_log_file = Path("/var/log/auto-cpufreq.log") auto_cpufreq_log_path = None
auto_cpufreq_log_file_snap = Path("/var/snap/auto-cpufreq/current/auto-cpufreq.log") auto_cpufreq_log_file = None
if os.getenv("PKG_MARKER") == "SNAP":
auto_cpufreq_log_path = Path("/var/snap/auto-cpufreq/current/auto-cpufreq.log")
else:
auto_cpufreq_log_path = Path("/var/log/auto-cpufreq.log")
# daemon check # daemon check
dcheck = getoutput("snapctl get daemon") dcheck = getoutput("snapctl get daemon")
def file_logging():
global auto_cpufreq_log_file
auto_cpufreq_log_file = open(auto_cpufreq_log_path, "w")
sys.stdout = auto_cpufreq_log_file
# ToDo: read version from snap/snapcraft.yaml and write to $SNAP/version for use with snap installs # ToDo: read version from snap/snapcraft.yaml and write to $SNAP/version for use with snap installs
# also come up with same kind of solution for AUR # also come up with same kind of solution for AUR
def app_version(): def app_version():
@ -220,7 +230,7 @@ def deploy_daemon():
except: except:
print("\nERROR:\nWas unable to turn off bluetooth on boot") print("\nERROR:\nWas unable to turn off bluetooth on boot")
auto_cpufreq_log_file.touch(exist_ok=True) auto_cpufreq_log_path.touch(exist_ok=True)
print("\n* Deploy auto-cpufreq install script") print("\n* Deploy auto-cpufreq install script")
shutil.copy(SCRIPTS_DIR / "auto-cpufreq-install.sh", "/usr/bin/auto-cpufreq-install") shutil.copy(SCRIPTS_DIR / "auto-cpufreq-install.sh", "/usr/bin/auto-cpufreq-install")
@ -261,8 +271,11 @@ def remove():
os.remove("/usr/bin/auto-cpufreq-remove") os.remove("/usr/bin/auto-cpufreq-remove")
# delete log file # delete log file
if auto_cpufreq_log_file.exists(): if auto_cpufreq_log_path.exists():
auto_cpufreq_log_file.unlink() if auto_cpufreq_log_file is not None:
auto_cpufreq_log_file.close()
auto_cpufreq_log_path.unlink()
# restore original cpufrectl script # restore original cpufrectl script
cpufreqctl_restore() cpufreqctl_restore()
@ -283,7 +296,6 @@ def root_check():
footer() footer()
exit(1) exit(1)
# refresh countdown # refresh countdown
def countdown(s): def countdown(s):
# Fix for wrong log output and "TERM environment variable not set" # Fix for wrong log output and "TERM environment variable not set"
@ -295,6 +307,12 @@ def countdown(s):
sys.stdout.flush() sys.stdout.flush()
time.sleep(1) time.sleep(1)
if auto_cpufreq_log_file is not None:
auto_cpufreq_log_file.seek(0)
auto_cpufreq_log_file.truncate(0)
else:
run("clear")
# get cpu usage + system load for (last minute) # get cpu usage + system load for (last minute)
def display_load(): def display_load():
@ -753,16 +771,9 @@ def no_log_msg():
# read log func # read log func
def read_log(): def read_log():
# readlog
# read log (snap) if os.path.isfile(auto_cpufreq_log_path):
if os.getenv("PKG_MARKER") == "SNAP": call(["tail", "-n 50", "-f", str(auto_cpufreq_log_path)], stderr=DEVNULL)
if os.path.isfile(auto_cpufreq_log_file_snap):
call(["tail", "-n 50", "-f", str(auto_cpufreq_log_file_snap)])
else:
no_log_msg()
# read log (non snap)
elif os.path.isfile(auto_cpufreq_log_file):
call(["tail", "-n 50", "-f", str(auto_cpufreq_log_file)])
else: else:
no_log_msg() no_log_msg()
footer() footer()

View File

@ -31,6 +31,7 @@ def main(monitor, live, daemon, install, log, debug):
else: else:
# Important: order does matter # Important: order does matter
if daemon: if daemon:
file_logging()
if os.getenv("PKG_MARKER") == "SNAP" and dcheck == "enabled": if os.getenv("PKG_MARKER") == "SNAP" and dcheck == "enabled":
while True: while True:
root_check() root_check()
@ -41,7 +42,6 @@ def main(monitor, live, daemon, install, log, debug):
sysinfo() sysinfo()
set_autofreq() set_autofreq()
countdown(5) countdown(5)
run("clear")
elif os.getenv("PKG_MARKER") != "SNAP": elif os.getenv("PKG_MARKER") != "SNAP":
while True: while True:
root_check() root_check()
@ -52,7 +52,6 @@ def main(monitor, live, daemon, install, log, debug):
sysinfo() sysinfo()
set_autofreq() set_autofreq()
countdown(5) countdown(5)
run("clear")
else: else:
daemon_not_found() daemon_not_found()
elif monitor: elif monitor:
@ -66,7 +65,6 @@ def main(monitor, live, daemon, install, log, debug):
sysinfo() sysinfo()
mon_autofreq() mon_autofreq()
countdown(5) countdown(5)
run("clear")
elif live: elif live:
while True: while True:
root_check() root_check()
@ -78,7 +76,6 @@ def main(monitor, live, daemon, install, log, debug):
sysinfo() sysinfo()
set_autofreq() set_autofreq()
countdown(5) countdown(5)
run("clear")
elif log: elif log:
read_log() read_log()
elif debug: elif debug:

View File

@ -1,12 +1,10 @@
[Unit] [Unit]
Description=auto-cpufreq - Automatic CPU speed & power optimizer for Linux Description=auto-cpufreq - Automatic CPU speed & power optimizer for Linux
After=network.target network-online.target After=network.target network-online.target
ConditionPathExists=/var/log/auto-cpufreq.log
[Service] [Service]
Type=simple Type=simple
User=root User=root
ExecStart=/usr/local/bin/auto-cpufreq --daemon ExecStart=/usr/local/bin/auto-cpufreq --daemon
StandardOutput=append:/var/log/auto-cpufreq.log
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target