Coverage for src/pyTRLCConverter/logger.py: 93%

15 statements  

« prev     ^ index     » next       coverage.py v7.10.3, created at 2025-08-14 10:59 +0000

1"""Log verbose functionality and errors. 

2 

3 Author: Andreas Merkle (andreas.merkle@newtec.de) 

4""" 

5 

6# pyTRLCConverter - A tool to convert TRLC files to specific formats. 

7# Copyright (c) 2024 - 2025 NewTec GmbH 

8# 

9# This file is part of pyTRLCConverter program. 

10# 

11# The pyTRLCConverter program is free software: you can redistribute it and/or modify it under 

12# the terms of the GNU General Public License as published by the Free Software Foundation, 

13# either version 3 of the License, or (at your option) any later version. 

14# 

15# The pyTRLCConverter program is distributed in the hope that it will be useful, but 

16# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 

17# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License along with pyTRLCConverter. 

20# If not, see <https://www.gnu.org/licenses/>. 

21 

22# Imports ********************************************************************** 

23 

24import sys 

25import logging 

26 

27# Variables ******************************************************************** 

28 

29_VERBOSE_ENABLED = False 

30logging.basicConfig(level=logging.INFO, 

31 format="%(asctime)s - %(levelname)s - %(message)s") 

32 

33# Classes ********************************************************************** 

34 

35# Functions ******************************************************************** 

36 

37def is_verbose_enabled() -> bool: 

38 # lobster-trace: SwRequirements.sw_req_verbose_mode 

39 """Check if verbose mode is enabled. 

40  

41 Returns: 

42 bool: True if verbose mode is enabled, False otherwise. 

43 """ 

44 return _VERBOSE_ENABLED 

45 

46def enable_verbose(enable : bool) -> None: 

47 # lobster-trace: SwRequirements.sw_req_verbose_mode 

48 """Enable or disable verbose mode. 

49  

50 Args: 

51 enable (bool): True to enable verbose mode, False to disable it. 

52 """ 

53 global _VERBOSE_ENABLED # pylint: disable=global-statement 

54 _VERBOSE_ENABLED = enable 

55 

56def log_verbose(message : str) -> None: 

57 # lobster-trace: SwRequirements.sw_req_verbose_mode 

58 """Print a message if verbose mode is enabled. 

59  

60 Args: 

61 message (str): The message to print. 

62 """ 

63 if _VERBOSE_ENABLED: 

64 print(message) 

65 

66def log_error(message : str, show_timestamp : str = False) -> None: 

67 # lobster-trace: SwRequirements.sw_req_error 

68 """Prints an error and optionally a timestamp with it 

69 

70 Args: 

71 message (str): The error message 

72 show_timestamp (bool, optional): Option to enable logging. Defaults to False. 

73 """ 

74 if show_timestamp: 

75 logging.error(message) 

76 else: 

77 print(message, file=sys.stderr) 

78 

79 

80# Main *************************************************************************