1use std::fmt;
2
3use super::*;
4use lz4_flex::block::uncompressed_size;
5use wasmer_wasix_types::wasi;
6
7#[derive(Debug)]
9pub enum JournalPrintingMode {
10 Text,
11 Json,
12}
13impl Default for JournalPrintingMode {
14 fn default() -> Self {
15 Self::Text
16 }
17}
18
19#[derive(Debug, Default)]
22pub struct PrintingJournal {
23 mode: JournalPrintingMode,
24}
25
26impl PrintingJournal {
27 pub fn new(mode: JournalPrintingMode) -> Self {
28 Self { mode }
29 }
30}
31
32impl ReadableJournal for PrintingJournal {
33 fn read(&self) -> anyhow::Result<Option<LogReadResult<'_>>> {
34 Ok(None)
35 }
36
37 fn as_restarted(&self) -> anyhow::Result<Box<DynReadableJournal>> {
38 Ok(Box::<PrintingJournal>::default())
39 }
40}
41
42impl WritableJournal for PrintingJournal {
43 fn write<'a>(&'a self, entry: JournalEntry<'a>) -> anyhow::Result<LogWriteResult> {
44 match self.mode {
45 JournalPrintingMode::Text => println!("{entry}"),
46 JournalPrintingMode::Json => {
47 println!("{}", serde_json::to_string_pretty(&entry)?)
48 }
49 }
50 Ok(LogWriteResult {
51 record_start: 0,
52 record_end: entry.estimate_size() as u64,
53 })
54 }
55
56 fn flush(&self) -> anyhow::Result<()> {
57 Ok(())
58 }
59}
60
61impl Journal for PrintingJournal {
62 fn split(self) -> (Box<DynWritableJournal>, Box<DynReadableJournal>) {
63 (
64 Box::<PrintingJournal>::default(),
65 Box::<PrintingJournal>::default(),
66 )
67 }
68}
69
70impl<'a> fmt::Display for JournalEntry<'a> {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 match self {
73 JournalEntry::InitModuleV1 { wasm_hash } => {
74 write!(f, "init-module (hash={wasm_hash:x?})")
75 }
76 JournalEntry::ClearEtherealV1 => {
77 write!(f, "clear-ethereal")
78 }
79 JournalEntry::UpdateMemoryRegionV1 {
80 region,
81 compressed_data,
82 } => write!(
83 f,
84 "memory-update (start={}, end={}, data.len={}, compressed.len={})",
85 region.start,
86 region.end,
87 uncompressed_size(compressed_data.as_ref())
88 .map(|a| a.0)
89 .unwrap_or_else(|_| compressed_data.as_ref().len()),
90 compressed_data.len()
91 ),
92 JournalEntry::ProcessExitV1 { exit_code } => {
93 write!(f, "process-exit (code={exit_code:?})")
94 }
95 JournalEntry::SetThreadV1 {
96 id,
97 call_stack,
98 memory_stack,
99 store_data,
100 ..
101 } => write!(
102 f,
103 "thread-update (id={}, call-stack.len={}, mem-stack.len={}, store-size={}",
104 id,
105 call_stack.len(),
106 memory_stack.len(),
107 store_data.len(),
108 ),
109 JournalEntry::CloseThreadV1 { id, exit_code } => {
110 write!(f, "thread-close (id={id}, code={exit_code:?})")
111 }
112 JournalEntry::FileDescriptorSeekV1 { fd, offset, whence } => {
113 write!(f, "fd-seek (fd={fd}, offset={offset}, whence={whence:?})")
114 },
115 JournalEntry::FileDescriptorWriteV1 {
116 fd, offset, data, ..
117 } => write!(f, "fd-write (fd={fd}, offset={offset}, data.len={})", data.len()),
118 JournalEntry::SetClockTimeV1 { clock_id, time } => {
119 write!(f, "set-clock-time (id={clock_id:?}, time={time})")
120 }
121 JournalEntry::CloseFileDescriptorV1 { fd } => write!(f, "fd-close (fd={fd})"),
122 JournalEntry::OpenFileDescriptorV1 {
123 fd, path, o_flags, ..
124 }
125 | JournalEntry::OpenFileDescriptorV2 {
126 fd, path, o_flags, ..
127 }=> {
128 if o_flags.contains(wasi::Oflags::CREATE) {
129 if o_flags.contains(wasi::Oflags::TRUNC) {
130 write!(f, "fd-create-new (fd={fd}, path={path})")
131 } else if o_flags.contains(wasi::Oflags::EXCL) {
132 write!(f, "fd-create-excl (fd={fd}, path={path})")
133 } else {
134 write!(f, "fd-create (fd={fd}, path={path})")
135 }
136 } else if o_flags.contains(wasi::Oflags::TRUNC) {
137 write!(f, "fd-open-new (fd={fd}, path={path})")
138 } else {
139 write!(f, "fd-open (fd={fd}, path={path})")
140 }
141 }
142 JournalEntry::RenumberFileDescriptorV1 { old_fd, new_fd } => {
143 write!(f, "fd-renumber (old={old_fd}, new={new_fd})")
144 }
145 JournalEntry::DuplicateFileDescriptorV1 {
146 original_fd,
147 copied_fd,
148 } => write!(f, "fd-duplicate (original={original_fd}, copied={copied_fd})"),
149 JournalEntry::DuplicateFileDescriptorV2 {
150 original_fd,
151 copied_fd,
152 cloexec
153 } => write!(f, "fd-duplicate (original={original_fd}, copied={copied_fd}, cloexec={cloexec})"),
154 JournalEntry::CreateDirectoryV1 { fd, path } => {
155 write!(f, "path-create-dir (fd={fd}, path={path})")
156 }
157 JournalEntry::RemoveDirectoryV1 { fd, path } => {
158 write!(f, "path-remove-dir (fd={fd}, path={path})")
159 }
160 JournalEntry::PathSetTimesV1 {
161 path,
162 st_atim,
163 st_mtim,
164 ..
165 } => write!(f, "path-set-times (path={path}, atime={st_atim}, mtime={st_mtim}))"),
166 JournalEntry::FileDescriptorSetTimesV1 {
167 fd,
168 st_atim,
169 st_mtim,
170 ..
171 } => write!(f, "fd-set-times (fd={fd}, atime={st_atim}, mtime={st_mtim})"),
172 JournalEntry::FileDescriptorSetFdFlagsV1 { fd, flags } => {
173 write!(f, "fd-set-fd-flags (fd={fd}, flags={flags:?})")
174 }
175 JournalEntry::FileDescriptorSetFlagsV1 { fd, flags } => {
176 write!(f, "fd-set-flags (fd={fd}, flags={flags:?})")
177 }
178 JournalEntry::FileDescriptorSetRightsV1 {
179 fd,
180 fs_rights_base,
181 fs_rights_inheriting,
182 } => write!(f, "fd-set-rights (fd={fd}, base={fs_rights_base:?}, inherited={fs_rights_inheriting:?})"),
183 JournalEntry::FileDescriptorSetSizeV1 { fd, st_size } => {
184 write!(f, "fd-set-size (fd={fd}, size={st_size})")
185 }
186 JournalEntry::FileDescriptorAdviseV1 {
187 fd, offset, len, ..
188 } => write!(f, "fd-advise (fd={fd}, offset={offset}, len={len})"),
189 JournalEntry::FileDescriptorAllocateV1 { fd, offset, len } => {
190 write!(f, "fd-allocate (fd={fd}, offset={offset}, len={len})")
191 }
192 JournalEntry::CreateHardLinkV1 {
193 old_path, new_path, ..
194 } => write!(f, "path-link (from={old_path}, to={new_path})"),
195 JournalEntry::CreateSymbolicLinkV1 {
196 old_path, new_path, ..
197 } => write!(f, "path-symlink (from={old_path}, to={new_path})"),
198 JournalEntry::UnlinkFileV1 { path, .. } => write!(f, "path-unlink (path={path})"),
199 JournalEntry::PathRenameV1 {
200 old_path, new_path, ..
201 } => write!(f, "path-rename (old-path={old_path}, new-path={new_path})"),
202 JournalEntry::ChangeDirectoryV1 { path } => write!(f, "chdir (path={path})"),
203 JournalEntry::EpollCreateV1 { fd } => write!(f, "epoll-create (fd={fd})"),
204 JournalEntry::EpollCtlV1 { epfd, op, fd, .. } => {
205 write!(f, "epoll-ctl (epfd={epfd}, op={op:?}, fd={fd})")
206 }
207 JournalEntry::TtySetV1 { tty, line_feeds } => write!(f, "tty-set (echo={}, buffering={}, feeds={})", tty.echo, tty.line_buffered, line_feeds),
208 JournalEntry::CreatePipeV1 { read_fd, write_fd } => {
209 write!(f, "fd-pipe (read_fd={read_fd}, write_fd={write_fd})")
210 }
211 JournalEntry::CreateEventV1 {
212 initial_val, fd, ..
213 } => write!(f, "fd-event (fd={fd}, initial={initial_val})"),
214 JournalEntry::PortAddAddrV1 { cidr } => {
215 write!(f, "port-addr-add (ip={}, prefix={})", cidr.ip, cidr.prefix)
216 }
217 JournalEntry::PortDelAddrV1 { addr } => write!(f, "port-addr-del (addr={addr})"),
218 JournalEntry::PortAddrClearV1 => write!(f, "port-addr-clear"),
219 JournalEntry::PortBridgeV1 { network, .. } => {
220 write!(f, "port-bridge (network={network})")
221 }
222 JournalEntry::PortUnbridgeV1 => write!(f, "port-unbridge"),
223 JournalEntry::PortDhcpAcquireV1 => write!(f, "port-dhcp-acquire"),
224 JournalEntry::PortGatewaySetV1 { ip } => write!(f, "port-gateway-set (ip={ip})"),
225 JournalEntry::PortRouteAddV1 {
226 cidr, via_router, ..
227 } => write!(
228 f,
229 "port-route-add (ip={}, prefix={}, via_router={})",
230 cidr.ip, cidr.prefix, via_router
231 ),
232 JournalEntry::PortRouteClearV1 => write!(f, "port-route-clear"),
233 JournalEntry::PortRouteDelV1 { ip } => write!(f, "port-route-del (ip={ip})"),
234 JournalEntry::SocketOpenV1 { af, ty, pt, fd } => {
235 write!(f, "sock-open (fd={fd}, af={af:?}, ty={ty:?}, pt={pt:?})")
236 }
237 JournalEntry::SocketPairV1 { fd1, fd2 } => {
238 write!(f, "sock-pair (fd1={fd1}, fd2={fd2})")
239 }
240 JournalEntry::SocketListenV1 { fd, backlog } => {
241 write!(f, "sock-listen (fd={fd}, backlog={backlog})")
242 }
243 JournalEntry::SocketBindV1 { fd, addr } => {
244 write!(f, "sock-bind (fd={fd}, addr={addr})")
245 }
246 JournalEntry::SocketConnectedV1 {
247 fd,
248 local_addr,
249 peer_addr,
250 } => {
251 write!(f, "sock-connect (fd={fd}, addr={local_addr}, peer={peer_addr})")
252 }
253 JournalEntry::SocketAcceptedV1 {
254 listen_fd,
255 fd,
256 local_addr,
257 peer_addr,
258 ..
259 } => write!(f, "sock-accept (listen-fd={listen_fd}, sock_fd={fd}, addr={local_addr}, peer={peer_addr})"),
260 JournalEntry::SocketJoinIpv4MulticastV1 {
261 fd,
262 multiaddr,
263 iface,
264 } => write!(f, "sock-join-mcast-ipv4 (fd={fd}, addr={multiaddr}, iface={iface})"),
265 JournalEntry::SocketJoinIpv6MulticastV1 {
266 fd,
267 multi_addr: multiaddr,
268 iface,
269 } => write!(f, "sock-join-mcast-ipv6 (fd={fd}, addr={multiaddr}, iface={iface})"),
270 JournalEntry::SocketLeaveIpv4MulticastV1 {
271 fd,
272 multi_addr: multiaddr,
273 iface,
274 } => write!(f, "sock-leave-mcast-ipv4 (fd={fd}, addr={multiaddr}, iface={iface})"),
275 JournalEntry::SocketLeaveIpv6MulticastV1 {
276 fd,
277 multi_addr: multiaddr,
278 iface,
279 } => write!(f, "sock-leave-mcast-ipv6 (fd={fd}, addr={multiaddr}, iface={iface})"),
280 JournalEntry::SocketSendFileV1 {
281 socket_fd,
282 file_fd,
283 offset,
284 count,
285 } => write!(f, "sock-send-file (sock-fd={socket_fd}, file-fd={file_fd}, offset={offset}, count={count})"),
286 JournalEntry::SocketSendToV1 { fd, data, addr, .. } => {
287 write!(f, "sock-send-to (fd={}, data.len={}, addr={})", fd, data.len(), addr)
288 }
289 JournalEntry::SocketSendV1 { fd, data, .. } => {
290 write!(f, "sock-send (fd={}, data.len={}", fd, data.len())
291 }
292 JournalEntry::SocketSetOptFlagV1 { fd, opt, flag } => {
293 write!(f, "sock-set-opt (fd={fd}, opt={opt:?}, flag={flag})")
294 }
295 JournalEntry::SocketSetOptSizeV1 { fd, opt, size } => {
296 write!(f, "sock-set-opt (fd={fd}, opt={opt:?}, size={size})")
297 }
298 JournalEntry::SocketSetOptTimeV1 { fd, ty, time } => {
299 write!(f, "sock-set-opt (fd={fd}, opt={ty:?}, time={time:?})")
300 }
301 JournalEntry::SocketShutdownV1 { fd, how } => {
302 write!(f, "sock-shutdown (fd={fd}, how={how:?})")
303 }
304 JournalEntry::SnapshotV1 { when, trigger } => {
305 write!(f, "snapshot (when={when:?}, trigger={trigger:?})")
306 }
307 }
308 }
309}