1
0
mirror of https://github.com/beego/bee.git synced 2024-11-25 09:50:55 +00:00
bee/util.go

173 lines
4.3 KiB
Go
Raw Normal View History

2013-09-03 17:23:58 +00:00
// Copyright 2013 bee authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
2013-07-06 07:30:57 +00:00
package main
2013-08-09 14:40:46 +00:00
import (
"fmt"
"os"
"path/filepath"
"runtime"
2013-10-30 23:39:44 +00:00
"strings"
"time"
2013-08-09 14:40:46 +00:00
)
2013-07-06 07:30:57 +00:00
// Go is a basic promise implementation: it wraps calls a function in a goroutine
// and returns a channel which will later return the function's return value.
func Go(f func() error) chan error {
ch := make(chan error)
go func() {
ch <- f()
}()
return ch
}
// if os.env DEBUG set, debug is on
func Debugf(format string, a ...interface{}) {
if os.Getenv("DEBUG") != "" {
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "<unknown>"
line = -1
} else {
file = filepath.Base(file)
}
fmt.Fprintf(os.Stderr, fmt.Sprintf("[debug] %s:%d %s\n", file, line, format), a...)
}
}
2013-10-30 23:39:44 +00:00
const (
Gray = uint8(iota + 90)
Red
Green
Yellow
Blue
Magenta
//NRed = uint8(31) // Normal
EndColor = "\033[0m"
TRAC = "TRAC"
ERRO = "ERRO"
WARN = "WARN"
SUCC = "SUCC"
2013-10-30 23:39:44 +00:00
)
// ColorLog colors log and print to stdout.
// See color rules in function 'ColorLogS'.
func ColorLog(format string, a ...interface{}) {
fmt.Print(ColorLogS(format, a...))
}
// ColorLogS colors log and return colored content.
// Log format: <level> <content [highlight][path]> [ error ].
// Level: TRAC -> blue; ERRO -> red; WARN -> Magenta; SUCC -> green; others -> default.
// Content: default; path: yellow; error -> red.
// Level has to be surrounded by "[" and "]".
// Highlights have to be surrounded by "# " and " #"(space), "#" will be deleted.
// Paths have to be surrounded by "( " and " )"(space).
// Errors have to be surrounded by "[ " and " ]"(space).
// Note: it hasn't support windows yet, contribute is welcome.
func ColorLogS(format string, a ...interface{}) string {
log := fmt.Sprintf(format, a...)
var clog string
if runtime.GOOS != "windows" {
// Level.
i := strings.Index(log, "]")
if log[0] == '[' && i > -1 {
clog += "[" + getColorLevel(log[1:i]) + "]"
}
log = log[i+1:]
// Error.
log = strings.Replace(log, "[ ", fmt.Sprintf("[\033[%dm", Red), -1)
log = strings.Replace(log, " ]", EndColor+"]", -1)
// Path.
log = strings.Replace(log, "( ", fmt.Sprintf("(\033[%dm", Yellow), -1)
log = strings.Replace(log, " )", EndColor+")", -1)
// Highlights.
log = strings.Replace(log, "# ", fmt.Sprintf("\033[%dm", Gray), -1)
log = strings.Replace(log, " #", EndColor, -1)
log = clog + log
} else {
// Level.
i := strings.Index(log, "]")
if log[0] == '[' && i > -1 {
clog += "[" + log[1:i] + "]"
}
log = log[i+1:]
// Error.
log = strings.Replace(log, "[ ", "[", -1)
log = strings.Replace(log, " ]", "]", -1)
// Path.
log = strings.Replace(log, "( ", "(", -1)
log = strings.Replace(log, " )", ")", -1)
// Highlights.
log = strings.Replace(log, "# ", "", -1)
log = strings.Replace(log, " #", "", -1)
log = clog + log
}
2013-10-30 23:54:53 +00:00
return strings.TrimPrefix(
time.Now().Format("2006-01-02 03:04:05 "), "20") + log
2013-10-30 23:39:44 +00:00
}
// getColorLevel returns colored level string by given level.
func getColorLevel(level string) string {
level = strings.ToUpper(level)
switch level {
case TRAC:
2013-10-30 23:39:44 +00:00
return fmt.Sprintf("\033[%dm%s\033[0m", Blue, level)
case ERRO:
2013-10-30 23:39:44 +00:00
return fmt.Sprintf("\033[%dm%s\033[0m", Red, level)
case WARN:
2013-10-30 23:39:44 +00:00
return fmt.Sprintf("\033[%dm%s\033[0m", Magenta, level)
case SUCC:
2013-10-30 23:39:44 +00:00
return fmt.Sprintf("\033[%dm%s\033[0m", Green, level)
default:
return level
}
return level
}
// IsExist returns whether a file or directory exists.
2013-11-04 03:16:15 +00:00
func isExist(path string) bool {
2013-10-30 23:39:44 +00:00
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
// GetGOPATHs returns all paths in GOPATH variable.
func GetGOPATHs() []string {
gopath := os.Getenv("GOPATH")
var paths []string
if runtime.GOOS == "windows" {
gopath = strings.Replace(gopath, "\\", "/", -1)
paths = strings.Split(gopath, ";")
} else {
paths = strings.Split(gopath, ":")
}
return paths
}