You could create a WHILE loop that executes a function, which writes certain data to a file.
Every time the fuction is executed it appends the data to the end of the file. You probably don't want to make a while loop that continues forever writing data to a file... Maybe only have it execute every few seconds? Or only if certain keys are pressed.