@@ -10,6 +10,19 @@ use crate::{
10
10
Address ,
11
11
} ;
12
12
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
+
13
26
pub struct FileWriter {
14
27
pub file : File ,
15
28
}
@@ -26,6 +39,31 @@ pub struct TerminalWriterMaker {
26
39
pub level : u8 ,
27
40
}
28
41
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
+
29
67
impl std:: io:: Write for FileWriter {
30
68
fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
31
69
// TODO: use non-blocking call instead? (.append() `send_and_await()`s)
@@ -82,7 +120,12 @@ impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for TerminalWriterMaker {
82
120
/// `node/vfs/package:publisher.os/log/process.log`, where `node` is your node's home
83
121
/// directory, `package` is the package name, `publisher.os` is the publisher of the
84
122
/// 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 < ( ) > {
86
129
let log_dir_path = create_drive ( our. package_id ( ) , "log" , None ) ?;
87
130
let log_file_path = format ! ( "{log_dir_path}/{}.log" , our. process( ) ) ;
88
131
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) ->
100
143
let debug_filter = tracing_subscriber:: filter:: filter_fn ( |metadata : & tracing:: Metadata < ' _ > | {
101
144
metadata. level ( ) == & Level :: DEBUG
102
145
} ) ;
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 } ;
108
151
109
152
let sub = tracing_subscriber:: registry ( )
110
153
. with ( ErrorLayer :: default ( ) )
111
154
. with (
112
155
fmt:: layer ( )
113
156
. with_file ( true )
114
157
. with_line_number ( true )
115
- . with_writer ( file_printer_maker )
158
+ . with_writer ( file_writer_maker )
116
159
. with_ansi ( false )
117
160
. with_target ( false )
118
161
. json ( )
@@ -123,7 +166,7 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
123
166
. with_file ( true )
124
167
. with_line_number ( true )
125
168
. without_time ( )
126
- . with_writer ( error_terminal_printer_maker )
169
+ . with_writer ( error_terminal_writer_maker )
127
170
. with_ansi ( true )
128
171
. with_level ( true )
129
172
. with_target ( true )
@@ -132,11 +175,94 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
132
175
) ;
133
176
134
177
// 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
+
135
261
if terminal_level >= Level :: DEBUG {
136
262
sub. with (
137
263
fmt:: layer ( )
138
264
. without_time ( )
139
- . with_writer ( warn_terminal_printer_maker )
265
+ . with_writer ( warn_terminal_writer_maker )
140
266
. with_ansi ( true )
141
267
. with_level ( true )
142
268
. with_target ( true )
@@ -146,7 +272,7 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
146
272
. with (
147
273
fmt:: layer ( )
148
274
. without_time ( )
149
- . with_writer ( info_terminal_printer_maker )
275
+ . with_writer ( info_terminal_writer_maker )
150
276
. with_ansi ( true )
151
277
. with_level ( true )
152
278
. with_target ( true )
@@ -156,7 +282,7 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
156
282
. with (
157
283
fmt:: layer ( )
158
284
. without_time ( )
159
- . with_writer ( debug_terminal_printer_maker )
285
+ . with_writer ( debug_terminal_writer_maker )
160
286
. with_ansi ( true )
161
287
. with_level ( true )
162
288
. with_target ( true )
@@ -168,7 +294,7 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
168
294
sub. with (
169
295
fmt:: layer ( )
170
296
. without_time ( )
171
- . with_writer ( warn_terminal_printer_maker )
297
+ . with_writer ( warn_terminal_writer_maker )
172
298
. with_ansi ( true )
173
299
. with_level ( true )
174
300
. with_target ( true )
@@ -178,7 +304,7 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
178
304
. with (
179
305
fmt:: layer ( )
180
306
. without_time ( )
181
- . with_writer ( info_terminal_printer_maker )
307
+ . with_writer ( info_terminal_writer_maker )
182
308
. with_ansi ( true )
183
309
. with_level ( true )
184
310
. with_target ( true )
@@ -190,7 +316,7 @@ pub fn init_logging(our: &Address, file_level: Level, terminal_level: Level) ->
190
316
sub. with (
191
317
fmt:: layer ( )
192
318
. without_time ( )
193
- . with_writer ( warn_terminal_printer_maker )
319
+ . with_writer ( warn_terminal_writer_maker )
194
320
. with_ansi ( true )
195
321
. with_level ( true )
196
322
. with_target ( true )
0 commit comments