def log(msg):
    gc.collect()
    t = time.monotonic()
    m = gc.mem_free()
    pt = rtc.RTC().datetime
    txt = f"{pt[0]}{pt[1]:02d}{pt[2]:02d}{pt[3]:02d}{pt[4]:02d}{pt[5]:02d},{t:.2f},{m},{msg}\n"
    try:
        time.sleep(.1)
        with open("/sd/runlog.csv", "a") as f:
            f.write(txt)
    except Exception as e:
            print("Error writing to runlog file:", e)
    print(txt)