mono / CppSharp

Tools and libraries to glue C/C++ APIs to high-level languages
MIT License
3.14k stars 518 forks source link

Unify all output messages generators (CppSharp.Diagnostics, Console, native stdout\stderr, CppSharp.Utils.FSM.ConsoleWriter) #1759

Open stachu99 opened 1 year ago

stachu99 commented 1 year ago
  1. In the CppSharp solution is used many various ways to produce messages/logs like CppSharp.Diagnostics, Console.WriteLine, native stdout\stderr (printf), CppSharp.Utils.FSM.ConsoleWriter. Finally all messages are listed at the Console and it's fine. What if I would like to implement own logging solution. I have options: a. The simplest way is run own CppSharp solution as a Process and redirect stdout\stderr, with losted details like:

    • DiagnosticKind for IDiagnostics,
    • CppSharp.Utils.FSM.ConsoleWriter, a message type represented as font color.

    b. Implement CppSharp.Diagnostics.IDiagnostics, redirect Console stdout\stderr and redirect native stdout\stderr (printf). In this option still lose CppSharp.Utils.FSM.ConsoleWriter message type. If is implemented a Console stdout\stderr redirection to own logging implementation and this implementation sinks loggs to Console there has made a loop.

    I thing that the best option would be using CppSharp.Diagnostics for all messages generated by CppSharp and definitively avoid using Console.Write/WriteLine.

    • catching native stdout\stderr and pass through CppSharp.Diagnostics.
    • Console.Write/WriteLine replacing with CppSharp.Diagnostics,
    • CppSharp.Utils.FSM.ConsoleWriter replacing with CppSharp.Diagnostics.

      Personally, I use CppSharp to generate C++/CLI and CSharp code so do not use classes with references to CppSharp.Utils.FSM.ConsoleWriter.

  2. Is there possibility to get more diagnostic logs like errors/warnings from clang compiler at parsing and semantic analysis stages. Similarly to produced output of command 'clang.exe -Xclang -fsyntax-only '.

I have got an issue to catch native stdout\stderr (printf). I use DriverOptions.Verbose turned on and some diagnostic messages are listed at the Console, e.g. compiler arguments, Target triple, includes. I have tried redirecting stdout\stderr to a file or to an AnonymousPipeServerStream using SetStdHandle() method from kernel32.dll and it failed. The file/stream is always empty and all messages are listed at the console. There is a part of code:

        //class member
        [DllImport("Kernel32.dll", SetLastError = true)]
        public static extern int SetStdHandle(int device, IntPtr handle);

        //method code
        var _outFileStream = System.IO.File.Open("stdout.txt", System.IO.FileMode.Create);
        SetStdHandle(-11, _outFileStream.SafeFileHandle.DangerousGetHandle()); //-11 - stdout, -12 - stderr

        //Dispose method code
        _outFileStream.Flush();
        _outFileStream.Dispose();

The file is created and there is none messages. Please, could you provide some worked code to redirect native stdout\stderr or any clue how do this? I would like to avoid to run my CppSharp implementation as a nested Process and redirect the process stdout\stderr.

Best regards, Stachu99

tritao commented 1 year ago

Hey @stachu99, yes, I've always thought the diagnostics system needs some improvements as well. This is something we can improve.

  1. In this option still lose CppSharp.Utils.FSM.ConsoleWriter message type.

This was something we imported from some other code, so we can get rid of it no problem.

I thing that the best option would be using CppSharp.Diagnostics for all messages generated by CppSharp and definitively avoid using Console.Write/WriteLine.

I think so too, we can definitely do this and seems like the simplest option. I don't like too much a library redirecting the standard output and error streams.

Please, could you provide some worked code to redirect native stdout\stderr or any clue how do this?

I don't really have any working code for this at this time, but I can improve the current codebase so everything goes through Diagnostics or similar API. At the moment the priority is to finish the upgrade to Clang 16, after that is done I can improve the diagnostics.

stachu99 commented 1 year ago

Thanks tritao.