How can I prevent Chromium from writing to the console?

954 Views Asked by At

I'm making a very simple Delphi console application ({$APPTYPE CONSOLE}) with a single TChromiumWindow on the main form. The purpose of the application is to retrieve a webpage, process the HTML and output some JSON to the console. This can not be done using plain HTTP requests due to the nature of the webpage, which requires running some javascript as well.

Everything works as expected, except for one problem. The chromium components output some error messages to the console as well, which makes my JSON invalid! For example, I always get the following two error messages on startup:

[0529/133941.811:ERROR:gpu_process_transport_factory.cc(990)] Lost UI shared context.
[0529/133941.832:ERROR:url_request_context_getter_impl.cc(130)] Cannot use V8 Proxy resolver in single process mode.

Of course the best solution would be to not get any error messages in the first place, but for several reasons (which mostly have to do with company legacy code) I can't for example disable single process mode.

So the next best thing would be to keep these error messages from being printed to the console. I've tried setting

GlobalCEFApp.LogSeverity := LOGSEVERITY_DISABLE;

but that didn't help. Specifying a logfile using GlobalCEFApp.LogFile doesn't help either.

So how can I prevent the Chromium components from writing to the console at all?

2

There are 2 best solutions below

2
J... On

The TChromium component provides an OnConsoleMessage event with signature :

 TOnConsoleMessage = procedure(Sender: TObject; const browser: ICefBrowser; 
                               const message, source: ustring; line: Integer; 
                               out Result: Boolean) of object;  

If you handle this event and set the Result variable to true the message output to the console is suppressed.

0
torres_jeff_br On

Set LogSeverity to LogSeverity.Fatal or n other desired.

var settings = new CefSettings()
        {
            //By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
            CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache"),
            //Set log severity to showup only fatal errors.
            LogSeverity = LogSeverity.Fatal,
        };

        //Autoshutdown when closing
        CefSharpSettings.ShutdownOnExit = true;

        //Perform dependency check to make sure all relevant resources are in our output directory.
        Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);