@@ -52,27 +52,37 @@ function Base.getproperty(e::StatusError, s::Symbol)
5252 end
5353end
5454
55- const LOGGER_FILE_REF = Ref {Libc.FILE} ()
56- const LOGGER_OPTIONS = Ref {aws_logger_standard_options} ()
57- const LOGGER = Ref {Ptr{Cvoid}} (C_NULL )
58-
5955# NOTE: this is global process logging in the aws-crt libraries; not appropriate for request-level
6056# logging, but more for debugging the library itself
61- function set_log_level! (level:: Integer )
57+ mutable struct AwsLogger
58+ ptr:: Ptr{aws_logger}
59+ file_ref:: Libc.FILE
60+ options:: aws_logger_standard_options
61+ function AwsLogger (level:: Integer , allocator:: Ptr{aws_allocator} )
62+ fr = Libc. FILE (Libc. RawFD (1 ), " w" )
63+ opts = aws_logger_standard_options (aws_log_level (0 ), C_NULL , Ptr {Libc.FILE} (fr. ptr))
64+ x = new (Ptr {aws_logger} (aws_mem_acquire (allocator, 64 )), fr, opts)
65+ aws_logger_init_standard (x. ptr, allocator, FieldRef (x, :options )) != 0 && aws_throw_error ()
66+ aws_logger_set (x. ptr)
67+ return finalizer (x) do x
68+ aws_logger_clean_up (x. ptr)
69+ aws_mem_release (allocator, x. ptr)
70+ end
71+ end
72+ end
73+
74+ const LOGGER = Ref {AwsLogger} ()
75+
76+ function set_log_level! (level:: Integer , allocator:: Ptr{aws_allocator} = default_aws_allocator ())
6277 @assert 0 <= level <= 7 " log level must be between 0 and 7"
63- @assert aws_logger_set_log_level (LOGGER[], aws_log_level (level)) == 0
78+ LOGGER[] = AwsLogger (level, allocator)
79+ @assert aws_logger_set_log_level (LOGGER[]. ptr, aws_log_level (level)) == 0
6480 return
6581end
6682
6783function __init__ ()
6884 allocator = default_aws_allocator ()
6985 LibAwsHTTP. init (allocator)
70- # initialize logger
71- LOGGER[] = aws_mem_acquire (allocator, 64 )
72- LOGGER_FILE_REF[] = Libc. FILE (Libc. RawFD (1 ), " w" )
73- LOGGER_OPTIONS[] = aws_logger_standard_options (aws_log_level (3 ), C_NULL , Ptr {Libc.FILE} (LOGGER_FILE_REF[]. ptr))
74- @assert aws_logger_init_standard (LOGGER[], allocator, LOGGER_OPTIONS) == 0
75- aws_logger_set (LOGGER[])
7686 # intialize c functions
7787 on_acquired[] = @cfunction (c_on_acquired, Cvoid, (Ptr{Cvoid}, Cint, Ptr{aws_retry_token}, Ptr{Cvoid}))
7888 # on_shutdown[] = @cfunction(c_on_shutdown, Cvoid, (Ptr{Cvoid}, Cint, Ptr{Cvoid}))
0 commit comments