added logLevel flag

This commit is contained in:
Jan Halfar 2013-09-12 23:40:59 +02:00
parent ad90df7fe2
commit 09e331075a
2 changed files with 43 additions and 7 deletions

18
main.go
View File

@ -6,6 +6,7 @@ import (
"github.com/foomo/ContentServer/server" "github.com/foomo/ContentServer/server"
"github.com/foomo/ContentServer/server/log" "github.com/foomo/ContentServer/server/log"
"os" "os"
"strings"
) )
const ( const (
@ -24,6 +25,19 @@ var contentServer string
var protocol = flag.String("protocol", PROTOCOL_TCP, "what protocol to server for") var protocol = flag.String("protocol", PROTOCOL_TCP, "what protocol to server for")
var address = flag.String("address", "127.0.0.1:8081", "address to bind host:port") var address = flag.String("address", "127.0.0.1:8081", "address to bind host:port")
var logLevelOptions = []string{
log.LOG_LEVEL_NAME_ERROR,
log.LOG_LEVEL_NAME_RECORD,
log.LOG_LEVEL_NAME_WARNING,
log.LOG_LEVEL_NAME_NOTICE,
log.LOG_LEVEL_NAME_DEBUG}
var logLevel = flag.String(
"logLevel",
log.LOG_LEVEL_NAME_RECORD,
fmt.Sprintf(
"one of %s",
strings.Join(logLevelOptions, ", ")))
func exitUsage(code int) { func exitUsage(code int) {
fmt.Printf("Usage: %s http(s)://your-content-server/path/to/content.json\n", os.Args[0]) fmt.Printf("Usage: %s http(s)://your-content-server/path/to/content.json\n", os.Args[0])
@ -33,15 +47,15 @@ func exitUsage(code int) {
func main() { func main() {
flag.Parse() flag.Parse()
log.SetLogLevel(log.LOG_LEVEL_DEBUG)
if len(flag.Args()) == 1 { if len(flag.Args()) == 1 {
fmt.Println(*protocol, *address, flag.Arg(0)) fmt.Println(*protocol, *address, flag.Arg(0))
//server.Run(":8080", "http://test.bestbytes/foomo/modules/Foomo.Page.Content/services/content.php") log.SetLogLevel(log.GetLogLevelByName(*logLevel))
switch *protocol { switch *protocol {
case PROTOCOL_TCP: case PROTOCOL_TCP:
server.RunSocketServer(flag.Arg(0), *address) server.RunSocketServer(flag.Arg(0), *address)
break break
case PROTOCOL_HTTP: case PROTOCOL_HTTP:
//server.Run(":8080", "http://test.bestbytes/foomo/modules/Foomo.Page.Content/services/content.php")
fmt.Println("http server does not work yet - use tcp instead") fmt.Println("http server does not work yet - use tcp instead")
break break
default: default:

View File

@ -11,10 +11,15 @@ var numErrors int64 = 0
const ( const (
LOG_LEVEL_ERROR = 0 LOG_LEVEL_ERROR = 0
LOG_LEVEL_NAME_ERROR = "error"
LOG_LEVEL_RECORD = 1 LOG_LEVEL_RECORD = 1
LOG_LEVEL_NAME_RECORD = "record"
LOG_LEVEL_WARNING = 2 LOG_LEVEL_WARNING = 2
LOG_LEVEL_NAME_WARNING = "warning"
LOG_LEVEL_NOTICE = 3 LOG_LEVEL_NOTICE = 3
LOG_LEVEL_NAME_NOTICE = "notice"
LOG_LEVEL_DEBUG = 4 LOG_LEVEL_DEBUG = 4
LOG_LEVEL_NAME_DEBUG = "debug"
) )
var prefices = map[int]string{ var prefices = map[int]string{
@ -25,6 +30,23 @@ var prefices = map[int]string{
LOG_LEVEL_DEBUG: "debug : ", LOG_LEVEL_DEBUG: "debug : ",
} }
var logLevelMap = map[string]int{
LOG_LEVEL_NAME_ERROR: LOG_LEVEL_ERROR,
LOG_LEVEL_NAME_RECORD: LOG_LEVEL_RECORD,
LOG_LEVEL_NAME_WARNING: LOG_LEVEL_WARNING,
LOG_LEVEL_NAME_NOTICE: LOG_LEVEL_NOTICE,
LOG_LEVEL_NAME_DEBUG: LOG_LEVEL_DEBUG,
}
func GetLogLevelByName(name string) int {
if level, ok := logLevelMap[name]; ok {
return level
} else {
return LOG_LEVEL_RECORD
}
}
func log(msg string, level int) string { func log(msg string, level int) string {
if level <= logLevel { if level <= logLevel {
prefix := time.Now().Format(time.RFC3339) + " " + prefices[level] prefix := time.Now().Format(time.RFC3339) + " " + prefices[level]