diff --git a/devmapper/deviceset_devmapper.go b/devmapper/deviceset_devmapper.go index d0d4fbf85c..2107a305ae 100644 --- a/devmapper/deviceset_devmapper.go +++ b/devmapper/deviceset_devmapper.go @@ -327,7 +327,18 @@ func setCloseOnExec(name string) { } } +func (devices *DeviceSetDM) log(level int, file string, line int, dmError int, message string) { + if level >= 7 { + return // Ignore _LOG_DEBUG + } + + utils.Debugf("libdevmapper(%d): %s:%d (%d) %s", level, file, line, dmError, message) +} + + func (devices *DeviceSetDM) initDevmapper() error { + logInit(devices) + info, err := getInfo(devices.getPoolName()) if info == nil { utils.Debugf("Error device getInfo: %s", err) diff --git a/devmapper/devmapper.go b/devmapper/devmapper.go index e4db906637..97a388e4c9 100644 --- a/devmapper/devmapper.go +++ b/devmapper/devmapper.go @@ -114,6 +114,28 @@ get_block_size(int fd) return (int64_t)size; } +extern void DevmapperLogCallback(int level, char *file, int line, int dm_errno_or_class, char *str); + +static void +log_cb(int level, const char *file, int line, + int dm_errno_or_class, const char *f, ...) +{ + char buffer[256]; + va_list ap; + + va_start(ap, f); + vsnprintf(buffer, 256, f, ap); + va_end(ap); + + DevmapperLogCallback(level, (char *)file, line, dm_errno_or_class, buffer); +} + +static void +log_with_errno_init () +{ + dm_log_with_errno_init(log_cb); +} + */ import "C" @@ -127,6 +149,10 @@ import ( "unsafe" ) +type DevmapperLogger interface { + log(level int, file string, line int, dmError int, message string) +} + const ( DeviceCreate TaskType = iota DeviceReload @@ -351,6 +377,13 @@ func LogInitVerbose(level int) { C.dm_log_init_verbose(C.int(level)) } +var dmLogger DevmapperLogger = nil + +func logInit(logger DevmapperLogger) { + dmLogger = logger + C.log_with_errno_init() +} + func SetDevDir(dir string) error { c_dir := C.CString(dir) defer free(c_dir) diff --git a/devmapper/devmapper_log.go b/devmapper/devmapper_log.go new file mode 100644 index 0000000000..1f95eb7bac --- /dev/null +++ b/devmapper/devmapper_log.go @@ -0,0 +1,13 @@ +package devmapper + +import "C" + +// Due to the way cgo works this has to be in a separate file, as devmapper.go has +// definitions in the cgo block, which is incompatible with using "//export" + +//export DevmapperLogCallback +func DevmapperLogCallback(level C.int, file *C.char, line C.int, dm_errno_or_class C.int, message *C.char) { + if dmLogger != nil { + dmLogger.log(int(level), C.GoString(file), int(line), int(dm_errno_or_class), C.GoString(message)) + } +}