forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add color support for messages to console. (Velocidex#551)
- Loading branch information
Showing
9 changed files
with
182 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// +build !windows | ||
|
||
package logging | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/rifflock/lfshook" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
color_map = map[string]string{ | ||
"reset": "\033[0m", | ||
"red": "\033[31m", | ||
"green": "\033[32m", | ||
"yellow": "\033[33m", | ||
"blue": "\033[34m", | ||
"purple": "\033[35m", | ||
"cyan": "\033[36m", | ||
"gray": "\033[37m", | ||
"white": "\033[97m", | ||
} | ||
) | ||
|
||
type Formatter struct { | ||
stderr_map lfshook.WriterMap | ||
} | ||
|
||
func (self *Formatter) Format(entry *logrus.Entry) ([]byte, error) { | ||
b := &bytes.Buffer{} | ||
|
||
levelText := strings.ToUpper(entry.Level.String()) | ||
fmt.Fprintf(b, "[%s] %v %s ", levelText, entry.Time.Format(time.RFC3339), | ||
replaceTagWithCode(strings.TrimRight(entry.Message, "\r\n"))) | ||
|
||
if len(entry.Data) > 0 { | ||
serialized, _ := json.Marshal(entry.Data) | ||
fmt.Fprintf(b, "%s", serialized) | ||
} | ||
|
||
return append(b.Bytes(), '\n'), nil | ||
} | ||
|
||
func replaceTagWithCode(message string) string { | ||
if NoColor { | ||
return clearTag(message) | ||
} | ||
|
||
result := tag_regex.ReplaceAllStringFunc(message, func(hit string) string { | ||
matches := tag_regex.FindStringSubmatch(hit) | ||
if len(matches) > 1 { | ||
code, pres := color_map[matches[1]] | ||
if pres { | ||
return code | ||
} | ||
} | ||
return hit | ||
}) | ||
|
||
reset := color_map["reset"] | ||
return closing_tag_regex.ReplaceAllString(result, reset) + reset | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// +build windows | ||
|
||
package logging | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gookit/color" | ||
"github.com/rifflock/lfshook" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type Formatter struct { | ||
stderr_map lfshook.WriterMap | ||
} | ||
|
||
func (self *Formatter) Format(entry *logrus.Entry) ([]byte, error) { | ||
b := &bytes.Buffer{} | ||
|
||
levelText := strings.ToUpper(entry.Level.String()) | ||
fmt.Fprintf(b, "[%s] %v %s ", levelText, entry.Time.Format(time.RFC3339), | ||
strings.TrimRight(entry.Message, "\r\n")) | ||
|
||
if len(entry.Data) > 0 { | ||
serialized, _ := json.Marshal(entry.Data) | ||
fmt.Fprintf(b, "%s", serialized) | ||
} | ||
|
||
// Only print the result to the console, if there is an stderr | ||
// map to it. | ||
_, pres := self.stderr_map[entry.Level] | ||
if pres { | ||
if NoColor { | ||
return []byte(clearTag(b.String())), nil | ||
} | ||
color.Println(normalize(b.String())) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func normalize(line string) string { | ||
// Get count of opening tags | ||
opening_matches := tag_regex.FindAllString(line, -1) | ||
closing_matches := closing_tag_regex.FindAllString(line, -1) | ||
|
||
if len(opening_matches) > len(closing_matches) { | ||
for i := 0; i < len(opening_matches)-len(closing_matches); i++ { | ||
line += "</>" | ||
} | ||
} else if len(opening_matches) < len(closing_matches) { | ||
line = closing_tag_regex.ReplaceAllString(line, "") | ||
} | ||
|
||
return line | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package inventory | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pkg/errors" | ||
artifacts_proto "www.velocidex.com/golang/velociraptor/artifacts/proto" | ||
config_proto "www.velocidex.com/golang/velociraptor/config/proto" | ||
"www.velocidex.com/golang/velociraptor/services" | ||
) | ||
|
||
type Dummy struct{} | ||
|
||
func (self Dummy) Get() *artifacts_proto.ThirdParty { | ||
return &artifacts_proto.ThirdParty{} | ||
} | ||
|
||
func (self Dummy) GetToolInfo(ctx context.Context, config_obj *config_proto.Config, | ||
tool string) (*artifacts_proto.Tool, error) { | ||
return nil, errors.New("Not found") | ||
} | ||
|
||
func (self Dummy) AddTool(config_obj *config_proto.Config, tool *artifacts_proto.Tool) error { | ||
return errors.New("Inventory service not available") | ||
} | ||
|
||
func (self Dummy) RemoveTool(config_obj *config_proto.Config, tool_name string) error { | ||
return errors.New("Inventory service not available") | ||
} | ||
|
||
func init() { | ||
services.RegisterInventory(&Dummy{}) | ||
} |