Skip to content

Commit

Permalink
add logging support (#219)
Browse files Browse the repository at this point in the history
* add logging support

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
PythonFZ and pre-commit-ci[bot] authored Sep 19, 2023
1 parent ad79402 commit baf0d4e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,26 @@ example notebook.
from zndraw import ZnDraw
import ase

zndraw = ZnDraw()
vis = ZnDraw()

zndraw.socket.sleep(2) # give it some time to fully connect
zndraw[0] = ase.Atoms(
vis.socket.sleep(2) # give it some time to fully connect
vis[0] = ase.Atoms(
"H2O", positions=[[0.75, -0.75, 0], [0.75, 0.75, 0], [0, 0, 0]]
)
```

ZnDraw also provides an interface to the Python
[logging](https://docs.python.org/3/library/logging.html) library, including
support for formatters and different logging levels.

```python
import logging

log = logging.getLogger(__name__)
log.addHandler(vis.get_logging_handler())
log.critical("Critical Message")
```

## User Interface

![ZnDraw UI](https://raw.githubusercontent.com/zincware/ZnDraw/main/misc/zndraw_ui.png "ZnDraw UI")
Expand Down
5 changes: 5 additions & 0 deletions zndraw/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,8 @@ def delete_atoms(data):
@io.on("atoms:insert")
def insert_atoms(data):
emit("atoms:insert", data, broadcast=True, include_self=False)


@io.on("message:log")
def log(data):
emit("message:log", data, broadcast=True, include_self=False)
4 changes: 4 additions & 0 deletions zndraw/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ function main() {
socket.emit("atoms:request", window.location.href, () => {
displayIncomingAtoms();
});

socket.on("message:log", (msg) => {
console.log(msg);
});
}

main();
25 changes: 25 additions & 0 deletions zndraw/zndraw.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import collections.abc
import contextlib
import dataclasses
import logging
import pathlib
import threading
import time
Expand All @@ -19,6 +20,23 @@
from zndraw.utils import get_port


class ZnDrawLoggingHandler(logging.Handler):
"""Logging handler which emits log messages to the ZnDraw server."""

def __init__(self, socket):
super().__init__()
self.socket = socket

def emit(self, record):
try:
msg = self.format(record)
self.socket.emit("message:log", msg)
except RecursionError: # See StreamHandler
raise
except Exception:
self.handleError(record)


@dataclasses.dataclass
class ZnDraw(collections.abc.MutableSequence):
url: str = None
Expand Down Expand Up @@ -187,6 +205,13 @@ def extend(self, values: list[ase.Atoms]) -> None:
if self.display_new:
self.display(len(self) - 1)

def log(self, message: str) -> None:
"""Log a message to the console"""
self.socket.emit("message:log", message)

def get_logging_handler(self) -> ZnDrawLoggingHandler:
return ZnDrawLoggingHandler(self.socket)

def read(self, filename: str, start: int, stop: int, step: int):
"""Read atoms from file and return a list of atoms dicts.
Expand Down

0 comments on commit baf0d4e

Please sign in to comment.