Skip to content

Commit

Permalink
preserve newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
tycooon committed Dec 12, 2024
1 parent 4b6172b commit 0bcfded
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/lamian/log_device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def string # :nodoc:
def truncate(msg)
return msg unless msg.size > max_log_length

suffix = "..."
suffix = +"..."
suffix << "\n" if msg.end_with?("\n")
msg = msg[0, max_log_length - suffix.size]

"#{msg}#{suffix}"
Expand Down
21 changes: 15 additions & 6 deletions spec/lamian/log_device_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@
subject(:logdev) { described_class.new(max_log_lines: 5, max_log_length: 20) }

it "saves log" do
logdev.write("Hello ")
logdev.write("world!")
expect(logdev.string).to eq("Hello world!")
logdev.write("Hello\n")
logdev.write("world!\n")
expect(logdev.string).to eq("Hello\nworld!\n")
end

it "only stores 5 latest log lines" do
8.times { |x| logdev.write(x + 1) }
expect(logdev.string).to eq("45678")
end

it "truncates long lines" do
logdev.write("Hello world, this is me!")
expect(logdev.string).to eq("Hello world, this...")
context "long log lines" do
it "truncates log" do
logdev.write("Hello world, this is me!")
expect(logdev.string).to eq("Hello world, this...")
end

context "line with newline in the end" do
it "truncates log and adds newline" do
logdev.write("Hello world, this is me!\n")
expect(logdev.string).to eq("Hello world, thi...\n")
end
end
end
end

0 comments on commit 0bcfded

Please sign in to comment.