Skip to content

Commit ea5850f

Browse files
committed
fix(log): log target name and kind
Instead of all unnecesary field, just track kind and in log. Detailed info can be retrieved from `cargo metadata` or other tools
1 parent b42bec2 commit ea5850f

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

src/cargo/core/compiler/fingerprint/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ pub fn prepare_target(
454454
if !dirty_reason.is_fresh_build() {
455455
logger.log(LogMessage::Rebuild {
456456
package_id: unit.pkg.package_id().to_spec(),
457-
target: unit.target.clone(),
457+
target: (&unit.target).into(),
458458
mode: unit.mode,
459459
cause: dirty_reason.clone(),
460460
});

src/cargo/core/compiler/timings/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ impl<'gctx> Timings<'gctx> {
277277
if let Some(logger) = build_runner.bcx.logger {
278278
logger.log(LogMessage::UnitFinished {
279279
package_id: unit_time.unit.pkg.package_id().to_spec(),
280-
target: unit_time.unit.target.clone(),
280+
target: (&unit_time.unit.target).into(),
281281
mode: unit_time.unit.mode,
282282
duration: unit_time.duration,
283283
rmeta_time: unit_time.rmeta_time,

src/cargo/util/log_message.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use cargo_util_schemas::core::PackageIdSpec;
77
use jiff::Timestamp;
88
use serde::Serialize;
99

10-
use crate::core::Target;
1110
use crate::core::compiler::CompilationSection;
1211
use crate::core::compiler::CompileMode;
1312
use crate::core::compiler::fingerprint::DirtyReason;
@@ -49,6 +48,29 @@ pub enum LogMessage {
4948
},
5049
}
5150

51+
#[derive(Serialize)]
52+
pub struct Target {
53+
name: String,
54+
kind: &'static str,
55+
}
56+
57+
impl From<&crate::core::Target> for Target {
58+
fn from(target: &crate::core::Target) -> Self {
59+
use crate::core::TargetKind;
60+
Self {
61+
name: target.name().to_string(),
62+
kind: match target.kind() {
63+
TargetKind::Lib(..) => "lib",
64+
TargetKind::Bin => "bin",
65+
TargetKind::Test => "test",
66+
TargetKind::Bench => "bench",
67+
TargetKind::ExampleLib(..) | TargetKind::ExampleBin => "example",
68+
TargetKind::CustomBuild => "build-script",
69+
},
70+
}
71+
}
72+
}
73+
5274
impl LogMessage {
5375
/// Serializes this message as a JSON log line directly to the writer.
5476
pub fn write_json_log<W: Write>(&self, writer: &mut W, run_id: &str) -> std::io::Result<()> {

tests/testsuite/build_analysis.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ fn log_msg_timing_info() {
127127
str![[r#"
128128
[
129129
{
130-
"reason": "build-started",
131-
"...": "{...}"
130+
"...": "{...}",
131+
"reason": "build-started"
132132
},
133133
{
134134
"duration": "{...}",
@@ -137,7 +137,10 @@ fn log_msg_timing_info() {
137137
"reason": "unit-finished",
138138
"rmeta_time": "{...}",
139139
"run_id": "[..]T[..]Z-[..]",
140-
"target": "{...}",
140+
"target": {
141+
"kind": "lib",
142+
"name": "bar"
143+
},
141144
"timestamp": "[..]T[..]Z"
142145
},
143146
{
@@ -147,7 +150,10 @@ fn log_msg_timing_info() {
147150
"reason": "unit-finished",
148151
"rmeta_time": "{...}",
149152
"run_id": "[..]T[..]Z-[..]",
150-
"target": "{...}",
153+
"target": {
154+
"kind": "lib",
155+
"name": "foo"
156+
},
151157
"timestamp": "[..]T[..]Z"
152158
}
153159
]
@@ -241,7 +247,10 @@ fn log_rebuild_reason_file_changed() {
241247
"package_id": "path+[ROOTURL]/foo#0.0.0",
242248
"reason": "rebuild",
243249
"run_id": "[..]T[..]Z-[..]",
244-
"target": "{...}",
250+
"target": {
251+
"kind": "lib",
252+
"name": "foo"
253+
},
245254
"timestamp": "[..]T[..]Z"
246255
},
247256
{

0 commit comments

Comments
 (0)