@@ -10,7 +10,7 @@ use slog::Drain;
10
10
use slog:: Level ;
11
11
use slog:: Logger ;
12
12
use std:: fs:: OpenOptions ;
13
- use std:: path:: Path ;
13
+ use std:: { io , path:: Path } ;
14
14
15
15
/**
16
16
* Represents the logging configuration for a server. This is expected to be a
@@ -77,7 +77,7 @@ impl ConfigLogging {
77
77
pub fn to_logger < S : AsRef < str > > (
78
78
& self ,
79
79
log_name : S ,
80
- ) -> Result < Logger , String > {
80
+ ) -> Result < Logger , io :: Error > {
81
81
match self {
82
82
ConfigLogging :: StderrTerminal {
83
83
level,
@@ -141,18 +141,12 @@ fn log_drain_for_file(
141
141
open_options : & OpenOptions ,
142
142
path : & Path ,
143
143
log_name : String ,
144
- ) -> Result < slog:: Fuse < slog_json:: Json < std:: fs:: File > > , String > {
144
+ ) -> Result < slog:: Fuse < slog_json:: Json < std:: fs:: File > > , io :: Error > {
145
145
if let Some ( parent) = path. parent ( ) {
146
- std:: fs:: create_dir_all ( parent) . map_err ( |e| {
147
- let p = path. display ( ) ;
148
- format ! ( "open log file \" {}\" : {}" , p, e)
149
- } ) ?;
146
+ std:: fs:: create_dir_all ( parent) ?;
150
147
}
151
148
152
- let file = open_options. open ( path) . map_err ( |e| {
153
- let p = path. display ( ) ;
154
- format ! ( "open log file \" {}\" : {}" , p, e)
155
- } ) ?;
149
+ let file = open_options. open ( path) ?;
156
150
157
151
/*
158
152
* Record a message to the stderr so that a reader who doesn't already know
@@ -180,10 +174,11 @@ mod test {
180
174
use crate :: test_util:: verify_bunyan_records_sequential;
181
175
use crate :: test_util:: BunyanLogRecordSpec ;
182
176
use crate :: ConfigLogging ;
177
+ use libc;
183
178
use slog:: Logger ;
184
179
use std:: fs;
185
180
use std:: path:: Path ;
186
- use std:: path:: PathBuf ;
181
+ use std:: { io , path:: PathBuf } ;
187
182
188
183
/**
189
184
* Generates a temporary filesystem path unique for the given label.
@@ -207,7 +202,7 @@ mod test {
207
202
fn read_config_and_create_logger (
208
203
label : & str ,
209
204
contents : & str ,
210
- ) -> Result < Logger , String > {
205
+ ) -> Result < Logger , io :: Error > {
211
206
let config = read_config :: < ConfigLogging > ( label, contents) . unwrap ( ) ;
212
207
let result = config. to_logger ( "test-logger" ) ;
213
208
if let Err ( ref error) = result {
@@ -224,7 +219,8 @@ mod test {
224
219
fn test_config_bad_log_mode ( ) {
225
220
let bad_config = r##" mode = "bonkers" "## ;
226
221
let error = read_config :: < ConfigLogging > ( "bad_log_mode" , bad_config)
227
- . unwrap_err ( ) ;
222
+ . unwrap_err ( )
223
+ . to_string ( ) ;
228
224
assert ! ( error. starts_with(
229
225
"unknown variant `bonkers`, expected `stderr-terminal` or `file` \
230
226
for key `mode`"
@@ -243,7 +239,8 @@ mod test {
243
239
let bad_config = r##" mode = "stderr-terminal" "## ;
244
240
assert_eq ! (
245
241
read_config:: <ConfigLogging >( "bad_terminal_no_level" , bad_config)
246
- . unwrap_err( ) ,
242
+ . unwrap_err( )
243
+ . to_string( ) ,
247
244
"missing field `level`"
248
245
) ;
249
246
}
@@ -256,7 +253,8 @@ mod test {
256
253
"## ;
257
254
assert_eq ! (
258
255
read_config:: <ConfigLogging >( "bad_terminal_bad_level" , bad_config)
259
- . unwrap_err( ) ,
256
+ . unwrap_err( )
257
+ . to_string( ) ,
260
258
"unknown variant `everything`, expected one of `trace`, `debug`, \
261
259
`info`, `warn`, `error`, `critical`"
262
260
) ;
@@ -296,7 +294,8 @@ mod test {
296
294
"## ;
297
295
let error =
298
296
read_config :: < ConfigLogging > ( "bad_file_no_file" , bad_config)
299
- . unwrap_err ( ) ;
297
+ . unwrap_err ( )
298
+ . to_string ( ) ;
300
299
assert_eq ! ( error, "missing field `path`" ) ;
301
300
}
302
301
@@ -308,7 +307,8 @@ mod test {
308
307
"## ;
309
308
let error =
310
309
read_config :: < ConfigLogging > ( "bad_file_no_level" , bad_config)
311
- . unwrap_err ( ) ;
310
+ . unwrap_err ( )
311
+ . to_string ( ) ;
312
312
assert_eq ! ( error, "missing field `level`" ) ;
313
313
}
314
314
@@ -357,34 +357,32 @@ mod test {
357
357
358
358
/**
359
359
* Records that the caller intends to create a directory with relative
360
- * path "path" underneath the root directory for this log test. Returns
361
- * a String representing the path to this directory. This directory
362
- * will be removed during teardown. Directories and files must be
363
- * recorded in the order they would be created so that the order can be
364
- * reversed at teardown (without needing any kind of recursive removal).
360
+ * path "path" underneath the root directory for this log test. Returns
361
+ * the path to this directory. This directory will be removed during
362
+ * teardown. Directories and files must be recorded in the order they
363
+ * would be created so that the order can be reversed at teardown
364
+ * (without needing any kind of recursive removal).
365
365
*/
366
- fn will_create_dir ( & mut self , path : & str ) -> String {
366
+ fn will_create_dir ( & mut self , path : & str ) -> PathBuf {
367
367
let mut pathbuf = self . directory . clone ( ) ;
368
368
pathbuf. push ( path) ;
369
- let rv = pathbuf. as_path ( ) . display ( ) . to_string ( ) ;
370
- self . cleanup_list . push ( LogTestCleanup :: Directory ( pathbuf) ) ;
371
- rv
369
+ self . cleanup_list . push ( LogTestCleanup :: Directory ( pathbuf. clone ( ) ) ) ;
370
+ pathbuf
372
371
}
373
372
374
373
/**
375
374
* Records that the caller intends to create a file with relative path
376
- * "path" underneath the root directory for this log test. Returns a
377
- * String representing the path to this file. This file will be removed
378
- * during teardown. Directories and files must be recorded in the order
379
- * they would be created so that the order can be reversed at teardown
380
- * (without needing any kind of recursive removal).
375
+ * "path" underneath the root directory for this log test. Returns a
376
+ * the path to this file. This file will be removed during teardown.
377
+ * Directories and files must be recorded in the order they would be
378
+ * created so that the order can be reversed at teardown (without
379
+ * needing any kind of recursive removal).
381
380
*/
382
- fn will_create_file ( & mut self , path : & str ) -> String {
381
+ fn will_create_file ( & mut self , path : & str ) -> PathBuf {
383
382
let mut pathbuf = self . directory . clone ( ) ;
384
383
pathbuf. push ( path) ;
385
- let rv = pathbuf. as_path ( ) . display ( ) . to_string ( ) ;
386
- self . cleanup_list . push ( LogTestCleanup :: File ( pathbuf) ) ;
387
- rv
384
+ self . cleanup_list . push ( LogTestCleanup :: File ( pathbuf. clone ( ) ) ) ;
385
+ pathbuf
388
386
}
389
387
}
390
388
@@ -413,26 +411,32 @@ mod test {
413
411
let path = logtest. will_create_dir ( "log_file_as_dir" ) ;
414
412
fs:: create_dir ( & path) . unwrap ( ) ;
415
413
414
+ // Windows paths need to have \ turned into \\
415
+ let escaped_path =
416
+ path. display ( ) . to_string ( ) . escape_default ( ) . to_string ( ) ;
417
+
416
418
let bad_config = format ! (
417
- "{}\" {}\" \n " ,
418
- r##"
419
+ r#"
419
420
mode = "file"
420
421
level = "warn"
421
422
if_exists = "append"
422
- path = "## ,
423
- & path
423
+ path = "{}"
424
+ "# ,
425
+ escaped_path
424
426
) ;
425
427
426
428
let error = read_config_and_create_logger (
427
429
"bad_file_bad_path_type" ,
428
430
& bad_config,
429
431
)
430
432
. unwrap_err ( ) ;
431
- let message = format ! ( "{}" , error) ;
432
- assert ! ( message. starts_with( & format!(
433
- "open log file \" {}\" : Is a directory" ,
434
- & path
435
- ) ) ) ;
433
+
434
+ if cfg ! ( windows) {
435
+ assert_eq ! ( error. kind( ) , std:: io:: ErrorKind :: PermissionDenied ) ;
436
+ } else {
437
+ assert_eq ! ( error. kind( ) , std:: io:: ErrorKind :: Other ) ;
438
+ assert_eq ! ( error. raw_os_error( ) , Some ( libc:: EISDIR ) ) ;
439
+ }
436
440
}
437
441
438
442
#[ test]
@@ -441,26 +445,27 @@ mod test {
441
445
let logpath = logtest. will_create_file ( "log.out" ) ;
442
446
fs:: write ( & logpath, "" ) . expect ( "writing empty file" ) ;
443
447
448
+ // Windows paths need to have \ turned into \\
449
+ let escaped_path =
450
+ logpath. display ( ) . to_string ( ) . escape_default ( ) . to_string ( ) ;
451
+
444
452
let bad_config = format ! (
445
- "{}\" {}\" \n " ,
446
- r##"
453
+ r#"
447
454
mode = "file"
448
455
level = "warn"
449
456
if_exists = "fail"
450
- path = "## ,
451
- & logpath
457
+ path = "{}"
458
+ "# ,
459
+ escaped_path
452
460
) ;
453
461
454
462
let error = read_config_and_create_logger (
455
463
"bad_file_bad_path_exists_fail" ,
456
464
& bad_config,
457
465
)
458
466
. unwrap_err ( ) ;
459
- let message = format ! ( "{}" , error) ;
460
- assert ! ( message. starts_with( & format!(
461
- "open log file \" {}\" : File exists" ,
462
- & logpath
463
- ) ) ) ;
467
+
468
+ assert_eq ! ( error. kind( ) , std:: io:: ErrorKind :: AlreadyExists ) ;
464
469
}
465
470
466
471
/*
@@ -475,15 +480,19 @@ mod test {
475
480
let logpath = logtest. will_create_file ( "log.out" ) ;
476
481
let time_before = chrono:: offset:: Utc :: now ( ) ;
477
482
483
+ // Windows paths need to have \ turned into \\
484
+ let escaped_path =
485
+ logpath. display ( ) . to_string ( ) . escape_default ( ) . to_string ( ) ;
486
+
478
487
/* The first attempt should succeed. The log file doesn't exist yet. */
479
488
let config = format ! (
480
- "{}\" {}\" \n " ,
481
- r##"
489
+ r#"
482
490
mode = "file"
483
491
level = "warn"
484
492
if_exists = "fail"
485
- path = "## ,
486
- & logpath
493
+ path = "{}"
494
+ "# ,
495
+ escaped_path
487
496
) ;
488
497
489
498
{
@@ -499,13 +508,13 @@ mod test {
499
508
500
509
/* Try again with if_exists = "append". This should also work. */
501
510
let config = format ! (
502
- "{}\" {}\" \n " ,
503
- r##"
511
+ r#"
504
512
mode = "file"
505
513
level = "warn"
506
514
if_exists = "append"
507
- path = "## ,
508
- & logpath
515
+ path = "{}"
516
+ "# ,
517
+ escaped_path
509
518
) ;
510
519
511
520
{
@@ -541,13 +550,13 @@ mod test {
541
550
let time_before = time_after;
542
551
let time_after = chrono:: offset:: Utc :: now ( ) ;
543
552
let config = format ! (
544
- "{}\" {}\" \n " ,
545
- r##"
553
+ r#"
546
554
mode = "file"
547
555
level = "trace"
548
556
if_exists = "truncate"
549
- path = "## ,
550
- & logpath
557
+ path = "{}"
558
+ "# ,
559
+ escaped_path
551
560
) ;
552
561
553
562
{
0 commit comments