Skip to content

Commit d239f4d

Browse files
committed
Add Memento to handle logs
1 parent 5e8a35e commit d239f4d

File tree

9 files changed

+35
-19
lines changed

9 files changed

+35
-19
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.0.1"
44

55
[deps]
66
PowerModels = "c36e90e8-916a-50a6-bd94-075b64ef4655"
7+
Memento = "f28f55f0-a522-5efc-85c2-fe41dfb9b2d9"
78

89
[compat]
910
PowerModels = "0.18"

src/PWF.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@ module PWF
22

33
# using packages
44
using PowerModels
5+
using Memento
6+
7+
# setting up Memento
8+
const _LOGGER = Memento.getlogger(@__MODULE__)
9+
10+
__init__() = Memento.register(_LOGGER)
11+
12+
function silence()
13+
Memento.info(_LOGGER, "Suppressing information and warning messages for the rest of this session. Use the Memento package for more fine-grained control of logging.")
14+
Memento.setlevel!(Memento.getlogger(PWF), "error")
15+
end
16+
17+
function logger_config!(level)
18+
Memento.config!(Memento.getlogger("PWF"), level)
19+
end
520

621
# include PWF parser file
722
include("pwf2dict.jl")

src/pwf2dict.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ function _parse_line_element!(data::Dict{String, Any}, line::String, section::Ab
498498
end
499499
catch
500500
if !_needs_default(element)
501-
@warn "Could not parse $element to $dtype inside $section section, setting it as a String"
501+
Memento.warn(_LOGGER, "Could not parse $element to $dtype inside $section section, setting it as a String")
502502
end
503503
data[field] = element
504504
end
@@ -521,7 +521,7 @@ function _parse_line_element!(data::Dict{String, Any}, lines::Vector{String}, se
521521
data[line[k]] = parse(mn_type, line[v])
522522
catch
523523
if !_needs_default(line[v])
524-
@warn "Could not parse $(line[v]) to $mn_type, setting it as a String"
524+
Memento.warn(_LOGGER, "Could not parse $(line[v]) to $mn_type, setting it as a String")
525525
end
526526
!_needs_default(line[k]) ? data[line[k]] = line[v] : nothing
527527
end
@@ -606,7 +606,7 @@ function _parse_section!(data::Dict{String, Any}, section_lines::Vector{String})
606606
_parse_divided_section!(section_data, section_lines, section)
607607

608608
else
609-
@warn "Currently there is no support for $section parsing"
609+
Memento.warn(_LOGGER, "Currently there is no support for $section parsing")
610610
section_data = nothing
611611
end
612612
data[section] = section_data
@@ -617,11 +617,11 @@ _needs_default(ch::Char) = ch == ' '
617617

618618
function _populate_defaults!(pwf_data::Dict{String, Any})
619619

620-
@warn "Populating defaults"
620+
Memento.info(_LOGGER, "Populating defaults")
621621

622622
for (section, section_data) in pwf_data
623623
if !haskey(_pwf_defaults, section)
624-
@warn "Parser doesn't have default values for section $(section)."
624+
Memento.warn(_LOGGER, "Parser doesn't have default values for section $(section)")
625625
else
626626
if section in keys(_pwf_dtypes)
627627
_populate_section_defaults!(pwf_data, section, section_data)

src/pwf2pm/branch.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ function _pwf2pm_DCSC_branch!(pm_data::Dict, pwf_data::Dict, branch::Dict; add_c
171171

172172
rep = findall(x -> x["f_bus"] == sub_data["f_bus"] && x["t_bus"] == sub_data["t_bus"] && x["circuit"] == sub_data["circuit"], pm_data["branch"])
173173
if length(rep) > 0
174-
@warn "Branch from $(sub_data["f_bus"]) to $(sub_data["t_bus"]) in circuit $(sub_data["circuit"]) is duplicated"
174+
Memento.warn(_LOGGER, "Branch from $(sub_data["f_bus"]) to $(sub_data["t_bus"]) in circuit $(sub_data["circuit"]) is duplicated")
175175
end
176176
idx = string(sub_data["index"])
177177
pm_data["branch"][idx] = sub_data

src/pwf2pm/bus.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ end
108108
function _pwf2pm_bus!(pm_data::Dict, pwf_data::Dict; add_control_data::Bool=false)
109109

110110
dict_dglt = haskey(pwf_data, "DGLT") ? _create_dict_dglt(pwf_data["DGLT"]) : nothing
111-
isa(dict_dglt, Dict) && length(dict_dglt) == 1 ? @warn("Only one limit voltage group definded, each bus will be considered as part of the group $(pwf_data["DGLT"]["1"]["GROUP"]), regardless of its defined group") : nothing
111+
isa(dict_dglt, Dict) && length(dict_dglt) == 1 ? Memento.warn(_LOGGER, "Only one limit voltage group definded, each bus will be considered as part of the group $(pwf_data["DGLT"]["1"]["GROUP"]), regardless of its defined group") : nothing
112112
dict_dgbt = haskey(pwf_data, "DGBT") ? _create_dict_dgbt(pwf_data["DGBT"]) : nothing
113-
isa(dict_dgbt, Dict) && length(dict_dgbt) == 1 ? @warn("Only one base voltage group definded, each bus will be considered as part of the group $(pwf_data["DGBT"]["1"]["GROUP"]), regardless of its defined group") : nothing
113+
isa(dict_dgbt, Dict) && length(dict_dgbt) == 1 ? Memento.warn(_LOGGER, "Only one base voltage group definded, each bus will be considered as part of the group $(pwf_data["DGBT"]["1"]["GROUP"]), regardless of its defined group") : nothing
114114

115115
pm_data["bus"] = Dict{String, Any}()
116116
if haskey(pwf_data, "DBAR")

src/pwf2pm/correct/anarede.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ function _pwf2pm_corrections_PQ!(pm_data::Dict, software::ANAREDE)
8282
# sum load power with the negative of generator power
8383
pm_data["load"][load_key[1]]["pd"] += - Pg
8484
pm_data["load"][load_key[1]]["qd"] += - Qg
85-
@warn "Active generator with QMIN = QMAX = 0 found in PQ bus $i. Adding generator power " *
86-
"to load power and changing generator status to off."
85+
Memento.warn(_LOGGER, "Active generator with QMIN = QMAX = 0 found in PQ bus $i. Adding generator power " *
86+
"to load power and changing generator status to off")
8787
end
8888
end
8989
end

src/pwf2pm/correct/organon.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function _pwf2pm_corrections_PV!(pm_data::Dict, pwf_data::Dict, software::Organo
1212
if isempty(load_from_bus(pm_data, parse(Int, i)))
1313
_pwf2pm_load!(pm_data, pwf_data, parse(Int,i))
1414
end
15-
@warn "Active generator with QMIN = QMAX found in a PV bus number $i. Changing bus type from PV to PQ."
15+
Memento.warn(_LOGGER, "Active generator with QMIN = QMAX found in a PV bus number $i. Changing bus type from PV to PQ")
1616
end
1717
end
1818
end
@@ -32,7 +32,7 @@ function _pwf2pm_corrections_PQ!(pm_data::Dict, software::Organon)
3232
if !isempty(gen_keys_case1)
3333
# change bus type to PV
3434
bus["bus_type"] = bus_type_str_to_num["PV"]
35-
@warn "Active generator with QMIN < QMAX found in a PQ bus. Changing bus $i type to PV."
35+
Memento.warn(_LOGGER, "Active generator with QMIN < QMAX found in a PQ bus. Changing bus $i type to PV")
3636
elseif !isempty(gen_keys_case2)
3737
# change generator status to off and sum load power with gen power
3838
Pg, Qg = sum_generators_power_and_turn_off(pm_data, gen_keys_case2)
@@ -41,8 +41,8 @@ function _pwf2pm_corrections_PQ!(pm_data::Dict, software::Organon)
4141
# sum load power with the negative of generator power
4242
pm_data["load"][load_key[1]]["pd"] += - Pg
4343
pm_data["load"][load_key[1]]["qd"] += - Qg
44-
@warn "Active generator with QMIN = QMAX found in PQ bus $i. Adding generator power " *
45-
"to load power and changing generator status to off."
44+
Memento.warn(_LOGGER, "Active generator with QMIN = QMAX found in PQ bus $i. Adding generator power " *
45+
"to load power and changing generator status to off")
4646
end
4747
end
4848
end

src/pwf2pm/dcline.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ function _pwf2pm_dcline!(pm_data::Dict, pwf_data::Dict, link::Dict)
5252
# pf = mdc == 'P' ? abs(setvl[1]) : mdc == 'C' ? - abs(setvl[1] / vschd[1] / 1000) : 0
5353
# pt = mdc == 'P' ? - abs(setvl[2]) : mdc == 'C' ? abs(setvl[2] / vschd[2] / 1000) : 0
5454

55-
pf = mdc == 'P' ? abs(setvl) : @error("The formulation is prepared only for power control")
56-
pt = mdc == 'P' ? - abs(setvl) + loss : @error("The formulation is prepared only for power control")
55+
pf = mdc == 'P' ? abs(setvl) : Memento.error(_LOGGER, "The formulation is prepared only for power control")
56+
pt = mdc == 'P' ? - abs(setvl) + loss : Memento.error(_LOGGER, "The formulation is prepared only for power control")
5757

5858
sub_data["f_bus"] = dict_dcnv[rect]["AC BUS"]
5959
sub_data["t_bus"] = dict_dcnv[inv]["AC BUS"]
@@ -83,7 +83,7 @@ function _pwf2pm_dcline!(pm_data::Dict, pwf_data::Dict, link::Dict)
8383
push!(anmn, angle)
8484
else
8585
push!(anmn, 0)
86-
@warn("$key outside reasonable limits, setting to 0 degress")
86+
Memento.warn(_LOGGER, "$key outside reasonable limits, setting to 0 degress")
8787
end
8888
end
8989

@@ -114,7 +114,7 @@ function _pwf2pm_dcline!(pm_data::Dict, pwf_data::Dict)
114114
pm_data["dcline"] = Dict{String, Any}()
115115

116116
if !(haskey(pwf_data, "DCBA") && haskey(pwf_data, "DCLI") && haskey(pwf_data, "DCNV") && haskey(pwf_data, "DCCV") && haskey(pwf_data, "DELO"))
117-
@warn("DC line will not be parsed due to the absence of at least one those sections: DCBA, DCLI, DCNV, DCCV, DELO")
117+
Memento.warn(_LOGGER, "DC line will not be parsed due to the absence of at least one those sections: DCBA, DCLI, DCNV, DCCV, DELO")
118118
return
119119
end
120120
@assert length(pwf_data["DCBA"]) == 4*length(pwf_data["DCLI"]) == 2*length(pwf_data["DCNV"]) == 2*length(pwf_data["DCCV"]) == 4*length(pwf_data["DELO"])

src/pwf2pm/gen.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ end
6060
function _pwf2pm_generator!(pm_data::Dict, pwf_data::Dict)
6161

6262
if !haskey(pwf_data, "DGER")
63-
@warn("DGER not found, setting pmin as 0.0 MW and pmax as 99999.0 MW")
63+
Memento.warn(_LOGGER, "DGER not found, setting pmin as 0.0 MW and pmax as 99999.0 MW")
6464
end
6565

6666
pm_data["gen"] = Dict{String, Any}()

0 commit comments

Comments
 (0)