Skip to content

Commit 57934d1

Browse files
committed
Update logger formatting and tests
1 parent 16fc349 commit 57934d1

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

src/logger.jl

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
export log_header, log_row
22

3-
const formats = Dict{DataType, String}(Signed => "%6d",
4-
AbstractFloat => "%8.1e",
5-
AbstractString => "%15s",
6-
Symbol => "%15s",
7-
Missing => "%15s"
8-
)
9-
10-
const default_headers = Dict{Symbol, String}(:name => "Name",
11-
:elapsed_time => "Time",
12-
:objective => "f(x)",
13-
:dual_feas => "Dual",
14-
:primal_feas => "Primal")
3+
const formats = Dict{DataType, String}(
4+
Signed => "%6d",
5+
AbstractFloat => "%8.1e",
6+
AbstractString => "%15s",
7+
Symbol => "%15s",
8+
Missing => "%15s",
9+
)
10+
11+
const default_headers = Dict{Symbol, String}(
12+
:name => "Name",
13+
:elapsed_time => "Time",
14+
:objective => "f(x)",
15+
:dual_feas => "Dual",
16+
:primal_feas => "Primal",
17+
)
1518

1619
for (typ, fmt) in formats
1720
hdr_fmt_foo = Symbol("header_formatter_$typ")
1821
len = match(r"\%([0-9]*)", fmt)[1]
1922
fmt2 = "%$(len)s"
2023

2124
@eval begin
22-
row_formatter(x :: $typ) = @sprintf($fmt, x)
23-
row_formatter(:: Type{<:$typ}) = @sprintf($fmt2, "-")
25+
row_formatter(x::$typ) = @sprintf($fmt, x)
26+
row_formatter(::Type{<:$typ}) = @sprintf($fmt2, "-")
2427

2528
$hdr_fmt_foo(x) = @sprintf($fmt2, x)
26-
header_formatter(x :: Union{Symbol,String}, :: Type{<:$typ}) = $hdr_fmt_foo(x)
29+
header_formatter(x::Union{Symbol, String}, ::Type{<:$typ}) = $hdr_fmt_foo(x)
2730
end
2831
end
2932

@@ -44,11 +47,13 @@ Keyword arguments:
4447
4548
See also [`log_row`](@ref).
4649
"""
47-
function log_header(colnames :: AbstractVector{Symbol}, coltypes :: AbstractVector{DataType};
48-
hdr_override :: Dict{Symbol,String} = Dict{Symbol,String}(),
49-
colsep :: Int = 2,
50-
)
51-
out = ""
50+
function log_header(
51+
colnames::AbstractVector{Symbol},
52+
coltypes::AbstractVector{DataType};
53+
hdr_override::Dict{Symbol, String} = Dict{Symbol, String}(),
54+
colsep::Int = 2,
55+
)
56+
out_vec = String[]
5257
for (name, typ) in zip(colnames, coltypes)
5358
x = if haskey(hdr_override, name)
5459
hdr_override[name]
@@ -57,9 +62,9 @@ function log_header(colnames :: AbstractVector{Symbol}, coltypes :: AbstractVect
5762
else
5863
string(name)
5964
end
60-
out *= header_formatter(x, typ) * " "^colsep
65+
push!(out_vec, header_formatter(x, typ))
6166
end
62-
return out
67+
return join(out_vec, " "^colsep)
6368
end
6469

6570
"""
@@ -82,7 +87,7 @@ Prints
8287
Keyword arguments:
8388
- `colsep::Int`: Number of spaces between columns (Default: 2)
8489
"""
85-
function log_row(vals; colsep :: Int = 2)
90+
function log_row(vals; colsep::Int = 2)
8691
string_cols = (row_formatter(val) for val in vals)
8792
return join(string_cols, " "^colsep)
8893
end

test/logger.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@testset "Logger" begin
2+
@info "Testing logger"
3+
s = log_header([:col_real, :col_int, :col_symbol, :col_string], [Float64, Int, Symbol, String])
4+
@test s == @sprintf("%8s %6s %15s %15s", "col_real", "col_int", "col_symbol", "col_string")
5+
s = log_row([1.0, 1, :one, "one"])
6+
@test s == @sprintf("%8.1e %6d %15s %15s", 1.0, 1, "one", "one")
7+
s = log_row([Float64, Int, Symbol, String])
8+
@test s == @sprintf("%8s %6s %15s %15s", "-", "-", "-", "-")
9+
end

0 commit comments

Comments
 (0)