1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! Module for writes log mesages from Wasm code.
//!
//! This module is client for Logger Wasm Module in Fluence WasmVm. In couple
//! they allow to write messages from Wasm code to log. Destination of logging
//! is depends on Vm implementation and basically it is 'stdout'. By default this
//! module is disabled in WasmVm.
//!
//! Note that this module is work only for Wasm environment and Fluence WasmVm.
//! Don't try use it for others targets and VMs.
//!
//! This module provides implementation for logging facade in crate
//! [`log`]. See examples below for usege:
//!
//! # Examples
//!
//! This example initializes [`WasmLogger`] only for Wasm target, for another
//! targets initializes [`simple_logger`]. Macroses from crate [`log`] used as
//! logging facade.
//!
//! ```
//! #[macro_use] extern crate log;
//! extern crate fluence_sdk;
//! extern crate simple_logger;
//!
//! fn main() {
//! if cfg!(target_arch = "wasm32") {
//! fluence_sdk::logger::WasmLogger::init_with_level(log::Level::Info).unwrap();
//! } else {
//! simple_logger::init_with_level(log::Level::Info).unwrap();
//! }
//!
//! error!("This message will be logged.");
//! trace!("This message will not be logged.");
//! }
//!
//! ```
//! This example provides method for initialization [`WasmLogger`] only for Wasm
//! target without specifying logger level. Macroses from crate [`log`] used as
//! logging facade.
//!
//! ```
//! #[macro_use] extern crate log;
//! extern crate fluence_sdk;
//!
//! /// This method initialize WasmLogger and should be called at the start of application
//! #[no_mangle]
//! #[cfg(target_arch = "wasm32")]
//! fn init_logger() {
//! fluence_sdk::logger::WasmLogger::init().unwrap();
//! info!("If you can see this message that logger was successfully initialized.");
//! }
//!
//! ```
//! You can also use [`static_lazy`] for [`WasmLogger`] initialization but laziness
//! has some caveats. You need to call [`lazy_static::initialize()`] for
//! eager initialization before first logger macros usage.
//!
//! ```
//! #[macro_use] extern crate log;
//! #[macro_use] extern crate lazy_static;
//! extern crate fluence_sdk;
//!
//! lazy_static! {
//! static ref _LOGGER: () = {
//! fluence_sdk::logger::WasmLogger::init_with_level(log::Level::Info);
//! };
//! }
//!
//! fn main() {
//! if cfg!(target_arch = "wasm32") {
//! // There is required to call init in a method or in another `lazy_static!` block
//! fluence_sdk::logger::WasmLogger::init_with_level(log::Level::Info).unwrap();
//! }
//!
//! // ...
//! }
//!
//! ```
//! [`WasmLogger`]: struct.WasmLogger.html
//! [`log`]: https://docs.rs/log
//! [`simple_logger`]: https://docs.rs/simple_logger
//! [`static_lazy`]: https://docs.rs/lazy_static
//! [`lazy_static::initialize()`]: https://docs.rs/lazy_static/1.2.0/lazy_static/fn.initialize.html
extern crate log;
/// The Wasm Logger.
///
/// This struct implements the [`Log`] trait from the [`log`] crate,
/// which allows it to act as a logger.
///
/// For initialization WasmLogger as default logger see [`init()`] and [`init_with_level()`]
///
/// [log-crate-url]: https://docs.rs/log/
/// [`Log`]: https://docs.rs/log/0.4.6/log/trait.Log.html
/// [`init_with_level()`]: struct.WasmLogger.html#method.init_with_level
/// [`init()`]: struct.WasmLogger.html#method.init
/// Wasm module for writing logs from Wasm code, provided by WasmVm.
extern "C"