Skip to content

Add logging of print statements #83

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

Open
wants to merge 14 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
5 changes: 4 additions & 1 deletion .github/workflows/test_pvade.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ jobs:
- name: Build Conda environment
uses: conda-incubator/setup-miniconda@v3
with:
# auto-update-conda: true
python-version: "3.10"
environment-file: environment.yaml
activate-environment: pvade
- name: Run pytest
shell: bash -l {0}
run: pytest -sv pvade/tests/
run: PYTHONPATH=. pytest -sv pvade/tests/

# Job 2 of 2 - enforce Black formatting
formatting:
Expand Down
2 changes: 1 addition & 1 deletion environment.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: pvade
channels:
- conda-forge
- defaults
- nodefaults
dependencies:
# Dependencies should be kept as loosely pinned to version numbers as possible
- alive-progress
Expand Down
84 changes: 57 additions & 27 deletions pvade/IO/DataStream.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,64 @@
# from dolfinx import *
import numpy as np
import time
import os
import shutil
from dolfinx.io import XDMFFile, VTKFile
from mpi4py import MPI
from pathlib import Path
import pytest
import dolfinx
from petsc4py import PETSc
import json
from dolfinx.io import XDMFFile

# from dolfinx.fem import create_nonmatching_meshes_interpolation_data
# import logging
import sys
from datetime import datetime

# hello

def start_print_and_log(rank, logfile_name):

class PrintAndLog:
"""
A class to capture normal print statements in a log file
along with displaying them to the terminal as usual.
"""

def __init__(self, logfile_name, rank, message_type):
self.logfile_name = logfile_name
self.rank = rank
self.message_type = message_type

if message_type == "INFO":
self.terminal = sys.__stdout__
elif message_type == "ERROR":
self.terminal = sys.__stdout__
else:
raise ValueError(f"Type {message_type} not recognized")

def write(self, message):
# Write to both the command line and save to the logfile
cleaned_message = message.rstrip()

# Can include a test like `len(message) > 0 and self.rank == 0`
# to limit this to only printing from rank 0, but for parallel
# debugging, it is often helpful to see messages from all ranks
# so we omit this check for now.
if len(cleaned_message) > 0:

cleaned_message += "\n"

self.terminal.write(f"{cleaned_message}")
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

with open(self.logfile_name, "a") as fp:
fp.write(f"{timestamp} [{self.message_type}] {cleaned_message}")

def flush(self):
# Dummy method
pass

with open(logfile_name, "w") as fp:
# Start with an empty file
pass

# Redirect stdout and stderr
sys.stdout = PrintAndLog(logfile_name, rank, message_type="INFO")
sys.stderr = PrintAndLog(logfile_name, rank, message_type="ERROR")

if rank == 0:
print("Starting PVade Run")


# test actions
class DataStream:
"""Input/Output and file writing class

Expand Down Expand Up @@ -50,11 +92,6 @@ def __init__(self, domain, flow, structure, params):
self.ndim = domain.fluid.msh.topology.dim
self.thermal_analysis = params.general.thermal_analysis

self.log_filename = f"{params.general.output_dir_sol}/log.txt"
if self.rank == 0:
with open(self.log_filename, "w") as fp:
fp.write("Run Started.\n")

# If doing a fluid simulation, start a fluid solution file
if params.general.fluid_analysis:
self.results_filename_fluid = (
Expand Down Expand Up @@ -171,13 +208,6 @@ def save_XDMF_files(self, fsi_object, domain, tt):
f"Got found fsi object name = {fsi_object.name}, not recognized."
)

def print_and_log(self, string_to_print):
if self.rank == 0:
print(string_to_print)

with open(self.log_filename, "a") as fp:
fp.write(f"{string_to_print}\n")

# def fluid_struct(self, domain, flow, elasticity, params):
# # print("tst")

Expand Down
2 changes: 1 addition & 1 deletion pvade/IO/Utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def get_input_file():
"--input_file",
metavar="",
type=str,
help="The full path to the input file, e.g., 'intputs/my_params.yaml'",
help="The full path to the input file, e.g., 'inputs/my_params.yaml'",
)

command_line_inputs, unknown = parser.parse_known_args()
Expand Down
5 changes: 4 additions & 1 deletion pvade_main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pvade.fluid.FlowManager import Flow
from pvade.IO.DataStream import DataStream
from pvade.IO.DataStream import DataStream, start_print_and_log
from pvade.fsi.FSI import FSI
from pvade.IO.Parameters import SimParams
from pvade.IO.Utilities import get_input_file, write_metrics
Expand All @@ -25,6 +25,9 @@ def main(input_file=None):
# Load the parameters object specified by the input file
params = SimParams(input_file)

logfile_name = os.path.join(params.general.output_dir, "logfile.log")
start_print_and_log(params.rank, logfile_name)

fluid_analysis = params.general.fluid_analysis
structural_analysis = params.general.structural_analysis
thermal_analysis = params.general.thermal_analysis
Expand Down
Loading