adds: [main] log files + no output on stdout unless -v flag

d4forward
Jean-Louis Huynen 2020-04-23 16:19:38 +02:00
parent 2f6da40367
commit 2d48e196f5
No known key found for this signature in database
GPG Key ID: 64799157F4BD6B93
1 changed files with 25 additions and 19 deletions

View File

@ -95,11 +95,12 @@ type (
) )
var ( var (
// verbose // Verbose mode and logging
buf bytes.Buffer buf bytes.Buffer
logger = log.New(&buf, "INFO: ", log.Lshortfile) logger = log.New(&buf, "INFO: ", log.Lshortfile)
infof = func(info string) { debugger = log.New(&buf, "DEBUG: ", log.Lmicroseconds)
logger.Output(2, info) debugf = func(debug string) {
debugger.Println("", debug)
} }
tmpct, _ = time.ParseDuration("5mn") tmpct, _ = time.ParseDuration("5mn")
@ -108,7 +109,7 @@ var (
tmprate, _ = time.ParseDuration("200ms") tmprate, _ = time.ParseDuration("200ms")
confdir = flag.String("c", "", "configuration directory") confdir = flag.String("c", "", "configuration directory")
debug = flag.Bool("v", false, "Set to True, true, TRUE, 1, or t to enable verbose output on stdout") debug = flag.Bool("v", false, "Set to True, true, TRUE, 1, or t to enable verbose output on stdout - Don't use in production")
ce = flag.Bool("ce", true, "Set to True, true, TRUE, 1, or t to enable TLS on network destination") ce = flag.Bool("ce", true, "Set to True, true, TRUE, 1, or t to enable TLS on network destination")
ct = flag.Duration("ct", tmpct, "Set timeout in human format") ct = flag.Duration("ct", tmpct, "Set timeout in human format")
cka = flag.Duration("cka", tmpcka, "Keep Alive time human format, 0 to disable") cka = flag.Duration("cka", tmpcka, "Keep Alive time human format, 0 to disable")
@ -122,6 +123,15 @@ func main() {
var d4 d4S var d4 d4S
d4p := &d4 d4p := &d4
// Setting up log file
f, err := os.OpenFile("d4-goclient.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
logger.SetOutput(f)
logger.Println("Init")
flag.Usage = func() { flag.Usage = func() {
fmt.Printf("d4 - d4 client\n") fmt.Printf("d4 - d4 client\n")
fmt.Printf("Read data from the configured <source> and send it to <destination>\n") fmt.Printf("Read data from the configured <source> and send it to <destination>\n")
@ -182,18 +192,16 @@ func main() {
if err != nil { if err != nil {
panic(fmt.Sprintf("Cannot initiate session %s", err)) panic(fmt.Sprintf("Cannot initiate session %s", err))
} }
infof(fmt.Sprintf("Meta-Header sent: %d bytes", nread)) logger.Println(fmt.Sprintf("Meta-Header sent: %d bytes", nread))
d4p.dst.restoreHeader() d4p.dst.restoreHeader()
} }
// copy routine // copy routine
go d4Copy(d4p, c) go d4Copy(d4p, c)
log.Printf("---d4Copy")
// Block until the rate limiter allow us to continue // Block until the rate limiter allow us to continue
<-ratelimiter <-ratelimiter
} else if d4.retry > 0 { } else if d4.retry > 0 {
go func() { go func() {
infof(fmt.Sprintf("Sleeping for %.f seconds before retry...\n", d4.retry.Seconds())) logger.Println(fmt.Sprintf("Sleeping for %.f seconds before retry...", d4.retry.Seconds()))
fmt.Printf("Sleeping for %.f seconds before retry...\n", d4.retry.Seconds())
time.Sleep(d4.retry) time.Sleep(d4.retry)
c <- "done waiting" c <- "done waiting"
}() }()
@ -203,19 +211,17 @@ func main() {
// Block until we catch an event // Block until we catch an event
select { select {
case str := <-c: case <-c:
infof(str)
//log.Printf("Channel c: %q\n", str)
continue continue
case <-s: case <-s:
fmt.Println(" Exiting") logger.Println("Exiting")
exit(d4p, 0) exit(d4p, 0)
} }
} }
} }
func exit(d4 *d4S, exitcode int) { func exit(d4 *d4S, exitcode int) {
// Output logging before closing if debug is enabled // Output debug info in the log before closing if debug is enabled
if *debug == true { if *debug == true {
(*d4).debug = true (*d4).debug = true
fmt.Print(&buf) fmt.Print(&buf)
@ -333,7 +339,7 @@ func d4loadConfig(d4 *d4S) bool {
panic(fmt.Sprintf("Cannot read Meta-Header file: %s", err)) panic(fmt.Sprintf("Cannot read Meta-Header file: %s", err))
} else { } else {
if err := json.Compact((*d4).mhb, data[:count]); err != nil { if err := json.Compact((*d4).mhb, data[:count]); err != nil {
infof("Failed to compact meta header file") logger.Println("Failed to compact meta header file")
} }
} }
} else { } else {
@ -395,7 +401,7 @@ func setReaderWriters(d4 *d4S) bool {
var err error var err error
(*d4).redisCon, err = (*d4).redisInputPool.Dial() (*d4).redisCon, err = (*d4).redisInputPool.Dial()
if err != nil { if err != nil {
log.Println("Could not connect to d4 Redis") logger.Println("Could not connect to d4 Redis")
return false return false
} }
(*d4).src, err = inputreader.NewLPOPReader(&(*d4).redisCon, (*d4).conf.redisDB, (*d4).conf.redisQueue, int(time.Second*(*d4).retry)) (*d4).src, err = inputreader.NewLPOPReader(&(*d4).redisCon, (*d4).conf.redisDB, (*d4).conf.redisQueue, int(time.Second*(*d4).retry))
@ -423,7 +429,7 @@ func setReaderWriters(d4 *d4S) bool {
if (*d4).ce == true { if (*d4).ce == true {
conn, errc := tls.DialWithDialer(&dial, "tcp", dstnet, &tlsc) conn, errc := tls.DialWithDialer(&dial, "tcp", dstnet, &tlsc)
if errc != nil { if errc != nil {
fmt.Println(errc) logger.Println(errc)
return false return false
} }
(*d4).dst = newD4Writer(conn, (*d4).conf.key) (*d4).dst = newD4Writer(conn, (*d4).conf.key)
@ -458,7 +464,7 @@ func generateUUIDv4() []byte {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
infof(fmt.Sprintf("UUIDv4: %s\n", uuid)) logger.Println(fmt.Sprintf("UUIDv4: %s\n", uuid))
return uuid.Bytes() return uuid.Bytes()
} }
@ -513,8 +519,8 @@ func (d4w *d4Writer) initHeader(d4 *d4S) bool {
// hmac is set to zero during hmac operations, so leave it alone // hmac is set to zero during hmac operations, so leave it alone
// init size of payload at 0 // init size of payload at 0
binary.LittleEndian.PutUint32(d4w.fb[58:62], uint32(0)) binary.LittleEndian.PutUint32(d4w.fb[58:62], uint32(0))
infof(fmt.Sprintf("Initialized a %d bytes header:\n", HDR_SIZE)) debugf(fmt.Sprintf("Initialized a %d bytes header:\n", HDR_SIZE))
infof(fmt.Sprintf("%b\n", d4w.fb[:HDR_SIZE])) debugf(fmt.Sprintf("%b\n", d4w.fb[:HDR_SIZE]))
return true return true
} }