Skip to content

Commit a7ad606

Browse files
committed
Add logger back
1 parent 6e808ee commit a7ad606

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/logger.jl

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
export log_header, log_row
2+
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")
15+
16+
for (typ, fmt) in formats
17+
hdr_fmt_foo = Symbol("header_formatter_$typ")
18+
len = match(r"\%([0-9]*)", fmt)[1]
19+
fmt2 = "%$(len)s"
20+
21+
@eval begin
22+
row_formatter(x :: $typ) = @sprintf($fmt, x)
23+
row_formatter(:: Type{<:$typ}) = @sprintf($fmt2, "-")
24+
25+
$hdr_fmt_foo(x) = @sprintf($fmt2, x)
26+
header_formatter(x :: Union{Symbol,String}, :: Type{<:$typ}) = $hdr_fmt_foo(x)
27+
end
28+
end
29+
30+
"""
31+
log_header(colnames, coltypes)
32+
33+
Creates a header using the names in `colnames` formatted according to the types in `coltypes`.
34+
Uses internal formatting specification given by `SolverCore.formats` and default header
35+
translation given by `SolverCore.default_headers`.
36+
37+
Input:
38+
- `colnames::Vector{Symbol}`: Column names.
39+
- `coltypes::Vector{DataType}`: Column types.
40+
41+
Keyword arguments:
42+
- `hdr_override::Dict{Symbol,String}`: Overrides the default headers.
43+
- `colsep::Int`: Number of spaces between columns (Default: 2)
44+
45+
See also [`log_row`](@ref).
46+
"""
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_vec = String[]
52+
for (name, typ) in zip(colnames, coltypes)
53+
x = if haskey(hdr_override, name)
54+
hdr_override[name]
55+
elseif haskey(default_headers, name)
56+
default_headers[name]
57+
else
58+
string(name)
59+
end
60+
push!(out_vec, header_formatter(x, typ))
61+
end
62+
return join(out_vec, " "^colsep)
63+
end
64+
65+
"""
66+
log_row(vals)
67+
68+
Creates a table row from the values on `vals` according to their types. Pass the names
69+
and types of `vals` to [`log_header`](@ref) for a logging table. Uses internal formatting
70+
specification given by `SolverCore.formats`.
71+
72+
To handle a missing value, add the type instead of the number:
73+
74+
@info log_row(Any[1.0, 1])
75+
@info log_row(Any[Float64, Int])
76+
77+
Prints
78+
79+
[ Info: 1.0e+00 1
80+
[ Info: - -
81+
82+
Keyword arguments:
83+
- `colsep::Int`: Number of spaces between columns (Default: 2)
84+
"""
85+
function log_row(vals; colsep :: Int = 2)
86+
string_cols = (row_formatter(val) for val in vals)
87+
return join(string_cols, " "^colsep)
88+
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)