From d91b372ae5d4e14f7e3596aa6e86f1b08c786247 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 15 Dec 2017 14:11:06 +0100 Subject: [PATCH 1/3] Use Base.CoreLogging --- deps/build.jl | 5 ++-- src/LLVM.jl | 6 ++--- src/core/context.jl | 4 ++-- src/core/instructions.jl | 2 +- src/core/type.jl | 2 +- src/core/value.jl | 2 +- src/util/logging.jl | 51 ---------------------------------------- test/core.jl | 4 ++-- 8 files changed, 13 insertions(+), 63 deletions(-) delete mode 100644 src/util/logging.jl diff --git a/deps/build.jl b/deps/build.jl index d1fd3fed..99bbe605 100644 --- a/deps/build.jl +++ b/deps/build.jl @@ -1,6 +1,7 @@ using Compat -include(joinpath(@__DIR__, "..", "src", "util", "logging.jl")) +using Logging +haskey(ENV, "DEBUG") && global_logger(SimpleLogger(global_logger().stream, Logging.Debug)) const config_path = joinpath(@__DIR__, "ext.jl") const previous_config_path = config_path * ".bak" @@ -39,7 +40,7 @@ function main() joinpath(dirname(JULIA_HOME), "lib", "julia", libllvm_name)] # dists end - debug("Looking for $(libllvm_name) in ", join(libllvm_paths, ", ")) + @debug "Looking for $(libllvm_name) in $(join(libllvm_paths, ", "))" filter!(isfile, libllvm_paths) isempty(libllvm_paths) && error("Could not find $(libllvm_name), is Julia built with USE_LLVM_SHLIB=1?") config[:libllvm_path] = first(libllvm_paths) diff --git a/src/LLVM.jl b/src/LLVM.jl index 2610da31..8a27ec74 100644 --- a/src/LLVM.jl +++ b/src/LLVM.jl @@ -2,6 +2,9 @@ __precompile__() module LLVM +using Logging +haskey(ENV, "DEBUG") && global_logger(SimpleLogger(global_logger().stream, Logging.Debug)) + using Unicode using Compat @@ -10,7 +13,6 @@ isfile(ext) || error("LLVM.jl has not been built, please run Pkg.build(\"LLVM\") include(ext) const libllvm = libllvm_path -include("util/logging.jl") include("util/types.jl") include("base.jl") @@ -63,8 +65,6 @@ if Compat.Sys.islinux() end function __init__() - __init_logging__() - _install_handlers() _install_handlers(GlobalContext()) diff --git a/src/core/context.jl b/src/core/context.jl index 48156e23..b16700c4 100644 --- a/src/core/context.jl +++ b/src/core/context.jl @@ -65,9 +65,9 @@ function handle_diagnostic(diag_ref::API.LLVMDiagnosticInfoRef, args::Ptr{Void}) if sev == API.LLVMDSError throw(LLVMException(msg)) elseif sev == API.LLVMDSWarning - warn(msg) + @warn msg elseif sev == API.LLVMDSRemark || sev == API.LLVMDSNote - debug(msg) + @debug msg else error("unknown diagnostic severity level $sev") end diff --git a/src/core/instructions.jl b/src/core/instructions.jl index 79d52ab1..26782b7f 100644 --- a/src/core/instructions.jl +++ b/src/core/instructions.jl @@ -12,7 +12,7 @@ identify(::Type{Instruction}, ::Val{K}) where {K} = bug("Unknown instruction kin @inline function check(::Type{T}, ref::API.LLVMValueRef) where T<:Instruction ref==C_NULL && throw(NullException()) - @static if DEBUG + @debug begin T′ = identify(Instruction, ref) if T != T′ error("invalid conversion of $T′ instruction reference to $T") diff --git a/src/core/type.jl b/src/core/type.jl index f8fe95e1..8147ddf9 100644 --- a/src/core/type.jl +++ b/src/core/type.jl @@ -9,7 +9,7 @@ identify(::Type{LLVMType}, ::Val{K}) where {K} = bug("Unknown type kind $K") @inline function check(::Type{T}, ref::API.LLVMTypeRef) where T<:LLVMType ref==C_NULL && throw(NullException()) - @static if DEBUG + @debug begin T′ = identify(LLVMType, ref) if T != T′ error("invalid conversion of $T′ type reference to $T") diff --git a/src/core/value.jl b/src/core/value.jl index 93cf515d..4eb62bdf 100644 --- a/src/core/value.jl +++ b/src/core/value.jl @@ -12,7 +12,7 @@ identify(::Type{Value}, ::Val{K}) where {K} = bug("Unknown value kind $K") @inline function check(::Type{T}, ref::API.LLVMValueRef) where T<:Value ref==C_NULL && throw(NullException()) - @static if DEBUG + @debug begin T′ = identify(Value, ref) if T != T′ error("invalid conversion of $T′ value reference to $T") diff --git a/src/util/logging.jl b/src/util/logging.jl deleted file mode 100644 index 6d517dfc..00000000 --- a/src/util/logging.jl +++ /dev/null @@ -1,51 +0,0 @@ -# Logging functionality - -# I/O without libuv, for use after STDOUT is finalized -raw_print(msg::AbstractString...) = - ccall(:write, Cssize_t, (Cint, Cstring, Csize_t), 1, join(msg), length(join(msg))) -raw_println(msg::AbstractString...) = raw_print(msg..., "\n") - -# safe version of `Base.print_with_color`, switching to raw I/O before finalizers are run -# (see `atexit` in `__init_logging__`) -const after_exit = Ref{Core.Core.Bool}(false) -function safe_print_with_color(color::Union{Int, Symbol}, io::IO, msg::AbstractString...) - if after_exit[] - raw_print(msg...) - else - print_with_color(color, io, msg...) - end -end - -const TRACE = haskey(ENV, "TRACE") -"Display a trace message. Only results in actual printing if the TRACE environment variable -is set." -@inline function trace(io::IO, msg...; prefix="TRACE: ", line=true) - @static if TRACE - safe_print_with_color(:cyan, io, prefix, chomp(string(msg...)), line ? "\n" : "") - end -end -@inline trace(msg...; kwargs...) = trace(STDERR, msg...; kwargs...) - -const DEBUG = TRACE || haskey(ENV, "DEBUG") -"Display a debug message. Only results in actual printing if the TRACE or DEBUG environment -variable is set." -@inline function debug(io::IO, msg...; prefix="DEBUG: ", line=true) - @static if DEBUG - safe_print_with_color(:green, io, prefix, chomp(string(msg...)), line ? "\n" : "") - end -end -@inline debug(msg...; kwargs...) = debug(STDERR, msg...; kwargs...) - -function __init_logging__() - if TRACE - trace("LLVM.jl is running in trace mode, this will generate a lot of additional output") - elseif DEBUG - debug("LLVM.jl is running in debug mode, this will generate additional output") - debug("Run with TRACE=1 to enable even more output") - end - - atexit(()->begin - debug("Dropping down to post-finalizer I/O") - after_exit[]=true - end) -end diff --git a/test/core.jl b/test/core.jl index 060fbd53..4657bfff 100644 --- a/test/core.jl +++ b/test/core.jl @@ -24,7 +24,7 @@ Context() do ctx @test typeof(LLVM.ref(typ)) == LLVM.API.LLVMTypeRef # untyped @test typeof(LLVM.IntegerType(LLVM.ref(typ))) == LLVM.IntegerType # type reconstructed - if LLVM.DEBUG + @debug begin # TODO: does this correspond with the loglevel of the LLVM module? @test_throws ErrorException LLVM.FunctionType(LLVM.ref(typ)) # wrong type end @test_throws NullException LLVM.FunctionType(LLVM.API.LLVMTypeRef(C_NULL)) @@ -182,7 +182,7 @@ LLVM.Module("SomeModule", ctx) do mod @test typeof(LLVM.ref(val)) == LLVM.API.LLVMValueRef # untyped @test typeof(LLVM.Instruction(LLVM.ref(val))) == LLVM.AllocaInst # type reconstructed - if LLVM.DEBUG + @debug begin # TODO: does this correspond with the loglevel of the LLVM module? @test_throws ErrorException LLVM.Function(LLVM.ref(val)) # wrong end @test_throws NullException LLVM.Function(LLVM.API.LLVMValueRef(C_NULL)) From d37898ee07fdd4e7e90e561340974a85f0fa9bce Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 15 Dec 2017 14:37:51 +0100 Subject: [PATCH 2/3] Simplify debug checks. --- src/core/instructions.jl | 4 +--- src/core/type.jl | 4 +--- src/core/value.jl | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/core/instructions.jl b/src/core/instructions.jl index 26782b7f..bb1af3d9 100644 --- a/src/core/instructions.jl +++ b/src/core/instructions.jl @@ -14,9 +14,7 @@ identify(::Type{Instruction}, ::Val{K}) where {K} = bug("Unknown instruction kin ref==C_NULL && throw(NullException()) @debug begin T′ = identify(Instruction, ref) - if T != T′ - error("invalid conversion of $T′ instruction reference to $T") - end + @assert T==T′ "invalid conversion of $T′ instruction reference to $T" end end diff --git a/src/core/type.jl b/src/core/type.jl index 8147ddf9..e1d84732 100644 --- a/src/core/type.jl +++ b/src/core/type.jl @@ -11,9 +11,7 @@ identify(::Type{LLVMType}, ::Val{K}) where {K} = bug("Unknown type kind $K") ref==C_NULL && throw(NullException()) @debug begin T′ = identify(LLVMType, ref) - if T != T′ - error("invalid conversion of $T′ type reference to $T") - end + @assert T==T′ "invalid conversion of $T′ type reference to $T" end end diff --git a/src/core/value.jl b/src/core/value.jl index 4eb62bdf..48f954ae 100644 --- a/src/core/value.jl +++ b/src/core/value.jl @@ -14,9 +14,7 @@ identify(::Type{Value}, ::Val{K}) where {K} = bug("Unknown value kind $K") ref==C_NULL && throw(NullException()) @debug begin T′ = identify(Value, ref) - if T != T′ - error("invalid conversion of $T′ value reference to $T") - end + @assert T==T′ "invalid conversion of $T′ value reference to $T" end end From a14d9e123d97e049a1769c14d2a9bfb6aeb6c3c4 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 15 Dec 2017 14:40:56 +0100 Subject: [PATCH 3/3] Properly parse booleans and enable debug testing on Travis. --- .travis.yml | 4 ++++ deps/build.jl | 2 +- src/LLVM.jl | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1371a3ac..68e2dcfc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,10 @@ os: - linux - osx +env: + - DEBUG=true + - DEBUG=false + julia: - nightly diff --git a/deps/build.jl b/deps/build.jl index 99bbe605..9d82dd97 100644 --- a/deps/build.jl +++ b/deps/build.jl @@ -1,7 +1,7 @@ using Compat using Logging -haskey(ENV, "DEBUG") && global_logger(SimpleLogger(global_logger().stream, Logging.Debug)) +parse(Bool, get(ENV, "DEBUG", "false")) && global_logger(SimpleLogger(global_logger().stream, Logging.Debug)) const config_path = joinpath(@__DIR__, "ext.jl") const previous_config_path = config_path * ".bak" diff --git a/src/LLVM.jl b/src/LLVM.jl index 8a27ec74..e6c6d0db 100644 --- a/src/LLVM.jl +++ b/src/LLVM.jl @@ -3,7 +3,7 @@ __precompile__() module LLVM using Logging -haskey(ENV, "DEBUG") && global_logger(SimpleLogger(global_logger().stream, Logging.Debug)) +parse(Bool, get(ENV, "DEBUG", "false")) && global_logger(SimpleLogger(global_logger().stream, Logging.Debug)) using Unicode using Compat