-
Notifications
You must be signed in to change notification settings - Fork 22
/
conn_log.go
73 lines (57 loc) · 1.5 KB
/
conn_log.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
// Copyright 2010 The go-pgsql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pgsql
import (
"bytes"
"errors"
"fmt"
"log"
"runtime"
)
func (conn *Conn) log(level LogLevel, v ...interface{}) {
log.Print(v...)
}
func (conn *Conn) logf(level LogLevel, format string, v ...interface{}) {
log.Printf(format, v...)
}
func (conn *Conn) logError(level LogLevel, err error) {
if conn.LogLevel >= level {
conn.log(level, err)
}
}
func (conn *Conn) logEnter(funcName string) string {
conn.log(LogDebug, "entering: ", "pgsql."+funcName)
return funcName
}
func (conn *Conn) logExit(funcName string) {
conn.log(LogDebug, "exiting: ", "pgsql."+funcName)
}
func (conn *Conn) logAndConvertPanic(x interface{}) (err error) {
buf := bytes.NewBuffer(nil)
buf.WriteString(fmt.Sprintf("Error: %v\nStack Trace:\n", x))
buf.WriteString("=======================================================\n")
i := 0
for {
pc, file, line, ok := runtime.Caller(i + 3)
if !ok {
break
}
if i > 0 {
buf.WriteString("-------------------------------------------------------\n")
}
fun := runtime.FuncForPC(pc)
name := fun.Name()
buf.WriteString(fmt.Sprintf("%s (%s, Line %d)\n", name, file, line))
i++
}
buf.WriteString("=======================================================\n")
if conn.LogLevel >= LogError {
conn.log(LogError, buf)
}
err, ok := x.(error)
if !ok {
err = errors.New(buf.String())
}
return
}