Skip to content

Commit 35fde9f

Browse files
authored
Merge pull request #394 from HyperloopUPV-H8/control-station/electron-configs
feat(backend): add logger destination on configuration
2 parents b43b359 + f6a68a3 commit 35fde9f

8 files changed

Lines changed: 28 additions & 6 deletions

File tree

backend/cmd/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"github.com/HyperloopUPV-H8/h9-backend/internal/server"
55
"github.com/HyperloopUPV-H8/h9-backend/internal/vehicle"
6+
"github.com/HyperloopUPV-H8/h9-backend/pkg/logger"
67
)
78

89
type App struct {
@@ -46,7 +47,8 @@ type TCP struct {
4647
}
4748

4849
type Logging struct {
49-
TimeUnit string `toml:"time_unit"`
50+
TimeUnit logger.TimeUnit `toml:"time_unit"`
51+
LoggingPath string `toml:"logging_path"`
5052
}
5153

5254
type Config struct {

backend/cmd/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ backoff_factor = 2 # Backoff multiplier for retry delays
5353
enable_progress = true # Enable progress callbacks during transfers
5454

5555
# Logger Configuration
56+
[logging]
5657
time_unit = "us" # Time unit for log timestamps (ns, us, ms, s) Default: ns
58+
logging_path = "." # Absolute path of the logger
5759

5860
# <-- DO NOT TOUCH BELOW THIS LINE -->
5961

backend/cmd/dev-config.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,5 @@ file = "backend.log" # Log file path (empty to disable file logging)
8484
max_size_mb = 100 # Maximum log file size in MB
8585
max_backups = 3 # Number of backup files to keep
8686
max_age_days = 7 # Maximum age of log files in days
87-
time_unit = "us" # Time unit for log timestamps (ns, us, ms, s)
87+
time_unit = "us" # Time unit for log timestamps (ns, us, ms, s)
88+
logging_path = "."

backend/cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func main() {
176176
order_logger.Name: order_logger.NewLogger(),
177177
}
178178

179-
logger.SetFormatTimestamp(logger.TimeUnit(config.Logging.TimeUnit)) // MUST be before creating subloggers
179+
logger.ConfigureLogger(config.Logging.TimeUnit, config.Logging.LoggingPath)
180180
loggerHandler := logger.NewLogger(subloggers, trace.Logger)
181181

182182
// <--- order transfer --->

backend/pkg/logger/base/logger.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,19 @@ func (sublogger *BaseLogger) Start() error {
5656

5757
func (sublogger *BaseLogger) CreateFile(filename string) (*os.File, error) {
5858

59-
err := os.MkdirAll(path.Dir(filename), os.ModePerm)
59+
// Includes the direcory specified by the user
60+
baseFilename := path.Join(loggerHandler.BasePath, filename)
61+
62+
err := os.MkdirAll(path.Dir(baseFilename), os.ModePerm)
6063
if err != nil {
6164
return nil, loggerHandler.ErrCreatingAllDir{
6265
Name: sublogger.Name,
6366
Timestamp: time.Now(),
64-
Path: filename,
67+
Path: baseFilename,
6568
}
6669
}
6770

68-
return os.Create(path.Join(filename))
71+
return os.Create(path.Join(baseFilename))
6972
}
7073

7174
// Create a base Logger with default values

backend/pkg/logger/logger.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ var _ abstraction.Logger = &Logger{}
3636
// Used on subloggers to get the current timestamp for folder or file names
3737
var Timestamp = time.Now()
3838

39+
var BasePath = "."
40+
3941
func (Logger) HandlerName() string { return HandlerName }
4042

4143
func NewLogger(keys map[abstraction.LoggerName]abstraction.Logger, baseLogger zerolog.Logger) *Logger {
@@ -164,3 +166,13 @@ func (logger *Logger) Stop() error {
164166
logger.trace.Info().Msg("stopped")
165167
return nil
166168
}
169+
170+
// Configures the logger atributes before inicialicing it
171+
func ConfigureLogger(unit TimeUnit, basePath string) {
172+
173+
// Start the sublogger
174+
SetFormatTimestamp(unit)
175+
176+
// Update base Path
177+
BasePath = basePath
178+
}

electron-app/build-electron.sh

100644100755
File mode changed.

packet-sender/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use crate::network::PacketSender;
1717
#[command(name = "packet-sender")]
1818
#[command(about = "Hyperloop packet sender for testing backend and frontend", long_about = None)]
1919
struct Cli {
20+
21+
/// TODO: TO CHANGE = GO'S os.UserCacheDir()
2022
/// Path to ADJ directory (defaults to ../backend/cmd/adj)
2123
#[arg(short, long, default_value = "../backend/cmd/adj")]
2224
adj_path: PathBuf,

0 commit comments

Comments
 (0)