Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions backend/cmd/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ boards = ["BCU", "BMSL", "HVSCU", "HVSCU-Cabinet", "LCU", "PCU", "VCU", "BLCU"]
[adj]
branch = "main" # Leave blank when using ADJ as a submodule (like this: "")

# Network Configuration
[network]
manual = false # Manual network device selection

# Transport Configuration
[transport]
propagate_fault = false
Expand Down
9 changes: 0 additions & 9 deletions backend/cmd/dev-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ boards = ["HVSCU", "HVSCU-Cabinet", "PCU", "LCU", "BCU", "BMSL"]
[adj]
branch = "software" # Leave blank when using ADJ as a submodule (like this: "")

# Network Configuration
[network]
manual = false # Manual network device selection

# Transport Configuration
[transport]
Expand Down Expand Up @@ -77,11 +74,5 @@ upload_order_id = 0 # Order ID for upload operations (0 = use default)

# Logging Configuration
[logging]
level = "debug" # Logging level (trace, debug, info, warn, error, fatal)
console = true # Enable console output
file = "backend.log" # Log file path (empty to disable file logging)
max_size_mb = 100 # Maximum log file size in MB
max_backups = 3 # Number of backup files to keep
max_age_days = 7 # Maximum age of log files in days
time_unit = "us" # Time unit for log timestamps (ns, us, ms, s)
logging_path = "."
29 changes: 12 additions & 17 deletions backend/cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package main

import (
"flag"
_ "net/http/pprof"
"os"
"os/signal"

adj_module "github.com/HyperloopUPV-H8/h9-backend/internal/adj"
"github.com/HyperloopUPV-H8/h9-backend/internal/config"
"github.com/HyperloopUPV-H8/h9-backend/internal/flags"
"github.com/HyperloopUPV-H8/h9-backend/internal/pod_data"
"github.com/HyperloopUPV-H8/h9-backend/internal/update_factory"
vehicle_models "github.com/HyperloopUPV-H8/h9-backend/internal/vehicle/models"
"github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction"
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport"
"github.com/HyperloopUPV-H8/h9-backend/pkg/websocket"
trace "github.com/rs/zerolog/log"
Expand All @@ -29,25 +28,13 @@ const (
RemoveStateOrder = "remove_state_order"
)

var configFile = flag.String("config", "config.toml", "path to configuration file")
var traceLevel = flag.String("trace", "info", "set the trace level (\"fatal\", \"error\", \"warn\", \"info\", \"debug\", \"trace\")")
var traceFile = flag.String("log", "", "set the trace log file")
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var enableSNTP = flag.Bool("sntp", false, "enables a simple SNTP server on port 123")
var networkDevice = flag.Int("dev", -1, "index of the network device to use, overrides device prompt")
var blockprofile = flag.Int("blockprofile", 0, "number of block profiles to include")
var playbackFile = flag.String("playback", "", "")
var versionFlag = flag.Bool("version", false, "Show the backend version")

type SubloggersMap map[abstraction.LoggerName]abstraction.Logger

func main() {
// Parse command line flags
flag.Parse()
flags.Init()
handleVersionFlag()

// Configure trace
traceFile := initTrace(*traceLevel, *traceFile)
traceFile := initTrace(flags.TraceLevel, flags.TraceFile)
if traceFile != nil {
defer traceFile.Close()
}
Expand All @@ -57,7 +44,7 @@ func main() {
defer cleanup()

// <--- config --->
config, err := config.GetConfig(*configFile)
config, err := config.GetConfig(flags.ConfigFile)
if err != nil {
trace.Fatal().Err(err).Msg("error unmarshaling toml file")
}
Expand Down Expand Up @@ -139,6 +126,14 @@ func main() {
os.Exit(1)
}

// Start logger
if flags.EnableLooger {
err = loggerHandler.Start()
if err != nil {
trace.Fatal().Err(err).Msg("starting logger")
}
}

// Open browser tabs
openBrowserTabs(config)

Expand Down
13 changes: 7 additions & 6 deletions backend/cmd/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

adj_module "github.com/HyperloopUPV-H8/h9-backend/internal/adj"
"github.com/HyperloopUPV-H8/h9-backend/internal/config"
"github.com/HyperloopUPV-H8/h9-backend/internal/flags"
"github.com/HyperloopUPV-H8/h9-backend/internal/pod_data"
"github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction"
"github.com/HyperloopUPV-H8/h9-backend/pkg/logger"
Expand All @@ -21,7 +22,7 @@ import (
// Handle version flag
func handleVersionFlag() {

if *versionFlag {
if flags.Version {
versionFile := "VERSION.txt"
versionData, err := os.ReadFile(versionFile)
if err == nil {
Expand All @@ -43,8 +44,8 @@ func setupRuntimeCPU() func() {
cleanup := func() {}

runtime.GOMAXPROCS(runtime.NumCPU())
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if flags.CPUProfile != "" {
f, err := os.Create(flags.CPUProfile)
if err != nil {
f.Close()
trace.Fatal().Stack().Err(err).Msg("could not set up CPU profiling")
Expand All @@ -57,7 +58,7 @@ func setupRuntimeCPU() func() {
f.Close()
}
}
runtime.SetBlockProfileRate(*blockprofile)
runtime.SetBlockProfileRate(flags.BlockProfile)

return cleanup
}
Expand Down Expand Up @@ -134,9 +135,9 @@ func createLookupTables(
createBoardToPackets(podData)
}

func setUpLogger(config config.Config) (*logger.Logger, SubloggersMap) {
func setUpLogger(config config.Config) (*logger.Logger, abstraction.SubloggersMap) {

var subloggers = SubloggersMap{
var subloggers = abstraction.SubloggersMap{
data_logger.Name: data_logger.NewLogger(),
order_logger.Name: order_logger.NewLogger(),
}
Expand Down
5 changes: 3 additions & 2 deletions backend/cmd/setup_vehicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"time"

"github.com/HyperloopUPV-H8/h9-backend/internal/flags"
vehicle_models "github.com/HyperloopUPV-H8/h9-backend/internal/vehicle/models"
h "github.com/HyperloopUPV-H8/h9-backend/pkg/http"
"github.com/HyperloopUPV-H8/h9-backend/pkg/websocket"
Expand All @@ -33,7 +34,7 @@ import (
trace "github.com/rs/zerolog/log"
)

func configureBroker(subloggers SubloggersMap, loggerHandler *logger.Logger, idToBoard map[abstraction.PacketId]string, connections chan *websocket.Client) (*broker.Broker, func()) {
func configureBroker(subloggers abstraction.SubloggersMap, loggerHandler *logger.Logger, idToBoard map[abstraction.PacketId]string, connections chan *websocket.Client) (*broker.Broker, func()) {

broker := broker.New(trace.Logger)

Expand Down Expand Up @@ -135,7 +136,7 @@ func configureVehicle(

func configureSNTP(adj adj_module.ADJ) bool {

if *enableSNTP {
if flags.EnableSNTP {
sntpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", adj.Info.Addresses[BACKEND], adj.Info.Ports[SNTP]))
if err != nil {
fmt.Fprintf(os.Stderr, "error resolving sntp address: %v\n", err)
Expand Down
6 changes: 0 additions & 6 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ require (
github.com/google/gopacket v1.1.19
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.5.0
github.com/hashicorp/go-version v1.7.0
github.com/jmaralo/sntp v0.0.0-20240116111937-45a0a3419272
github.com/pelletier/go-toml/v2 v2.0.7
github.com/pin/tftp/v3 v3.0.0
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/rs/zerolog v1.29.0
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
)

Expand All @@ -23,7 +21,6 @@ require (
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
Expand All @@ -34,7 +31,6 @@ require (
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
Expand All @@ -43,10 +39,8 @@ require (
golang.org/x/sys v0.31.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/fatih/color v1.15.0
golang.org/x/net v0.38.0 // indirect
)
10 changes: 8 additions & 2 deletions backend/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"

"github.com/HyperloopUPV-H8/h9-backend/internal/flags"
"github.com/pelletier/go-toml/v2"
trace "github.com/rs/zerolog/log"
)
Expand All @@ -22,8 +23,13 @@ func GetConfig(path string) (Config, error) {

var config Config

// TODO: add strict mode (DisallowUnkownFields)
decodeErr := toml.NewDecoder(reader).Decode(&config)
decode := toml.NewDecoder(reader)

// Set whether to disallow unknown fields based on the flag
if !flags.ConfigAllowUnknown {
decode.DisallowUnknownFields()
}
decodeErr := decode.Decode(&config)

if decodeErr != nil {

Expand Down
5 changes: 0 additions & 5 deletions backend/internal/config/config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ type Adj struct {
Branch string `toml:"branch"`
}

type Network struct {
Manual bool `toml:"manual"`
}

type Transport struct {
PropagateFault bool `toml:"propagate_fault"`
}
Expand Down Expand Up @@ -55,7 +51,6 @@ type Config struct {
Vehicle vehicle.Config
Server server.Config
Adj Adj
Network Network
Transport Transport
TFTP TFTP
TCP TCP
Expand Down
43 changes: 43 additions & 0 deletions backend/internal/flags/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Package flags defines command-line flags for the backend application.
package flags

import "flag"

var (

// ConfigFile specifies the path to the configuration file.
ConfigFile string
// ConfigAllowUnknown enables non-strict mode for configuration parsing.
ConfigAllowUnknown bool

// TraceLevel sets the logging level for tracing.
TraceLevel string
// TraceFile specifies the file to write trace logs to.
TraceFile string

// CPUProfile specifies the file to write CPU profiling data to.
CPUProfile string
// EnableSNTP enables a simple SNTP server on port 123.
EnableSNTP bool
// BlockProfile sets the number of block profiles to include.
BlockProfile int
// Version shows the backend version when set.
Version bool
// EnableLooger enables logging (note: likely a typo for "Logger").
EnableLooger bool
)

// Init sets up the command-line flags with their default values and descriptions.
func Init() {
flag.StringVar(&ConfigFile, "config", "config.toml", "path to configuration file")
flag.BoolVar(&ConfigAllowUnknown, "config-allow-unknown", false, "allow unknown fields in configuration file")
flag.StringVar(&TraceLevel, "trace", "info", "set the trace level (\"fatal\", \"error\", \"warn\", \"info\", \"debug\", \"trace\")")
flag.StringVar(&TraceFile, "log", "", "set the trace log file")
flag.StringVar(&CPUProfile, "cpuprofile", "", "write cpu profile to file")
flag.BoolVar(&EnableSNTP, "sntp", false, "enables a simple SNTP server on port 123")
flag.IntVar(&BlockProfile, "blockprofile", 0, "number of block profiles to include")
flag.BoolVar(&Version, "version", false, "Show the backend version")
flag.BoolVar(&EnableLooger, "L", false, "enable logging")

flag.Parse()
}
4 changes: 4 additions & 0 deletions backend/pkg/abstraction/logger.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package abstraction provides interfaces for logging functionality.
package abstraction

// LoggerName is the name of the logger that manages a piece of data
Expand All @@ -23,3 +24,6 @@ type Logger interface {
// PullRecord will retrieve a record from disk
PullRecord(LoggerRequest) (LoggerRecord, error)
}

// SubloggersMap is a map of logger names to their respective loggers
type SubloggersMap map[LoggerName]Logger
11 changes: 6 additions & 5 deletions backend/pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package logger provides logging functionality for the HyperLoop backend.
package logger

import (
Expand All @@ -12,7 +13,7 @@ import (
const (
Name = "loggerHandler"
HandlerName = "logger"
TimestampFormat = "02-Jan-2006_15-04-05.000"
TimestampFormat = "2006-01-02T15-04-05" // ISO 8601 date for ensuring correct lexicographical order
)

// Logger is a struct that implements the abstraction.Logger interface
Expand All @@ -21,7 +22,7 @@ type Logger struct {
running *atomic.Bool
subloggersLock *sync.RWMutex
// The subloggers are only the loggers selected at the start of the log
subloggers map[abstraction.LoggerName]abstraction.Logger
subloggers abstraction.SubloggersMap

trace zerolog.Logger

Expand All @@ -33,14 +34,14 @@ type Logger struct {
***************/
var _ abstraction.Logger = &Logger{}

// Used on subloggers to get the current timestamp for folder or file names
// Timestamp is used on subloggers to get the current timestamp for folder or file names
var Timestamp = time.Now()

var BasePath = "."

func (Logger) HandlerName() string { return HandlerName }

func NewLogger(keys map[abstraction.LoggerName]abstraction.Logger, baseLogger zerolog.Logger) *Logger {
func NewLogger(keys abstraction.SubloggersMap, baseLogger zerolog.Logger) *Logger {
trace := baseLogger.Sample(zerolog.LevelSampler{
TraceSampler: zerolog.RandomSampler(25000),
DebugSampler: zerolog.RandomSampler(1),
Expand Down Expand Up @@ -167,7 +168,7 @@ func (logger *Logger) Stop() error {
return nil
}

// Configures the logger atributes before inicialicing it
// ConfigureLogger configures the logger attributes before initializing it.
func ConfigureLogger(unit TimeUnit, basePath string) {

// Start the sublogger
Expand Down
4 changes: 2 additions & 2 deletions backend/pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ func TestLoggerGroup_Errors(t *testing.T) {
_ = chdirTemp(t) // Change to a temporary directory

// Logger with empty map → PushRecord should return error (no sublogger)
lEmpty := logger.NewLogger(map[abstraction.LoggerName]abstraction.Logger{}, zerolog.New(os.Stdout))
lEmpty := logger.NewLogger(abstraction.SubloggersMap{}, zerolog.New(os.Stdout))
err := lEmpty.PushRecord(&mockRecord{n: abstraction.LoggerName("missing")})
if err == nil {
t.Fatalf("expected error when PushRecord to non-existent sublogger, got nil")
}

// Logger whose sublogger returns error on Start → Start should propagate the error
wantErr := os.ErrPermission
badMap := map[abstraction.LoggerName]abstraction.Logger{
badMap := abstraction.SubloggersMap{
abstraction.LoggerName("bad"): &mockSublogger{startErr: wantErr},
}
lBad := logger.NewLogger(badMap, zerolog.New(os.Stdout))
Expand Down
1 change: 1 addition & 0 deletions backend/pkg/transport/network/tftp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testfile.txt
1 change: 0 additions & 1 deletion backend/pkg/transport/network/tftp/testfile.txt

This file was deleted.

Loading