import sys
import datetime
import os
ARCHIVO_LOG = "mcp_server.log"
def main():
try:
sys.stdin.reconfigure(encoding='utf-8')
except AttributeError:
pass
print(f"Logger iniciado. Escribiendo en {ARCHIVO_LOG} (Más reciente arriba)", file=sys.stderr)
for linea in sys.stdin:
marca_tiempo = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
contenido = linea.strip()
if not contenido:
continue
entrada_formateada = f"[{marca_tiempo}] {contenido}\n"
print(entrada_formateada.strip())
try:
contenido_existente = ""
if os.path.exists(ARCHIVO_LOG):
try:
with open(ARCHIVO_LOG, 'r', encoding='utf-8') as f:
contenido_existente = f.read()
except Exception:
pass
with open(ARCHIVO_LOG, 'w', encoding='utf-8') as f:
f.write(entrada_formateada + contenido_existente)
except Exception as e:
print(f"Error escribiendo log: {e}", file=sys.stderr)
if __name__ == "__main__":
main()