Skip to content

Commit

Permalink
Use raw mode and avoid record -> struct conversion (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim authored and onkel-dirtus committed Aug 7, 2019
1 parent 312867b commit c9d9d59
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/logger_file_backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ defmodule LoggerFileBackend do

@type path :: String.t
@type file :: :file.io_device
@type inode :: File.Stat.t
@type inode :: integer
@type format :: String.t
@type level :: Logger.level
@type metadata :: [atom]

require Record
Record.defrecordp :file_info, Record.extract(:file_info, from_lib: "kernel/include/file.hrl")

@default_format "$time $metadata[$level] $message\n"

Expand Down Expand Up @@ -99,9 +101,11 @@ defmodule LoggerFileBackend do

defp rotate(path, %{max_bytes: max_bytes, keep: keep }) when is_integer(max_bytes) and is_integer(keep) and keep > 0 do

case File.stat(path) do
{:ok, %{size: size}} -> if size >= max_bytes, do: rename_file(path, keep) , else: true
_ -> true
case :file.read_file_info(path, [:raw]) do
{:ok, file_info(size: size)} ->
if size >= max_bytes, do: rename_file(path, keep) , else: true
_ ->
true
end

end
Expand Down Expand Up @@ -155,8 +159,8 @@ defmodule LoggerFileBackend do


defp get_inode(path) do
case File.stat(path) do
{:ok, %File.Stat{inode: inode}} -> inode
case :file.read_file_info(path, [:raw]) do
{:ok, file_info(inode: inode)} -> inode
{:error, _} -> nil
end
end
Expand Down

0 comments on commit c9d9d59

Please sign in to comment.