Skip to content

Commit f8d6dff

Browse files
authored
Logging (#478)
Logging
2 parents 0d0e50a + e31988c commit f8d6dff

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

Manifest.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ git-tree-sha1 = "e2fc7a55bb2224e203bbd8b59f72b91323233458"
186186
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
187187
version = "0.5.3"
188188

189+
189190
[[Markdown]]
190191
deps = ["Base64"]
191192
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ CUDAdrv = "c5f51814-7f29-56b8-a69c-e4d8f6be1fde"
99
CUDAnative = "be33ccc6-a3ff-5ff2-a52e-74243cff1e17"
1010
Cassette = "7057c7e9-c182-5462-911a-8362d720325c"
1111
CuArrays = "3a865a2d-5b23-5a0f-bc46-62713ec82fae"
12+
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
1213
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
1314
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
1415
GPUifyLoops = "ba82f77b-6841-5d2e-bd9f-4daf811aec27"
1516
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
1617
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
18+
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
1719
NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
1820
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
1921
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"

src/Oceananigans.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export
1111
# Architectures
1212
CPU, GPU,
1313

14+
# Logging
15+
ModelLogger, Diagnostic, Setup, Simulation,
16+
1417
# Constants
1518
second, minute, hour, day,
1619

@@ -238,6 +241,8 @@ function TimeStepper end
238241
function run_diagnostic end
239242
function write_output end
240243

244+
using Logging
245+
241246
include("utils.jl")
242247

243248
include("clock.jl")
@@ -263,6 +268,7 @@ include("Solvers/Solvers.jl")
263268
using .Solvers
264269

265270
include("forcing.jl")
271+
include("logger.jl")
266272
include("models.jl")
267273

268274
include("Diagnostics/Diagnostics.jl")

src/logger.jl

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Logging, Dates
2+
3+
4+
####
5+
#### Custom LogLevels
6+
####
7+
_custom_log_level_docs = """
8+
Severity Order:
9+
Debug < Diagnostic < Simulation < Setup < Info
10+
11+
Usage:
12+
@logmsg Logging.LogLevel "Log Message"
13+
14+
@logmsg comes from Base/Logging
15+
LogLevel can be any Base/Logging.LogLevel
16+
Log Message can be any expression that evaluates to a string (preferably human readable!)
17+
"""
18+
19+
const Diagnostic = Logging.LogLevel(-500) # Sits between Debug and Info
20+
const Simulation = Logging.LogLevel(-250)
21+
const Setup = Logging.LogLevel(-125)
22+
23+
24+
####
25+
#### ModelLogger
26+
####
27+
_model_logger_docs = """
28+
29+
ModelLogger(stream::IO, level::LogLevel)
30+
31+
Based on Logging.SimpleLogger it tries to log all messages in the following format
32+
33+
message --- [dd/mm/yyyy HH:MM:SS] log_level source_file:line_number
34+
35+
The logger will handle any message from Diagnostic up by default.
36+
"""
37+
struct ModelLogger <: Logging.AbstractLogger
38+
stream::IO
39+
min_level::Logging.LogLevel
40+
message_limits::Dict{Any,Int}
41+
end
42+
ModelLogger(stream::IO=stderr, level=Diagnostic) = ModelLogger(stream, level, Dict{Any,Int}())
43+
44+
Logging.shouldlog(logger::ModelLogger, level, _module, group, id) = get(logger.message_limits, id, 1) > 0
45+
46+
Logging.min_enabled_level(logger::ModelLogger) = logger.min_level
47+
48+
Logging.catch_exceptions(logger::ModelLogger) = false
49+
50+
function level_to_string(level::Logging.LogLevel)
51+
if level == Diagnostic
52+
"Diagnostic"
53+
elseif level == Setup
54+
"Setup"
55+
elseif level == Logging.Warn
56+
"Warning"
57+
else
58+
string(level)
59+
end
60+
end
61+
62+
function Logging.handle_message(logger::ModelLogger, level, message, _module, group, id, filepath, line; maxlog = nothing, kwargs...)
63+
if maxlog !== nothing && maxlog isa Integer
64+
remaining = get!(logger.message_limits, id, maxlog)
65+
logger.message_limits[id] = remaining - 1
66+
remaining > 0 || return
67+
end
68+
buf = IOBuffer()
69+
iob = IOContext(buf, logger.stream)
70+
level_name = level_to_string(level)
71+
module_name = something(_module, "nothing")
72+
file_name = something(filepath, "nothing")
73+
line_number = something(line, "nothing")
74+
msg_timestamp = Dates.format(Dates.now(), "[dd/mm/yyyy HH:MM:SS]")
75+
formatted_message = "$message --- $msg_timestamp $level_name $file_name:$line_number"
76+
println(iob, formatted_message)
77+
write(logger.stream, take!(buf))
78+
return nothing
79+
end

0 commit comments

Comments
 (0)