Why is it hard to mimic `tail -f` behavior

xuanbao · · 203 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I needed to tail a log file to monitor it for certain keywords. </p> <p>after googling around I ended up with <code>https://github.com/hpcloud/tail</code> which gets the job done, but it&#39;s a big dependency.</p> <p>which made me wonder if it is actually a challenging thing to do even in system languages ?</p> <hr/>**评论:**<br/><br/>MohamedBassem: <pre><p>Here&#39;s a talk from FOSDEM 2017 about implementing <code>tail -f</code> in Go : <a href="https://www.youtube.com/watch?v=lLDWF59aZAo">https://www.youtube.com/watch?v=lLDWF59aZAo</a></p></pre>lobster_johnson: <pre><p>The challenge isn&#39;t really tailing a file as such, it&#39;s tailing it in a way that is cross-platform, detects file changes (e.g. deletes or renames, which requires watching the file descriptor through APIs like inotify), handles named pipes, etc. That <code>hpcloud/tail</code> library also does things like rate limiting.</p> <p>If you just want to tail a single file descriptor on a POSIX system, just keep reading, and sleep a little bit if you get <code>io.EOF</code>.</p> <p>Depending on how portable you want your program to be, you could also consider executing <code>tail</code> as a subprocess, since they&#39;ve already done all the work for you.</p></pre>lstokeworth: <pre><p>Read until EOF. Sleep. Repeat.</p></pre>Sphax: <pre><p>and then you need to handle rotation.</p></pre>eikenberry: <pre><p>tail -f doesn&#39;t do anything special for file rotation except maybe by not choking on file truncates.</p></pre>TheMue: <pre><p>I&#39;ve once had a similar problem and wrote a package to solve it for different sources and targets. See <a href="https://godoc.org/github.com/tideland/golib/scroller" rel="nofollow">https://godoc.org/github.com/tideland/golib/scroller</a></p></pre>disq: <pre><p>There&#39;s also this: <a href="https://github.com/gongo/9t" rel="nofollow">https://github.com/gongo/9t</a> (uh, looks like it&#39;s also using hpcloud/tail)</p></pre>sontags: <pre><p>Perhaps this helps, either to cheat or to use: <a href="https://github.com/google/mtail" rel="nofollow">https://github.com/google/mtail</a></p></pre>prohulaelk: <pre><p>It isn&#39;t, terribly.</p> <p><a href="https://gist.github.com/prohulaelk/204ebe69d65093064a7e2d13a70f7967" rel="nofollow">here</a> is a gist of a simple tail I wrote for use at my job a while ago. Not terribly fantastic code looking back on it (why did I roll my own linked list...?), but it&#39;s less than 200 LOC, only uses the standard library packages, and has worked flawlessly for what I needed it to do for the last couple years.</p></pre>Jarek_m: <pre><p>You can try implementing it using this <a href="https://github.com/fsnotify/fsnotify" rel="nofollow">https://github.com/fsnotify/fsnotify</a></p></pre>adgjzcb: <pre><p>tail&#39;s related repository is <strong><a href="http://github.oldjpg.com/repository/284367" rel="nofollow">file-tail</a></strong> </p> <blockquote> <p>File::Tail for Ruby</p> </blockquote></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

203 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传