Skip to content

Commit

Permalink
feat(logging): Update logging format (#18)
Browse files Browse the repository at this point in the history
- Include a prefix (ERROR, INFO, etc.)
    - Remove needless noise
  • Loading branch information
freddiehaddad authored Oct 31, 2023
1 parent e7636f1 commit 17d0175
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 40 deletions.
6 changes: 3 additions & 3 deletions pkg/cpu/temp/temp.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func New(sensor string, interval time.Duration) (*CPUTemp, error) {
}

func (c *CPUTemp) Update() (descriptor.Descriptor, error) {
log.Println("Updating CPU temperature sensor", c.Sensor)
log.Printf("INFO: Updating CPU temperature sensor=%s", c.Sensor)
descriptor := descriptor.Descriptor{
Component: "cputemp",
Value: "",
Expand All @@ -38,7 +38,7 @@ func (c *CPUTemp) Update() (descriptor.Descriptor, error) {
sensor := fmt.Sprintf("/sys/class/hwmon/hwmon3/%s", c.Sensor)
sensorValue, err := utils.GetSensorValue(sensor)
if err != nil {
log.Println("Error reading", sensor, err)
log.Printf("ERROR: GetSensorValue sensor=%s err=%s", sensor, err)
return descriptor, err
}

Expand All @@ -55,7 +55,7 @@ func (c *CPUTemp) Start(buffer chan descriptor.Descriptor) {
for c.Enabled.Load() {
descriptor, err := c.Update()
if err != nil {
log.Println("Error during update", err)
log.Printf("ERROR: Update err=%s", err)
} else {
buffer <- descriptor
}
Expand Down
18 changes: 7 additions & 11 deletions pkg/cpu/utilization/utilization.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func New(interval time.Duration) (*CPUUtilization, error) {
}

func (c *CPUUtilization) Update() (descriptor.Descriptor, error) {
log.Println("Updating CPU utilization")
log.Printf("INFO: Updating CPU utilization")
descriptor := descriptor.Descriptor{
Component: "cpuutil",
Value: "",
Expand All @@ -54,7 +54,7 @@ func (c *CPUUtilization) Update() (descriptor.Descriptor, error) {
statPath := "/proc/stat"
statRaw, err := os.ReadFile(statPath)
if err != nil {
log.Println("Error reading", statPath, err)
log.Printf("ERROR: ReadFile statPath=%s err=%s", statPath, err)
return descriptor, err
}

Expand All @@ -66,7 +66,7 @@ func (c *CPUUtilization) Update() (descriptor.Descriptor, error) {

currentStatValues, err := parseInts(currentStatValuesRaw)
if err != nil {
log.Printf("Error parsing currentStatValuesRaw=%v, err=%s\n", currentStatValuesRaw, err)
log.Printf("ERROR: parseInts currentStatValuesRaw=%v err=%s", currentStatValuesRaw, err)
return descriptor, err
}

Expand Down Expand Up @@ -97,7 +97,7 @@ func (c *CPUUtilization) Start(buffer chan descriptor.Descriptor) {
for c.Enabled.Load() {
descriptor, err := c.Update()
if err != nil {
log.Println("Error during update", err)
log.Printf("ERROR: Update err=%s", err)
} else {
buffer <- descriptor
}
Expand All @@ -123,27 +123,23 @@ func getStatValues(bytes []byte) ([]string, error) {
s := string(bytes)
split := strings.SplitAfterN(s, rawSeparator, numSplits)
if len(split) != 2 {
err := fmt.Errorf("error splitting %s, expected a length %d, but got length %d", s, expectedLength, len(split))
log.Println(err)
err := fmt.Errorf("length error len=%d got=%d s=%s rawSeparator=%s numSplits=%d", expectedLength, len(split), s, rawSeparator, numSplits)
return finalValues, err
}

stats := split[0]
log.Println("Prepping", stats)

if len(stats) <= len(firstValue) {
err := fmt.Errorf("length of %s: %d is not as expected", stats, len(stats))
err := fmt.Errorf("length error stats=%s len=%d <= firstValue=%s len=%d", stats, len(stats), firstValue, len(firstValue))
return finalValues, err
}

stats = strings.TrimPrefix(stats, firstValue)
stats = strings.TrimSpace(stats)

log.Println("Finished prepping", stats)

values := strings.Split(stats, valueSeparator)
if len(values) != valuesExpected {
err := fmt.Errorf("error procesing values, expected length %d, but got length %d", valuesExpected, len(values))
err := fmt.Errorf("split error stats=%s did not produce valuesExpected=%d got=%d", stats, valuesExpected, len(values))
return finalValues, err
}

Expand Down
23 changes: 11 additions & 12 deletions pkg/cpu/utilization/utilization_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utilization

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -70,46 +69,46 @@ func TestSumArray(t *testing.T) {

func TestGetStatValues(t *testing.T) {
tests := []struct {
input []byte
expected []string
err error
input []byte
expected []string
shouldFail bool
}{
{
[]byte("cpu 0 0 0 0 0 0 0 0 0 0\n"),
[]string{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0"},
nil,
false,
},
{
[]byte("cpu 1528029 235 1406982 142714174 167167 223736 40373 0 0 0\n"),
[]string{"1528029", "235", "1406982", "142714174", "167167", "223736", "40373", "0", "0", "0"},
nil,
false,
},
{
[]byte("cpu"),
nil,
fmt.Errorf("error splitting cpu, expected a length 2, but got length 1"),
true,
},
{
[]byte("cpu\n"),
nil,
fmt.Errorf("error procesing values, expected length 10, but got length 1"),
false,
},
{
[]byte("cpu 0 0 0 0 0 0 0 0 0\n"),
nil,
fmt.Errorf("error procesing values, expected length 10, but got length 9"),
false,
},
}

for index, test := range tests {
result, err := getStatValues(test.input)
if test.err != nil && test.err.Error() != err.Error() {
t.Errorf("Test %d failed. Expected err=%v, got err=%v", index, test.err, err)
if test.shouldFail && err == nil {
t.Errorf("Test %d failed. shouldFail=%v, err=%v", index, test.shouldFail, err)
continue
}

if len(test.expected) != len(result) {
t.Errorf("Test %d failed. Expected result len=%d, got len=%d", index, len(test.expected), len(result))
t.Errorf("Test %d failed. Result expected len=%d, got len=%d", index, len(test.expected), len(result))
continue
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/date/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func New(format string, interval time.Duration) (*Date, error) {
}

func (d *Date) Update() (descriptor.Descriptor, error) {
log.Println("Updating", "date")
log.Printf("INFO: Updating date")
descriptor := descriptor.Descriptor{
Component: "date",
Value: "",
Expand All @@ -48,7 +48,7 @@ func (d *Date) Start(buffer chan descriptor.Descriptor) {
for d.Enabled.Load() {
descriptor, err := d.Update()
if err != nil {
log.Println("Error during update", err)
log.Printf("ERROR: Update err=%s", err)
} else {
buffer <- descriptor
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/gpu/gpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func New(sensor string, interval time.Duration) (*GPU, error) {
}

func (c *GPU) Update() (descriptor.Descriptor, error) {
log.Println("Updating GPU temperature sensor", c.Sensor)
log.Printf("INFO: Updating GPU temperature sensor=%s", c.Sensor)
descriptor := descriptor.Descriptor{
Component: "gpu",
Value: "",
Expand All @@ -38,7 +38,7 @@ func (c *GPU) Update() (descriptor.Descriptor, error) {
sensor := fmt.Sprintf("/sys/class/hwmon/hwmon1/%s", c.Sensor)
sensorValue, err := utils.GetSensorValue(sensor)
if err != nil {
log.Println("Error reading", sensor, err)
log.Printf("ERROR: GetSensorValue sensor=%s err=%s", sensor, err)
return descriptor, err
}

Expand All @@ -55,7 +55,7 @@ func (c *GPU) Start(buffer chan descriptor.Descriptor) {
for c.Enabled.Load() {
descriptor, err := c.Update()
if err != nil {
log.Println("Error during update", err)
log.Printf("ERROR: Update err=%s", err)
} else {
buffer <- descriptor
}
Expand Down
10 changes: 4 additions & 6 deletions pkg/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ func getBytesTransferred(device, direction string) (int64, error) {
dataPath := fmt.Sprintf("/sys/class/net/%s/statistics/%s", device, direction)
dataRaw, err := os.ReadFile(dataPath)
if err != nil {
log.Println("Error reading", dataPath, err)
return 0, err
}
dataString := strings.TrimSuffix(string(dataRaw), "\n")
bytesTransferred, err := strconv.ParseInt(dataString, 10, 64)
if err != nil {
log.Println("Error parsing int64", dataString, err)
return 0, err
}
return bytesTransferred, nil
Expand Down Expand Up @@ -92,7 +90,7 @@ func calculateThroughput(prevTimeNanoseconds, prevBytes, currTimeNanoseconds, cu
}

func (n *Network) Update() (descriptor.Descriptor, error) {
log.Println("Updating", n.Device)
log.Printf("INFO: Updating network throughput device=%s", n.Device)
var s string
descriptor := descriptor.Descriptor{
Component: "network",
Expand All @@ -105,15 +103,15 @@ func (n *Network) Update() (descriptor.Descriptor, error) {
s = "rx_bytes"
rxBytesTransferred, rxErr := getBytesTransferred(n.Device, s)
if rxErr != nil {
log.Println("Error getting", s, rxErr)
log.Printf("ERROR: getBytesTransferred device=%s s=%s err=%s", n.Device, s, rxErr)
return descriptor, rxErr

}

s = "tx_bytes"
txBytesTransferred, txErr := getBytesTransferred(n.Device, s)
if txErr != nil {
log.Println("Error getting", s, txErr)
log.Printf("ERROR: getBytesTransferred device=%s s=%s err=%s", n.Device, s, txErr)
return descriptor, txErr
}

Expand Down Expand Up @@ -143,7 +141,7 @@ func (n *Network) Start(buffer chan descriptor.Descriptor) {
for n.Enabled.Load() {
descriptor, err := n.Update()
if err != nil {
log.Println("Error during update", err)
log.Printf("ERROR: Update err=%s", err)
} else {
buffer <- descriptor
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"log"
"os"
"strconv"
"strings"
Expand All @@ -22,13 +21,11 @@ import (
func GetSensorValue(sensor string) (int64, error) {
sensorValueRaw, err := os.ReadFile(sensor)
if err != nil {
log.Println("Error reading", sensor, err)
return 0, err
}
sensorValueTrimmed := strings.TrimSuffix(string(sensorValueRaw), "\n")
sensorValue, err := strconv.ParseInt(sensorValueTrimmed, 10, 32)
if err != nil {
log.Println("Error parsing value", sensorValueTrimmed, err)
return 0, err
}
return sensorValue, nil
Expand Down

0 comments on commit 17d0175

Please sign in to comment.