Skip to content

Commit

Permalink
add log handler/writer functions and a bit of cleanup in GLib
Browse files Browse the repository at this point in the history
  • Loading branch information
jwahlstrand committed Oct 7, 2024
1 parent 6db1672 commit 20c5de3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
8 changes: 4 additions & 4 deletions GI/src/giexport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ function struct_constructor_exprs!(exprs,ns;constructor_skiplist=[], struct_skip
s=get_non_skipped(ns,GIStructInfo,struct_skiplist,exclude_deprecated)
structs = get_name.(s)
for ss in vcat(first_list, structs)
println("struct constructor: ",ss)
ssi=gi_find_by_name(ns,ss)
constructors = get_constructors(ssi;skiplist=constructor_skiplist, struct_skiplist=struct_skiplist, exclude_deprecated=exclude_deprecated)
if !isempty(constructors)
Expand Down Expand Up @@ -192,9 +191,10 @@ function all_struct_exprs!(exprs,exports,ns;print_summary=true,excludelist=[],co
struct_skiplist, loaded
end

function all_callbacks!(exprs, exports, ns)
function all_callbacks!(exprs, exports, ns; callback_skiplist = [])
callbacks=get_all(ns,GICallbackInfo)
for c in callbacks
get_name(c) in callback_skiplist && continue
try
push!(exprs, decl(c))
catch e
Expand All @@ -209,7 +209,7 @@ function all_callbacks!(exprs, exports, ns)
nothing
end

function export_struct_exprs!(ns,path,prefix, struct_skiplist, import_as_opaque; doc_xml = nothing, doc_prefix = prefix, constructor_skiplist = [], first_list = [], output_boxed_cache_init = true, output_object_cache_init = true, output_object_cache_define = true, object_skiplist = [], object_constructor_skiplist = [], interface_skiplist = [], signal_skiplist = [], expr_init = nothing, output_gtype_wrapper_cache_def = false, output_boxed_types_def = true, output_callbacks = true, exclude_deprecated = true, doc_skiplist = [])
function export_struct_exprs!(ns,path,prefix, struct_skiplist, import_as_opaque; doc_xml = nothing, doc_prefix = prefix, constructor_skiplist = [], first_list = [], output_boxed_cache_init = true, output_object_cache_init = true, output_object_cache_define = true, object_skiplist = [], object_constructor_skiplist = [], interface_skiplist = [], signal_skiplist = [], expr_init = nothing, output_gtype_wrapper_cache_def = false, output_boxed_types_def = true, output_callbacks = true, exclude_deprecated = true, doc_skiplist = [], callback_skiplist = [])
toplevel, exprs, exports = GI.output_exprs()

if output_boxed_types_def
Expand All @@ -235,7 +235,7 @@ function export_struct_exprs!(ns,path,prefix, struct_skiplist, import_as_opaque;
end
all_object_signals!(exprs, ns;skiplist=signal_skiplist,object_skiplist=object_skiplist, exclude_deprecated = exclude_deprecated)
if output_callbacks
all_callbacks!(exprs, exports, ns)
all_callbacks!(exprs, exports, ns; callback_skiplist)
end
push!(exprs,exports)
write_to_file(path,"$(prefix)_structs",toplevel)
Expand Down
3 changes: 2 additions & 1 deletion gen/gen_glib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ struct_skiplist=vcat(disguised, special, [:Cond,:HashTableIter,:Hook,
:RecMutex,:Scanner,
:TestLogBuffer,:TestLogMsg,:Thread,:ThreadPool,:Tree,:UriParamsIter])

callback_skiplist=[:LogWriterFunc]
constructor_skiplist=[:new,:new_take,:new_from_unix_utc,:new_now_utc,:new_utc,:new_maybe]

GI.export_struct_exprs!(ns,path, "glib", struct_skiplist, import_as_opaque; doc_xml = dglib, constructor_skiplist = constructor_skiplist, output_object_cache_init = false, output_object_cache_define = false, output_boxed_types_def = false)
GI.export_struct_exprs!(ns,path, "glib", struct_skiplist, import_as_opaque; doc_xml = dglib, constructor_skiplist = constructor_skiplist, output_object_cache_init = false, output_object_cache_define = false, output_boxed_types_def = false, callback_skiplist)

## struct methods

Expand Down
26 changes: 26 additions & 0 deletions src/GLib/GLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,32 @@ include("loop.jl")
include("actions.jl")
include("gio.jl")

function set_default_log_handler(log_func)
global func = @cfunction(GLogFunc, Nothing, (Cstring, Cuint, Cstring, Ref{Function}))
ref, deref = gc_ref_closure(log_func)
ccall((:g_log_set_default_handler, libglib), Ptr{Cvoid}, (Ptr{Cvoid},Ptr{Cvoid}), func, ref)
end

function set_log_handler(log_domain, log_levels, log_func)
func = @cfunction(GLogFunc, Nothing, (Cstring, Cuint, Cstring, Ref{Function}))
ref, deref = gc_ref_closure(log_func)
ccall((:g_log_set_handler_full, libglib), Cuint, (Cstring, Cint, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}), log_domain, Cuint(log_levels), func, ref, deref)
end

function GLogWriterFunc(log_level, fields, n_fields, user_data)
log_level = LogLevelFlags(log_level)
fields = collect(unsafe_wrap(Vector{_GLogField}, fields, n_fields))
f = user_data
ret = f(log_level, fields)
convert(UInt32, ret)
end

function set_log_writer_func(log_func)
func = @cfunction(GLogWriterFunc, Cuint, (Cuint, Ptr{_GLogField}, Csize_t, Ref{Function}))
ref, deref = gc_ref_closure(log_func)
ccall((:g_log_set_writer_func, libglib), Nothing, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}), func, ref, deref)
end

const exiting = Ref(false)
function __init__()
# check that libglib is compatible with what the GI generated code expects
Expand Down
6 changes: 3 additions & 3 deletions src/GLib/gobject.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ function show(io::IO, w::GObject)
n = Ref{Cuint}()
props = ccall((:g_object_class_list_properties, libgobject), Ptr{Ptr{GParamSpec}},
(Ptr{Nothing}, Ptr{Cuint}), G_OBJECT_GET_CLASS(w), n)
v = gvalue(String)
if get(io, :compact, false)::Bool
print(io, getfield(w,:handle)) # show pointer
else # show properties
v = gvalue(String)
first = true
for i = 1:n[]
param = unsafe_load(unsafe_load(props, i))
Expand All @@ -59,9 +59,9 @@ function show(io::IO, w::GObject)
end
end
end
ccall((:g_value_unset, libgobject), Ptr{Nothing}, (Ptr{GValue},), v)
end
print(io, ')')
ccall((:g_value_unset, libgobject), Ptr{Nothing}, (Ptr{GValue},), v)
nothing
end

Expand All @@ -71,7 +71,7 @@ end
Prints information about a property of the GObject `w`, including a
brief description, its type, its default value, and its current value.
"""
function propertyinfo(w::GObject, name::AbstractString)
function propertyinfo(@nospecialize(w::GObject), name::AbstractString)
p = ccall((:g_object_class_find_property, libgobject), Ptr{GParamSpec}, (Ptr{Nothing}, Ptr{UInt8}), G_OBJECT_GET_CLASS(w), name)
if p == C_NULL
error("No property with that name")
Expand Down
2 changes: 1 addition & 1 deletion src/GLib/gvalues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ end
"""
gtk_propertynames(w::GObject)
Prints a list of property names for the GObject `w`.
Get a list of property names for the GObject `w`.
"""
function gtk_propertynames(w::GObject)
n = Ref{Cuint}()
Expand Down
9 changes: 1 addition & 8 deletions src/gen/glib_structs
Original file line number Diff line number Diff line change
Expand Up @@ -768,13 +768,6 @@ $(Expr(:toplevel, quote
ret = f(log_domain, log_level, message)
nothing
end
function GLogWriterFunc(log_level, fields, n_fields, user_data)
log_level = LogLevelFlags(log_level)
fields = collect(unsafe_wrap(Vector{_GLogField}, fields, m_n_fields[]))
f = user_data
ret = f(log_level, fields, n_fields)
convert(UInt32, ret)
end
function GNodeForeachFunc(node, data)
node = convert(GNodeLike, node)
f = data
Expand Down Expand Up @@ -886,6 +879,6 @@ $(Expr(:toplevel, quote
ret = f(fd, condition)
convert(Cint, ret)
end
export GBookmarkFile, GBytes, GChecksum, GDate, GDateLike, _GDate, GDateTime, GDebugKey, GDebugKeyLike, _GDebugKey, GKeyFile, GLogField, GLogFieldLike, _GLogField, GMainContext, GMainLoop, GMappedFile, GMarkupParseContext, GMatchInfo, GOptionEntry, GOptionEntryLike, _GOptionEntry, GPatternSpec, GRegex, GScannerConfig, GScannerConfigLike, _GScannerConfig, GSource, GSourceLike, _GSource, GSourceCallbackFuncs, GSourceCallbackFuncsLike, _GSourceCallbackFuncs, GString, GStringLike, _GString, GTestConfig, GTestConfigLike, _GTestConfig, GTimeZone, GUri, GVariantBuilder, GVariantDict, GVariantType, GChildWatchFunc, GCompareDataFunc, GCopyFunc, GDataForeachFunc, GDestroyNotify, GDuplicateFunc, GEqualFuncFull, GFreeFunc, GFunc, GHFunc, GHRFunc, GHookCheckFunc, GHookCheckMarshaller, GHookFindFunc, GHookFunc, GHookMarshaller, GIOFunc, GLogFunc, GLogWriterFunc, GNodeForeachFunc, GNodeTraverseFunc, GOptionArgFunc, GOptionErrorFunc, GOptionParseFunc, GRegexEvalCallback, GSequenceIterCompareFunc, GSourceFunc, GSourceOnceFunc, GSpawnChildSetupFunc, GTestDataFunc, GTestFixtureFunc, GTestLogFatalFunc, GThreadFunc, GTranslateFunc, GTraverseFunc, GTraverseNodeFunc, GUnixFDSourceFunc
export GBookmarkFile, GBytes, GChecksum, GDate, GDateLike, _GDate, GDateTime, GDebugKey, GDebugKeyLike, _GDebugKey, GKeyFile, GLogField, GLogFieldLike, _GLogField, GMainContext, GMainLoop, GMappedFile, GMarkupParseContext, GMatchInfo, GOptionEntry, GOptionEntryLike, _GOptionEntry, GPatternSpec, GRegex, GScannerConfig, GScannerConfigLike, _GScannerConfig, GSource, GSourceLike, _GSource, GSourceCallbackFuncs, GSourceCallbackFuncsLike, _GSourceCallbackFuncs, GString, GStringLike, _GString, GTestConfig, GTestConfigLike, _GTestConfig, GTimeZone, GUri, GVariantBuilder, GVariantDict, GVariantType, GChildWatchFunc, GCompareDataFunc, GCopyFunc, GDataForeachFunc, GDestroyNotify, GDuplicateFunc, GEqualFuncFull, GFreeFunc, GFunc, GHFunc, GHRFunc, GHookCheckFunc, GHookCheckMarshaller, GHookFindFunc, GHookFunc, GHookMarshaller, GIOFunc, GLogFunc, GNodeForeachFunc, GNodeTraverseFunc, GOptionArgFunc, GOptionErrorFunc, GOptionParseFunc, GRegexEvalCallback, GSequenceIterCompareFunc, GSourceFunc, GSourceOnceFunc, GSpawnChildSetupFunc, GTestDataFunc, GTestFixtureFunc, GTestLogFatalFunc, GThreadFunc, GTranslateFunc, GTraverseFunc, GTraverseNodeFunc, GUnixFDSourceFunc
end))
end

0 comments on commit 20c5de3

Please sign in to comment.