1#!/usr/bin/env python3 2# coding=utf-8 3# 4# Copyright (c) 2024 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17 18"""Module provides system functions like logging and open files.""" 19import logging 20import os 21import sys 22 23logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format="%(levelname)s:[%(name)s]: %(message)s", force=True) 24logging.root.name = "h_parser" 25 26LOGGING = 0 27DEBUG_LOGGING = 0 28INFO_LOGGING = 1 29DEBUG_LOGGING = 0 30WARNING_LOGGING = 1 31ERROR_LOGGIN = 1 32PARSING_LOGGING = 0 33 34 35LIB_GEN_FOLDER: str = "" 36 37 38def init_log(lib_gen_folder: str) -> None: 39 global LIB_GEN_FOLDER # pylint: disable=W0603 40 LIB_GEN_FOLDER = lib_gen_folder 41 os.makedirs(os.path.join(LIB_GEN_FOLDER, "./gen/logs"), exist_ok=True) 42 43 44def info_log(msg: str) -> None: 45 if INFO_LOGGING: 46 logging.info(msg) 47 48 49def debug_log(msg: str) -> None: 50 if DEBUG_LOGGING: 51 logging.debug(msg) 52 53 54def warning_log(msg: str) -> None: 55 if WARNING_LOGGING: 56 logging.warning(msg) 57 58 59def parsing_failed_msg(file: str) -> None: 60 logging.info("NON FATAL ERROR: Headers parser failed on {file}.\n" 61 "To reproduce locally: run 'ninja gen_yamls' and check " 62 "<es2panda_lib::binary_root>/gen/logs/error_logs.txt") 63 64 65def error_log(msg: str) -> None: 66 path = os.path.join(LIB_GEN_FOLDER, "./gen/logs/error_logs.txt") 67 if ERROR_LOGGIN: 68 with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT | os.O_APPEND, mode=511), "a", encoding="utf-8") as f: 69 f.write(msg + "\n") 70