1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-18 07:36:24 -07:00

remove unnecessary GcLogger struct, use exported functions instead

This commit is contained in:
Eggbertx 2020-06-06 12:52:12 -07:00
parent 9ab597be64
commit 418468ff97
2 changed files with 56 additions and 74 deletions

View file

@ -9,8 +9,6 @@ import (
"time"
)
var gclog *GcLogger
const (
logTimeFmt = "2006/01/02 15:04:05 "
logFileFlags = os.O_APPEND | os.O_CREATE | os.O_RDWR
@ -26,48 +24,47 @@ const (
LFatal
)
// GcLogger is used for printing to access, error, and staff logs
type GcLogger struct {
var (
accessFile *os.File
errorFile *os.File
staffFile *os.File
debug bool
}
debugLog bool
)
func (gcl *GcLogger) selectLogs(flags int) []*os.File {
func selectLogs(flags int) []*os.File {
var logs []*os.File
if flags&LAccessLog > 0 {
logs = append(logs, gcl.accessFile)
logs = append(logs, accessFile)
}
if flags&LErrorLog > 0 {
logs = append(logs, gcl.errorFile)
logs = append(logs, errorFile)
}
if flags&LStaffLog > 0 {
logs = append(logs, gcl.staffFile)
logs = append(logs, staffFile)
}
if (flags&LStdLog > 0) || gcl.debug {
if (flags&LStdLog > 0) || debugLog {
logs = append(logs, os.Stdout)
}
return logs
}
func (gcl *GcLogger) getPrefix() string {
func getPrefix() string {
prefix := time.Now().Format(logTimeFmt)
_, file, line, _ := runtime.Caller(3)
_, file, line, _ := runtime.Caller(2)
prefix += fmt.Sprint(file, ":", line, ": ")
return prefix
}
// Print is comparable to log.Print but takes binary flags
func (gcl *GcLogger) Print(flags int, v ...interface{}) string {
func Print(flags int, v ...interface{}) string {
str := fmt.Sprint(v...)
logs := gcl.selectLogs(flags)
logs := selectLogs(flags)
for _, l := range logs {
if l == os.Stdout {
io.WriteString(l, str+"\n")
} else {
io.WriteString(l, gcl.getPrefix()+str+"\n")
io.WriteString(l, getPrefix()+str+"\n")
}
}
if flags&LFatal > 0 {
@ -77,14 +74,14 @@ func (gcl *GcLogger) Print(flags int, v ...interface{}) string {
}
// Printf is comparable to log.Logger.Printf but takes binary OR'd flags
func (gcl *GcLogger) Printf(flags int, format string, v ...interface{}) string {
func Printf(flags int, format string, v ...interface{}) string {
str := fmt.Sprintf(format, v...)
logs := gcl.selectLogs(flags)
logs := selectLogs(flags)
for _, l := range logs {
if l == os.Stdout {
io.WriteString(l, str+"\n")
} else {
io.WriteString(l, gcl.getPrefix()+str+"\n")
io.WriteString(l, getPrefix()+str+"\n")
}
}
if flags&LFatal > 0 {
@ -94,64 +91,46 @@ func (gcl *GcLogger) Printf(flags int, format string, v ...interface{}) string {
}
// Println is comparable to log.Logger.Println but takes binary OR'd flags
func (gcl *GcLogger) Println(flags int, v ...interface{}) string {
func Println(flags int, v ...interface{}) string {
str := fmt.Sprintln(v...)
logs := gcl.selectLogs(flags)
logs := selectLogs(flags)
for _, l := range logs {
if l == os.Stdout {
io.WriteString(l, str)
} else {
io.WriteString(l, gcl.getPrefix()+str)
io.WriteString(l, getPrefix()+str)
}
}
if flags&LFatal > 0 {
os.Exit(1)
}
return str
return str[:len(str)-1]
}
// Close closes the log file handles
func (gcl *GcLogger) Close() {
gcl.accessFile.Close()
gcl.errorFile.Close()
gcl.staffFile.Close()
}
// Print is comparable to log.Print but takes binary OR'd flags
func Print(flags int, v ...interface{}) string {
if gclog == nil {
return ""
func Close() {
if accessFile != nil {
accessFile.Close()
}
return gclog.Print(flags, v...)
}
// Printf is comparable to log.Printf but takes binary OR'd flags
func Printf(flags int, format string, v ...interface{}) string {
if gclog == nil {
return ""
if errorFile != nil {
errorFile.Close()
}
return gclog.Printf(flags, format, v...)
}
// Println is comparable to log.Println but takes binary OR'd flags
func Println(flags int, v ...interface{}) string {
if gclog == nil {
return ""
if staffFile != nil {
staffFile.Close()
}
return gclog.Println(flags, v...)
}
// InitLogs initializes the log files to be used by gochan
func InitLogs(accessLogPath, errorLogPath, staffLogPath string, debugMode bool) error {
gclog = &GcLogger{debug: debugMode}
debugLog = debugMode
var err error
if gclog.accessFile, err = os.OpenFile(accessLogPath, logFileFlags, 0777); err != nil {
if accessFile, err = os.OpenFile(accessLogPath, logFileFlags, 0777); err != nil {
return errors.New("Error loading " + accessLogPath + ": " + err.Error())
}
if gclog.errorFile, err = os.OpenFile(errorLogPath, logFileFlags, 0777); err != nil {
if errorFile, err = os.OpenFile(errorLogPath, logFileFlags, 0777); err != nil {
return errors.New("Error loading " + errorLogPath + ": " + err.Error())
}
if gclog.staffFile, err = os.OpenFile(staffLogPath, logFileFlags, 0777); err != nil {
if staffFile, err = os.OpenFile(staffLogPath, logFileFlags, 0777); err != nil {
return errors.New("Error loading " + staffLogPath + ": " + err.Error())
}

View file

@ -19,30 +19,33 @@ func TestGochanLog(t *testing.T) {
}
// test Print usage
gclog.Print(LStdLog, "os.Stdout log", "(Print)")
gclog.Print(LStdLog|LAccessLog|LErrorLog|LStaffLog, "all logs", "(Print)")
gclog.Print(LAccessLog, "Access log", "(Print)")
gclog.Print(LErrorLog, "Error log", "(Print)")
gclog.Print(LStaffLog, "Staff log", "(Print)")
gclog.Print(LAccessLog|LErrorLog, "Access and error log", "(Print)")
gclog.Print(LAccessLog|LStaffLog, "Access and staff log", "(Print)")
Print(LStdLog, "os.Stdout log", "(Print)")
Print(LStdLog|LAccessLog|LErrorLog|LStaffLog, "all logs", "(Print)")
Print(LAccessLog, "Access log", "(Print)")
Print(LErrorLog, "Error log", "(Print)")
Print(LStaffLog, "Staff log", "(Print)")
Print(LAccessLog|LErrorLog, "Access and error log", "(Print)")
Print(LAccessLog|LStaffLog, "Access and staff log", "(Print)")
// test Printf usage
gclog.Printf(LStdLog, "os.Stdout log %q", "(Println)")
gclog.Printf(LStdLog|LAccessLog|LErrorLog|LStaffLog, "all logs %q", "(Printf)")
gclog.Printf(LAccessLog, "Access log %q", "(Printf)")
gclog.Printf(LErrorLog, "Error log %q", "(Printf)")
gclog.Printf(LStaffLog, "Staff log %q", "(Printf)")
gclog.Printf(LAccessLog|LErrorLog, "Access and error log %q", "(Printf)")
gclog.Printf(LAccessLog|LStaffLog, "Access and staff log %q", "(Printf)")
Printf(LStdLog, "os.Stdout log %q", "(Println)")
Printf(LStdLog|LAccessLog|LErrorLog|LStaffLog, "all logs %q", "(Printf)")
Printf(LAccessLog, "Access log %q", "(Printf)")
Printf(LErrorLog, "Error log %q", "(Printf)")
Printf(LStaffLog, "Staff log %q", "(Printf)")
Printf(LAccessLog|LErrorLog, "Access and error log %q", "(Printf)")
Printf(LAccessLog|LStaffLog, "Access and staff log %q", "(Printf)")
// test Println usage (proper spacing and no extra newlines)
gclog.Println(LStdLog, "os.Stdout log", "(Println)")
gclog.Println(LStdLog|LAccessLog|LErrorLog|LStaffLog, "all logs", "(Println)")
gclog.Println(LAccessLog, "Access log", "(Println)")
gclog.Println(LErrorLog, "Error log", "(Println)")
gclog.Println(LStaffLog, "Staff log", "(Println)")
gclog.Println(LAccessLog|LErrorLog, "Access and error log", "(Println)")
gclog.Println(LAccessLog|LStaffLog|LFatal, "Fatal access and staff log", "(Println)")
gclog.Println(LAccessLog, "This shouldn't be here", "(Println)")
Println(LStdLog, "os.Stdout log", "(Println)")
t.Logf("%q", Println(LStdLog, "Testing log chaining for errors", "(Println)"))
Println(LStdLog|LAccessLog|LErrorLog|LStaffLog, "all logs", "(Println)")
Println(LAccessLog, "Access log", "(Println)")
Println(LErrorLog, "Error log", "(Println)")
Println(LStaffLog, "Staff log", "(Println)")
Println(LAccessLog|LErrorLog, "Access and error log", "(Println)")
Println(LAccessLog|LStaffLog|LFatal, "Fatal access and staff log", "(Println)")
Println(LAccessLog, "This shouldn't be here", "(Println)")
}