Watch log

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Salvatore Di Fazio

    #1

    Watch log

    Hi guys,
    I've an application that writes a log file.
    I need to write an application with python that will read this file.

    I would like wait until a new line will be write in the file.

    Something like the "watch cat Log" command on Linux os.

    How can I check the eof and restart the reading?

  • Bjoern Schliessmann

    #2
    Re: Watch log

    Salvatore Di Fazio wrote:
    I would like wait until a new line will be write in the file.
    >
    Something like the "watch cat Log" command on Linux os.
    Why not read the file continuously and only do something if a new
    line is complete (i. e. a newline char is detected)?
    How can I check the eof and restart the reading?
    Why do you want to check for an EOF character?

    Regards,


    Björn

    --
    BOFH excuse #212:

    Of course it doesn't work. We've performed a software upgrade.

    Comment

    • Salvatore Di Fazio

      #3
      Re: Watch log

      Bjoern Schliessmann ha scritto:
      Why not read the file continuously and only do something if a new
      line is complete (i. e. a newline char is detected)?
      How can I read the file continuously?

      Comment

      • Larry Bates

        #4
        Re: Watch log

        Salvatore Di Fazio wrote:
        Hi guys,
        I've an application that writes a log file.
        I need to write an application with python that will read this file.
        >
        I would like wait until a new line will be write in the file.
        >
        Something like the "watch cat Log" command on Linux os.
        >
        How can I check the eof and restart the reading?
        >
        You didn't provide much info so here goes. If you use the
        logger module built into Python you can set up a logger that
        uses sockets. Have your program log to a socket and have the
        other program read from the socket. That way you will always
        have the latest log entries in the monitor program.

        If you can't control the program that writes the log file,
        have your monitoring program look for change in the length
        of the logfile (you can use the os.stat module for this).
        When it changes, open the file, seek to the old length and
        read bytes up to the new length and close the logfile.
        What is read will be the new log entry (or entries).
        Reset the logfile length high water mark and loop.

        Hope information helps.

        -Larry Bates

        Comment

        • Thinker

          #5
          Re: Watch log

          Salvatore Di Fazio wrote:
          Bjoern Schliessmann ha scritto:
          >
          >
          >Why not read the file continuously and only do something if a new
          >line is complete (i. e. a newline char is detected)?
          >>
          >
          How can I read the file continuously?
          >
          >
          What you want is something like 'tail -f' in linux.
          If you look into it's source code, you will find it check file periodically.
          When a read() reach the end of the file, read() will return a empty string.
          You can try to read data from the file periodically after a EOF.
          I think it is what you want.

          --
          Thinker Li - thinker@branda. to thinker.li@gmai l.com


          Comment

          • Michele Simionato

            #6
            Re: Watch log

            Salvatore Di Fazio wrote:
            Hi guys,
            I've an application that writes a log file.
            I need to write an application with python that will read this file.
            You may begin from this:

            $ cat showlog.py
            import sys
            from ScrolledText import ScrolledText

            interval =50 # ms

            def displayfile(f, textwidget):
            textwidget.inse rt('end', f.readline())
            textwidget.afte r(interval, displayfile, f, textwidget)

            if __name__ == '__main__':
            st = ScrolledText()
            st.pack()
            displayfile(sys .stdin, st)
            st.mainloop()

            $ python showlog.py < mylogfile.log

            HTH,
            Michele Simionato

            Comment

            • Salvatore Di Fazio

              #7
              Re: Watch log

              Thinker ha scritto:
              What you want is something like 'tail -f' in linux.
              If you look into it's source code, you will find it check file periodically.
              When a read() reach the end of the file, read() will return a empty string.
              You can try to read data from the file periodically after a EOF.
              I think it is what you want.
              So every 2 sec:
              - open the file
              - read the file from the oldPosition to the EOF
              - save the last position in file
              - first point

              mmm ok

              Comment

              • Salvatore Di Fazio

                #8
                Re: Watch log

                Michele Simionato ha scritto:
                You may begin from this:
                Tnx Michele

                Comment

                Working...