...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package logger
16
17 import (
18 "fmt"
19 "net"
20 "os"
21 )
22
23
24 type Named map[string]interface{}
25
26
27 func getHostname() (string, error) {
28 hostname, err := os.Hostname()
29
30 if err != nil {
31 return "localhost", NewRuntimeError("cannot get hostname", err)
32 }
33
34 return hostname, nil
35 }
36
37
38 func getAddress() (string, error) {
39 connection, err := net.Dial("udp", "8.8.8.8:80")
40
41 if err != nil {
42 return "127.0.0.1", NewRuntimeError("cannot connect to primary Google DNS", err)
43 }
44
45 defer func() {
46 err := connection.Close()
47
48 if err != nil {
49 printError(NewRuntimeError("cannot close UDP connection", err))
50 }
51 }()
52
53 return connection.LocalAddr().(*net.UDPAddr).IP.String(), nil
54 }
55
56
57 func printError(err error) {
58 fmt.Fprintln(os.Stderr, "Logger error:", err)
59 }
60
View as plain text