Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use one method of setup logging #83

Merged
merged 1 commit into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/acra_addzone/acra_addzone.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func main() {
output_dir := flag.String("output_dir", keystore.DEFAULT_KEY_DIR_SHORT, "Folder where will be saved generated zone keys")
fs_keystore := flag.Bool("fs", true, "Use filesystem key store")

cmd.SetLogLevel(cmd.LOG_VERBOSE)

err := cmd.Parse(DEFAULT_CONFIG_PATH)
if err != nil {
fmt.Printf("Error: %v\n", ErrorMessage("Can't parse args", err))
Expand Down
2 changes: 2 additions & 0 deletions cmd/acra_genkeys/acra_genkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func main() {
data_keys := flag.Bool("storage", false, "Create keypair for data encryption/decryption")
output_dir := flag.String("output", keystore.DEFAULT_KEY_DIR_SHORT, "Folder where will be saved keys")

cmd.SetLogLevel(cmd.LOG_VERBOSE)

err := cmd.Parse(DEFAULT_CONFIG_PATH)
if err != nil {
fmt.Printf("Error: %v\n", utils.ErrorMessage("Can't parse args", err))
Expand Down
2 changes: 2 additions & 0 deletions cmd/acra_genpoisonrecord/acra_genpoisonrecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func main() {
keys_dir := flag.String("keys_dir", keystore.DEFAULT_KEY_DIR_SHORT, "Folder from which will be loaded keys")
data_length := flag.Int("data_length", poison.DEFAULT_DATA_LENGTH, fmt.Sprintf("Length of random data for data block in acrastruct. -1 is random in range 1..%v", poison.MAX_DATA_LENGTH))

cmd.SetLogLevel(cmd.LOG_DISCARD)

err := cmd.Parse(DEFAULT_CONFIG_PATH)
if err != nil {
fmt.Printf("Error: %v\n", utils.ErrorMessage("Can't parse args", err))
Expand Down
3 changes: 3 additions & 0 deletions cmd/acra_rollback/acra_rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ func main() {
output_file := flag.String("output_file", "decrypted.sql", "File for store inserts queries")
execute := flag.Bool("execute", false, "Execute inserts")
escape_format := flag.Bool("escape", false, "Escape bytea format")

cmd.SetLogLevel(cmd.LOG_VERBOSE)

err := cmd.Parse(DEFAULT_CONFIG_PATH)
if err != nil {
fmt.Printf("Error: %v\n", utils.ErrorMessage("Can't parse args", err))
Expand Down
5 changes: 2 additions & 3 deletions cmd/acraproxy/acraproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
Expand Down Expand Up @@ -295,9 +294,9 @@ func main() {
}

if *verbose {
log.SetOutput(os.Stdout)
cmd.SetLogLevel(cmd.LOG_VERBOSE)
} else {
log.SetOutput(ioutil.Discard)
cmd.SetLogLevel(cmd.LOG_DISCARD)
}
if runtime.GOOS != "linux" {
*disable_user_check = true
Expand Down
25 changes: 3 additions & 22 deletions cmd/acraserver/acraserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
package main

import (
"bytes"
"flag"
"fmt"
"github.com/cossacklabs/acra/cmd"
"github.com/cossacklabs/acra/keystore"
"github.com/cossacklabs/acra/utils"
"io"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
Expand All @@ -30,22 +27,6 @@ import (

var DEFAULT_CONFIG_PATH = utils.GetConfigPathByName("acraserver")

var DEBUG_PREFIX = []byte("Debug: ")

type NotDebugWriter struct {
writer io.Writer
}

func NewNotDebugWriter(writer io.Writer) *NotDebugWriter {
return &NotDebugWriter{writer: writer}
}
func (wr *NotDebugWriter) Write(p []byte) (int, error) {
if bytes.Contains(p, DEBUG_PREFIX) {
return 0, nil
}
return wr.writer.Write(p)
}

func main() {
db_host := flag.String("db_host", "", "Host to db")
db_port := flag.Int("db_port", 5432, "Port to db")
Expand Down Expand Up @@ -81,11 +62,11 @@ func main() {
}

if *debug {
log.SetOutput(os.Stdout)
cmd.SetLogLevel(cmd.LOG_DEBUG)
} else if *verbose {
log.SetOutput(NewNotDebugWriter(os.Stdout))
cmd.SetLogLevel(cmd.LOG_VERBOSE)
} else {
log.SetOutput(ioutil.Discard)
cmd.SetLogLevel(cmd.LOG_DISCARD)
}
if *db_host == "" {
fmt.Println("Error: you must specify db_host")
Expand Down
36 changes: 36 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cmd

import (
"bytes"
flag_ "flag"
"fmt"
"github.com/cossacklabs/acra/utils"
"github.com/go-yaml/yaml"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
Expand All @@ -17,6 +19,12 @@ var (
dumpconfig = flag_.Bool("dumpconfig", false, "dump config")
)

const (
LOG_DEBUG = iota
LOG_VERBOSE
LOG_DISCARD
)

func init() {
// override default usage message by ours
flag_.CommandLine.Usage = PrintDefaults
Expand Down Expand Up @@ -186,3 +194,31 @@ func Parse(config_path string) error {
}
return nil
}

var DEBUG_PREFIX = []byte("Debug: ")

type NotDebugWriter struct {
writer io.Writer
}

func NewNotDebugWriter(writer io.Writer) *NotDebugWriter {
return &NotDebugWriter{writer: writer}
}
func (wr *NotDebugWriter) Write(p []byte) (int, error) {
if bytes.Contains(p, DEBUG_PREFIX) {
return 0, nil
}
return wr.writer.Write(p)
}

func SetLogLevel(level int) {
if level == LOG_DEBUG {
log.SetOutput(os.Stdout)
} else if level == LOG_VERBOSE {
log.SetOutput(NewNotDebugWriter(os.Stdout))
} else if level == LOG_DISCARD {
log.SetOutput(ioutil.Discard)
} else {
panic(fmt.Sprintf("Incorrect log level - %v", level))
}
}