1- use std:: io:: BufWriter ;
2-
31pub use tracing:: { debug, error, info, warn, Level } ;
42use tracing_error:: ErrorLayer ;
53use tracing_subscriber:: {
64 fmt, layer:: SubscriberExt , prelude:: * , util:: SubscriberInitExt , EnvFilter ,
75} ;
86
9- use crate :: { print_to_terminal, Address } ;
7+ use crate :: {
8+ print_to_terminal,
9+ vfs:: { create_drive, open_file, File } ,
10+ Address ,
11+ } ;
1012
11- pub struct FilePrinter {
12- pub our : Address ,
13+ pub struct FileWriter {
14+ pub file : File ,
1315}
1416
15- pub struct FilePrinterMaker {
16- pub our : Address ,
17+ pub struct FileWriterMaker {
18+ pub file : File ,
1719}
1820
19- pub struct TerminalPrinter {
21+ pub struct TerminalWriter {
2022 pub level : u8 ,
2123}
2224
23- pub struct TerminalPrinterMaker {
25+ pub struct TerminalWriterMaker {
2426 pub level : u8 ,
2527}
2628
27- impl std:: io:: Write for FilePrinter {
29+ impl std:: io:: Write for FileWriter {
2830 fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
29- // TODO: change to use vfs?
30- let log_file_name = format ! ( "{}.log" , self . our. process( ) ) ;
31- let log_path =
32- std:: path:: PathBuf :: from ( std:: env:: var ( "LOGDIR" ) . unwrap ( ) ) . join ( log_file_name) ;
33- let file = std:: fs:: File :: options ( )
34- . append ( true )
35- . create ( true )
36- . open ( & log_path)
37- . unwrap ( ) ;
38- let mut writer = BufWriter :: new ( file) ;
39- writer. write ( buf)
31+ // TODO: use non-blocking call instead? (.append() `send_and_await()`s)
32+ self . file
33+ . append ( buf)
34+ . map_err ( |e| std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , e) ) ?;
35+ Ok ( buf. len ( ) )
4036 }
4137
4238 fn flush ( & mut self ) -> std:: io:: Result < ( ) > {
4339 Ok ( ( ) )
4440 }
4541}
4642
47- impl < ' a > tracing_subscriber:: fmt:: MakeWriter < ' a > for FilePrinterMaker {
48- type Writer = FilePrinter ;
43+ impl < ' a > tracing_subscriber:: fmt:: MakeWriter < ' a > for FileWriterMaker {
44+ type Writer = FileWriter ;
4945
5046 fn make_writer ( & ' a self ) -> Self :: Writer {
51- FilePrinter {
52- our : self . our . clone ( ) ,
47+ FileWriter {
48+ file : File :: new ( self . file . path . clone ( ) , self . file . timeout ) ,
5349 }
5450 }
5551}
5652
57- impl std:: io:: Write for TerminalPrinter {
53+ impl std:: io:: Write for TerminalWriter {
5854 fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
5955 let string = String :: from_utf8 ( buf. to_vec ( ) )
6056 . map_err ( |e| std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidData , e) ) ?;
@@ -67,11 +63,11 @@ impl std::io::Write for TerminalPrinter {
6763 }
6864}
6965
70- impl < ' a > tracing_subscriber:: fmt:: MakeWriter < ' a > for TerminalPrinterMaker {
71- type Writer = TerminalPrinter ;
66+ impl < ' a > tracing_subscriber:: fmt:: MakeWriter < ' a > for TerminalWriterMaker {
67+ type Writer = TerminalWriter ;
7268
7369 fn make_writer ( & ' a self ) -> Self :: Writer {
74- TerminalPrinter { level : self . level }
70+ TerminalWriter { level : self . level }
7571 }
7672}
7773
@@ -86,7 +82,11 @@ impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for TerminalPrinterMaker {
8682/// `node/vfs/package:publisher.os/log/process.log`, where `node` is your node's home
8783/// directory, `package` is the package name, `publisher.os` is the publisher of the
8884/// package, and `process` is the process name of the process doing the logging.
89- pub fn init_logging ( our : & Address , file_level : Level , terminal_level : Level ) {
85+ pub fn init_logging ( our : & Address , file_level : Level , terminal_level : Level ) -> anyhow:: Result < ( ) > {
86+ let log_dir_path = create_drive ( our. package_id ( ) , "log" , None ) ?;
87+ let log_file_path = format ! ( "{log_dir_path}/{}.log" , our. process( ) ) ;
88+ let log_file = open_file ( & log_file_path, true , None ) ?;
89+
9090 let file_filter = EnvFilter :: new ( file_level. as_str ( ) ) ;
9191 let error_filter = tracing_subscriber:: filter:: filter_fn ( |metadata : & tracing:: Metadata < ' _ > | {
9292 metadata. level ( ) == & Level :: ERROR
@@ -100,11 +100,11 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) {
100100 let debug_filter = tracing_subscriber:: filter:: filter_fn ( |metadata : & tracing:: Metadata < ' _ > | {
101101 metadata. level ( ) == & Level :: DEBUG
102102 } ) ;
103- let file_printer_maker = FilePrinterMaker { our : our . clone ( ) } ;
104- let error_terminal_printer_maker = TerminalPrinterMaker { level : 0 } ;
105- let warn_terminal_printer_maker = TerminalPrinterMaker { level : 1 } ;
106- let info_terminal_printer_maker = TerminalPrinterMaker { level : 2 } ;
107- let debug_terminal_printer_maker = TerminalPrinterMaker { level : 3 } ;
103+ let file_printer_maker = FileWriterMaker { file : log_file } ;
104+ let error_terminal_printer_maker = TerminalWriterMaker { level : 0 } ;
105+ let warn_terminal_printer_maker = TerminalWriterMaker { level : 1 } ;
106+ let info_terminal_printer_maker = TerminalWriterMaker { level : 2 } ;
107+ let debug_terminal_printer_maker = TerminalWriterMaker { level : 3 } ;
108108
109109 let sub = tracing_subscriber:: registry ( )
110110 . with ( ErrorLayer :: default ( ) )
@@ -199,4 +199,6 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) {
199199 )
200200 . init ( ) ;
201201 }
202+
203+ Ok ( ( ) )
202204}
0 commit comments