@@ -1072,6 +1072,17 @@ where
1072
1072
let _ = self . drain . log ( record, & self . list ) ;
1073
1073
}
1074
1074
1075
+ /// Flush all pending log records,
1076
+ /// blocking until competion.
1077
+ ///
1078
+ /// Will call [`std::io::Write::flush`] if applicable.
1079
+ ///
1080
+ /// Returns [`FlushError::NotSupported`] if the underlying drain does not support [`Drain::flush`].
1081
+ #[ inline]
1082
+ pub fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
1083
+ self . drain . flush ( )
1084
+ }
1085
+
1075
1086
/// Get list of key-value pairs assigned to this `Logger`
1076
1087
pub fn list ( & self ) -> & OwnedKVList {
1077
1088
& self . list
@@ -1152,6 +1163,52 @@ where
1152
1163
fn is_enabled ( & self , level : Level ) -> bool {
1153
1164
self . drain . is_enabled ( level)
1154
1165
}
1166
+
1167
+ #[ inline]
1168
+ fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
1169
+ self . drain . flush ( )
1170
+ }
1171
+ }
1172
+
1173
+ /// An error that occurs when calling [`Drain::flush`].
1174
+ #[ non_exhaustive]
1175
+ #[ derive( Debug ) ]
1176
+ pub enum FlushError {
1177
+ /// An error that occurs doing IO.
1178
+ ///
1179
+ /// Often triggered by [`std::io::]
1180
+ #[ cfg( feature = "std" ) ]
1181
+ Io ( std:: io:: Error ) ,
1182
+ /// Indicates this drain does not support flushing.
1183
+ NotSupported ,
1184
+ }
1185
+ #[ cfg( feature = "std" ) ]
1186
+ impl From < std:: io:: Error > for FlushError {
1187
+ fn from ( value : std:: io:: Error ) -> Self {
1188
+ FlushError :: Io ( value)
1189
+ }
1190
+ }
1191
+ #[ cfg( has_std_error) ]
1192
+ impl StdError for FlushError {
1193
+ fn source ( & self ) -> Option < & ( dyn StdError + ' static ) > {
1194
+ match self {
1195
+ #[ cfg( feature = "std" ) ]
1196
+ FlushError :: Io ( cause) => Some ( cause) ,
1197
+ FlushError :: NotSupported => None ,
1198
+ }
1199
+ }
1200
+ }
1201
+ impl fmt:: Display for FlushError {
1202
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1203
+ match self {
1204
+ FlushError :: Io ( _) => {
1205
+ f. write_str ( "Encountered IO error during flushing" )
1206
+ }
1207
+ FlushError :: NotSupported => {
1208
+ f. write_str ( "Drain does not support flushing" )
1209
+ }
1210
+ }
1211
+ }
1155
1212
}
1156
1213
1157
1214
// {{{ Drain
@@ -1196,6 +1253,15 @@ pub trait Drain {
1196
1253
values : & OwnedKVList ,
1197
1254
) -> result:: Result < Self :: Ok , Self :: Err > ;
1198
1255
1256
+ /// Flush all pending log records, blocking until competion.
1257
+ ///
1258
+ /// Should call [`std::io::Write::flush`] if applicable.
1259
+ ///
1260
+ /// Returns [`FlushError::NotSupported`] if the drain has not implemented this method.
1261
+ fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
1262
+ Err ( FlushError :: NotSupported )
1263
+ }
1264
+
1199
1265
/// **Avoid**: Check if messages at the specified log level are **maybe**
1200
1266
/// enabled for this logger.
1201
1267
///
@@ -1358,6 +1424,10 @@ impl<'a, D: Drain + 'a> Drain for &'a D {
1358
1424
fn is_enabled ( & self , level : Level ) -> bool {
1359
1425
( * * self ) . is_enabled ( level)
1360
1426
}
1427
+ #[ inline]
1428
+ fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
1429
+ ( * * self ) . flush ( )
1430
+ }
1361
1431
}
1362
1432
1363
1433
impl < ' a , D : Drain + ' a > Drain for & ' a mut D {
@@ -1375,6 +1445,10 @@ impl<'a, D: Drain + 'a> Drain for &'a mut D {
1375
1445
fn is_enabled ( & self , level : Level ) -> bool {
1376
1446
( * * self ) . is_enabled ( level)
1377
1447
}
1448
+ #[ inline]
1449
+ fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
1450
+ ( * * self ) . flush ( )
1451
+ }
1378
1452
}
1379
1453
1380
1454
/// Internal utility module used to "maybe" bound traits
@@ -1536,6 +1610,10 @@ impl<D: Drain + ?Sized> Drain for Box<D> {
1536
1610
fn is_enabled ( & self , level : Level ) -> bool {
1537
1611
( * * self ) . is_enabled ( level)
1538
1612
}
1613
+ #[ inline]
1614
+ fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
1615
+ ( * * self ) . flush ( )
1616
+ }
1539
1617
}
1540
1618
1541
1619
impl < D : Drain + ?Sized > Drain for Arc < D > {
@@ -1552,6 +1630,10 @@ impl<D: Drain + ?Sized> Drain for Arc<D> {
1552
1630
fn is_enabled ( & self , level : Level ) -> bool {
1553
1631
( * * self ) . is_enabled ( level)
1554
1632
}
1633
+ #[ inline]
1634
+ fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
1635
+ ( * * self ) . flush ( )
1636
+ }
1555
1637
}
1556
1638
1557
1639
/// `Drain` discarding everything
@@ -1575,6 +1657,10 @@ impl Drain for Discard {
1575
1657
fn is_enabled ( & self , _level : Level ) -> bool {
1576
1658
false
1577
1659
}
1660
+ #[ inline]
1661
+ fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
1662
+ Ok ( ( ) )
1663
+ }
1578
1664
}
1579
1665
1580
1666
/// `Drain` filtering records
@@ -1623,6 +1709,10 @@ where
1623
1709
*/
1624
1710
self . 0 . is_enabled ( level)
1625
1711
}
1712
+ #[ inline]
1713
+ fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
1714
+ self . 0 . flush ( )
1715
+ }
1626
1716
}
1627
1717
1628
1718
/// `Drain` filtering records by `Record` logging level
@@ -1663,6 +1753,10 @@ impl<D: Drain> Drain for LevelFilter<D> {
1663
1753
fn is_enabled ( & self , level : Level ) -> bool {
1664
1754
level. is_at_least ( self . 1 ) && self . 0 . is_enabled ( level)
1665
1755
}
1756
+ #[ inline]
1757
+ fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
1758
+ self . 0 . flush ( )
1759
+ }
1666
1760
}
1667
1761
1668
1762
/// `Drain` mapping error returned by another `Drain`
@@ -1704,6 +1798,10 @@ impl<D: Drain, E> Drain for MapError<D, E> {
1704
1798
fn is_enabled ( & self , level : Level ) -> bool {
1705
1799
self . drain . is_enabled ( level)
1706
1800
}
1801
+ #[ inline]
1802
+ fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
1803
+ self . drain . flush ( )
1804
+ }
1707
1805
}
1708
1806
1709
1807
/// `Drain` duplicating records into two other `Drain`s
@@ -1743,6 +1841,17 @@ impl<D1: Drain, D2: Drain> Drain for Duplicate<D1, D2> {
1743
1841
fn is_enabled ( & self , level : Level ) -> bool {
1744
1842
self . 0 . is_enabled ( level) || self . 1 . is_enabled ( level)
1745
1843
}
1844
+ /// Flush both drains.
1845
+ ///
1846
+ /// Will return [`FlushError::NotSupported`] if either drain does not support flushing.
1847
+ /// If one drain supports flushing and the other does not,
1848
+ /// it is unspecified whether or not anything will be flushed at all.
1849
+ #[ inline]
1850
+ fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
1851
+ self . 0 . flush ( ) ?;
1852
+ self . 1 . flush ( ) ?;
1853
+ Ok ( ( ) )
1854
+ }
1746
1855
}
1747
1856
1748
1857
/// `Drain` panicking on error
@@ -1789,6 +1898,10 @@ where
1789
1898
fn is_enabled ( & self , level : Level ) -> bool {
1790
1899
self . 0 . is_enabled ( level)
1791
1900
}
1901
+ #[ inline]
1902
+ fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
1903
+ self . 0 . flush ( )
1904
+ }
1792
1905
}
1793
1906
1794
1907
/// `Drain` ignoring result
@@ -1825,6 +1938,11 @@ impl<D: Drain> Drain for IgnoreResult<D> {
1825
1938
fn is_enabled ( & self , level : Level ) -> bool {
1826
1939
self . drain . is_enabled ( level)
1827
1940
}
1941
+
1942
+ #[ inline]
1943
+ fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
1944
+ self . drain . flush ( )
1945
+ }
1828
1946
}
1829
1947
1830
1948
/// Error returned by `Mutex<D : Drain>`
@@ -1920,6 +2038,13 @@ impl<D: Drain> Drain for std::sync::Mutex<D> {
1920
2038
fn is_enabled ( & self , level : Level ) -> bool {
1921
2039
self . lock ( ) . ok ( ) . map_or ( true , |lock| lock. is_enabled ( level) )
1922
2040
}
2041
+ #[ inline]
2042
+ fn flush ( & self ) -> result:: Result < ( ) , FlushError > {
2043
+ let guard = self . lock ( ) . map_err ( |_poison| {
2044
+ std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "Mutex is poisoned" )
2045
+ } ) ?;
2046
+ guard. flush ( )
2047
+ }
1923
2048
}
1924
2049
// }}}
1925
2050
0 commit comments