Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging functionality #27

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/wands/adios.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# import numpy as np
# import warnings
import adios2
from logger import logger


# Adios object (only one per application) therefore a global variable within this module TODO rename to adios
Expand Down Expand Up @@ -102,11 +103,11 @@ def get_avail_variables(self):
return self._io.AvailableVariables()

def print_info(self):
print(f"Name: {self.get_link()!s}")
print(f"Engine: {self.get_engine()!s}")
print(f"Parameters: {self.get_parameters()!s}")
print(f"Variables: {self.get_avail_variables()!s}")
print(f"Attributes: {self.get_avail_attributes()!s}")
logger.info(f"Name: {self.get_link()!s}")
logger.info(f"Engine: {self.get_engine()!s}")
logger.info(f"Parameters: {self.get_parameters()!s}")
logger.info(f"Variables: {self.get_avail_variables()!s}")
logger.info(f"Attributes: {self.get_avail_attributes()!s}")

# def remove_all_variables(self):
# self.RemoveAllVariables()
Expand Down
43 changes: 22 additions & 21 deletions src/wands/data_cache.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import adios2
import re
import numpy as np
from pathlib import Path
from .adios import AdiosObject

from logger import logger


class DataCache:
"""
Expand Down Expand Up @@ -60,22 +63,22 @@ def write(self, filename: str, data_dict: dict):

# modify string to delete .hdf5 with string find
bpfilename_path = self._path / filename.replace(".h5", ".bp")
# print(bpfilename_path)
logger.info(bpfilename_path)
writer = self._adob.get_IO().Open(f"{bpfilename_path!s}", adios2.Mode.Append)

writer.BeginStep()
for var_name, data in data_dict.items():
# Test if the variable already exists
if self._adob.get_IO().InquireVariable(var_name):
print(f" Debug: variable {var_name!s} exists already")
logger.info(f" Debug: variable {var_name!s} exists already")
continue

shape = data.shape
# print(f"shape in send: {shape!s}")
logger.debug(f"shape in send: {shape!s}")
count = shape
# print(f"count in send {count!s}")
logger.debug(f"count in send {count!s}")
start = (0,) * len(shape)
# print(f"start in send {start!s}")
logger.debug(f"start in send {start!s}")
sendbuffer = self._adob.get_IO().DefineVariable(
var_name, data, shape, start, count, adios2.ConstantDims
)
Expand Down Expand Up @@ -113,16 +116,12 @@ def check_availability(self, filename: str, data_list: list):
--------

"""
# print( type(filename.replace('.h5','.bp')))
# print(self._path)
# print(type(self._path))
bpfilename_path = self._path / filename.replace(".h5", ".bp")
# print(type(bpfilename_path))
# print(f"check av: data type data_list: {type(data_list)}")
# print(f"Check availability")

bpfilename_path = self._path / re.sub("\.\w+$", ".bp", filename)

local_list = []
remote_list = []
# print(f"trying to open {bpfilename_path}")
logger.info(f"trying to open {bpfilename_path}")
if bpfilename_path.exists():
reader = self._adob.get_IO().Open(f"{bpfilename_path!s}", adios2.Mode.Read)
if reader:
Expand All @@ -135,7 +134,7 @@ def check_availability(self, filename: str, data_list: list):
remote_list.append(signal)
else:
# This should never happen since we checked before that the file exists
print(
logger.warn(
f"[WARNING] Even though the {bpfilename_path!s} exists WANDS was unable to open it. Requesting all datasets remotely"
)
remote_list = data_list
Expand All @@ -147,7 +146,7 @@ def check_availability(self, filename: str, data_list: list):

def load_from_cache(self, filename: str, local_list: list):
bpfilename_path = self._path / filename.replace(".h5", ".bp")
# print(f"beginning of cache: {local_list!s}")
logger.debug(f"beginning of cache: {local_list!s}")
# self._adob.print_info()
local_dict = {}
if local_list:
Expand All @@ -160,16 +159,18 @@ def load_from_cache(self, filename: str, local_list: list):

while True:
stepStatus = reader.BeginStep()
# print(stepStatus)
logger.debug(stepStatus)
if stepStatus == adios2.StepStatus.OK:
# print(f"Current Step: {reader.CurrentStep()!s} reader Steps= {reader.Steps()!s} local dict = {local_dict!s} ")
logger.debug(
f"Current Step: {reader.CurrentStep()!s} reader Steps= {reader.Steps()!s} local dict = {local_dict!s} "
)
for signal in local_list:
if signal not in local_dict:
variable = self._adob.get_IO().InquireVariable(
signal
)
if variable:
# print(variable.Type())
logger.debug(variable.Type())
data = np.zeros(
variable.Shape(), dtype=variable.Type()
)
Expand All @@ -183,10 +184,10 @@ def load_from_cache(self, filename: str, local_list: list):
)

reader.EndStep()
# print(f"call close next")
logger.debug(f"call close next")
reader.Close()
# print(f"{bpfilename_path!s} closed")
# print(local_list)
logger.info(f"{bpfilename_path!s} closed")
logger.debug(local_list)
else:
raise ValueError(f" file {bpfilename_path} not found")

Expand Down
57 changes: 57 additions & 0 deletions src/wands/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
import the object logger from this file and log to it.

It writes all information to a log file, INFO to stdout, and warnings,
errors, to stderr.
"""
import sys
import os
import logging
from pathlib import Path
import re

# Allow set the directory to write the log files in with the environment
# variable WANDSLOGPATH
# Default to CWD
if LOGPATH := os.environ.get("WANDSLOGPATH"):
LOGPATH = Path(LOGPATH)
else:
LOGPATH = Path(".")

LOGFILE = "WANDS"
n = 0
EXT = ".log"

pattern = LOGFILE + "(\d*)" + EXT

# Increment the n value
for f in os.listdir(LOGPATH):
if res := re.search(pattern, f):
if int(res.group(1)) > n:
n = int(res.group(1))

logging.basicConfig(filename=str(LOGPATH / (LOGFILE + n + EXT)), level=logging.DEBUG)

logger = logging.getLogger("WANDS")

# Filter to isolate INFO for stdout
class _LoggingFilter(logging.Filter):
def __init__(self, level: int):
super().__init__()
self._level = level

def filter(self, record: logging.LogRecord) -> bool:
return record.levelno == self._level


# INFO to stdout
outhandler = logging.StreamHandler(sys.stdout)
outhandler.setLevel(logging.INFO)
outhandler.addFilter(_LoggingFilter(logging.INFO))

# WARNING and above to stderr
errhandler = logging.StreamHandler(sys.stderr)
errhandler.setLevel(logging.WARNING)

logger.addHandler(outhandler)
logger.addHandler(errhandler)
Loading