Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Fail to create log file from boot.py

    Scheduled Pinned Locked Moved LoPy
    3 Posts 3 Posters 27 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B Offline
      beelzmon
      last edited by

      I have set up a logging class in my main.py, being:

      class Logger:
          file_name = None
          def __init__(self,file_name):
              self.file_name = file_name
          def write(self,msg):
              with open(self.file_name,"a") as file:#write a message to log 
                  file.write(msg)
                  file.write('\n')
          def clear(self):
              with open(self.file_name,"w")#remove closed file
                  pass
      

      In my boot file and my main file i call on it to create logs, however it does not create any of the logs it should for the boot file -- only for the main.
      Below is the boot:

      from machine import UART
      from network import LoRa
      from network import WLAN,Bluetooth
      from main import Logger
      import binascii
      import os
      import pycom
      logger = Logger("SessionLog.txt")
      logger.write("Booting")
      pycom.heartbeat(False)
      # Setting up the UART to dump the output to the console
      uart = UART(0, 115200)
      os.dupterm(uart)
      logger.write("UART enabled and console dumped")
      # Disable bluetooth
      bluetooth = Bluetooth()
      logger.write("Bluetooth initialized")
      bluetooth.deinit()
      logger.write("Bluetooth disabled")
      pycom.wifi_on_boot(False)
      logger.write("Wifi on boot turned off")
      lora = LoRa(mode=LoRa.LORAWAN, public=1, adr=0, tx_retries=0) # Getting the LoRa MAC
      logger.write("Lora initialized for grabbing MAC address")
      print("LORA MAC")
      logger.write("LORA MAC")
      print(binascii.hexlify(lora.mac()))
      logger.write(binascii.hexlify(lora.mac()))
      

      The same sort of routine logs are written in the main.py, and these are the only lines reflected in the SessionLog.txt file. So the issue is that the boot.py specifically does not write to the file. No error is raised.

      robert-hhR 1 Reply Last reply Reply Quote 0
      • K Offline
        kjm
        last edited by

        My understanding is main.py runs after boot.py so there would be no logger available for boot.py

        1 Reply Last reply Reply Quote 0
        • robert-hhR Offline
          robert-hh @beelzmon
          last edited by

          @beelzmon
          I thinkk there is a problem with the line
          from main.py import Logger
          in boot.py. That one causes main.py to be compiled and all statements at top level to be executed. So you get the main.py log messages eventually before the boot.py log messages in the log file.
          You could try to either:

          • put the class logger into a separate file, or
          • put the class logger into boot.py.

          Since both boot.py and main.py share the same name space, the class logger would be available to both. I prefer the separate file for the logger class.

          1 Reply Last reply Reply Quote 1

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom