Skip to content

Commit

Permalink
Cleaning up log message formats
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Feb 15, 2024
1 parent 9142c93 commit 3c3f118
Show file tree
Hide file tree
Showing 32 changed files with 75 additions and 75 deletions.
17 changes: 9 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@ yq -P -oy sample.json
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cmd.SetOut(cmd.OutOrStdout())
level := logging.WARNING
stringFormat := `[%{level}] %{color}%{time:15:04:05}%{color:reset} %{message}`

var format = logging.MustStringFormatter(
`[%{level:5.5s}] %{color}%{time:15:04:05} %{color:bold} %{shortfile:-30s} %{shortfunc:-25s}%{color:reset} %{message}`,
)
if verbose {
level = logging.DEBUG
stringFormat = `[%{level:5.5s}] %{color}%{time:15:04:05}%{color:bold} %{shortfile:-33s} %{shortfunc:-25s}%{color:reset} %{message}`
}

var format = logging.MustStringFormatter(stringFormat)
var backend = logging.AddModuleLevel(
logging.NewBackendFormatter(logging.NewLogBackend(os.Stderr, "", 0), format))

if verbose {
backend.SetLevel(logging.DEBUG, "")
} else {
backend.SetLevel(logging.WARNING, "")
}
backend.SetLevel(level, "")

logging.SetBackend(backend)
yqlib.InitExpressionParser()
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/data_tree_navigator.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (d *dataTreeNavigator) DeeplyAssign(context Context, path []interface{}, rh
assignmentOp := &Operation{OperationType: assignOpType, Preferences: assignPreferences{}}

if rhsCandidateNode.Kind == MappingNode {
log.Debug("[DataTreeNavigator] DeeplyAssign: deeply merging object")
log.Debug("DeeplyAssign: deeply merging object")
// if the rhs is a map, we need to deeply merge it in.
// otherwise we'll clobber any existing fields
assignmentOp = &Operation{OperationType: multiplyAssignOpType, Preferences: multiplyPreferences{
Expand Down
27 changes: 13 additions & 14 deletions pkg/yqlib/decoder_toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (dec *tomlDecoder) getFullPath(tomlNode *toml.Node) []interface{} {
func (dec *tomlDecoder) processKeyValueIntoMap(rootMap *CandidateNode, tomlNode *toml.Node) error {
value := tomlNode.Value()
path := dec.getFullPath(value.Next())
log.Debug("[DecoderTOML] processKeyValueIntoMap: %v", path)
log.Debug("processKeyValueIntoMap: %v", path)

valueNode, err := dec.decodeNode(value)
if err != nil {
Expand All @@ -70,33 +70,32 @@ func (dec *tomlDecoder) processKeyValueIntoMap(rootMap *CandidateNode, tomlNode
}

func (dec *tomlDecoder) decodeKeyValuesIntoMap(rootMap *CandidateNode, tomlNode *toml.Node) (bool, error) {
log.Debug("[DecoderTOML] decodeKeyValuesIntoMap -- processing first (current) entry")
log.Debug("decodeKeyValuesIntoMap -- processing first (current) entry")
if err := dec.processKeyValueIntoMap(rootMap, tomlNode); err != nil {
return false, err
}

for dec.parser.NextExpression() {
nextItem := dec.parser.Expression()
log.Debug("[DecoderTOML] decodeKeyValuesIntoMap -- next exp, its a %v", nextItem.Kind)
log.Debug("decodeKeyValuesIntoMap -- next exp, its a %v", nextItem.Kind)

if nextItem.Kind == toml.KeyValue {
if err := dec.processKeyValueIntoMap(rootMap, nextItem); err != nil {
return false, err
}
} else {
// run out of key values
log.Debug("! DECODE_KV_INTO_MAP - ok we are done in decodeKeyValuesIntoMap, gota a %v", nextItem.Kind)
log.Debug("! DECODE_KV_INTO_MAP - processAgainstCurrentExp = true!")
log.Debug("done in decodeKeyValuesIntoMap, gota a %v", nextItem.Kind)
return true, nil
}
}
log.Debug("! DECODE_KV_INTO_MAP - no more things to read in")
log.Debug("no more things to read in")
return false, nil
}

func (dec *tomlDecoder) createInlineTableMap(tomlNode *toml.Node) (*CandidateNode, error) {
content := make([]*CandidateNode, 0)
log.Debug("!! createInlineTableMap")
log.Debug("createInlineTableMap")

iterator := tomlNode.Children()
for iterator.Next() {
Expand Down Expand Up @@ -249,7 +248,7 @@ func (dec *tomlDecoder) Decode() (*CandidateNode, error) {
func (dec *tomlDecoder) processTopLevelNode(currentNode *toml.Node) (bool, error) {
var runAgainstCurrentExp bool
var err error
log.Debug("[DecoderTOML] processTopLevelNode: Going to process %v state is current %v", currentNode.Kind, NodeToString(dec.rootMap))
log.Debug("processTopLevelNode: Going to process %v state is current %v", currentNode.Kind, NodeToString(dec.rootMap))
if currentNode.Kind == toml.Table {
runAgainstCurrentExp, err = dec.processTable(currentNode)
} else if currentNode.Kind == toml.ArrayTable {
Expand All @@ -258,14 +257,14 @@ func (dec *tomlDecoder) processTopLevelNode(currentNode *toml.Node) (bool, error
runAgainstCurrentExp, err = dec.decodeKeyValuesIntoMap(dec.rootMap, currentNode)
}

log.Debug("[DecoderTOML] processTopLevelNode: DONE Processing state is now %v", NodeToString(dec.rootMap))
log.Debug("processTopLevelNode: DONE Processing state is now %v", NodeToString(dec.rootMap))
return runAgainstCurrentExp, err
}

func (dec *tomlDecoder) processTable(currentNode *toml.Node) (bool, error) {
log.Debug("[DecoderTOML] Enter processTable")
log.Debug("Enter processTable")
fullPath := dec.getFullPath(currentNode.Child())
log.Debug("[DecoderTOML] fullpath: %v", fullPath)
log.Debug("fullpath: %v", fullPath)

tableNodeValue := &CandidateNode{
Kind: MappingNode,
Expand Down Expand Up @@ -302,7 +301,7 @@ func (dec *tomlDecoder) processTable(currentNode *toml.Node) (bool, error) {
}

func (dec *tomlDecoder) arrayAppend(context Context, path []interface{}, rhsNode *CandidateNode) error {
log.Debug("[DecoderTOML] arrayAppend to path: %v,%v", path, NodeToString(rhsNode))
log.Debug("arrayAppend to path: %v,%v", path, NodeToString(rhsNode))
rhsCandidateNode := &CandidateNode{
Kind: SequenceNode,
Tag: "!!seq",
Expand All @@ -324,9 +323,9 @@ func (dec *tomlDecoder) arrayAppend(context Context, path []interface{}, rhsNode
}

func (dec *tomlDecoder) processArrayTable(currentNode *toml.Node) (bool, error) {
log.Debug("[DecoderTOML] Entering processArrayTable")
log.Debug("Entering processArrayTable")
fullPath := dec.getFullPath(currentNode.Child())
log.Debug("[DecoderTOML] Fullpath: %v", fullPath)
log.Debug("Fullpath: %v", fullPath)

// need to use the array append exp to add another entry to
// this array: fullpath += [ thing ]
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/encoder_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (ye *yamlEncoder) CanHandleAliases() bool {

func (ye *yamlEncoder) PrintDocumentSeparator(writer io.Writer) error {
if ye.prefs.PrintDocSeparators {
log.Debug("-- writing doc sep")
log.Debug("writing doc sep")
if err := writeString(writer, "---\n"); err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/yqlib/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ func handleToken(tokens []*token, index int, postProcessedTokens []*token) (toke
//need to put a traverse array then a collect currentToken
// do this by adding traverse then converting currentToken to collect

log.Debug(" adding self")
log.Debug("adding self")
op := &Operation{OperationType: selfReferenceOpType, StringValue: "SELF"}
postProcessedTokens = append(postProcessedTokens, &token{TokenType: operationToken, Operation: op})

log.Debug(" adding traverse array")
log.Debug("adding traverse array")
op = &Operation{OperationType: traverseArrayOpType, StringValue: "TRAVERSE_ARRAY"}
postProcessedTokens = append(postProcessedTokens, &token{TokenType: operationToken, Operation: op})

Expand All @@ -135,13 +135,13 @@ func handleToken(tokens []*token, index int, postProcessedTokens []*token) (toke

if index != len(tokens)-1 && currentToken.AssignOperation != nil &&
tokenIsOpType(tokens[index+1], assignOpType) {
log.Debug(" its an update assign")
log.Debug("its an update assign")
currentToken.Operation = currentToken.AssignOperation
currentToken.Operation.UpdateAssign = tokens[index+1].Operation.UpdateAssign
skipNextToken = true
}

log.Debug(" adding token to the fixed list")
log.Debug("adding token to the fixed list")
postProcessedTokens = append(postProcessedTokens, currentToken)

if tokenIsOpType(currentToken, createMapOpType) {
Expand All @@ -158,7 +158,7 @@ func handleToken(tokens []*token, index int, postProcessedTokens []*token) (toke
if index != len(tokens)-1 &&
((currentToken.TokenType == openCollect && tokens[index+1].TokenType == closeCollect) ||
(currentToken.TokenType == openCollectObject && tokens[index+1].TokenType == closeCollectObject)) {
log.Debug(" adding empty")
log.Debug("adding empty")
op := &Operation{OperationType: emptyOpType, StringValue: "EMPTY"}
postProcessedTokens = append(postProcessedTokens, &token{TokenType: operationToken, Operation: op})
}
Expand All @@ -167,14 +167,14 @@ func handleToken(tokens []*token, index int, postProcessedTokens []*token) (toke

(tokenIsOpType(tokens[index+1], traversePathOpType) ||
(tokens[index+1].TokenType == traverseArrayCollect)) {
log.Debug(" adding pipe because the next thing is traverse")
log.Debug("adding pipe because the next thing is traverse")
op := &Operation{OperationType: shortPipeOpType, Value: "PIPE", StringValue: "."}
postProcessedTokens = append(postProcessedTokens, &token{TokenType: operationToken, Operation: op})
}
if index != len(tokens)-1 && currentToken.CheckForPostTraverse &&
tokens[index+1].TokenType == openCollect {

log.Debug(" adding traverseArray because next is opencollect")
log.Debug("adding traverseArray because next is opencollect")
op := &Operation{OperationType: traverseArrayOpType}
postProcessedTokens = append(postProcessedTokens, &token{TokenType: operationToken, Operation: op})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/operator_alternative.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package yqlib

func alternativeOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("-- alternative")
log.Debugf("alternative")
prefs := crossFunctionPreferences{
CalcWhenEmpty: true,
Calculation: alternativeFunc,
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/operator_anchors_aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func getAnchorOperator(_ *dataTreeNavigator, context Context, _ *ExpressionNode)
}

func explodeOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("-- ExplodeOperation")
log.Debugf("ExplodeOperation")

for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/operator_booleans.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func andOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi
}

func notOperator(_ *dataTreeNavigator, context Context, _ *ExpressionNode) (Context, error) {
log.Debugf("-- notOperation")
log.Debugf("notOperation")
var results = list.New()

for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/operator_collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func collectTogether(d *dataTreeNavigator, context Context, expressionNode *Expr
}

func collectOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("-- collectOperation")
log.Debugf("collectOperation")

if context.MatchingNodes.Len() == 0 {
log.Debugf("nothing to collect")
Expand Down
16 changes: 8 additions & 8 deletions pkg/yqlib/operator_collect_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (
*/

func collectObjectOperator(d *dataTreeNavigator, originalContext Context, _ *ExpressionNode) (Context, error) {
log.Debugf("-- collectObjectOperation")
log.Debugf("collectObjectOperation")

context := originalContext.WritableClone()

if context.MatchingNodes.Len() == 0 {
candidate := &CandidateNode{Kind: MappingNode, Tag: "!!map", Value: "{}"}
log.Debugf("-- collectObjectOperation - starting with empty map")
log.Debugf("collectObjectOperation - starting with empty map")
return context.SingleChildContext(candidate), nil
}
first := context.MatchingNodes.Front().Value.(*CandidateNode)
Expand All @@ -41,7 +41,7 @@ func collectObjectOperator(d *dataTreeNavigator, originalContext Context, _ *Exp
rotated[i].PushBack(candidateNode.Content[i])
}
}
log.Debugf("-- collectObjectOperation, length of rotated is %v", len(rotated))
log.Debugf("collectObjectOperation, length of rotated is %v", len(rotated))

newObject := list.New()
for i := 0; i < len(first.Content); i++ {
Expand All @@ -58,7 +58,7 @@ func collectObjectOperator(d *dataTreeNavigator, originalContext Context, _ *Exp
additionCopy.SetParent(nil)
additionCopy.Key = nil

log.Debugf("-- collectObjectOperation, adding result %v", NodeToString(additionCopy))
log.Debugf("collectObjectOperation, adding result %v", NodeToString(additionCopy))

newObject.PushBack(additionCopy)
}
Expand All @@ -74,7 +74,7 @@ func collect(d *dataTreeNavigator, context Context, remainingMatches *list.List)
}

candidate := remainingMatches.Remove(remainingMatches.Front()).(*CandidateNode)
log.Debugf("-- collectObjectOperation - collect %v", NodeToString(candidate))
log.Debugf("collectObjectOperation - collect %v", NodeToString(candidate))

splatted, err := splat(context.SingleChildContext(candidate),
traversePreferences{DontFollowAlias: true, IncludeMapKeys: false})
Expand All @@ -84,7 +84,7 @@ func collect(d *dataTreeNavigator, context Context, remainingMatches *list.List)
}

if context.MatchingNodes.Len() == 0 {
log.Debugf("-- collectObjectOperation - collect context is empty, next")
log.Debugf("collectObjectOperation - collect context is empty, next")
return collect(d, splatted, remainingMatches)
}

Expand All @@ -94,9 +94,9 @@ func collect(d *dataTreeNavigator, context Context, remainingMatches *list.List)
aggCandidate := el.Value.(*CandidateNode)
for splatEl := splatted.MatchingNodes.Front(); splatEl != nil; splatEl = splatEl.Next() {
splatCandidate := splatEl.Value.(*CandidateNode)
log.Debugf("-- collectObjectOperation; splatCandidate: %v", NodeToString(splatCandidate))
log.Debugf("collectObjectOperation; splatCandidate: %v", NodeToString(splatCandidate))
newCandidate := aggCandidate.Copy()
log.Debugf("-- collectObjectOperation; aggCandidate: %v", NodeToString(aggCandidate))
log.Debugf("collectObjectOperation; aggCandidate: %v", NodeToString(aggCandidate))

newCandidate, err = multiply(multiplyPreferences{AppendArrays: false})(d, context, newCandidate, splatCandidate)

Expand Down
4 changes: 2 additions & 2 deletions pkg/yqlib/operator_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ type compareTypePref struct {
}

func compareOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("-- compareOperator")
log.Debugf("compareOperator")
prefs := expressionNode.Operation.Preferences.(compareTypePref)
return crossFunction(d, context, expressionNode, compare(prefs), true)
}

func compare(prefs compareTypePref) func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
return func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
log.Debugf("-- compare cross function")
log.Debugf("compare cross function")
if lhs == nil && rhs == nil {
owner := &CandidateNode{}
return createBooleanCandidate(owner, prefs.OrEqual), nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/operator_create_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func createMapOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("-- createMapOperation")
log.Debugf("createMapOperation")

//each matchingNodes entry should turn into a sequence of keys to create.
//then collect object should do a cross function of the same index sequence for all matches.
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/operator_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func withEntriesOperator(d *dataTreeNavigator, context Context, expressionNode *
collected.HeadComment = candidate.HeadComment
collected.FootComment = candidate.FootComment

log.Debugf("**** collected %v", collected.LeadingContent)
log.Debugf("collected %v", collected.LeadingContent)

fromEntries, err := fromEntriesOperator(d, context.SingleChildContext(collected), expressionNode)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/yqlib/operator_equals.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package yqlib

func equalsOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("-- equalsOperation")
log.Debugf("equalsOperation")
return crossFunction(d, context, expressionNode, isEquals(false), true)
}

func isEquals(flip bool) func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
return func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
value := false
log.Debugf("-- isEquals cross function")
log.Debugf("isEquals cross function")
if lhs == nil && rhs == nil {
log.Debugf("-- both are nil")
log.Debugf("both are nil")
owner := &CandidateNode{}
return createBooleanCandidate(owner, !flip), nil
} else if lhs == nil {
Expand Down Expand Up @@ -43,6 +43,6 @@ func isEquals(flip bool) func(d *dataTreeNavigator, context Context, lhs *Candid
}

func notEqualsOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("-- notEqualsOperator")
log.Debugf("notEqualsOperator")
return crossFunction(d, context.ReadOnlyClone(), expressionNode, isEquals(true), true)
}
2 changes: 1 addition & 1 deletion pkg/yqlib/operator_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func errorOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {

log.Debugf("-- errorOperation")
log.Debugf("errorOperation")

rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/operator_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func filterOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("-- filterOperation")
log.Debugf("filterOperation")
var results = list.New()

for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/operator_flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func flatten(node *CandidateNode, depth int) {

func flattenOp(_ *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {

log.Debugf("-- flatten Operator")
log.Debugf("flatten Operator")
depth := expressionNode.Operation.Preferences.(flattenPreferences).depth

for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/operator_group_by.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func processIntoGroups(d *dataTreeNavigator, context Context, rhsExp *Expression

func groupBy(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {

log.Debugf("-- groupBy Operator")
log.Debugf("groupBy Operator")
var results = list.New()

for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
Expand Down
Loading

0 comments on commit 3c3f118

Please sign in to comment.