logged stream
v0.4.0
logged-stream是一个锈库,它提供了一个LoggedStream结构,可以用作io对象tokio基础包装器,该对象实现了std::io::Write and std::io::Read特质或读取其异步类似物,以启用所有读取和写入和写入和写入和误差,错误,错误,错误,误差和删除。
来自四个部分的LoggedStream结构结构:
std::io::Write and std::io::Read特征或它们的异步类似物来自tokio库: tokio::io::AsyncRead and tokio::io::AsyncWrite 。BufferFormatter特征。 LoggedStream的这一部分负责您将看到输入和输出字节的形式。当前,该库提供了以下BufferFormatter特征的实现: LowercaseHexadecimalFormatter , UppercaseHexadecimalFormatter ,DemalFormatter, DecimalFormatter , BinaryFormatter和OctalFormatter 。同样, BufferFormatter是公共特征,因此您可以自由构建自己的实现。RecordFilter性状。 LoggedStream的这一部分负责日志记录过滤。当前,该库提供了以下RecordFilter特性的实现: DefaultFilter ,该特性接受所有日志记录和RecordKindFilter ,该记录和recordKindFilter接受构造过程中指定的种类的日志。 RecordFilter也是公共特征,您可以自由构建自己的实现。Logger特征。 LoggedStream的这一部分负责使用构造,格式化和过滤的日志记录进行进一步的工作。例如,可以将其输出到写入文件的控制台,写入数据库,写在存储器上以进一步使用或由频道发送。当前,该库提供了以下Logger特征的实现: ConsoleLogger , MemoryStorageLogger , ChannelLogger和FileLogger 。也是Logger是公共特征,您可以自由构建自己的实现。要使用logged-stream ,请在您的Cargo.toml中添加以下行:toml:
[ dependencies ]
logged-stream = " 0.4 "或在项目目录中运行以下货物命令:
$ cargo add [email protected]
这是带有std::net::TcpStream的LoggedStream结构的简单用法示例,它是底座IO对象,该对象连接到某些回声服务器,小写较低的六核格式格式,默认过滤器和控制台记录器。
fn main ( ) {
env :: set_var ( "RUST_LOG" , "debug" ) ;
env_logger :: init ( ) ;
let mut client = LoggedStream :: new (
net :: TcpStream :: connect ( "127.0.0.1:8080" ) . unwrap ( ) ,
LowercaseHexadecimalFormatter :: new ( None ) ,
DefaultFilter :: default ( ) ,
ConsoleLogger :: new_unchecked ( "debug" ) ,
) ;
let send = [ 0x01 , 0x02 , 0x03 , 0x04 ] ;
client . write_all ( & send ) . unwrap ( ) ;
let mut response = [ 0u8 ; 4 ] ;
client . read_exact ( & mut response ) . unwrap ( ) ;
let send = [ 0x05 , 0x06 , 0x07 , 0x08 ] ;
client . write_all ( & send ) . unwrap ( ) ;
let mut response = [ 0u8 ; 4 ] ;
client . read_exact ( & mut response ) . unwrap ( ) ;
let send = [ 0x09 , 0x0a , 0x0b , 0x0c ] ;
client . write_all ( & send ) . unwrap ( ) ;
let mut response = [ 0u8 ; 4 ] ;
client . read_exact ( & mut response ) . unwrap ( ) ;
let send = [ 0x01 , 0x02 , 0x03 , 0x04 ] ;
client . write_all ( & send ) . unwrap ( ) ;
let mut response = [ 0u8 ; 4 ] ;
client . read_exact ( & mut response ) . unwrap ( ) ;
}输出到控制台:
[2023-04-18T08:18:45.895Z DEBUG logged_stream::logger] > 01:02:03:04
[2023-04-18T08:18:45.895Z DEBUG logged_stream::logger] < 01:02:03:04
[2023-04-18T08:18:45.895Z DEBUG logged_stream::logger] > 05:06:07:08
[2023-04-18T08:18:45.895Z DEBUG logged_stream::logger] < 05:06:07:08
[2023-04-18T08:18:45.895Z DEBUG logged_stream::logger] > 09:0a:0b:0c
[2023-04-18T08:18:45.896Z DEBUG logged_stream::logger] < 09:0a:0b:0c
[2023-04-18T08:18:45.896Z DEBUG logged_stream::logger] > 01:02:03:04
[2023-04-18T08:18:45.896Z DEBUG logged_stream::logger] < 01:02:03:04
[2023-04-18T08:18:45.896Z DEBUG logged_stream::logger] x Deallocated.
可以在此处找到此示例的完整版本。
相同的例子,但是使用异步API重写,可以在那里找到。
根据任何一个
可以选择。
除非您另有明确说明,否则任何有意提交的捐款(如Apache-2.0许可证中定义)应为双重许可,如上所述,没有任何其他条款或条件。