mirror of
https://github.com/AdnanHodzic/auto-cpufreq.git
synced 2025-07-01 10:54:38 +02:00
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:
parent
7169561054
commit
f3f652d69e
@ -12,7 +12,7 @@ import warnings
|
||||
from math import isclose
|
||||
from pathlib import Path
|
||||
from pprint import pformat
|
||||
from subprocess import getoutput, call, run, check_output
|
||||
from subprocess import getoutput, call, run, check_output, DEVNULL
|
||||
|
||||
import psutil
|
||||
import distro
|
||||
@ -38,13 +38,23 @@ CPUS = os.cpu_count()
|
||||
powersave_load_threshold = (75*CPUS)/100
|
||||
performance_load_threshold = (50*CPUS)/100
|
||||
|
||||
# auto-cpufreq log file
|
||||
auto_cpufreq_log_file = Path("/var/log/auto-cpufreq.log")
|
||||
auto_cpufreq_log_file_snap = Path("/var/snap/auto-cpufreq/current/auto-cpufreq.log")
|
||||
# auto-cpufreq log file path
|
||||
auto_cpufreq_log_path = None
|
||||
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
|
||||
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
|
||||
# also come up with same kind of solution for AUR
|
||||
def app_version():
|
||||
@ -220,7 +230,7 @@ def deploy_daemon():
|
||||
except:
|
||||
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")
|
||||
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")
|
||||
|
||||
# delete log file
|
||||
if auto_cpufreq_log_file.exists():
|
||||
auto_cpufreq_log_file.unlink()
|
||||
if auto_cpufreq_log_path.exists():
|
||||
if auto_cpufreq_log_file is not None:
|
||||
auto_cpufreq_log_file.close()
|
||||
|
||||
auto_cpufreq_log_path.unlink()
|
||||
|
||||
# restore original cpufrectl script
|
||||
cpufreqctl_restore()
|
||||
@ -283,7 +296,6 @@ def root_check():
|
||||
footer()
|
||||
exit(1)
|
||||
|
||||
|
||||
# refresh countdown
|
||||
def countdown(s):
|
||||
# Fix for wrong log output and "TERM environment variable not set"
|
||||
@ -295,6 +307,12 @@ def countdown(s):
|
||||
sys.stdout.flush()
|
||||
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)
|
||||
def display_load():
|
||||
|
||||
@ -753,16 +771,9 @@ def no_log_msg():
|
||||
|
||||
# read log func
|
||||
def read_log():
|
||||
|
||||
# read log (snap)
|
||||
if os.getenv("PKG_MARKER") == "SNAP":
|
||||
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)])
|
||||
# readlog
|
||||
if os.path.isfile(auto_cpufreq_log_path):
|
||||
call(["tail", "-n 50", "-f", str(auto_cpufreq_log_path)], stderr=DEVNULL)
|
||||
else:
|
||||
no_log_msg()
|
||||
footer()
|
||||
|
@ -31,6 +31,7 @@ def main(monitor, live, daemon, install, log, debug):
|
||||
else:
|
||||
# Important: order does matter
|
||||
if daemon:
|
||||
file_logging()
|
||||
if os.getenv("PKG_MARKER") == "SNAP" and dcheck == "enabled":
|
||||
while True:
|
||||
root_check()
|
||||
@ -41,7 +42,6 @@ def main(monitor, live, daemon, install, log, debug):
|
||||
sysinfo()
|
||||
set_autofreq()
|
||||
countdown(5)
|
||||
run("clear")
|
||||
elif os.getenv("PKG_MARKER") != "SNAP":
|
||||
while True:
|
||||
root_check()
|
||||
@ -52,7 +52,6 @@ def main(monitor, live, daemon, install, log, debug):
|
||||
sysinfo()
|
||||
set_autofreq()
|
||||
countdown(5)
|
||||
run("clear")
|
||||
else:
|
||||
daemon_not_found()
|
||||
elif monitor:
|
||||
@ -66,7 +65,6 @@ def main(monitor, live, daemon, install, log, debug):
|
||||
sysinfo()
|
||||
mon_autofreq()
|
||||
countdown(5)
|
||||
run("clear")
|
||||
elif live:
|
||||
while True:
|
||||
root_check()
|
||||
@ -78,7 +76,6 @@ def main(monitor, live, daemon, install, log, debug):
|
||||
sysinfo()
|
||||
set_autofreq()
|
||||
countdown(5)
|
||||
run("clear")
|
||||
elif log:
|
||||
read_log()
|
||||
elif debug:
|
||||
|
@ -1,12 +1,10 @@
|
||||
[Unit]
|
||||
Description=auto-cpufreq - Automatic CPU speed & power optimizer for Linux
|
||||
After=network.target network-online.target
|
||||
ConditionPathExists=/var/log/auto-cpufreq.log
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=root
|
||||
ExecStart=/usr/local/bin/auto-cpufreq --daemon
|
||||
StandardOutput=append:/var/log/auto-cpufreq.log
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
Loading…
x
Reference in New Issue
Block a user