Skip to content

Commit

Permalink
Merge pull request #88 from svalinn/null-logger
Browse files Browse the repository at this point in the history
  • Loading branch information
gonuke authored Apr 23, 2024
2 parents a3f6d5d + dbe8910 commit 30a7477
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 25 deletions.
19 changes: 18 additions & 1 deletion parastell/invessel_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,8 @@ def radial_build(self, build_dict):
}

for name, component in self._radial_build.items():
component['thickness_matrix'] = \
np.array(component['thickness_matrix'])
if (
component['thickness_matrix'].shape !=
(len(self._toroidal_angles), len(self._poloidal_angles))
Expand Down Expand Up @@ -740,6 +742,15 @@ def parse_args():
'filename',
help='YAML file defining ParaStell in-vessel component configuration'
)
parser.add_argument(
'-l', '--logger',
default=False,
help=(
'Flag to indicate whether to instantiate a logger object (default: '
'False)'
),
metavar=''
)

return parser.parse_args()

Expand All @@ -761,6 +772,11 @@ def generate_invessel_build():

vmec_file, invessel_build_dict = read_yaml_config(args.filename)

if args.logger == True:
logger = log.init()
else:
logger = log.NullLogger()

vmec_obj = read_vmec.VMECData(vmec_file)

rb_allowed_kwargs = ['plasma_mat_tag', 'sol_mat_tag']
Expand All @@ -774,6 +790,7 @@ def generate_invessel_build():
invessel_build_dict['poloidal_angles'],
invessel_build_dict['wall_s'],
invessel_build_dict['radial_build'],
logger=logger
**rb_kwargs
)

Expand All @@ -786,7 +803,7 @@ def generate_invessel_build():
invessel_build = InVesselBuild(
vmec_obj,
radial_build,
logger=radial_build.logger,
logger=logger,
**ivb_kwargs
)

Expand Down
69 changes: 47 additions & 22 deletions parastell/log.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,69 @@
import logging
import time


def check_init(logger_obj):
"""Checks if a logger object has been instantiated, and if not,
instantiates one.
Arguments:
logger_obj (object or None): logger object input.
Returns:
logger_obj (object): logger object.
"""
if logger_obj != None and logger_obj.hasHandlers():
return logger_obj
else:
return NullLogger()


class NullLogger(object):
"""Creates a pseudo logger object mimicking an actual logger object whose
methods do nothing when called.
"""
def __init__(self):
pass

def hasHandlers(self):
return True

def info(self, message):
current_time = time.localtime()
current_time = time.strftime('%H:%M:%S', current_time)
print(f'{current_time}: {message}')

def warning(self, *args):
pass

def error(self, *args):
pass


def init():
"""Creates and configures logger with separate stream and file handlers.
Returns:
logger (object): logger object.
"""
# Create logger
logger = logging.getLogger('log')
# Configure base logger message level

logger.setLevel(logging.INFO)
# Configure stream handler

s_handler = logging.StreamHandler()
# Configure file handler
f_handler = logging.FileHandler('stellarator.log')
# Define and set logging format
f_handler = logging.FileHandler(
filename='stellarator.log',
mode='w'
)

format = logging.Formatter(
fmt = '%(asctime)s: %(message)s',
datefmt = '%H:%M:%S'
)
s_handler.setFormatter(format)
f_handler.setFormatter(format)
# Add handlers to logger

logger.addHandler(s_handler)
logger.addHandler(f_handler)

return logger


def check_init(logger_obj):
"""Checks if a logger object has been instantiated, and if not,
instantiates one.
Arguments:
logger_obj (object or None): logger object input.
Returns:
logger_obj (object): logger object.
"""
if logger_obj != None and logger_obj.hasHandlers():
return logger_obj
else:
return init()
15 changes: 15 additions & 0 deletions parastell/magnet_coils.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,15 @@ def parse_args():
parser.add_argument(
'filename', help='YAML file defining ParaStell magnet configuration'
)
parser.add_argument(
'-l', '--logger',
default=False,
help=(
'Flag to indicate whether to instantiate a logger object (default: '
'False)'
),
metavar=''
)

return parser.parse_args()

Expand All @@ -609,6 +618,11 @@ def generate_magnet_set():

magnet_coils_dict = read_yaml_config(args.filename)

if args.logger == True:
logger = log.init()
else:
logger = log.NullLogger()

mc_allowed_kwargs = [
'start_line', 'sample_mod', 'scale', 'mat_tag'
]
Expand All @@ -621,6 +635,7 @@ def generate_magnet_set():
magnet_coils_dict['coils_file'],
magnet_coils_dict['cross_section'],
magnet_coils_dict['toroidal_extent'],
logger=logger
**mc_kwargs
)

Expand Down
19 changes: 18 additions & 1 deletion parastell/parastell.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,15 @@ def parse_args():
'filename',
help='YAML file defining ParaStell stellarator configuration'
)
parser.add_argument(
'-l', '--logger',
default=False,
help=(
'Flag to indicate whether to instantiate a logger object (default: '
'False)'
),
metavar=''
)

return parser.parse_args()

Expand Down Expand Up @@ -494,7 +503,15 @@ def parastell():
vmec_file, invessel_build, magnet_coils, source_mesh, dagmc_export
) = read_yaml_config(args.filename)

stellarator = Stellarator(vmec_file)
if args.logger == True:
logger = log.init()
else:
logger = log.NullLogger()

stellarator = Stellarator(
vmec_file,
logger=logger
)

# In-Vessel Build

Expand Down
17 changes: 16 additions & 1 deletion parastell/source_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,15 @@ def parse_args():
'filename',
help='YAML file defining ParaStell source mesh configuration'
)
parser.add_argument(
'-l', '--logger',
default=False,
help=(
'Flag to indicate whether to instantiate a logger object (default: '
'False)'
),
metavar=''
)

return parser.parse_args()

Expand All @@ -430,6 +439,11 @@ def generate_source_mesh():

vmec_file, source_mesh_dict = read_yaml_src(args.filename)

if args.logger == True:
logger = log.init()
else:
logger = log.NullLogger()

vmec_obj = read_vmec.VMECData(vmec_file)

sm_allowed_kwargs = ['scale']
Expand All @@ -441,7 +455,8 @@ def generate_source_mesh():
source_mesh = SourceMesh(
vmec_obj,
source_mesh_dict['mesh_size'],
source_mesh_dict['toroidal_extent']
source_mesh_dict['toroidal_extent'],
logger=logger
**sm_kwargs
)

Expand Down

0 comments on commit 30a7477

Please sign in to comment.