Skip to content

Commit

Permalink
logging: disable unless DEBUG environmental variable is not empty (re…
Browse files Browse the repository at this point in the history
…solves namecoin#23)
  • Loading branch information
aerth committed Jul 23, 2019
1 parent 936f6f9 commit 9cd2a88
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,39 @@ package main
import "C"

import (
"io"
"unsafe"
"io/ioutil"
"log"
"os"
"path/filepath"
"unsafe"
)

var logfile io.Closer
var backend Backend

var debug = os.Getenv("DEBUG") != ""

func init() {
f, err := os.OpenFile(os.Getenv("HOME")+"/testlogfile", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
log.Fatalf("error opening file: %v", err)
log.SetOutput(ioutil.Discard)
if debug {
f, err := os.OpenFile(filepath.Join(os.Getenv("HOME"), "ncp11.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
log.Printf("error opening file: %v", err)
return
}
log.SetOutput(f)
log.Println("Namecoin PKCS#11 module loading")
}
log.SetOutput(f)
logfile = f
log.Println("Namecoin PKCS#11 module loading")
}

func init() {
backend = BackendNamecoin{}
}

//export GoLog
func GoLog(s unsafe.Pointer) {
if debug {
return
}
log.Println(C.GoString((*C.char)(s)))
}

Expand All @@ -77,8 +87,8 @@ func Go_GetInfo(p C.CK_INFO_PTR) C.CK_RV {
// TODO: Packing on Windows may break in this function. Should we be
// using C.ckInfo here?

if (p == nil) {
return C.CKR_ARGUMENTS_BAD;
if p == nil {
return C.CKR_ARGUMENTS_BAD
}

info, err := backend.GetInfo()
Expand Down

0 comments on commit 9cd2a88

Please sign in to comment.