Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Add Logging System #3

Open
san-ghun opened this issue Nov 20, 2024 · 0 comments
Open

[feat] Add Logging System #3

san-ghun opened this issue Nov 20, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@san-ghun
Copy link
Owner

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()
@san-ghun san-ghun added the enhancement New feature or request label Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant