logger.go 1.68 KB
Newer Older
Lukas Matt's avatar
Lukas Matt committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
package federation
//
// GangGo Diaspora Federation Library
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import (
  "fmt"
  "runtime"
23 24
  "os"
  "log"
Lukas Matt's avatar
Lukas Matt committed
25 26
)

27
const (
Lukas Matt's avatar
Lukas Matt committed
28
  LOG_C_TUR = "\033[0;36m"
29 30 31 32 33 34
  LOG_C_RED = "\033[31m"
  LOG_C_YELLOW = "\033[33m"
  LOG_C_RESET = "\033[0m"
)

var (
35
  logger Logger
36 37 38 39
  defaultPrefix string
)

func init() {
Lukas Matt's avatar
Lukas Matt committed
40
  pc := make([]uintptr, 1)
Lukas Matt's avatar
Lukas Matt committed
41 42 43 44
  runtime.Callers(3, pc)
  f := runtime.FuncForPC(pc[0])
  file, line := f.FileLine(pc[0])

45
  defaultPrefix = fmt.Sprintf("%s:%d %s ", file, line, f.Name())
Lukas Matt's avatar
Lukas Matt committed
46 47 48 49
  logger = Logger{
    log.New(os.Stdout, defaultPrefix, log.Lshortfile),
    LOG_C_TUR,
  }
50 51
}

52
type LogWriter interface {
53 54
  Println(v... interface{})
}
Lukas Matt's avatar
Lukas Matt committed
55

56 57
type Logger struct{
  LogWriter
Lukas Matt's avatar
Lukas Matt committed
58 59

  Prefix string
60 61
}

62
func SetLogger(writer LogWriter) {
Lukas Matt's avatar
Lukas Matt committed
63
  logger = Logger{writer, LOG_C_TUR}
Lukas Matt's avatar
Lukas Matt committed
64 65
}

66
func (l Logger) Info(values... interface{}) {
67
  l.Println(l.Prefix, values, LOG_C_RESET)
Lukas Matt's avatar
Lukas Matt committed
68 69
}

70
func (l Logger) Error(values... interface{}) {
Lukas Matt's avatar
Lukas Matt committed
71 72
  l.Prefix = LOG_C_RED
  l.Info(values)
Lukas Matt's avatar
Lukas Matt committed
73 74
}

75
func (l Logger) Warn(values... interface{}) {
Lukas Matt's avatar
Lukas Matt committed
76 77
  l.Prefix = LOG_C_YELLOW
  l.Info(values)
Lukas Matt's avatar
Lukas Matt committed
78
}