Skip to content

[feat] Add Logging System #3

@san-ghun

Description

@san-ghun

Add a proper logging system instead of using print statements. Reference the following files:

def _check_environment() -> bool:
    """Verify the execution environment is valid.
    
    Checks:
    - Current directory is within configured workspace
    - Docker is running
    - tiny42 container exists and is running
    
    Returns:
        bool: True if environment is valid, False otherwise
    """
    current_path: str = os.getcwd()
    
    # Check if current path is within workspace
    if not current_path.startswith(TINY42_WORKSPACE):
        print(f"{TINY42_RED}You are not inside the workspace specified.{TINY42_WHITE}")
        print(f"{TINY42_BLUE}tiny42 can only be ran inside the specified workspace, "
              f"currently it is set to \"{TINY42_WORKSPACE}\".{TINY42_WHITE}")
        return False
    
    # Check if tiny42 container is running
    try:
        output: str = subprocess.check_output(['docker', 'ps'], text=True)
        if 'tiny42' not in output:
            # Check if image exists
            images: str = subprocess.check_output(['docker', 'images'], text=True)
            if 'tiny42' not in images:
                init_tiny42()
            else:
                run_cmd: List[str] = ['docker', 'run', '-itd']
                port_mapping: Optional[str] = get_port_mapping()
                if port_mapping:
                    run_cmd.append(port_mapping)
                    
                run_cmd.extend([
                    '-v', f'{TINY42_WORKSPACE}:/tiny42_workspace',
                    '--name=tiny42', 'tiny42'
                ])
                
                subprocess.run(run_cmd)
    except subprocess.CalledProcessError:
        return False
    return True

Replace print statements with a logging system:

import logging

def setup_logging():
    """Configure logging for tiny42."""
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[
            logging.StreamHandler(),
            logging.FileHandler(Path.home() / '.config' / 'tiny42' / 'tiny42.log')
        ]
    )
    return logging.getLogger('tiny42')

logger = setup_logging()

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions