0

So this is my issue. A programs writes log files to a specific folder. Once a log file is created it is written to about every 2 seconds until it reaches approx 5MB. A new file is created then. I am Parsi g the log files to gather data real time.

I am using directory monitor to listen for changes to a directory. When a change is detected I am able to compare the NSfilemodocation date to get the newest file name name to monitor.

I then use that file name to create an NSinput stream. I create my buffer and I then open the stream and check if it "hasBytesAvail" and begin Parsing.

My issue is this: I am reaching the end of the file and EventEndOfStream is being called which destroys my stream.

I have two options:

  1. keep a bytes read variable current until a new file is created. Which means every 2 seconds I need to be recreating the stream over and over, which seems like a waste.

  2. Prevent the stream from being closed as long as the current file is the newest file in the directory. Which sounded simple, but in order to do that I have to read to the end of the file which triggers the stream to be closed and deallocated as well.

In its simplest terms I want to monitor a folder to get the name of a file to monitor. Then monitor the file and only read the new bytes since the last time it was read. Without needless instances of NSInputstream.

Does anyone have any suggestions?

Jeff Brown
  • 86
  • 8
  • Here is crazy idea for your Jeff. Use the unix command "tail -f" within your app, or indeed find the source and you'll find your solution. – user3069232 Mar 17 '16 at 05:04
  • OMG , I never thought of that. I literally have been racking my brain for hours. I will give that a stab and respond afterwards – Jeff Brown Mar 17 '16 at 05:15
  • Any thoughts on the overhead to update an Nsdata object using get bytes range every second using something like an nstimer and a read bytes count? Nsdata primitive method here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/BinaryData/Tasks/WorkingBinaryData.html#//apple_ref/doc/uid/20000717-CJBDBHCB. I just don't want to have a memory hog program. – Jeff Brown Mar 17 '16 at 05:35
  • Every second in computing terms isn't that frequent :) but you need to see. I think it is important to make sure the quality of the service you set on the process [I talking swift operation blocks here] is background, and well let the OS decide ultimately. Still in the world NIX if your worried than look at something like uptime, which you could use to mediate how frequently you run your check assuming its not mission critical. – user3069232 Mar 17 '16 at 08:50

0 Answers0