""" Utility functions for the application. """ import logging import traceback from datetime import datetime # Configure logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler('app.log'), logging.StreamHandler() ] ) logger = logging.getLogger(__name__) def log_error(message, exception=None): """Log error message with optional exception details.""" if exception: logger.error(f"{message}: {str(exception)}") logger.error(traceback.format_exc()) else: logger.error(message) def log_info(message): """Log info message.""" logger.info(message) def get_timestamp(): """Get current timestamp as string.""" return datetime.now().strftime("%Y-%m-%d %H:%M:%S") def sanitize_filename(filename): """Sanitize filename for safe file operations.""" # Remove or replace unsafe characters unsafe_chars = ['<', '>', ':', '"', '/', '\\', '|', '?', '*'] for char in unsafe_chars: filename = filename.replace(char, '_') return filename.strip()