1// Discordgo - Discord bindings for Go
2// Available at https://github.com/bwmarrin/discordgo
3
4// Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>.  All rights reserved.
5// Use of this source code is governed by a BSD-style
6// license that can be found in the LICENSE file.
7
8// This file contains code related to discordgo package logging
9
10package discordgo
11
12import (
13	"fmt"
14	"log"
15	"runtime"
16	"strings"
17)
18
19const (
20
21	// LogError level is used for critical errors that could lead to data loss
22	// or panic that would not be returned to a calling function.
23	LogError int = iota
24
25	// LogWarning level is used for very abnormal events and errors that are
26	// also returned to a calling function.
27	LogWarning
28
29	// LogInformational level is used for normal non-error activity
30	LogInformational
31
32	// LogDebug level is for very detailed non-error activity.  This is
33	// very spammy and will impact performance.
34	LogDebug
35)
36
37// Logger can be used to replace the standard logging for discordgo
38var Logger func(msgL, caller int, format string, a ...interface{})
39
40// msglog provides package wide logging consistency for discordgo
41// the format, a...  portion this command follows that of fmt.Printf
42//   msgL   : LogLevel of the message
43//   caller : 1 + the number of callers away from the message source
44//   format : Printf style message format
45//   a ...  : comma separated list of values to pass
46func msglog(msgL, caller int, format string, a ...interface{}) {
47
48	if Logger != nil {
49		Logger(msgL, caller, format, a...)
50	} else {
51
52		pc, file, line, _ := runtime.Caller(caller)
53
54		files := strings.Split(file, "/")
55		file = files[len(files)-1]
56
57		name := runtime.FuncForPC(pc).Name()
58		fns := strings.Split(name, ".")
59		name = fns[len(fns)-1]
60
61		msg := fmt.Sprintf(format, a...)
62
63		log.Printf("[DG%d] %s:%d:%s() %s\n", msgL, file, line, name, msg)
64	}
65}
66
67// helper function that wraps msglog for the Session struct
68// This adds a check to insure the message is only logged
69// if the session log level is equal or higher than the
70// message log level
71func (s *Session) log(msgL int, format string, a ...interface{}) {
72
73	if msgL > s.LogLevel {
74		return
75	}
76
77	msglog(msgL, 2, format, a...)
78}
79
80// helper function that wraps msglog for the VoiceConnection struct
81// This adds a check to insure the message is only logged
82// if the voice connection log level is equal or higher than the
83// message log level
84func (v *VoiceConnection) log(msgL int, format string, a ...interface{}) {
85
86	if msgL > v.LogLevel {
87		return
88	}
89
90	msglog(msgL, 2, format, a...)
91}
92
93// printJSON is a helper function to display JSON data in a easy to read format.
94/* NOT USED ATM
95func printJSON(body []byte) {
96	var prettyJSON bytes.Buffer
97	error := json.Indent(&prettyJSON, body, "", "\t")
98	if error != nil {
99		log.Print("JSON parse error: ", error)
100	}
101	log.Println(string(prettyJSON.Bytes()))
102}
103*/
104