Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved the problem for never return the last line if it's not followed by a newline #126

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Code-Hex
Copy link

@Code-Hex Code-Hex commented Jul 24, 2017

What was the problem?

Even though enable the Follow mode, Could not get the last line if it is not newline on end of the line.

For example, I had read the file that is not followed by a newline on the last line using github.com/hpcloud/tail. file is like this.

host:192.168.12.34	user:-	time:2015-09-06T05:58:05+09:00	method:POST	uri:/foo/bar?token=xxx&uuid=1234	protocol:HTTP/1.1	status:200	size:12	api_status:-	request_time:0.247	apptime:0.057	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T05:58:41+09:00	method:POST	uri:/foo/bar?token=yyy	protocol:HTTP/1.1	status:200	size:34	api_status:-	request_time:0.012	apptime:0.100	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T06:00:42+09:00	method:GET	uri:/foo/bar?token=zzz	protocol:HTTP/1.1	status:200	size:56	api_status:-	request_time:0.034	apptime:0.123	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T06:00:43+09:00	method:GET	uri:/foo/bar	protocol:HTTP/1.1	status:400	size:15	api_status:-	request_time:-	apptime:-	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T05:58:44+09:00	method:POST	uri:/foo/bar?token=yyy	protocol:HTTP/1.1	status:200	size:34	api_status:-	request_time:0.123	apptime:0.234	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T05:58:44+09:00	method:POST	uri:/hoge/piyo?id=yyy	protocol:HTTP/1.1	status:200	size:34	api_status:-	request_time:0.123	apptime:0.234	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T05:58:05+09:00	method:POST	uri:/foo/bar?token=xxx&uuid=1234	protocol:HTTP/1.1	status:200	size:12	api_status:-	request_time:0.247	apptime:0.057	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T05:58:41+09:00	method:POST	uri:/foo/bar?token=yyy	protocol:HTTP/1.1	status:200	size:34	api_status:-	request_time:0.012	apptime:0.100	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T06:00:42+09:00	method:GET	uri:/foo/bar?token=zzz	protocol:HTTP/1.1	status:200	size:56	api_status:-	request_time:0.034	apptime:0.123	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T06:00:43+09:00	method:GET	uri:/foo/bar	protocol:HTTP/1.1	status:400	size:15	api_status:-	request_time:-	apptime:-	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T06:00:43+09:00	method:GET	uri:/diary/entry/1234	protocol:HTTP/1.1	status:200	size:15	api_status:-	request_time:-	apptime:0.135	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T06:00:43+09:00	method:GET	uri:/diary/entry/5678	protocol:HTTP/1.1	status:200	size:30	api_status:-	request_time:-	apptime:0.432	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T06:00:42+09:00	method:GET	uri:/foo/bar?token=zzz	protocol:HTTP/1.1	status:200	size:56	api_status:-	request_time:0.034	apptime:0.123	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T06:00:43+09:00	method:GET	uri:/foo/bar	protocol:HTTP/1.1	status:400	size:15	api_status:-	request_time:-	apptime:-	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T06:00:43+09:00	method:GET	uri:/diary/entry/1234	protocol:HTTP/1.1	status:200	size:15	api_status:-	request_time:-	apptime:0.135	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T06:00:43+09:00	method:GET	uri:/diary/entry/5678	protocol:HTTP/1.1	status:200	size:30	api_status:-	request_time:-	apptime:0.432	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T05:58:05+09:00	method:POST	uri:/foo/bar?token=xxx&uuid=1234	protocol:HTTP/1.1	status:200	size:12	api_status:-	request_time:0.247	apptime:0.057	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-  user_id:-
host:192.168.12.34	user:-	time:2015-09-06T05:58:41+09:00	method:POST	uri:/foo/bar?token=yyy	protocol:HTTP/1.1	status:200	size:34	api_status:-	request_time:0.012	apptime:0.100	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T06:00:42+09:00	method:GET	uri:/foo/bar?token=zzz	protocol:HTTP/1.1	status:200	size:56	api_status:-	request_time:0.034	apptime:0.123	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T06:00:43+09:00	method:GET	uri:/foo/bar	protocol:HTTP/1.1	status:400	size:15	api_status:-	request_time:-	apptime:-	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T06:00:43+09:00	method:GET	uri:/foo/bar	protocol:HTTP/1.1	status:400	size:15	api_status:-	request_time:-	apptime:-	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2	uuid:-	user_id:-
host:192.168.12.34	user:-	time:2015-09-06T06:00:43+09:00	method:GET	uri:/foo/bar	protocol:HTTP/1.1	status:400	size:15	api_status:-	request_time:-	apptime:-	upstream_addr:127.0.0.1:12345	referer:-	user_agent:curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.4 libidn/1.18 libssh2/1.4.3	uuid:-	user_id:-

But, when I use tail -f, I could get the the last line.
We must need to fix this problem.

Thank you.

@Code-Hex
Copy link
Author

I think you should not trim newline from the line when write to file.
https://github.com/Code-Hex/tail/blob/5ad6447941c464f021ac1c2475ddb04a6e2caf47/tail.go#L221

Because, We can think about these cases.
This code compare with tail -f.

for line := range t.Lines {
    fmt.Println(line.Text)
}

I am going to write a some messages.

% printf "hello\n" >> a.txt
% printf wor >> a.txt
% printf "ld\n" >> a.txt

Result,

# tail -f
hello
world(Displayed `ld` after once time displayed `wor`)

# hpcloud/tail
hello
world(It is displayed `world` at once)

# Code-Hex/tail
hello
wor(newlined)
ld

So, If you deleted code to trim the newline, We can get the correct result like follow this.

for line := range t.Lines {
    fmt.Print(line.Text)
}

Then,

# tail -f
hello
world(Displayed `ld` after once time displayed `wor`)

# After remove code to trim newline from the line.
hello
world(Displayed `ld` after once time displayed `wor`)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant