Skip to content

Commit

Permalink
impl logging function
Browse files Browse the repository at this point in the history
  • Loading branch information
nna774 committed Jun 14, 2021
1 parent faced3d commit 550ff20
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
29 changes: 27 additions & 2 deletions exif.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ package exif
#include <stdlib.h>
#include <libexif/exif-data.h>
#include <libexif/exif-loader.h>
#include <libexif/exif-log.h>
#include "_cgo/types.h"
exif_value_t* pop_exif_value(exif_stack_t *);
void free_exif_value(exif_value_t* n);
exif_stack_t* exif_dump(ExifData *);
void set_exif_log(ExifLog *log);
*/
import "C"

Expand All @@ -50,8 +52,10 @@ var (

// Data stores the EXIF tags of a file.
type Data struct {
exifLoader *C.ExifLoader
Tags map[string]string
exifLoader *C.ExifLoader
exifLog *C.ExifLog
loggingEnabled bool
Tags map[string]string
}

// New creates and returns a new exif.Data object.
Expand Down Expand Up @@ -111,6 +115,13 @@ func (d *Data) Write(p []byte) (n int, err error) {
d.exifLoader = C.exif_loader_new()
runtime.SetFinalizer(d, (*Data).cleanup)
}
if d.loggingEnabled && d.exifLog == nil {
d.exifLog = C.exif_log_new()
C.exif_loader_log(d.exifLoader, d.exifLog)
C.set_exif_log(d.exifLog)
C.exif_log_ref(d.exifLog)
// cleanup will be defered by above
}

res := C.exif_loader_write(d.exifLoader, (*C.uchar)(unsafe.Pointer(&p[0])), C.uint(len(p)))

Expand Down Expand Up @@ -141,4 +152,18 @@ func (d *Data) cleanup() {
C.exif_loader_unref(d.exifLoader)
d.exifLoader = nil
}
if d.exifLog != nil {
C.exif_log_unref(d.exifLog)
d.exifLog = nil
}
}

type LoggingOption struct{}

func (d *Data) EnableLogging(opt *LoggingOption) {
d.loggingEnabled = true
}

func (d *Data) DisableLogging() {
d.loggingEnabled = false
}
11 changes: 11 additions & 0 deletions exif_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <string.h>

#include <libexif/exif-data.h>
#include <libexif/exif-log.h>

#include "_cgo/types.h"

Expand Down Expand Up @@ -106,3 +107,13 @@ exif_stack_t* exif_dump(ExifData* data) {

return user_data;
}

void show_log(ExifLog *log, ExifLogCode code, const char *domain, const char *format, va_list args, void* _) {
printf("domain %s: %s: %s\n ", domain, exif_log_code_get_title(code), exif_log_code_get_message (code));
printf(format, args);
puts("");
}

void set_exif_log(ExifLog *log) {
exif_log_set_func(log, show_log, NULL);
}

0 comments on commit 550ff20

Please sign in to comment.