Changeset 247 in code for trunk/logger.go
- Timestamp:
- Apr 7, 2020, 7:50:12 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/logger.go
r235 r247 15 15 entity string 16 16 17 filenamestring18 file 17 path string 18 file *os.File 19 19 } 20 20 … … 24 24 entity: entity, 25 25 } 26 } 27 28 func logPath(network *network, entity string, t time.Time) string { 29 user := network.user 30 srv := user.srv 31 32 // TODO: handle/forbid network/entity names with illegal path characters 33 year, month, day := t.Date() 34 filename := fmt.Sprintf("%04d-%02d-%02d.log", year, month, day) 35 return filepath.Join(srv.LogPath, user.Username, network.GetName(), entity, filename) 26 36 } 27 37 … … 37 47 // TODO: handle non-monotonic clock behaviour 38 48 now := time.Now() 39 year, month, day := now.Date() 40 filename := fmt.Sprintf("%04d-%02d-%02d.log", year, month, day) 41 if ml.filename != filename { 49 path := logPath(ml.conn.network, ml.entity, now) 50 if ml.path != path { 42 51 if ml.file != nil { 43 52 ml.file.Close() 44 53 } 45 54 46 // TODO: handle/forbid network/entity names with illegal path characters 47 dir := filepath.Join(ml.conn.srv.LogPath, ml.conn.user.Username, ml.conn.network.GetName(), ml.entity) 55 dir := filepath.Dir(path) 48 56 if err := os.MkdirAll(dir, 0700); err != nil { 49 57 return fmt.Errorf("failed to create logs directory %q: %v", dir, err) 50 58 } 51 59 52 path := filepath.Join(dir, filename)53 60 f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) 54 61 if err != nil { … … 56 63 } 57 64 58 ml. filename = filename65 ml.path = path 59 66 ml.file = f 60 67 } … … 62 69 _, err := fmt.Fprintf(ml.file, "[%02d:%02d:%02d] %s\n", now.Hour(), now.Minute(), now.Second(), s) 63 70 if err != nil { 64 return fmt.Errorf("failed to log message to %q: %v", ml. filename, err)71 return fmt.Errorf("failed to log message to %q: %v", ml.path, err) 65 72 } 66 73 return nil
Note:
See TracChangeset
for help on using the changeset viewer.