PaddleClas/ppcls/utils/logger.py

74 lines
2.4 KiB
Python
Raw Normal View History

2020-04-20 21:16:04 +08:00
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
2020-04-11 02:35:22 +08:00
#
2020-04-20 21:16:04 +08:00
# 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
2020-04-11 02:35:22 +08:00
#
# http://www.apache.org/licenses/LICENSE-2.0
#
2020-04-20 21:16:04 +08:00
# 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.
2020-04-11 02:35:22 +08:00
2020-04-22 17:36:21 +08:00
import logging
import os
2020-04-11 02:35:22 +08:00
2020-04-22 17:36:21 +08:00
logging.basicConfig(level=logging.INFO)
_logger = logging.getLogger(__name__)
2020-04-11 02:35:22 +08:00
2020-04-22 17:36:21 +08:00
def anti_fleet(log):
2020-04-11 02:35:22 +08:00
"""
2020-04-22 17:36:21 +08:00
Because of the fucking Fleet, logs will print multi-times.
So we only display one of them and ignore the others.
2020-04-11 02:35:22 +08:00
"""
2020-04-22 17:36:21 +08:00
def wrapper(fmt, *args):
if int(os.getenv("PADDLE_TRAINER_ID", 0)) == 0:
log(fmt, *args)
return wrapper
@anti_fleet
2020-04-11 02:35:22 +08:00
def info(fmt, *args):
_logger.info(fmt, *args)
2020-04-22 17:36:21 +08:00
@anti_fleet
2020-04-11 02:35:22 +08:00
def warning(fmt, *args):
_logger.warning(fmt, *args)
2020-04-22 17:36:21 +08:00
@anti_fleet
2020-04-11 02:35:22 +08:00
def error(fmt, *args):
_logger.error(fmt, *args)
2020-04-20 21:16:04 +08:00
2020-04-21 01:37:27 +08:00
def advertise():
2020-04-21 00:49:09 +08:00
"""
Show the advertising message like the following:
===========================================================
== PaddleClas is powered by PaddlePaddle ! ==
===========================================================
== ==
== For more info please go to the following website. ==
== ==
== https://github.com/PaddlePaddle/PaddleClas ==
===========================================================
"""
2020-04-20 21:16:04 +08:00
copyright = "PaddleClas is powered by PaddlePaddle !"
2020-04-22 17:36:21 +08:00
ad = "For more info please go to the following website."
2020-04-20 21:16:04 +08:00
website = "https://github.com/PaddlePaddle/PaddleClas"
2020-04-22 17:36:21 +08:00
AD_LEN = 6 + len(max([copyright, ad, website], key=len))
2020-04-20 21:16:04 +08:00
2020-04-22 17:36:21 +08:00
info("\n{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n".format(
2020-04-20 21:16:04 +08:00
"=" * (AD_LEN + 4),
"=={}==".format(copyright.center(AD_LEN)),
"=" * (AD_LEN + 4),
"=={}==".format(' ' * AD_LEN),
2020-04-22 17:36:21 +08:00
"=={}==".format(ad.center(AD_LEN)),
2020-04-20 21:16:04 +08:00
"=={}==".format(' ' * AD_LEN),
"=={}==".format(website.center(AD_LEN)),
2020-04-22 17:36:21 +08:00
"=" * (AD_LEN + 4) ))