Created
February 22, 2017 00:02
-
-
Save vladkosarev/5d4597ea9d16b4133d7ee27935df2eb0 to your computer and use it in GitHub Desktop.
File tail script in F#. Run with - fsi fs-tail.fsx <folder> <file>, i.e fsi fs-tail.fsx . test.log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| open System.IO | |
| open System | |
| let args = fsi.CommandLineArgs | |
| let watcher = new FileSystemWatcher(Path = args.[1], Filter = args.[2], EnableRaisingEvents = true) | |
| let fullPath = Path.Combine(args.[1], args.[2]) | |
| let stream = File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) | |
| stream.Seek(0L, SeekOrigin.End) | |
| printfn "Watching %s" fullPath | |
| let sr = new StreamReader(stream) | |
| let rec loop() = | |
| async { | |
| let! e = Async.AwaitEvent watcher.Changed | |
| match e.ChangeType with | |
| | WatcherChangeTypes.Changed -> | |
| let! newText = sr.ReadToEndAsync() |> Async.AwaitTask | |
| printf "%s" newText | |
| | _ -> () | |
| return! loop () | |
| } | |
| Async.Start (loop()) | |
| Console.ReadLine() |> ignore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment