https://github.com/GeoscienceAustralia/PyRate
Tip revision: d0a634bff0ab79420b9e28abd21f1f48146f642c authored by Matt Garthwaite on 22 May 2017, 05:42:23 UTC
update version to 0.2.0
update version to 0.2.0
Tip revision: d0a634b
pyratelog.py
# This Python module is part of the PyRate software package.
#
# Copyright 2017 Geoscience Australia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This Python module contains functions to control PyRate log outputs
"""
import logging
import sys
import traceback
import warnings
from pyrate import mpiops
def configure(verbosity):
"""
Function to configure logging properties
:param str verbosity: ['DEBUG', 'INFO', 'WARNING', or 'ERROR']
:return: None
"""
log = logging.getLogger("")
log.setLevel(verbosity)
stream = _MPIStreamHandler()
formatter = _ElapsedFormatter()
stream.setFormatter(formatter)
log.addHandler(stream)
class _MPIStreamHandler(logging.StreamHandler):
"""
Only logs messages from Node 0
"""
def emit(self, record):
if mpiops.rank == 0:
super(_MPIStreamHandler, self).emit(record)
class _ElapsedFormatter:
"""
Convenience class used to show timestamps
"""
# pylint: disable=too-few-public-methods
# pylint: disable=no-self-use
def format(self, record):
""" time formatter """
lvl = record.levelname
name = record.name
t = int(round(record.relativeCreated/1000.0))
msg = record.getMessage()
logstr = "+{}s {}:{} {}".format(t, name, lvl, msg)
return logstr
#TODO: Not currently used. Remove in future?
def _warn_with_traceback(message, category, filename, lineno, line=None):
"""
copied from:
http://stackoverflow.com/questions/22373927/get-traceback-of-warnings
"""
traceback.print_stack()
log = sys.stderr
log.write(warnings.formatwarning(
message, category, filename, lineno, line))
