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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (C) 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

//! Logging utilities for the Simics crate for use with the Simics logging API

#[macro_export]
/// Log an error message via the SIMICS logging API.
///
/// If an object is provided, the message will be logged through that object. If not,
/// the message will be logged through the base `sim` object. Note that errors logged
/// may trigger simulator exit if the simulator is run with the `-werror` flag.
///
/// # Examples
///
/// ```rust,ignore
/// use simics::error;
///
/// let module_instance = get_object("object_name")?;
/// let parameter = 0;
///
/// error!(module_instance, "Error message with parameter {}", parameter);
/// error!("Error message without object with parameter {}", parameter);
/// ```
///
/// # Panics
///
/// This macro will panic of there is an error in the logging call. This is unlikely if the
/// object is valid, but if your use case requires handling errors or is dynamically generating
/// objects without static lifetimes, you should use the internal [`log_error`] API instead.
///
/// This macro will cause simulator exit, triggering a cascading panic, if it is called while
/// the simulator is run with `-werror`.
macro_rules! error {
    ($obj:expr, $fmt:literal $($args:tt)*) => {
        simics::log!(simics::LogLevel::Error, $obj, $fmt $($args)*)
    };
    ($fmt:literal $($args:tt)*) => {
        simics::log!(
            simics::LogLevel::Warn,
            $fmt
            $($args)*
        )
    };
}

#[macro_export]
/// Log a warning message via the SIMICS logging API. If an object is provided, the
/// message will be logged through that object. If not, the message will be logged
/// through the base `sim` object.
///
/// # Examples
///
/// ```rust,ignore
/// use simics::warn;
///
/// let module_instance = get_object("object_name")?;
/// let parameter = 0;
///
/// warn!(module_instance, "Warning message with parameter {}", parameter);
/// warn!("Warning message without object with parameter {}", parameter);
/// ```
///
/// # Panics
///
/// This macro will panic of there is an error in the logging call. This is unlikely if the
/// object is valid, but if your use case requires handling errors or is dynamically generating
/// objects without static lifetimes, you should use the internal [`log_info`] API instead.
macro_rules! warn {
    ($obj:expr, $fmt:literal $($args:tt)*) => {
        simics::log!(simics::LogLevel::Warn, $obj, $fmt $($args)*)
    };
    ($fmt:literal $($args:tt)*) => {
        simics::log!(
            simics::LogLevel::Warn,
            $fmt
            $($args)*
        )
    };
}

#[macro_export]
/// Log an informational message via the SIMICS logging API. If an object is provided, the
/// message will be logged through that object. If not, the message will be logged
/// through the base `sim` object.
///
/// # Examples
///
/// ```rust,ignore
/// use simics::info;
///
/// let module_instance = get_object("object_name")?;
/// let parameter = 0;
///
/// info!(module_instance, "Info message with parameter {}", parameter);
/// info!("Info message without object with parameter {}", parameter);
/// ```
///
/// # Panics
///
/// This macro will panic of there is an error in the logging call. This is unlikely if the
/// object is valid, but if your use case requires handling errors or is dynamically generating
/// objects without static lifetimes, you should use the internal [`log_info`] API instead.
macro_rules! info {
    ($obj:expr, $fmt:literal $($args:tt)*) => {
        simics::log!(simics::LogLevel::Info, $obj, $fmt $($args)*)
    };
    ($fmt:literal $($args:tt)*) => {
        simics::log!(
            simics::LogLevel::Info,
            $fmt
            $($args)*
        )
    };
}

#[macro_export]
/// Log a debug message via the SIMICS logging API. If an object is provided, the
/// message will be logged through that object. If not, the message will be logged
/// through the base `sim` object.
///
/// # Examples
///
/// ```rust,ignore
/// use simics::debug;
///
/// let module_instance = get_object("object_name")?;
/// let parameter = 0;
///
/// debug!(module_instance, "Debug message with parameter {}", parameter);
/// debug!("Debug message without object with parameter {}", parameter);
/// ```
///
/// # Panics
///
/// This macro will panic of there is an error in the logging call. This is unlikely if the
/// object is valid, but if your use case requires handling errors or is dynamically generating
/// objects without static lifetimes, you should use the internal [`log_info`] API instead.
macro_rules! debug {
    ($obj:expr, $fmt:literal $($args:tt)*) => {
        simics::log!(simics::LogLevel::Debug, $obj, $fmt $($args)*)
    };
    ($fmt:literal $($args:tt)*) => {
        simics::log!(
            simics::LogLevel::Debug,
            $fmt
            $($args)*
        )
    };
}

#[macro_export]
/// Log a trace message via the SIMICS logging API. If an object is provided, the
/// message will be logged through that object. If not, the message will be logged
/// through the base `sim` object.
///
/// # Examples
///
/// ```rust,ignore
/// use simics::trace;
///
/// let module_instance = get_object("object_name")?;
/// let parameter = 0;
///
/// trace!(module_instance, "Trace message with parameter {}", parameter);
/// trace!("Trace message without object with parameter {}", parameter);
/// ```
///
/// # Panics
///
/// This macro will panic of there is an error in the logging call. This is unlikely if the
/// object is valid, but if your use case requires handling errors or is dynamically generating
/// objects without static lifetimes, you should use the internal [`log_info`] API instead.
macro_rules! trace {
    ($obj:expr, $fmt:literal $($args:tt)*) => {
        simics::log!(simics::LogLevel::Trace, $obj, $fmt $($args)*)
    };
    ($fmt:literal $($args:tt)*) => {
        simics::log!(
            simics::LogLevel::Trace,
            $fmt
            $($args)*
        )
    };
}

#[macro_export]
/// Log a message via the SIMICS logging API.
///
/// If an object is provided, the message will be logged through that object. If not,
/// the message will be logged through the base `sim` object. [`trace`], [`debug`],
/// [`info`], [`warn`] , and [`error`] messages use this macro internally. This macro
/// takes the log level as its first parameter.
///
/// # Examples
///
/// ```rust,ignore
/// use simics::log;
///
/// let module_instance = get_object("object_name")?;
/// let parameter = 0;
///
/// log!(LogLevel::Debug, module_instance, "Debug message with parameter {}", parameter);
/// log!(LogLevel::Debug, "Debug message without object with parameter {}", parameter);
/// ```
///
/// # Panics
///
/// This macro will panic of there is an error in the logging call. This is unlikely if the
/// object is valid, but if your use case requires handling errors or is dynamically generating
/// objects without static lifetimes, you should use the internal [`log_info`] API instead.
macro_rules! log {
    ($level:expr, $obj:expr, $fmt:literal $($args:tt)*) => {
        match $level {
            simics::LogLevel::Error => {
                #[allow(unnecessary_cast)]
                simics::log_error(
                    $obj as *mut simics::ConfObject,
                    format!($fmt $($args)*),
                ).unwrap_or_else(|e| {
                    panic!(
                        "Fatal error attempting to log message {}: {}",
                        format!($fmt $($args)*),
                        e
                    )
                })
            }
            _ => {
                #[allow(unnecessary_cast)]
                simics::log_info(
                    $level,
                    $obj as *mut simics::ConfObject,
                    format!($fmt $($args)*),
                ).unwrap_or_else(|e| {
                    panic!(
                        "Fatal error attempting to log message {}: {}",
                        format!($fmt $($args)*),
                        e
                    )
                })
            }
        }
    };
    ($level:expr, $fmt:literal $($args:tt)*) => {
        simics::log!(
            $level,
            simics::get_object("sim")
                .unwrap_or_else(|e| panic!("Unable to get base sim object: {e}")),
            $fmt
            $($args)*
        )
    };
}