Writing to text file to Log Debug prints



  • I've wrote a logger class to log my print statements as I can't debug my gpy+pytrack once I put it into sleep mode with the following code

    class Logger():
        def __init__(self,filename):
            self.filename = 'Debug.txt'#filename
            self.isLogging = True
    
        def write(self,msg):
            if self.isLogging:
                print(msg)
                with open('Debug.txt', 'a') as file:
                    file.write(msg)
                    file.write("\n")
    

    I'm using atom to upload/download files and I have a blank Debug.txt file in my project. When I go and run my code the logger runs fine. I then try and download the files from gpy back to my project and the Debug.txt file is always empty. So either it's not downloading the file or its not appending to the file. This seems like a really simple thing to code, Im not sure why I'm having issues?

    Some more oddities... If I write 'Hello world' in my first line of Debug.txt and then load it to my gpy and then read and print the file its empty...



  • @PeterB There are two ways you could do it.

    Firstly you could turn off buffering all together for your debug file. This means any write will be flushed straight away. It will slow down each write a little bit, but won't return to user-space till the write to disk has happened. To do this try changing:

    with open('Debug.txt', 'a') as file:
    

    to

    with open('Debug.txt', 'a', buffering=0) as file:
    

    Alternatively, you could leave the file buffered, as it is now, and ask the OS to flush the contents of the buffer to disk. To do this, try adding the following:

    def flush(self):
            if self.isLogging:
                print(msg)
                with open('Debug.txt', 'a') as file:
                    file.write(msg)
                    file.write("\n")
                    file.flush()
    

    Don't do both as it's a waste of CPU time. Good luck - let me know how you go.



  • @John-Baird said in Writing to text file to Log Debug prints:

    @Fin I suspect it could be to do with flushing the file to disk. I had the same writing output to stdout just before I went to deepsleep. Adding flush=True caused the write to be flushed and fixed my issue. Is it possible the read has a similar affect ? Worth a try

    Where to add that flush=True ?



  • @Fin I suspect it could be to do with flushing the file to disk. I had the same writing output to stdout just before I went to deepsleep. Adding flush=True caused the write to be flushed and fixed my issue. Is it possible the read has a similar affect ? Worth a try



  • @PeterB said in Writing to text file to Log Debug prints:

    Then I would propose:

    ...
    

    Slightly improved version:

    class Logger():
        def __init__(self,
                     filename='Debug.txt',
                     file_enabled=True,
                     stdout_enabled=False):
            self.filename = filename
            self.file_enabled = file_enabled
            self.stdout_enabled = stdout_enabled
    
        def write(self, msg):
            if self.stdout_enabled:
                print("Logger: {}".format(msg))
            if self.file_enabled:
                try:
                    with open(self.filename, 'r') as file:
                        print(file.read())
                except:
                    pass  # in case the file does not exist yet
                with open(self.filename, 'a') as file:
                    file.write(msg)
                    file.write("\n")
    


  • Then I would propose:

    class Logger():
        def __init__(self, filename='Debug.txt'):
            self.filename = filename
            self.isLogging = True
    
        def write(self, msg):
            if self.isLogging:
                print(msg)
                try:
                    with open(self.filename, 'r') as file:
                        print(file.read())
                except:
                    pass  # in case the file does not exist yet
                with open(self.filename, 'a') as file:
                    file.write(msg)
                    file.write("\n")
    


  • I changed the code to this and it started to work...

    class Logger():
        def __init__(self,filename):
            self.filename = 'Debug.txt'#filename
            self.isLogging = True
    
        def write(self,msg):
            if self.isLogging:
                print(msg)
                with open('Debug.txt', 'r') as file:
                    print(file.read())
                with open('Debug.txt', 'a') as file:
                    file.write(msg)
                    file.write("\n")
    

    Unsure why adding a read before that write fixed or maybe the pytrack was just in a weird state that finally got cleared but it's working now.


Log in to reply
 

Pycom on Twitter