Intercept wxLogError calls

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Intercept wxLogError calls

Post by Widgets »

In my current app I need to analyze images of various formats and sizes.
All regular app error messages are redirected to a log window -> wxTextCtrl.

The problem I am running into for some large image files, mainly 50 - 100 MB TIF files, is, that the image handler code does not succeed in allocating enough memory and the call img.IsOk() fails. but without much feedback unless I can intercept the output from wxLogError somehow. (And yes, I understand some of these files could be reduced in size by compression - but that is not an option at this stage)

Can I wrap the block which calls img.IsOk() with some sort of special wxLog target or can I somehow inspect the redirected log output at run time to get a better handle on what size will cause trouble and provide more meaningful feedback for the user?

The basic code abstract looks like:

Code: Select all

   wxMemoryInputStream mis( fileBuffer, fileSize.ToULong() );

   fsImage image( mis );
   if( image.IsOk() )
   {
        .. handle the successful  case
   }
   else // if( !image.IsOk() )
   {
       .... handle the problem cases with proper feedback
       and here is where I would like to get the string passed to wxLogError or wxLogWarning .....
  }
TIA
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Intercept wxLogError calls

Post by doublemax »

You can create a custom log target.
https://docs.wxwidgets.org/trunk/overvi ... _customize
Use the source, Luke!
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Intercept wxLogError calls

Post by Widgets »

Thank you.
I'll have to wrap my head around all of this; fortunately it triggered some memories of another wxLog related issue - years ago - where I needed to filter out only certain messages;
see the earlier discussion: viewtopic.php?t=44070
As time permits, I will try to adapt that code for this problem.
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Intercept wxLogError calls

Post by doublemax »

You need the custom log target only if you want to access the actual error message. If you just want to suppress it, use wxLogNull.
Use the source, Luke!
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Intercept wxLogError calls

Post by Widgets »

Initially, I had used wxLogNull, but I ended up with some very puzzling situations because of errors from deep within the wxWidgets code (failure to allocate enough memory) and that is what got me to this point.
I do want/need to intercept these errors and display them to the user with more details of the real issue rather than have them all lumped together as some 'generic error'.
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Intercept wxLogError calls

Post by Widgets »

After implementing the sub-classed log code, I can intercept the error messages produced by calls to wxLogError & wxLogWarning by code deep inside wxImage:: LoadFile() but the calls to the intercept logic are delayed by way too much time to be usable in the code after the call to LoadFile() returns.

The original code loops through the files in a directory, calling LoadFile for each file and stores the resulting data into a database for later review & processing.
Recently I have run into some files which cause issues because of errors within the file format and/or file sizes, which wxImage cannot allocate sufficient memory for.

The log intercept code is only called after the data for the current file have been processed and I have not found any way to flush the data in a way which will make it available in time to make the error report part of the data to be stored.

FWIW, I also have all of the logging data redirected to a separate text control where it is visible and it was the trigger for me to try and intercept these errors.
For now, my only option left to consider is to 'busy wait' after the call to LoadFile() until the error data becomes available. LoadFile() does return false when an error occurred, so I know when to wait, but this is very far from ideal and from any preferred solution.
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 466
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: Intercept wxLogError calls

Post by New Pagodi »

It sounds like you should be using libtiff directly instead of wxWidgets wrapper for it.
Widgets
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 534
Joined: Thu Jun 01, 2006 4:36 pm
Location: Right here!

Re: Intercept wxLogError calls

Post by Widgets »

That may well be the best option :-)
For my current apps, I am really only interested in getting a thumbnail and checking for minimal compliance with the TIFF standards,
Will have to see if libtiff will give me that much. Though it would be preferable not to have to treat TIFFs as a special case.
Environment: Win 10/11 64-bit & Mint 21.1
MSVC Express 2019/2022
wxWidgets 3.2.2
Post Reply