-
Notifications
You must be signed in to change notification settings - Fork 0
/
slog.go
75 lines (62 loc) · 1.93 KB
/
slog.go
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
// Copyright 2017 Inca Roads LLC. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
// Server log support
package main
import (
"fmt"
"hash/crc32"
"os"
"strings"
"time"
)
// ServerLogFilename gets the current log file name for the current instance
func ServerLogFilename(extension string) string {
prefix := time.Now().UTC().Format("2006-01-")
filename := prefix + TTServeInstanceID + extension
return filename
}
// ServerLogSecret gets secret that only allows the URLs from the Slack command to function
func ServerLogSecret() string {
timestr := ControlFileTime(TTServerSlackCommandControlFile, "").Format(logDateFormat)
checksum := crc32.ChecksumIEEE([]byte(timestr))
checkstr := fmt.Sprintf("%d", checksum)
return checkstr
}
// ServerLog logs a string to the instance's log file
func ServerLog(sWithoutDate string) {
// Add a standard header unless it begins with a newline
s := sWithoutDate
if !strings.HasPrefix(sWithoutDate, "\n") {
s = fmt.Sprintf("%s %s", LogTime(), sWithoutDate)
}
// Print it to the console
fmt.Printf("%s", s)
// Open it
file := SafecastDirectory() + TTServerLogPath + "/" + ServerLogFilename(".log")
fd, err := os.OpenFile(file, os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
// Don't attempt to create it if it already exists
_, err2 := os.Stat(file)
if err2 == nil {
fmt.Printf("ServerLogging: Can't log to %s: %s\n", file, err)
return
}
if err2 == nil {
if !os.IsNotExist(err2) {
fmt.Printf("ServerLogging: Ignoring attempt to create %s: %s\n", file, err2)
return
}
}
// Attempt to create the file because it doesn't already exist
fd, err = os.OpenFile(file, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
fmt.Printf("ServerLogging: error creating file %s: %s\n", file, err)
return
}
}
// Append it
fd.WriteString(s)
// Close and exit
fd.Close()
}