# frozen_string_literal: true require_relative 'safe' module FFI module Libfuse module Adapter # Debug callbacks # # When included in a filesystem class, and if debugging is enabled via {Main#fuse_debug}, then installs a wrapper # via #{FuseCallbacks#fuse_wrappers} to log callbacks. # # Simple format options can be handled by #{debug_config}, or override the **Module Functions** on an including # class for more programmatic control of output. # # @note {Debug} includes {Safe} as it expects to handle (and re-raise) exceptions. module Debug include Safe # Default format # @see debug_callback DEFAULT_FORMAT = "%
s % : The value of prefix option
# @raise [SystemCallError]
# expected Errors raised from callbacks are logged with their cause (if any)
# @raise [StandardError,ScriptError]
# unexpected Errors raised from callbacks are logged with their backtrace
def debug_callback(fuse_method, *args, **options)
result = yield(*args)
debug(fuse_method, args, result, **options)
result
rescue StandardError, ScriptError => e
debug(fuse_method, args, error_message(e), **options)
debug_error(e)
raise
end
# @!group Module Functions
# Logs the callback
def debug(fuse_method, args, result, **options)
warn debug_format(fuse_method, args, result, **options)
end
# @param [Exception] err
# @return [String] the detailed error message for err
def error_message(err)
if err.is_a?(SystemCallError)
"#{err.class.name}(errno=#{err.errno}): #{err.message}"
else
"#{err.class.name}: #{err.message}"
end
end
# Log additional information for errors (cause/backtrace etc)
# @see debug_callback
def debug_error(err)
if err.is_a?(SystemCallError)
warn "Caused by #{error_message(err.cause)}" if err.cause
else
warn err.backtrace.join("\n\t")
end
end
# @return [String] the formatted debug message
# @see debug_callback
def debug_format(fuse_method, args, result, prefix: 'DEBUG', strftime: '%FT%T%:z', format: DEFAULT_FORMAT)
format(format,
p: prefix,
n: Time.now.strftime(strftime),
t: Thread.current.name || Thread.current,
m: fuse_method,
a: args.map(&:to_s).join(','),
r: result)
end
# @!endgroup
end
end
end
end