@@ -10,6 +10,19 @@ use crate::{
1010 Address ,
1111} ;
1212
13+ pub struct RemoteLogSettings {
14+ pub target : Address ,
15+ pub level : u8 ,
16+ }
17+
18+ pub struct RemoteWriter {
19+ pub target : Address ,
20+ }
21+
22+ pub struct RemoteWriterMaker {
23+ pub target : Address ,
24+ }
25+
1326pub struct FileWriter {
1427 pub file : File ,
1528}
@@ -26,6 +39,31 @@ pub struct TerminalWriterMaker {
2639 pub level : u8 ,
2740}
2841
42+ impl std:: io:: Write for RemoteWriter {
43+ fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
44+ Request :: to ( self . target )
45+ . body ( buf)
46+ . send ( )
47+ . unwrap ( )
48+ . map_err ( |e| std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , e) ) ?;
49+ Ok ( buf. len ( ) )
50+ }
51+
52+ fn flush ( & mut self ) -> std:: io:: Result < ( ) > {
53+ Ok ( ( ) )
54+ }
55+ }
56+
57+ impl < ' a > tracing_subscriber:: fmt:: MakeWriter < ' a > for RemoteWriterMaker {
58+ type Writer = FileWriter ;
59+
60+ fn make_writer ( & ' a self ) -> Self :: Writer {
61+ FileWriter {
62+ file : File :: new ( self . file . path . clone ( ) , self . file . timeout ) ,
63+ }
64+ }
65+ }
66+
2967impl std:: io:: Write for FileWriter {
3068 fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
3169 // TODO: use non-blocking call instead? (.append() `send_and_await()`s)
@@ -82,7 +120,12 @@ impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for TerminalWriterMaker {
82120/// `node/vfs/package:publisher.os/log/process.log`, where `node` is your node's home
83121/// directory, `package` is the package name, `publisher.os` is the publisher of the
84122/// package, and `process` is the process name of the process doing the logging.
85- pub fn init_logging ( our : & Address , file_level : Level , terminal_level : Level ) -> anyhow:: Result < ( ) > {
123+ pub fn init_logging (
124+ our : & Address ,
125+ file_level : Level ,
126+ terminal_level : Level ,
127+ remote : Option < RemoteLogSettings > ,
128+ ) -> anyhow:: Result < ( ) > {
86129 let log_dir_path = create_drive ( our. package_id ( ) , "log" , None ) ?;
87130 let log_file_path = format ! ( "{log_dir_path}/{}.log" , our. process( ) ) ;
88131 let log_file = open_file ( & log_file_path, true , None ) ?;
@@ -100,19 +143,19 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
100143 let debug_filter = tracing_subscriber:: filter:: filter_fn ( |metadata : & tracing:: Metadata < ' _ > | {
101144 metadata. level ( ) == & Level :: DEBUG
102145 } ) ;
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 } ;
146+ let file_writer_maker = FileWriterMaker { file : log_file } ;
147+ let error_terminal_writer_maker = TerminalWriterMaker { level : 0 } ;
148+ let warn_terminal_writer_maker = TerminalWriterMaker { level : 1 } ;
149+ let info_terminal_writer_maker = TerminalWriterMaker { level : 2 } ;
150+ let debug_terminal_writer_maker = TerminalWriterMaker { level : 3 } ;
108151
109152 let sub = tracing_subscriber:: registry ( )
110153 . with ( ErrorLayer :: default ( ) )
111154 . with (
112155 fmt:: layer ( )
113156 . with_file ( true )
114157 . with_line_number ( true )
115- . with_writer ( file_printer_maker )
158+ . with_writer ( file_writer_maker )
116159 . with_ansi ( false )
117160 . with_target ( false )
118161 . json ( )
@@ -123,7 +166,7 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
123166 . with_file ( true )
124167 . with_line_number ( true )
125168 . without_time ( )
126- . with_writer ( error_terminal_printer_maker )
169+ . with_writer ( error_terminal_writer_maker )
127170 . with_ansi ( true )
128171 . with_level ( true )
129172 . with_target ( true )
@@ -132,11 +175,94 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
132175 ) ;
133176
134177 // TODO: can we DRY?
178+ let Some ( remote) = remote else {
179+ let remote_filter = EnvFilter :: new ( remote. level . as_str ( ) ) ;
180+ let sub = sub. with (
181+ fmt:: layer ( )
182+ . with_file ( true )
183+ . with_line_number ( true )
184+ . with_writer ( remote_writer_maker)
185+ . with_ansi ( false )
186+ . with_target ( false )
187+ . json ( )
188+ . with_filter ( remote_filter) ,
189+ ) ;
190+ if terminal_level >= Level :: DEBUG {
191+ sub. with (
192+ fmt:: layer ( )
193+ . without_time ( )
194+ . with_writer ( warn_terminal_writer_maker)
195+ . with_ansi ( true )
196+ . with_level ( true )
197+ . with_target ( true )
198+ . fmt_fields ( fmt:: format:: PrettyFields :: new ( ) )
199+ . with_filter ( warn_filter) ,
200+ )
201+ . with (
202+ fmt:: layer ( )
203+ . without_time ( )
204+ . with_writer ( info_terminal_writer_maker)
205+ . with_ansi ( true )
206+ . with_level ( true )
207+ . with_target ( true )
208+ . fmt_fields ( fmt:: format:: PrettyFields :: new ( ) )
209+ . with_filter ( info_filter) ,
210+ )
211+ . with (
212+ fmt:: layer ( )
213+ . without_time ( )
214+ . with_writer ( debug_terminal_writer_maker)
215+ . with_ansi ( true )
216+ . with_level ( true )
217+ . with_target ( true )
218+ . fmt_fields ( fmt:: format:: PrettyFields :: new ( ) )
219+ . with_filter ( debug_filter) ,
220+ )
221+ . init ( ) ;
222+ } else if terminal_level >= Level :: INFO {
223+ sub. with (
224+ fmt:: layer ( )
225+ . without_time ( )
226+ . with_writer ( warn_terminal_writer_maker)
227+ . with_ansi ( true )
228+ . with_level ( true )
229+ . with_target ( true )
230+ . fmt_fields ( fmt:: format:: PrettyFields :: new ( ) )
231+ . with_filter ( warn_filter) ,
232+ )
233+ . with (
234+ fmt:: layer ( )
235+ . without_time ( )
236+ . with_writer ( info_terminal_writer_maker)
237+ . with_ansi ( true )
238+ . with_level ( true )
239+ . with_target ( true )
240+ . fmt_fields ( fmt:: format:: PrettyFields :: new ( ) )
241+ . with_filter ( info_filter) ,
242+ )
243+ . init ( ) ;
244+ } else if terminal_level >= Level :: WARN {
245+ sub. with (
246+ fmt:: layer ( )
247+ . without_time ( )
248+ . with_writer ( warn_terminal_writer_maker)
249+ . with_ansi ( true )
250+ . with_level ( true )
251+ . with_target ( true )
252+ . fmt_fields ( fmt:: format:: PrettyFields :: new ( ) )
253+ . with_filter ( warn_filter) ,
254+ )
255+ . init ( ) ;
256+ }
257+
258+ return Ok ( ( ) ) ;
259+ } ;
260+
135261 if terminal_level >= Level :: DEBUG {
136262 sub. with (
137263 fmt:: layer ( )
138264 . without_time ( )
139- . with_writer ( warn_terminal_printer_maker )
265+ . with_writer ( warn_terminal_writer_maker )
140266 . with_ansi ( true )
141267 . with_level ( true )
142268 . with_target ( true )
@@ -146,7 +272,7 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
146272 . with (
147273 fmt:: layer ( )
148274 . without_time ( )
149- . with_writer ( info_terminal_printer_maker )
275+ . with_writer ( info_terminal_writer_maker )
150276 . with_ansi ( true )
151277 . with_level ( true )
152278 . with_target ( true )
@@ -156,7 +282,7 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
156282 . with (
157283 fmt:: layer ( )
158284 . without_time ( )
159- . with_writer ( debug_terminal_printer_maker )
285+ . with_writer ( debug_terminal_writer_maker )
160286 . with_ansi ( true )
161287 . with_level ( true )
162288 . with_target ( true )
@@ -168,7 +294,7 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
168294 sub. with (
169295 fmt:: layer ( )
170296 . without_time ( )
171- . with_writer ( warn_terminal_printer_maker )
297+ . with_writer ( warn_terminal_writer_maker )
172298 . with_ansi ( true )
173299 . with_level ( true )
174300 . with_target ( true )
@@ -178,7 +304,7 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
178304 . with (
179305 fmt:: layer ( )
180306 . without_time ( )
181- . with_writer ( info_terminal_printer_maker )
307+ . with_writer ( info_terminal_writer_maker )
182308 . with_ansi ( true )
183309 . with_level ( true )
184310 . with_target ( true )
@@ -190,7 +316,7 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
190316 sub. with (
191317 fmt:: layer ( )
192318 . without_time ( )
193- . with_writer ( warn_terminal_printer_maker )
319+ . with_writer ( warn_terminal_writer_maker )
194320 . with_ansi ( true )
195321 . with_level ( true )
196322 . with_target ( true )
0 commit comments