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 os 20 21LOGGING = 0 22DEBUG_LOGGING = 0 23INFO_LOGGING = 1 24WARNING_LOGGING = 1 25ERROR_LOGGIN = 1 26PARSING_LOGGING = 0 27 28 29LIB_GEN_FOLDER: str = "" 30 31 32def init_log(lib_gen_folder: str) -> None: 33 global LIB_GEN_FOLDER # pylint: disable=W0603 34 LIB_GEN_FOLDER = lib_gen_folder 35 os.makedirs(os.path.join(LIB_GEN_FOLDER, "./gen/logs"), exist_ok=True) 36 37 38def debug_log(msg: str) -> None: 39 if DEBUG_LOGGING: 40 print(msg) 41 42 43def console_log(msg: str) -> None: 44 if LOGGING: 45 print(msg) 46 47 48def info_log(msg: str) -> None: 49 if INFO_LOGGING: 50 print(msg) 51 52 53def warning_log(msg: str) -> None: 54 path = os.path.join(LIB_GEN_FOLDER, "./gen/logs/warning_logs.txt") 55 if WARNING_LOGGING: 56 with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT | os.O_APPEND, mode=511), "a", encoding="utf-8") as f: 57 f.write("Warning! " + msg + "\n") 58 59 60def error_log(msg: str) -> None: 61 path = os.path.join(LIB_GEN_FOLDER, "./gen/logs/error_logs.txt") 62 if ERROR_LOGGIN: 63 with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT | os.O_APPEND, mode=511), "a", encoding="utf-8") as f: 64 f.write(msg) 65 66 67def parsing_log(msg: str) -> None: 68 if PARSING_LOGGING: 69 print(msg) 70 71 72def tmp_log(msg: str) -> None: 73 print(msg) 74 75 76def dump_to_file(file_name: str, msg: str) -> None: 77 path = LIB_GEN_FOLDER + file_name 78 with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT, mode=511), "w", encoding="utf-8") as f: 79 f.write(msg) 80 console_log("Data dumped to '" + path + "'") 81