Reputation: 1119
There seems to be no easy-to-use debugger for julia 0.6 and so I tried to construct a simple println
debugger as suggested here. The following code is a module that provides @out
macro for writing expressions and their values into a file:
module debug
using DataStructures: OrderedDict
import JSON
function fn_print_dict(file, d)
print(file, JSON.json(d,2))
end
function println_alltypes(file_to_write, eval_relevant_symbol, typeof_eval_relevant_symbol)
if typeof_eval_relevant_symbol <: Dict
fn_print_dict(file_to_write, eval_relevant_symbol)
elseif typeof_eval_relevant_symbol <: AbstractArray || typeof_eval_relevant_symbol <: Set
itr_collection_counter = 1
dict_collection = OrderedDict()
for item in eval_relevant_symbol
dict_collection[itr_collection_counter] = item
itr_collection_counter += 1
end
println_alltypes(file_to_write, dict_collection, Dict)
else
println(file_to_write, eval_relevant_symbol)
end
println(file_to_write)
end
export @out
macro out(relevant_symbols...)
return esc(quote
file_to_write_str = joinpath(homedir(), "output.txt")
if !isdefined(current_module(),:debug_counter_int)
debug_counter_int = 0
end
debug_counter_int += 1
if debug_counter_int == 1
open(file_to_write_str, "w") do file_to_write
println(file_to_write)
end
end
open(file_to_write_str, "a") do file_to_write
println(file_to_write, "===================================")
end
for relevant_symbol in $relevant_symbols
relevant_symbol_str = string(relevant_symbol)
eval_relevant_symbol = eval(relevant_symbol)
typeof_eval_relevant_symbol = typeof(eval_relevant_symbol)
open(file_to_write_str, "a") do file_to_write
println(file_to_write, "[$relevant_symbol_str]", " ($typeof_eval_relevant_symbol):")
$println_alltypes(file_to_write, eval_relevant_symbol, typeof_eval_relevant_symbol)
end
end
end)
end
end
For example,
julia> using debug
julia> a = [4, 5, Dict("a"=>44, "b"=>55)]
3-element Array{Any,1}:
4
5
Dict("b"=>55,"a"=>44)
julia> @out 2+3
julia> @out a
julia> @out [a, a]
julia>
Will write the following content into output.txt
:
===================================
[2 + 3] (Int64):
5
===================================
[a] (Array{Any,1}):
{
"1": 4,
"2": 5,
"3": {
"b": 55,
"a": 44
}
}
===================================
[[a, a]] (Array{Array{Any,1},1}):
{
"1": [
4,
5,
{
"b": 55,
"a": 44
}
],
"2": [
4,
5,
{
"b": 55,
"a": 44
}
]
}
The issue is that @out
will raise an error when used in a function such as
function fn(y)
@out y
end
fn (generic function with 1 method)
I appreciate any help in fixing this issue or proposing an alternative approach. I know that eval
should not be used in a macro, but I'm not sure what would be the alternative here.
Upvotes: 1
Views: 74
Reputation: 4370
I don't have quite enough metaprogramming experience to debug your macro easily, but as a stopgap I might suggest the built-in @show
macro, which should do most of what you want, except it'll print the results to stdout rather than to file (but you should be able to just redirect to a file when running at the command line if that's something you need).
As the comments have suggested, Julia 0.6 is indeed ancient by now. Given your task of upgrading a large Julia 0.6 codebase, I can heavily second @jling's suggestion of running on Julia 0.7, which is equivalent to 0.6 except with deprecations to warn you about all the things you will need to change to run on 1.x
.
Upvotes: 4