Skip to content

Commit

Permalink
using log.Printf() instead of fmt.Printf()
Browse files Browse the repository at this point in the history
  • Loading branch information
stremovsky committed Aug 6, 2024
1 parent f9fb82e commit 7cf2f3e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 41 deletions.
7 changes: 3 additions & 4 deletions src/agreements_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"
"net/http"
"reflect"

Expand Down Expand Up @@ -103,7 +104,7 @@ func (e mainEnv) agreementAccept(w http.ResponseWriter, r *http.Request, ps http
case "phone":
identity = normalizePhone(identity, e.conf.Sms.DefaultCountry)
}
fmt.Printf("Processing agreement, status: %s\n", status)
log.Printf("Processing agreement, status: %s\n", status)
e.db.acceptAgreement(userTOKEN, mode, identity, brief, status, agreementmethod,
referencecode, lastmodifiedby, starttime, expiration)
/*
Expand Down Expand Up @@ -319,7 +320,6 @@ func (e mainEnv) getUserAgreements(w http.ResponseWriter, r *http.Request, ps ht
returnError(w, r, "internal error", 405, err, event)
return
}
//fmt.Printf("Total count of rows: %d\n", numRecords)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
str := fmt.Sprintf(`{"status":"ok","total":%d,"rows":%s}`, numRecords, resultJSON)
Expand Down Expand Up @@ -395,7 +395,6 @@ func (e mainEnv) getUserAgreement(w http.ResponseWriter, r *http.Request, ps htt
returnError(w, r, "not found", 405, err, event)
return
}
//fmt.Printf("Total count of rows: %d\n", numRecords)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
str := fmt.Sprintf(`{"status":"ok","data":%s}`, resultJSON)
Expand Down Expand Up @@ -479,7 +478,7 @@ func (e mainEnv) consentFilterRecords(w http.ResponseWriter, r *http.Request, ps
returnError(w, r, "internal error", 405, err, event)
return
}
fmt.Printf("Total count of rows: %d\n", numRecords)
log.Printf("Total count of rows: %d\n", numRecords)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
str := fmt.Sprintf(`{"status":"ok","total":%d,"rows":%s}`, numRecords, resultJSON)
Expand Down
1 change: 0 additions & 1 deletion src/agreements_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"encoding/json"
//"errors"
"fmt"
"time"

Expand Down
7 changes: 4 additions & 3 deletions src/autocontext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package autocontext
import (
"errors"
"fmt"
"log"
"net/http"
"regexp"
"runtime"
Expand Down Expand Up @@ -68,13 +69,13 @@ func getRequestAddress() (string, error) {
//fmt.Printf("Stack of %d bytes: %s\n", count, trace)
match := regexServeHTTP.FindStringSubmatch(string(trace[0:count]))
if len(match) != 2 {
fmt.Println("Autocontext: regex not found in stack")
log.Println("Autocontext: regex not found in stack")
//fmt.Printf("*** STACK ***\n%s\n", trace)
return "", errors.New("Failed to find *http.Request address")
}
if count > 3072 {
fmt.Println("Autocontext: stack frame too large, check for bugs")
fmt.Printf("*** STACK ***\n%s\n", trace)
log.Println("Autocontext: stack frame too large, check for bugs")
log.Printf("*** STACK ***\n%s\n", trace)
}
//fmt.Printf("*** extracted address from stacktrace: %s\n", match[1])
return match[1], nil
Expand Down
33 changes: 12 additions & 21 deletions src/bunker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func helpServe(request *http.Request) (map[string]interface{}, error) {
request.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
router.ServeHTTP(rr, request)
fmt.Printf("[%d] %s%s\n", rr.Code, request.Host, request.URL.Path)
fmt.Printf("Response: %s\n", rr.Body.Bytes())
log.Printf("[%d] %s%s %s\n", rr.Code, request.Host, request.URL.Path, rr.Body.Bytes())
var raw map[string]interface{}
if rr.Body.Bytes()[0] == '{' {
json.Unmarshal(rr.Body.Bytes(), &raw)
Expand All @@ -52,8 +51,7 @@ func helpServe2(request *http.Request) (map[string]interface{}, error) {
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rr := httptest.NewRecorder()
router.ServeHTTP(rr, request)
fmt.Printf("[%d] %s%s\n", rr.Code, request.Host, request.URL.Path)
fmt.Printf("Response: %s\n", rr.Body.Bytes())
log.Printf("[%d] %s%s %s\n", rr.Code, request.Host, request.URL.Path, rr.Body.Bytes())
var raw map[string]interface{}
if rr.Body.Bytes()[0] == '{' {
json.Unmarshal(rr.Body.Bytes(), &raw)
Expand Down Expand Up @@ -86,12 +84,11 @@ func helpConfigurationDump(token string) ([]byte, error) {
}

func init() {
fmt.Printf("**INIT*TEST*CODE***\n")
log.Printf("**INIT*TEST*CODE***\n")
testDBFile := storage.CreateTestDB()
db, myRootToken, err := setupDB(&testDBFile, nil, "")
if err != nil {
//log.Panic("error %s", err.Error())
fmt.Printf("error %s", err.Error())
log.Printf("Init error %s", err.Error())
}
rootToken = myRootToken
var cfg Config
Expand All @@ -111,23 +108,22 @@ func init() {
e = mainEnv{db, cfg, make(chan struct{})}
rootToken2, err := e.db.getRootXtoken()
if err != nil {
fmt.Printf("Failed to retrieve root token: %s\n", err)
log.Printf("Failed to retrieve root token: %s\n", err)
}
fmt.Printf("Hashed root token: %s\n", rootToken2)
log.Printf("Hashed root token: %s\n", rootToken2)
router = e.setupRouter()
router = e.setupConfRouter(router)
//test1 := &testEnv{e, rootToken, router}
e.dbCleanupDo()
fmt.Printf("**INIT*DONE***\n")
log.Printf("**INIT*DONE***\n")
}

/*
func TestBackupOK(t *testing.T) {
fmt.Printf("root token: %s\n", rootToken)
log.Printf("root token: %s\n", rootToken)
raw, err := helpBackupRequest(rootToken)
if err != nil {
//log.Panic("error %s", err.Error())
log.Fatalf("failed to backup db %s", err.Error())
log.Fatalf("Failed to backup db %s", err.Error())
}
if strings.Contains(string(raw), "CREATE TABLE") == false {
t.Fatalf("Backup failed\n")
Expand All @@ -138,8 +134,7 @@ func TestBackupOK(t *testing.T) {
func TestMetrics(t *testing.T) {
raw, err := helpMetricsRequest(rootToken)
if err != nil {
//log.Panic("error %s", err.Error())
log.Fatalf("failed to get metrics %s", err.Error())
log.Fatalf("Failed to get metrics %s", err.Error())
}
if strings.Contains(string(raw), "go_memstats") == false {
t.Fatalf("metrics failed\n")
Expand All @@ -161,21 +156,17 @@ func TestAnonPage(t *testing.T) {
pattern := value["pattern"].(string)
request := httptest.NewRequest("GET", url, nil)
raw, _ := helpServe0(request)
//if err != nil {
// log.Fatalf("failed to get page %s", err.Error())
//}
if strings.Contains(string(raw), pattern) == false {
t.Fatalf("pattern detection failed\n")
}
}
}

func TestConfigurationOK(t *testing.T) {
fmt.Printf("root token: %s\n", rootToken)
log.Printf("Generated root token: %s\n", rootToken)
raw, err := helpConfigurationDump(rootToken)
if err != nil {
//log.Panic("error %s", err.Error())
log.Fatalf("failed to fetch configuration: %s", err.Error())
log.Fatalf("Failed to fetch configuration: %s", err.Error())
}
if strings.Contains(string(raw), "CreateUserWithoutAccessToken") == false {
t.Fatalf("Configuration dump failed\n")
Expand Down
3 changes: 2 additions & 1 deletion src/pactivities_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"
"net/http"
"reflect"

Expand Down Expand Up @@ -158,7 +159,7 @@ func (e mainEnv) pactivityList(w http.ResponseWriter, r *http.Request, ps httpro
returnError(w, r, "internal error", 405, err, nil)
return
}
fmt.Printf("Total count of rows: %d\n", numRecords)
log.Printf("Total count of rows: %d\n", numRecords)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
str := fmt.Sprintf(`{"status":"ok","total":%d,"rows":%s}`, numRecords, resultJSON)
Expand Down
2 changes: 1 addition & 1 deletion src/storage/sqlite-storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ func (dbobj SQLiteDB) CleanupRecord(t Tbl, keyName string, keyValue string, bdel
tbl := GetTable(t)
cleanup := dbobj.decodeForCleanup(bdel)
q := "update " + tbl + " SET " + cleanup + " WHERE " + dbobj.escapeName(keyName) + "=$1"
log.Printf("CleanupRecord q: %s\n", q)
log.Printf("q: %s\n", q)

tx, err := dbobj.db.Begin()
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions src/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -98,7 +99,7 @@ func TestUtilSMS(t *testing.T) {
rw.WriteHeader(200)
defer req.Body.Close()
bodyBytes, _ := ioutil.ReadAll(req.Body)
fmt.Printf("body: %s\n", string(bodyBytes))
log.Printf("body: %s\n", string(bodyBytes))
if string(bodyBytes) != "Body=Data+Bunker+code+1234&From=from1234&To=4444" {
t.Fatalf("bad request: %s", string(bodyBytes))
}
Expand All @@ -118,7 +119,7 @@ func TestUtilNotifyConsentChange(t *testing.T) {
rw.WriteHeader(200)
defer req.Body.Close()
bodyBytes, _ := ioutil.ReadAll(req.Body)
fmt.Printf("body: %s\n", string(bodyBytes))
log.Printf("body: %s\n", string(bodyBytes))
if string(bodyBytes) != `{"action":"consentchange","brief":"brief","identity":"[email protected]","mode":"email","status":"no"}` {
q <- fmt.Sprintf("bad request in notifyConsentChange: %s", string(bodyBytes))
} else {
Expand All @@ -141,7 +142,7 @@ func TestUtilNotifyProfileNew(t *testing.T) {
rw.WriteHeader(200)
defer req.Body.Close()
bodyBytes, _ := ioutil.ReadAll(req.Body)
fmt.Printf("body: %s\n", string(bodyBytes))
log.Printf("body: %s\n", string(bodyBytes))
if string(bodyBytes) != `{"action":"profilenew","identity":"[email protected]","mode":"email","profile":{"name":"alex"}}` {
q <- fmt.Sprintf("bad request in notifyConsentChange: %s", string(bodyBytes))
} else {
Expand All @@ -165,7 +166,7 @@ func TestUtilNotifyForgetMe(t *testing.T) {
rw.WriteHeader(200)
defer req.Body.Close()
bodyBytes, _ := ioutil.ReadAll(req.Body)
fmt.Printf("body: %s\n", string(bodyBytes))
log.Printf("body: %s\n", string(bodyBytes))
if string(bodyBytes) != `{"action":"forgetme","identity":"[email protected]","mode":"email","profile":{"name":"alex"}}` {
q <- fmt.Sprintf("bad request in notifyConsentChange: %s", string(bodyBytes))
} else {
Expand All @@ -189,7 +190,7 @@ func TestUtilNotifyProfileChange(t *testing.T) {
rw.WriteHeader(200)
defer req.Body.Close()
bodyBytes, _ := ioutil.ReadAll(req.Body)
fmt.Printf("body: %s\n", string(bodyBytes))
log.Printf("body: %s\n", string(bodyBytes))
if string(bodyBytes) != `{"action":"profilechange","identity":"[email protected]","mode":"email","old":{"name":"alex2"},"profile":{"name":"alex3"}}` {
q <- fmt.Sprintf("bad request in notifyConsentChange: %s", string(bodyBytes))
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/xtokens_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"fmt"
"log"
"net/http/httptest"
"strings"
"testing"
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestUserLoginDelete(t *testing.T) {
t.Fatalf("Failed to create user login: %s", raw["message"].(string))
}
xtoken := raw["xtoken"].(string)
fmt.Printf("User login *** xtoken: %s\n", xtoken)
log.Printf("User login *** xtoken: %s\n", xtoken)
oldRootToken := rootToken
rootToken = xtoken
raw, _ = helpAcceptAgreement("token", userTOKEN, "contract1", "")
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestUserLoginDelete(t *testing.T) {
if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" {
t.Fatalf("Failed to get user app list with user xtoken\n")
}
fmt.Printf("apps: %s\n", raw["apps"])
log.Printf("apps: %s\n", raw["apps"])
// user asks to forget-me
raw, _ = helpDeleteUser("token", userTOKEN)
if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" {
Expand All @@ -141,7 +141,7 @@ func TestUserLoginDelete(t *testing.T) {
}
rtoken0 := raw["rtoken"].(string)
raw, _ = helpGetUserAppList(userTOKEN)
fmt.Printf("apps: %s\n", raw["apps"])
log.Printf("apps: %s\n", raw["apps"])

rootToken = oldRootToken
// get user requests
Expand All @@ -161,7 +161,7 @@ func TestUserLoginDelete(t *testing.T) {
if rtoken != rtoken0 {
t.Fatalf("Rtoken0 is wrong\n")
}
fmt.Printf("** User request record: %s\n", rtoken)
log.Printf("** User request record: %s\n", rtoken)
}
raw8, _ := helpGetUserRequest(rtoken)
if raw8["status"].(string) != "ok" {
Expand Down

0 comments on commit 7cf2f3e

Please sign in to comment.