log-write function from reddit user gnarlesincharge