Skip to content

DEVS 엔진에서 디버깅 모드일 때만 출력 필요 #46

Description

@sumannam

GPT 답변

디버그 모드를 위한 코드를 작성해드리겠습니다. Python의 logging 모듈을 사용하거나 디버그 플래그를 사용하는 두 가지 방법이 있습니다. 두 가지 방법 모두 보여드리겠습니다:

logging 모듈 사용:

import logging

# 로깅 설정
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class YourClass:
    def diffChildModel(self, target_model, json_child_list):
        """! 
        @fn         diffChildModel
        @brief      타겟 결합 모델의 하위 모델 비교
        @details    
        
        @reference  https://github.com/sumannam/DEVS-Python/issues/31
        
        @param  target_model        결합 모델 인스턴스 
        @param  json_child_list     json 파일의 하위 모델 리스트

        @author     남수만(sumannam@gmail.com)
        @date       2025.01.22
        """
        child_list = target_model.getChildModelNameList()
        
        # 리스트를 스트링으로 변환
        child_list_str = ', '.join(child_list)
        json_list_str = json_child_list.get('child_models')
        
        # DEBUG 레벨에서만 출력
        logger.debug("=== Child Model ===")
        logger.debug(f"\t child_models : {child_list_str}")
        logger.debug(f"\t {self.diffStrings(child_list_str, json_list_str)}")
        logger.debug("\n")

디버그 플래그 사용:

class YourClass:
    def __init__(self):
        self.debug_mode = False  # 디버그 모드 플래그

    def set_debug_mode(self, mode: bool):
        """디버그 모드 설정"""
        self.debug_mode = mode

    def debug_print(self, *args, **kwargs):
        """디버그 모드일 때만 출력"""
        if self.debug_mode:
            print(*args, **kwargs)

    def diffChildModel(self, target_model, json_child_list):
        """! 
        @fn         diffChildModel
        @brief      타겟 결합 모델의 하위 모델 비교
        @details    
        
        @reference  https://github.com/sumannam/DEVS-Python/issues/31
        
        @param  target_model        결합 모델 인스턴스 
        @param  json_child_list     json 파일의 하위 모델 리스트

        @author     남수만(sumannam@gmail.com)
        @date       2025.01.22
        """
        child_list = target_model.getChildModelNameList()
        
        # 리스트를 스트링으로 변환
        child_list_str = ', '.join(child_list)
        json_list_str = json_child_list.get('child_models')
        
        # 디버그 모드일 때만 출력
        self.debug_print("=== Child Model ===")
        self.debug_print(f"\t child_models : {child_list_str}")
        self.debug_print(f"\t {self.diffStrings(child_list_str, json_list_str)}")
        self.debug_print("\n")

사용 예시:

# logging 모듈 사용 시
logging.getLogger(__name__).setLevel(logging.DEBUG)  # 디버그 모드 활성화
# 또는
logging.getLogger(__name__).setLevel(logging.INFO)   # 디버그 모드 비활성화

# 디버그 플래그 사용 시
instance = YourClass()
instance.set_debug_mode(True)   # 디버그 모드 활성화
# 또는
instance.set_debug_mode(False)  # 디버그 모드 비활성화

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions