Skip to content

Commit

Permalink
Rearrange slicer behaviour and slicers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivor committed Oct 24, 2024
1 parent 381614a commit 2593c9b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 45 deletions.
45 changes: 0 additions & 45 deletions lib/meta_logger/slicer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,4 @@ defmodule MetaLogger.Slicer do
@typedoc "Max length in bytes or `:infinity` if the entry should not be sliced."
@type max_entry_length :: non_neg_integer() | :infinity
@callback slice(String.t(), max_entry_length()) :: [String.t()]

defmodule DefaultImpl do
@moduledoc """
Responsible for slicing log entries according to the given max length option.
"""

@behaviour MetaLogger.Slicer
@doc """
Returns sliced log entries according to the given max entry length.
If the entry is smaller than given max length, or if `:infinity ` is given
as option, a list with one entry is returned. Otherwise a list with multiple
entries is returned.
## Examples
iex> #{inspect(__MODULE__)}.slice("1234567890", 10)
["1234567890"]
iex> #{inspect(__MODULE__)}.slice("1234567890", :infinity)
["1234567890"]
iex> #{inspect(__MODULE__)}.slice("1234567890", 5)
["12345", "67890"]
"""
@impl MetaLogger.Slicer
def slice(entry, max_entry_length)
when max_entry_length == :infinity
when byte_size(entry) <= max_entry_length,
do: [entry]

def slice(entry, max_entry_length) do
entry_length = byte_size(entry)
rem = rem(entry_length, max_entry_length)
sliced_entries = for <<slice::binary-size(max_entry_length) <- entry>>, do: slice

if rem > 0 do
remainder_entry = binary_part(entry, entry_length, rem * -1)
sliced_entries ++ [remainder_entry]
else
sliced_entries
end
end
end
end
44 changes: 44 additions & 0 deletions lib/meta_logger/slicer/default_impl.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule MetaLogger.Slicer.DefaultImpl do
@moduledoc """
Responsible for slicing log entries according to the given max length option.
"""

@behaviour MetaLogger.Slicer
@doc """
Returns sliced log entries according to the given max entry length.
If the entry is smaller than given max length, or if `:infinity ` is given
as option, a list with one entry is returned. Otherwise a list with multiple
entries is returned.
## Examples
iex> #{inspect(__MODULE__)}.slice("1234567890", 10)
["1234567890"]
iex> #{inspect(__MODULE__)}.slice("1234567890", :infinity)
["1234567890"]
iex> #{inspect(__MODULE__)}.slice("1234567890", 5)
["12345", "67890"]
"""
@impl MetaLogger.Slicer
def slice(entry, max_entry_length)
when max_entry_length == :infinity
when byte_size(entry) <= max_entry_length,
do: [entry]

def slice(entry, max_entry_length) do
entry_length = byte_size(entry)
rem = rem(entry_length, max_entry_length)
sliced_entries = for <<slice::binary-size(max_entry_length) <- entry>>, do: slice

if rem > 0 do
remainder_entry = binary_part(entry, entry_length, rem * -1)
sliced_entries ++ [remainder_entry]
else
sliced_entries
end
end
end
File renamed without changes.

0 comments on commit 2593c9b

Please sign in to comment.