gnodet-bot commented on code in PR #13832:
URL: https://github.com/apache/maven/pull/13832#discussion_r4607345860


##########
impl/maven-core/src/main/java/org/apache/maven/logging/ProjectBuildLogAppender.java:
##########
@@ -109,29 +115,106 @@ public static void setForkingMojoId(String 
forkingMojoId) {
         }
     }
 
-    public static void updateMdc() {
-        String id = getProjectId();
-        if (id != null) {
-            MDC.put(KEY_PROJECT_ID, id);
-        } else {
-            MDC.remove(KEY_PROJECT_ID);
-        }
+    private static final AtomicLong SEQUENCE = new AtomicLong();
+
+    private static volatile Consumer<LogEvent> reportCapture;
+
+    public static void setReportCapture(Consumer<LogEvent> capture) {
+        ProjectBuildLogAppender.reportCapture = capture;
     }
 
     private final BuildEventListener buildEventListener;
+    private final ConcurrentLinkedQueue<LogEvent> queue = new 
ConcurrentLinkedQueue<>();
+    private final ReentrantLock drainLock = new ReentrantLock();
+    private volatile boolean needsMetadata;
 
     public ProjectBuildLogAppender(BuildEventListener buildEventListener) {
         this.buildEventListener = buildEventListener;
-        MavenSimpleLogger.setLogSink(this::accept);
+        LoggerManager.get().setLogSink(new Sink());
+    }
+
+    public void setNeedsMetadata(boolean needsMetadata) {
+        this.needsMetadata = needsMetadata;
+    }
+
+    private class Sink implements LogSink {
+        @Override
+        public void accept(LogEvent event) {
+            final LogEvent full;
+            String projectId = event.projectId() != null ? event.projectId() : 
getProjectId();
+            String mojoId = event.mojoId() != null ? event.mojoId() : 
getMojoId();
+            if (event instanceof DefaultLogEvent
+                    && Objects.equals(projectId, event.projectId())
+                    && Objects.equals(mojoId, event.mojoId())) {
+                full = event;
+            } else {
+                full = new DefaultLogEvent(
+                        event.timestamp() != null ? event.timestamp() : 
MonotonicClock.now(),
+                        event.level(),
+                        event.message(),
+                        event.loggerName(),
+                        event.throwable(),
+                        event.sourceClassName(),
+                        event.sourceMethodName(),
+                        event.threadId(),
+                        SEQUENCE.getAndIncrement(),
+                        event.threadName(),
+                        projectId,
+                        mojoId,
+                        event.pluginId(),
+                        event.keyValues(),
+                        event.messageTemplate(),
+                        event.messageArgs(),
+                        event.renderedLine());
+            }
+
+            queue.add(full);
+            tryDrain();
+        }
+
+        @Override
+        public boolean needsMetadata() {
+            return needsMetadata || reportCapture != null;
+        }
+    }
+
+    private void tryDrain() {
+        if (drainLock.tryLock()) {
+            try {
+                drain();
+            } finally {
+                drainLock.unlock();
+            }
+            if (!queue.isEmpty() && drainLock.tryLock()) {
+                try {
+                    drain();
+                } finally {
+                    drainLock.unlock();
+                }
+            }
+        }
     }
 
-    protected void accept(String message) {
-        String projectId = MDC.get(KEY_PROJECT_ID);
-        buildEventListener.projectLogMessage(projectId, message);
+    private void drain() {
+        LogEvent event;
+        while ((event = queue.poll()) != null) {
+            buildEventListener.projectLogMessage(event.projectId(), event);
+
+            Consumer<LogEvent> capture = reportCapture;
+            if (capture != null) {
+                capture.accept(event);
+            }
+        }
     }
 
     @Override
     public void close() throws Exception {
-        MavenSimpleLogger.setLogSink(null);
+        drainLock.lock();
+        try {
+            drain();
+        } finally {
+            drainLock.unlock();
+        }
+        LoggerManager.get().setLogSink(null);

Review Comment:
   🟡 **Event loss between `drain()` and `setLogSink(null)`**
   
   After `drainLock.unlock()` and before `setLogSink(null)`, a concurrent 
thread can enqueue an event via `Sink.accept()` and then fail `tryDrain()` 
(lock contention). `setLogSink(null)` then detaches the sink — that event is 
never drained. This silently drops the last log line(s) in a parallel build.
   
   Fix: detach the sink inside the lock, before draining:
   
   ```suggestion
           drainLock.lock();
           try {
               LoggerManager.get().setLogSink(null);
               drain();
           } finally {
               drainLock.unlock();
           }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to