Skip to content

Commit 8cb5700

Browse files
committed
sdk log output
1 parent cc28d37 commit 8cb5700

File tree

1 file changed

+39
-92
lines changed

1 file changed

+39
-92
lines changed

src/Peachpie.NET.Sdk/BuildTask.cs

+39-92
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,19 @@ public override bool Execute()
384384
UseShellExecute = false,
385385
};
386386

387+
var output = new DataReceivedEventHandler((o, e) =>
388+
{
389+
if (e.Data != null)
390+
{
391+
if (this.Log.LogMessageFromText(e.Data, MessageImportance.High) == false)
392+
{
393+
// plain text
394+
this.Log.LogMessage(MessageImportance.High, e.Data);
395+
Console.WriteLine(e.Data);
396+
}
397+
}
398+
});
399+
387400
// non-windows?
388401
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
389402
{
@@ -392,29 +405,35 @@ public override bool Execute()
392405
}
393406

394407
//
395-
var process = Process.Start(pi);
396-
var dataReceived = new DataReceivedEventHandler((o, e) =>
408+
this.Log.LogCommandLine(MessageImportance.High, $"{pi.FileName} {pi.Arguments}");
409+
410+
//
411+
using (var process = new Process() { StartInfo = pi, })
397412
{
398-
this.Log.LogMessageFromText(e.Data, MessageImportance.High);
399-
});
413+
process.OutputDataReceived += output;
414+
process.ErrorDataReceived += output;
400415

401-
process.OutputDataReceived += dataReceived;
402-
process.ErrorDataReceived += dataReceived;
416+
process.Start();
417+
418+
process.BeginOutputReadLine();
419+
process.BeginErrorReadLine();
403420

404-
using (var cancellationHandler = cancellation.Register(() =>
405-
{
406-
if (process.HasExited == false)
421+
//
422+
using (var cancellationHandler = cancellation.Register(() =>
423+
{
424+
if (process.HasExited == false)
425+
{
426+
this.Log.LogMessageFromText("Cancelled by user", MessageImportance.High);
427+
// TODO: send signal first
428+
process.Kill();
429+
}
430+
}))
407431
{
408-
this.Log.LogMessageFromText("Cancelled by user", MessageImportance.High);
409-
// TODO: send signal first
410-
process.Kill();
432+
process.WaitForExit();
433+
434+
//
435+
return process.ExitCode == 0;
411436
}
412-
}))
413-
{
414-
process.WaitForExit();
415-
process.StandardOutput.ReadToEnd();
416-
//
417-
return process.ExitCode == 0;
418437
}
419438
}
420439

@@ -464,8 +483,8 @@ static string FlatternArgs(string[] args)
464483
// sanitize {arg}
465484
sb.Append('\"');
466485
sb.Append(arg.Trim()
467-
//.Replace("\\", "\\\\")
468-
//.Replace("\"", "\\\"")
486+
//.Replace("\\", "\\\\")
487+
//.Replace("\"", "\\\"")
469488
);
470489
sb.Append('\"');
471490
}
@@ -530,77 +549,5 @@ bool HasDebugPlus
530549
return false;
531550
}
532551
}
533-
534-
// honestly I don't know why msbuild in VS does not handle Console.Output,
535-
// so we have our custom TextWriter that we pass to Log
536-
sealed class LogWriter : TextWriter
537-
{
538-
TaskLoggingHelper Log { get; }
539-
540-
StringBuilder Buffer { get; } = new StringBuilder();
541-
542-
public override Encoding Encoding => Encoding.UTF8;
543-
544-
public LogWriter(TaskLoggingHelper log)
545-
{
546-
Debug.Assert(log != null);
547-
548-
this.Log = log;
549-
this.NewLine = "\n";
550-
}
551-
552-
bool TryLogCompleteMessage()
553-
{
554-
string line = null;
555-
556-
lock (Buffer) // accessed in parallel
557-
{
558-
// get line from the buffer:
559-
for (int i = 0; i < Buffer.Length; i++)
560-
{
561-
if (Buffer[i] == '\n')
562-
{
563-
line = Buffer.ToString(0, i);
564-
565-
Buffer.Remove(0, i + 1);
566-
}
567-
}
568-
}
569-
570-
//
571-
return line != null && LogCompleteMessage(line);
572-
}
573-
574-
bool LogCompleteMessage(string line)
575-
{
576-
// TODO: following logs only Warnings and Errors,
577-
// to log Info diagnostics properly, parse it by ourselves
578-
579-
return this.Log.LogMessageFromText(line.Trim(), MessageImportance.High);
580-
}
581-
582-
public override void Write(char value)
583-
{
584-
lock (Buffer) // accessed in parallel
585-
{
586-
Buffer.Append(value);
587-
}
588-
589-
if (value == '\n')
590-
{
591-
TryLogCompleteMessage();
592-
}
593-
}
594-
595-
public override void Write(string value)
596-
{
597-
lock (Buffer)
598-
{
599-
Buffer.Append(value);
600-
}
601-
602-
TryLogCompleteMessage();
603-
}
604-
}
605552
}
606553
}

0 commit comments

Comments
 (0)