From 2593c9b48c8d6063039dd72b776397c1b644ba4a Mon Sep 17 00:00:00 2001 From: Ivor Paul Date: Thu, 24 Oct 2024 21:17:58 +0200 Subject: [PATCH] Rearrange slicer behaviour and slicers. --- lib/meta_logger/slicer.ex | 45 ------------------- lib/meta_logger/slicer/default_impl.ex | 44 ++++++++++++++++++ .../{utf8_slicer.ex => slicer/utf8_impl.ex} | 0 3 files changed, 44 insertions(+), 45 deletions(-) create mode 100644 lib/meta_logger/slicer/default_impl.ex rename lib/meta_logger/{utf8_slicer.ex => slicer/utf8_impl.ex} (100%) diff --git a/lib/meta_logger/slicer.ex b/lib/meta_logger/slicer.ex index ba08223..ccd8181 100644 --- a/lib/meta_logger/slicer.ex +++ b/lib/meta_logger/slicer.ex @@ -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 <>, 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 diff --git a/lib/meta_logger/slicer/default_impl.ex b/lib/meta_logger/slicer/default_impl.ex new file mode 100644 index 0000000..eed11fe --- /dev/null +++ b/lib/meta_logger/slicer/default_impl.ex @@ -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 <>, 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 diff --git a/lib/meta_logger/utf8_slicer.ex b/lib/meta_logger/slicer/utf8_impl.ex similarity index 100% rename from lib/meta_logger/utf8_slicer.ex rename to lib/meta_logger/slicer/utf8_impl.ex