Browse files

Properly adhere to bytes and offset parameters for /_log

Closes COUCHDB-887
  • Loading branch information...
1 parent 6dcd3a4 commit a0284a483f83d2fb98ff2755743053f3a3898454 @janl janl committed Oct 31, 2011
Showing with 19 additions and 2 deletions.
  1. +19 −2 src/couchdb/couch_log.erl
View
21 src/couchdb/couch_log.erl
@@ -172,6 +172,23 @@ get_log_messages(Pid, Level, Format, Args) ->
FileMsg = ["[", httpd_util:rfc1123_date(), "] ", ConsoleMsg],
{ConsoleMsg, iolist_to_binary(FileMsg)}.
+
+% Read Bytes bytes from the end of log file, jumping Offset bytes towards
+% the beginning of the file first.
+%
+% Log File FilePos
+% ----------
+% | | 10
+% | | 20
+% | | 30
+% | | 40
+% | | 50
+% | | 60
+% | | 70 -- Bytes = 20 --
+% | | 80 | Chunk
+% | | 90 -- Offset = 10 --
+% |__________| 100
+
read(Bytes, Offset) ->
LogFileName = couch_config:get("log", "file"),
LogFileSize = filelib:file_size(LogFileName),
@@ -186,11 +203,11 @@ read(Bytes, Offset) ->
end,
{ok, Fd} = file:open(LogFileName, [read]),
- Start = lists:max([LogFileSize - Bytes, 0]) + Offset,
+ Start = lists:max([LogFileSize - Bytes - Offset, 0]),
% TODO: truncate chopped first line
% TODO: make streaming
- {ok, Chunk} = file:pread(Fd, Start, LogFileSize),
+ {ok, Chunk} = file:pread(Fd, Start, Bytes),
ok = file:close(Fd),
Chunk.

0 comments on commit a0284a4

Please sign in to comment.