Skip to content

Commit

Permalink
Merge pull request #25158 from c42f/depwarn-caller-fix
Browse files Browse the repository at this point in the history
Depwarn caller fix
  • Loading branch information
StefanKarpinski authored Dec 21, 2017
2 parents b57a592 + 16bdf88 commit f9e2d99
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function firstcaller(bt::Vector, funcsyms)
for frame in bt
lkups = StackTraces.lookup(frame)
for outer lkup in lkups
if lkup == StackTraces.UNKNOWN
if lkup == StackTraces.UNKNOWN || lkup.from_c
continue
end
if found
Expand Down
5 changes: 3 additions & 2 deletions base/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,14 @@ exists for the current task.
global_logger(logger)
Set the global logger to `logger`.
Set the global logger to `logger`, and return the previous global logger.
"""
global_logger() = _global_logstate.logger

function global_logger(logger::AbstractLogger)
prev = _global_logstate.logger
global _global_logstate = LogState(logger)
logger
prev
end

"""
Expand Down
18 changes: 18 additions & 0 deletions test/deprecation_exec.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Test
using Logging

module DeprecationTests # to test @deprecate
f() = true
Expand Down Expand Up @@ -81,3 +82,20 @@ depwarn24658() = Base.firstcaller(backtrace(), :_func_not_found_)
# issue #24658
@test eval(:(if true; f24658(); end)) == (Ptr{Cvoid}(0),StackTraces.UNKNOWN)
end

# issue #25130
f25130() = Base.depwarn("f25130 message", :f25130)
# The following test is for the depwarn behavior of expressions evaluated at
# top-level, so we can't use the usual `collect_test_logs()` / `with_logger()`
testlogger = Test.TestLogger()
prev_logger = global_logger(testlogger)
# Each call at top level should be distinct. This won't be true if they're
# attributed to internal C frames (including generic dispatch machinery)
f25130()
f25130()
testlogs = testlogger.logs
@test length(testlogs) == 2
@test testlogs[1].id != testlogs[2].id
@test testlogs[1].kwargs.caller.func == Symbol("top-level scope")
@test all(l.message == "f25130 message" for l in testlogs)
global_logger(prev_logger)
28 changes: 13 additions & 15 deletions test/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,6 @@ end
end
end

@testset "Log filtering, global logger" begin
old_logger = global_logger()
logs = let
logger = TestLogger(min_level=Warn)
global_logger(logger)
@info "b"
@warn "c"
logger.logs
end
global_logger(old_logger)

@test length(logs) == 1
@test ismatch((Warn , "c"), logs[1])
end

@testset "Log level filtering - global flag" begin
# Test utility: Log once at each standard level
function log_each_level()
Expand Down Expand Up @@ -191,6 +176,19 @@ end
end


#-------------------------------------------------------------------------------
@testset "Logger installation and access" begin
@testset "Global logger" begin
logger1 = global_logger()
logger2 = TestLogger()
# global_logger() returns the previously installed logger
@test logger1 === global_logger(logger2)
# current logger looks up global logger by default.
@test current_logger() === logger2
global_logger(logger1) # Restore global logger
end
end

#-------------------------------------------------------------------------------

# Custom log levels
Expand Down

0 comments on commit f9e2d99

Please sign in to comment.