Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
arcage committed Nov 29, 2016
0 parents commit 97b1deb
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
@@ -0,0 +1,10 @@
/doc/
/libs/
/lib/
/.crystal/
/.shards/
/test/

# Libraries don't need dependency lock
# Dependencies will be locked in application that uses them
/shard.lock
1 change: 1 addition & 0 deletions .travis.yml
@@ -0,0 +1 @@
language: crystal
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 ʕ·ᴥ·ʔAKJ

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
58 changes: 58 additions & 0 deletions README.md
@@ -0,0 +1,58 @@
# LogReader

Reading lines in the text file which is growing and may be rotated, such as unix system log file.

* When the current file reaches EOF, this will wait new line added to the file. (like `tail -f` command)
* Even when the current file is rotated, this will trace new file and continue reading lines.

## Installation

Add this to your application's `shard.yml`:

```yaml
dependencies:
logchaser:
github: arcage/crystal-logreader
```

## Usage

```crystal
require "logreader"
# create LogReader object
log = LogReader.new("/var/log/maillog")
# reads single line from the file
# waits new line, when the file reaches EOF
puts log.read_line
# iterates each line in the file
log.each do |line|
puts line
end
```

`LogReader` has only two public instance method `#read_line` and `#each`

`#each` will not finish in normal operation. if you want to stop it, you have to do it forcibly by using `Ctrl+C`, `kill` command etc.

### Internal behaviour

When reading no data for reasons such as EOF, `LogReader` try to read more data every second.

For every consecutive 5 results of reading are no data, `LogReader` checks whether or not the inode number associated with the file name was changed.

If it had been changed, `LogReader` close current file and re-open the file.

## Contributing

1. Fork it ( https://github.com/[your-github-name]/logchaser/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request

## Contributors

- [arcage](https://github.com/arcage) ʕ·ᴥ·ʔAKJ - creator, maintainer
7 changes: 7 additions & 0 deletions shard.yml
@@ -0,0 +1,7 @@
name: logchaser
version: 0.1.0

authors:
- ʕ·ᴥ·ʔAKJ <arcage@denchu.org>

license: MIT
9 changes: 9 additions & 0 deletions spec/logreader_spec.cr
@@ -0,0 +1,9 @@
require "./spec_helper"

describe LogReader do
# TODO: Write tests

it "works" do
false.should eq(true)
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
@@ -0,0 +1,2 @@
require "spec"
require "../src/logreader"
63 changes: 63 additions & 0 deletions src/logreader.cr
@@ -0,0 +1,63 @@
require "./logreader/*"

class LogReader
class Error < Exception; end

@path : String
@file : File

def initialize(path : String)
@path = File.expand_path(path)
@file = open_file
end

def each
loop do
yield read_line
end
end

def read_line : String
eof_count = 0
not_exist = false
String.build do |str|
loop do
if char = @file.read_char
str << char
break if char == '\n'
p eof_count = 0
p not_exist = false
elsif eof_count < 5
sleep(1)
p eof_count += 1
else
begin
@file = reopen_file if File.stat(@path).ino != ino
rescue err : Errno
if err.errno != Errno::ENOENT || not_exist
raise err
else
p not_exist = true
end
ensure
p eof_count = 0
end
end
end
end
end

private def ino
@file.stat.ino
end

private def reopen_file : File
@file.close unless @file.closed?
open_file
end

private def open_file : File
raise Error.new("#{@path} is not a readable file.") unless File.file?(@path) && File.readable?(@path)
File.open(@path)
end
end
3 changes: 3 additions & 0 deletions src/logreader/version.cr
@@ -0,0 +1,3 @@
class LogReader
VERSION = "0.1.0"
end

0 comments on commit 97b1deb

Please sign in to comment.