import io | |
import sys | |
from logger import logger | |
def run_code(code): | |
# Redirect stdout to capture code output | |
old_stdout = sys.stdout | |
redirected_output = sys.stdout = io.StringIO() | |
try: | |
exec(code) | |
except Exception as e: | |
logger.error(f"Execution error: {e}") | |
return f"Error: {e}" | |
finally: | |
# Reset stdout | |
sys.stdout = old_stdout | |
return redirected_output.getvalue() | |