Guest User

Untitled

a guest
Aug 4th, 2022
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. # Hey there,
  2. #
  3. # Just for the people that are interested for creating separate log files for each time the device reboots / deepsleeps, I have made the following alteration that keeps the logfiles from being overwritten every reboot/deepsleep. Notice that this solution does not need any time keeping (and therefore an internet connection to synchronize time). I hope that it helps anyone facing debugging issues on long running devices where serial connections over longer periods do not make sense. Cheers.
  4.  
  5.  
  6.  
  7.  
  8. # Logging every console output to a file for debugging puroposes on long running solutions.
  9. import io, os
  10.  
  11. esp32_files = os.listdir()
  12.  
  13. # Removing the other files that are not log files.
  14. esp32_files.remove('main.py')
  15. esp32_files.remove('boot.py')
  16.  
  17. # Determining the highest ending number of the log files in order to increase the number for the next log write (after reboot or deepsleep).
  18. highest_number = 0
  19.  
  20. # The following is nested in a try statement, in order to create an initial log file.
  21. try:
  22.     for filename in esp32_files:
  23.         temp_number = int(filename[8:-4])
  24.         if temp_number >= highest_number:
  25.             highest_number = temp_number
  26.     print('last logfile number is:', highest_number)
  27.     print('logging now on logfile number ', highest_number+1)
  28. except:
  29.     pass
  30.  
  31.  
  32. class logToFile(io.IOBase):
  33.     def __init__(self):
  34.         pass
  35.  
  36.     def write(self, data):
  37.         logFileName = 'logfile_{}.log'.format(highest_number+1)
  38.         with open(logFileName, mode="a") as f:
  39.             f.write(data)
  40.         return len(data)
  41.  
  42. # now your console text output is saved into file
  43. os.dupterm(logToFile())
  44.  
  45. # disable logging to file
  46. # os.dupterm(None)
Advertisement
Add Comment
Please, Sign In to add comment