File: log.go

package info (click to toggle)
golang-collectd 0.3.0%2Bgit20181025.f80706d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 312 kB
  • sloc: makefile: 3
file content (103 lines) | stat: -rw-r--r-- 2,786 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// +build go1.5,cgo

package plugin // import "collectd.org/plugin"

// #cgo CPPFLAGS: -DHAVE_CONFIG_H
// #cgo LDFLAGS: -ldl
// #include <stdlib.h>
// #include <dlfcn.h>
// #include "plugin.h"
//
// static void (*plugin_log_) (int, char const *, ...) = NULL;
// void wrap_plugin_log(int severity, char *msg) {
//   if (plugin_log_ == NULL) {
//     void *hnd = dlopen(NULL, RTLD_LAZY);
//     plugin_log_ = dlsym(hnd, "plugin_log");
//     dlclose(hnd);
//   }
//   (*plugin_log_) (severity, "%s", msg);
// }
import "C"

import (
	"fmt"
	"unsafe"
)

type severity int

const (
	logErr     severity = 3
	logWarning severity = 4
	logNotice  severity = 5
	logInfo    severity = 6
	logDebug   severity = 7
)

func log(s severity, msg string) error {
	ptr := C.CString(msg)
	defer C.free(unsafe.Pointer(ptr))

	_, err := C.wrap_plugin_log(C.int(s), ptr)
	return err
}

// Error logs an error using plugin_log(). Arguments are handled in the manner
// of fmt.Print.
func Error(v ...interface{}) error {
	return log(logErr, fmt.Sprint(v...))
}

// Errorf logs an error using plugin_log(). Arguments are handled in the manner
// of fmt.Printf.
func Errorf(format string, v ...interface{}) error {
	return Error(fmt.Sprintf(format, v...))
}

// Warning logs a warning using plugin_log(). Arguments are handled in the
// manner of fmt.Print.
func Warning(v ...interface{}) error {
	return log(logWarning, fmt.Sprint(v...))
}

// Warningf logs a warning using plugin_log(). Arguments are handled in the
// manner of fmt.Printf.
func Warningf(format string, v ...interface{}) error {
	return Warning(fmt.Sprintf(format, v...))
}

// Notice logs a notice using plugin_log(). Arguments are handled in the manner
// of fmt.Print.
func Notice(v ...interface{}) error {
	return log(logNotice, fmt.Sprint(v...))
}

// Noticef logs a notice using plugin_log(). Arguments are handled in the
// manner of fmt.Printf.
func Noticef(format string, v ...interface{}) error {
	return Notice(fmt.Sprintf(format, v...))
}

// Info logs a purely informal message using plugin_log(). Arguments are
// handled in the manner of fmt.Print.
func Info(v ...interface{}) error {
	return log(logInfo, fmt.Sprint(v...))
}

// Infof logs a purely informal message using plugin_log(). Arguments are
// handled in the manner of fmt.Printf.
func Infof(format string, v ...interface{}) error {
	return Info(fmt.Sprintf(format, v...))
}

// Debug logs a debugging message using plugin_log(). Arguments are handled in
// the manner of fmt.Print.
func Debug(v ...interface{}) error {
	return log(logDebug, fmt.Sprint(v...))
}

// Debugf logs a debugging message using plugin_log(). Arguments are handled in
// the manner of fmt.Printf.
func Debugf(format string, v ...interface{}) error {
	return Debug(fmt.Sprintf(format, v...))
}