Skip to content

Commit

Permalink
Merge pull request #349 from linien-org/feature/add-logging
Browse files Browse the repository at this point in the history
Add (debug) logging
  • Loading branch information
bleykauf authored Jul 17, 2023
2 parents 553da6f + f6467d1 commit e3b2dec
Show file tree
Hide file tree
Showing 25 changed files with 280 additions and 110 deletions.
21 changes: 21 additions & 0 deletions linien-client/linien_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
import importlib.metadata
import logging
from logging.handlers import RotatingFileHandler

from linien_common.config import LOG_FILE_PATH

__version__ = importlib.metadata.version("linien-client") # noqa: F401

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

file_handler = RotatingFileHandler(LOG_FILE_PATH, maxBytes=1000000, backupCount=10)
file_handler.setLevel(logging.DEBUG)
file_formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)

console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_formatter = logging.Formatter("%(name)-30s %(levelname)-8s %(message)s")
console_handler.setFormatter(console_formatter)
logger.addHandler(console_handler)
22 changes: 14 additions & 8 deletions linien-client/linien_client/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with Linien. If not, see <http://www.gnu.org/licenses/>.

import logging
import random
import string
from socket import gaierror
Expand All @@ -37,6 +38,9 @@
)
from .remote_parameters import RemoteParameters

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


class ServiceWithAuth(rpyc.Service):
def __init__(self, uuid: str, user: str, password: str) -> None:
Expand Down Expand Up @@ -87,7 +91,7 @@ def connect(
while True:
i += 1
try:
print(f"Try to connect to {self.host}:{self.port}")
logger.info(f"Try to connect to {self.host}:{self.port}")

self.connection = rpyc.connect(
self.host,
Expand All @@ -108,26 +112,28 @@ def connect(
break
except gaierror:
# host not found
print(f"Error: host {self.host} not found")
logger.error(f"Error: host {self.host} not found")
break
except EOFError:
print("EOFError! Probably authentication failed")
logger.error("EOFError! Probably authentication failed")
raise RPYCAuthenticationException()
except ConnectionRefusedError:
if not autostart_server:
raise ServerNotRunningException()

if i == 0:
print("Server is not running. Launching it!")
logger.error("Server is not running. Launching it!")
start_remote_server(self.host, self.user, self.password)
sleep(3)
else:
if i < 20:
print("Server still not running, waiting (may take some time).")
logger.info(
"Server still not running, waiting (may take some time)."
)
sleep(1)
else:
print_exc()
print(
logger.error(
"Error: connection to the server could not be established"
)
break
Expand All @@ -143,7 +149,7 @@ def connect(
raise InvalidServerVersionException(local_version, remote_version)

self.connected = True
print("Connection established!")
logger.info("Connection established!")

def disconnect(self) -> None:
if self.connection is not None:
Expand All @@ -169,7 +175,7 @@ def wrapped(*args, method=method, **kwargs):
try:
return method(*args, **kwargs)
except (EOFError,):
print("Connection lost")
logger.error("Connection lost")
self.connected = False
call_on_error()
raise
Expand Down
10 changes: 7 additions & 3 deletions linien-client/linien_client/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with Linien. If not, see <http://www.gnu.org/licenses/>.

import logging
import os
import sys

Expand All @@ -27,6 +28,9 @@
)
from linien_common.communication import hash_username_and_password

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


def read_remote_version(
host: str, user: str, password: str, port: int = 22, out_stream=sys.stdout
Expand Down Expand Up @@ -72,7 +76,7 @@ def start_remote_server(
if (local_version != remote_version) and not ("dev" in local_version):
raise InvalidServerVersionException(local_version, remote_version)

print("Sending credentials")
logger.debug("Sending credentials")
conn.run(
'python3 -c "from linien_common.communication import write_hash_to_file;'
f"write_hash_to_file('{hash_username_and_password(user, password)}')\"",
Expand All @@ -81,7 +85,7 @@ def start_remote_server(
warn=True,
)

print("Starting server")
logger.debug("Starting server")
conn.run(
"linien_start_server.sh",
out_stream=out_stream,
Expand Down Expand Up @@ -116,4 +120,4 @@ def install_remote_server(
cmd, out_stream=out_stream, err_stream=out_stream, warn=True
)
if result.ok:
print(f"Sucesfully executed '{result.command}'")
logger.debug(f"Sucesfully executed '{result.command}'")
22 changes: 22 additions & 0 deletions linien-common/linien_common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
import logging
from logging.handlers import RotatingFileHandler

import importlib_metadata

from .config import LOG_FILE_PATH

__version__ = importlib_metadata.version("linien-common") # noqa: F401

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

file_handler = RotatingFileHandler(LOG_FILE_PATH, maxBytes=1000000, backupCount=10)
file_handler.setLevel(logging.DEBUG)
file_formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)

console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_formatter = logging.Formatter("%(name)-30s %(levelname)-8s %(message)s")
console_handler.setFormatter(console_formatter)
logger.addHandler(console_handler)
2 changes: 2 additions & 0 deletions linien-common/linien_common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@
DEFAULT_SWEEP_SPEED = (125 * 2048) << 6

USER_DATA_PATH = Path(AppDirs("linien").user_data_dir)
LOG_FILE_PATH = USER_DATA_PATH / "linien.log"
LOG_FILE_PATH.parent.mkdir(parents=True, exist_ok=True)
6 changes: 5 additions & 1 deletion linien-common/linien_common/influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
# along with Linien. If not, see <http://www.gnu.org/licenses/>.

import json
import logging

from .config import USER_DATA_PATH

CREDENTIAL_STORE_FILENAME = "influxdb_credentials.json"

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


class InfluxDBCredentials:
def __init__(
Expand Down Expand Up @@ -63,7 +67,7 @@ def save_credentials(credentials: InfluxDBCredentials) -> None:
f,
indent=2,
)
print("Saved InfluxDB credentials to ", filename)
logger.info("Saved InfluxDB credentials to %s" % filename)


def restore_credentials() -> InfluxDBCredentials:
Expand Down
21 changes: 21 additions & 0 deletions linien-gui/linien_gui/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
import importlib.metadata
import logging
from logging.handlers import RotatingFileHandler

from linien_common.config import LOG_FILE_PATH

__version__ = importlib.metadata.version("linien-gui") # noqa: F401

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

file_handler = RotatingFileHandler(LOG_FILE_PATH, maxBytes=1000000, backupCount=10)
file_handler.setLevel(logging.DEBUG)
file_formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)

console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_formatter = logging.Formatter("%(name)-30s %(levelname)-8s %(message)s")
console_handler.setFormatter(console_formatter)
logger.addHandler(console_handler)
13 changes: 8 additions & 5 deletions linien-gui/linien_gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
# You should have received a copy of the GNU General Public License
# along with Linien. If not, see <http://www.gnu.org/licenses/>.

import logging
import signal
import sys
from traceback import print_exc

import click
from linien_client.connection import LinienClient
Expand All @@ -34,6 +34,9 @@

sys.path += [str(UI_PATH)]

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


class LinienApp(QtWidgets.QApplication):
connection_established = pyqtSignal()
Expand Down Expand Up @@ -69,8 +72,7 @@ def periodically_check_for_changed_parameters(self):
try:
self.parameters.check_for_changed_parameters()
except AttributeError:
print("check_for_changed_parameters() failed")
print_exc()
logger.exception("check_for_changed_parameters() failed")

QtCore.QTimer.singleShot(50, self.periodically_check_for_changed_parameters)

Expand Down Expand Up @@ -100,10 +102,10 @@ def check_for_new_version(self):

def new_version_available(self, new_version_available):
if new_version_available:
print("New version available")
logger.info("New version available")
self.main_window.show_new_version_available()
else:
print("No new version available")
logger.info("No new version available")
QtCore.QTimer.singleShot(1000 * 60 * 60, self.check_for_new_version)


Expand All @@ -113,6 +115,7 @@ def new_version_available(self, new_version_available):
@click.version_option(__version__)
def run_application():
app = LinienApp(sys.argv)
logger.info("Starting Linien GUI")

# catch ctrl-c and shutdown
signal.signal(signal.SIGINT, signal.SIG_DFL)
Expand Down
6 changes: 5 additions & 1 deletion linien-gui/linien_gui/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
# along with Linien. If not, see <http://www.gnu.org/licenses/>.

import json
import logging
import pickle
from enum import Enum
from typing import Callable, Iterator, List, Tuple

import rpyc
from linien_common.config import USER_DATA_PATH

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# don't plot more often than once per `DEFAULT_PLOT_RATE_LIMIT` seconds
DEFAULT_PLOT_RATE_LIMIT = 0.1

Expand Down Expand Up @@ -159,7 +163,7 @@ def save_parameter(
try:
device["params"][param_name] = rpyc.classic.obtain(value)
except Exception:
print("unable to obtain and save parameter", param_name)
logger.exception("unable to obtain and save parameter %s" % param_name)
else:
try:
del device["params"][param_name]
Expand Down
12 changes: 9 additions & 3 deletions linien-gui/linien_gui/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with Linien. If not, see <http://www.gnu.org/licenses/>.

import logging
import traceback

from linien_client.connection import LinienClient
Expand All @@ -31,6 +32,9 @@

from .config import get_saved_parameters, save_parameter

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


class RemoteOutStream(QObject):
new_item = pyqtSignal(str)
Expand Down Expand Up @@ -141,7 +145,7 @@ def restore_parameters(self, dry_run=False):
* `False`, the local parameters are uploaded to the server
"""
params = get_saved_parameters(self.device["key"])
print("restoring parameters")
logger.info("Restoring parameters")

differences = False

Expand All @@ -150,15 +154,17 @@ def restore_parameters(self, dry_run=False):
param = getattr(self.client.parameters, k)
if param.value != v:
if dry_run:
print("parameter", k, "differs")
logger.info(f"parameter {k} differs")
differences = True
break
else:
param.value = v
else:
# This may happen if the settings were written with a different version
# of linien.
print(f"Unable to restore parameter {k}. Delete the cached value.")
logger.warning(
f"Unable to restore parameter {k}. Delete the cached value."
)
save_parameter(self.device["key"], k, None, delete=True)

if not dry_run:
Expand Down
12 changes: 8 additions & 4 deletions linien-gui/linien_gui/ui/logging_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with Linien. If not, see <http://www.gnu.org/licenses/>.

import logging
import pickle

from linien_client.remote_parameters import RemoteParameters
Expand All @@ -27,6 +28,9 @@
START_LOG_BUTTON_TEXT = "Start Logging"
STOP_LOG_BUTTON_TEXT = "Stop Logging"

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


class LoggingPanel(QtWidgets.QWidget):
set_parameter_log = pyqtSignal(str, bool)
Expand Down Expand Up @@ -68,7 +72,7 @@ def on_connection_established(self) -> None:

# getting the influxdb credentials from the remote
credentials = pickle.loads(self.control.exposed_get_influxdb_credentials())
print("Received InfluxDB credentials from server.")
logger.debug("Received InfluxDB credentials from server.")
self.lineEditURL.setText(credentials.url)
self.lineEditOrg.setText(credentials.org)
self.lineEditToken.setText(credentials.token)
Expand Down Expand Up @@ -106,10 +110,10 @@ def on_influx_update_button_clicked(self) -> None:
sucess, status_code, message = self.control.exposed_update_influxdb_credentials(
credentials
)
print(f"Update of InfluxDB credentials successful: {sucess}")
update_msg = f"Update of InfluxDB credentials successful: {sucess}"
if not sucess:
print(f"Status code: {status_code}")
print(f"Message: {message}")
update_msg += f" (Status {status_code}): {message}"
logger.info(update_msg)
self.influx_credentials_update.emit(sucess, status_code, message)

def on_influxdb_credentials_updated(
Expand Down
Loading

0 comments on commit e3b2dec

Please sign in to comment.