Skip to content

Commit d113b9a

Browse files
authored
Merge pull request #29 from Arkoniak/squash_delimiter
fullsquash mode
2 parents eb001a3 + 80e29c5 commit d113b9a

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MiniLoggers"
22
uuid = "93f3dd0f-005d-4452-894a-a31841fa4078"
33
authors = ["Andrey Oskin"]
4-
version = "0.4.0"
4+
version = "0.4.1"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/minilogger.jl

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
abstract type AbstractMode end
22
struct NoTransformations <: AbstractMode end
33
struct Squash <: AbstractMode end
4+
struct FullSquash <: AbstractMode end
45
struct MDown <: AbstractMode end
56

67
struct MiniLogger{AM <: AbstractMode, IOT1 <: IO, IOT2 <: IO, DFT <: DateFormat} <: AbstractLogger
@@ -31,6 +32,8 @@ function getmode(mode::Symbol)
3132
return NoTransformations()
3233
elseif mode == :squash
3334
return Squash()
35+
elseif mode == :fullsquash
36+
return FullSquash()
3437
elseif mode == :markdown
3538
return MDown()
3639
end
@@ -61,6 +64,7 @@ Supported keyword arguments include:
6164
* `message_mode` (default: `:squash`): choose how message is transformed before being printed out. Following modes are supported:
6265
* `:notransformations`: message printed out as is, without any extra transformations
6366
* `:squash`: message is squashed to a single line, i.e. all `\\n` are changed to `squash_delimiter` and `\\r` are removed.
67+
* `:fullsquash`: all messages including error stacktraces are squashed to a single line, i.e. all `\\n` are changed to `squash_delimiter` and `\\r` are removed
6468
* `:markdown`: message is treated as if it is written in markdown
6569
* `squash_delimiter`: (default: "\\t"): defines which delimiter to use in squash mode.
6670
* `flush` (default: `true`): whether to `flush` IO stream for each log message. Flush behaviour also affected by `flush_threshold` argument.
@@ -138,6 +142,44 @@ showmessage(io, e::Tuple{Exception,Any}, logger, mode) = showvalue(io, e, logger
138142
showmessage(io, ex::Exception, logger, mode) = showvalue(io, ex, logger, mode)
139143
showmessage(io, ex::AbstractVector{Union{Ptr{Nothing}, Base.InterpreterIP}}, logger, mode) = Base.show_backtrace(io, ex)
140144

145+
function postprocess(mode, delimiter, iobuf)
146+
print(iobuf, "\n")
147+
take!(iobuf)
148+
end
149+
150+
function postprocess(mode::FullSquash, delimiter, iobuf)
151+
buf = take!(iobuf)
152+
delm = Vector{UInt8}(delimiter)
153+
res = similar(buf)
154+
L = length(res)
155+
j = 1
156+
@inbounds for (i, c) in pairs(buf)
157+
c == UInt8('\r') && continue
158+
if c == UInt8('\n')
159+
for c2 in delm
160+
if j > L
161+
resize!(res, 2*L)
162+
L *= 2
163+
end
164+
res[j] = c2
165+
j += 1
166+
end
167+
continue
168+
end
169+
res[j] = c
170+
j += 1
171+
end
172+
if j > L
173+
resize!(res, 2*L)
174+
L *= 2
175+
end
176+
res[j] = UInt8('\n')
177+
178+
resize!(res, j)
179+
180+
return res
181+
end
182+
141183
tsnow(dtf) = Dates.format(Dates.now(), dtf)
142184

143185
function colorfunc(level::LogLevel, _module, group, id, filepath, line, element)
@@ -224,8 +266,7 @@ function handle_message(logger::MiniLogger, level, message, _module, group, id,
224266
printwcolor(iob, val, c)
225267
end
226268
end
227-
print(iob, "\n")
228-
write(io, take!(buf))
269+
write(io, postprocess(logger.mode, logger.squash_delimiter, buf))
229270
if logger.flush
230271
if logger.flush_threshold <= 0
231272
flush(io)

0 commit comments

Comments
 (0)