You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/11/13 17:24:44 UTC

[GitHub] ccollins476ad closed pull request #110: log: Display CBOR/binary log entries as text

ccollins476ad closed pull request #110: log: Display CBOR/binary log entries as text
URL: https://github.com/apache/mynewt-newtmgr/pull/110
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/newtmgr/cli/log.go b/newtmgr/cli/log.go
index 930fa85d..1977c8b4 100644
--- a/newtmgr/cli/log.go
+++ b/newtmgr/cli/log.go
@@ -20,6 +20,8 @@
 package cli
 
 import (
+	"encoding/hex"
+	"encoding/json"
 	"fmt"
 	"sort"
 	"strconv"
@@ -29,9 +31,25 @@ import (
 	"mynewt.apache.org/newt/util"
 	"mynewt.apache.org/newtmgr/newtmgr/nmutil"
 	"mynewt.apache.org/newtmgr/nmxact/nmp"
+	"mynewt.apache.org/newtmgr/nmxact/nmxutil"
 	"mynewt.apache.org/newtmgr/nmxact/xact"
 )
 
+// Converts the provided CBOR map to a JSON string.
+func logCborMsgText(cborMap []byte) (string, error) {
+	cm, err := nmxutil.DecodeCborMap(cborMap)
+	if err != nil {
+		return "", err
+	}
+
+	msg, err := json.Marshal(cm)
+	if err != nil {
+		return "", util.ChildNewtError(err)
+	}
+
+	return string(msg), nil
+}
+
 func logShowCmd(cmd *cobra.Command, args []string) {
 	c := xact.NewLogShowCmd()
 	c.SetTxOptions(nmutil.TxOptions())
@@ -89,16 +107,45 @@ func logShowCmd(cmd *cobra.Command, args []string) {
 			return
 		}
 
-		fmt.Printf("%10s %22s | %11s %11s %6s %s\n",
-			"[index]", "[timestamp]", "[module]", "[level]", "[type]", "[message]")
+		fmt.Printf("%10s %22s | %16s %16s %6s %s\n",
+			"[index]", "[timestamp]", "[module]", "[level]", "[type]",
+			"[message]")
 		for _, entry := range log.Entries {
-			fmt.Printf("%10d %20dus | %11s %11s %6s %s\n",
+			modText := fmt.Sprintf("%s (%d)",
+				nmp.LogModuleToString(int(entry.Module)), entry.Module)
+			levText := fmt.Sprintf("%s (%d)",
+				nmp.LogLevelToString(int(entry.Level)), entry.Level)
+
+			msgText := ""
+			switch entry.Type {
+			case nmp.LOG_ENTRY_TYPE_STRING:
+				msgText = string(entry.Msg)
+			case nmp.LOG_ENTRY_TYPE_CBOR:
+				msgText, err = logCborMsgText(entry.Msg)
+				if err != nil {
+					fmt.Printf("Error decoding CBOR entry: %s; "+
+						"idx=%d",
+						err.Error(), entry.Index)
+					msgText = hex.EncodeToString(entry.Msg)
+				}
+
+			case nmp.LOG_ENTRY_TYPE_BINARY:
+				msgText = hex.EncodeToString(entry.Msg)
+
+			default:
+				fmt.Printf(
+					"Error decoding entry: unknown entry type (%d); idx=%d",
+					int(entry.Type), entry.Index)
+				msgText = hex.EncodeToString(entry.Msg)
+			}
+			fmt.Printf("%10d %20dus | %16s %16s %6s %s\n",
 				entry.Index,
 				entry.Timestamp,
-				nmp.LogModuleToString(int(entry.Module)),
-				nmp.LogLevelToString(int(entry.Level)),
+				modText,
+				levText,
 				entry.Type,
-				entry.Msg)
+				msgText)
+
 		}
 	}
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services