[Solved] supressing gtk warning messages

Post your Gambas programming questions here.
Post Reply
User avatar
BruceSteers
Posts: 654
Joined: Thursday 23rd July 2020 5:20pm

[Solved] supressing gtk warning messages

Post by BruceSteers » Saturday 07th August 2021 8:38pm

Anyone know of a way to suppress the gtk warning messages?

The best answer i have so far is this "hack"...
Uses gb.args

if the arg -q or --quiet is given then before the app loads it launches itself with any and all args plus "2>/dev/null" to redirect the error output and an additional arg -s.

when the 2nd run instance quits the 1st one that launched the second one also quits before it's even done anything.

the check works earliest in the _new() method. in _init() gb.args does not work.

PS. as it runs it's own exe you will need to "Make Executable" before it can work

Public Sub _new()
 
  Args.Begin()
  Dim bQuiet As Boolean = Args.Has("q", "quiet", "supress gtk error messages")
  If bQuiet Then bQuiet = (Args.Has("s", "supressed", "supressed gtk error warnings") == False)
  Args.End()

  If bQuiet And File.In.IsTerm Then  ' if we are not run from a terminal then no need for any of this
    Dim sRestOfargs As String[] = Args.All.Copy()

    ' the next 2 lines just handle if being run from the IDE and make Arg[0] be executable name.
    If Not InStr(sRestOfargs[0], "/") Then sRestOfargs[0] = Application.Path &/ Application.Name
    If File.Ext(sRestOfargs[0]) <> "gambas" Then sRestOfargs[0] &= ".gambas"  

    sRestOfargs.Insert(["-s", "2>/dev/null"])
    Shell sRestOfargs.Join(" ")
    Me.Close
    Return
  Endif

End
Attached is a simple test app
It's a form with a tiny little TextArea in it, so small it produces warnings on my system when i move the mouse around the window...
(Test:79673): Gdk-CRITICAL **: 20:26:58.775: gdk_window_is_visible: assertion 'GDK_IS_WINDOW (window)' failed

Run with -q and all is quiet.
./Test.gambas -q

Of course this method will suppress ANY error messages ,
shell commands can be fixed by adding '>2&1'
Shell "/run/mycommand 2>&1"
that redirects error output to stdout and stdout is still showing

Please somebody tell me there's a better way...
Attachments
Test-0.0.28.zip
(116.22 KiB) Downloaded 16 times
Last edited by BruceSteers on Monday 09th August 2021 2:55pm, edited 1 time in total.
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
BruceSteers
Posts: 654
Joined: Thursday 23rd July 2020 5:20pm

Re: supressing gtk warning messages

Post by BruceSteers » Saturday 07th August 2021 9:35pm

BruceSteers wrote:
Saturday 07th August 2021 8:38pm

PS. as it runs it's own exe you will need to "Make Executable" before it can work
or do this i guess if you want to add something like this to your own projects ...
(see additional commented routine at line 15 to compile exe if run from IDE)

Note: i also did away with the -s arg and made it just remove the -q or --quiet arg on relaunch
Public Sub _new()

  Dim sRestOfargs As String[] = Args.All.Copy()

  Args.Begin()
    Dim bQuiet As Boolean = Args.Has("q", "quiet", "supress gtk error messages")
  Args.End()

  If bQuiet And File.In.IsTerm Then
    If Not InStr(sRestOfargs[0], "/") Then sRestOfargs[0] = Application.Path &/ Application.Name
    If File.Ext(sRestOfargs[0]) <> "gambas" Then sRestOfargs[0] &= ".gambas"

' With this command the project is compiled before it's relaunched (If run from the IDE Arg[0] has no path).
   If Not InStr(Args[0], "/") Then 
     Shell "cd '" & File.Dir(sRestOfargs[0]) & "'\ngbc3 -xawg\ngba3\necho 'Compiling complete'" Wait
   Endif

    If sRestOfargs.Exist("-q") Then sRestOfargs.Remove(sRestOfargs.Find("-q"))
    If sRestOfargs.Exist("--quiet") Then sRestOfargs.Remove(sRestOfargs.Find("--quiet"))

    sRestOfargs.Add("2>/dev/null")

    Shell sRestOfargs.Join(" ")
    Me.Close
    Return
  Endif

End

Making the exe compile itself first before relaunching helps when developing as in the IDE you can just hit the run button without having to compile first.
You have to compile before test running the app in the IDE and using the -q arg to suppress the gtk garbage as the app will be relaunching itself if you do not "make exe" then the relaunch runs the old exe.
So the addition above will make the project compile a new exe (with your changes) before relaunch.

Note: DO NOT APPEND -q to other args. Ie. if you have other args say -V and -s you CANNOT use -qVs or -Vsq etc you MUST use something like '-Vs -q'
Any of the other args can be appended and be okay but the -q MUST be on it's own to be removed or the app will just cycle relaunching.
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
BruceSteers
Posts: 654
Joined: Thursday 23rd July 2020 5:20pm

Re: supressing gtk warning messages

Post by BruceSteers » Monday 09th August 2021 2:55pm

Thanks to BrianG on the gambas m/l for his help, I now have this solution.

The code below will divert outgoing Error/Debug messages through the File_Read() event where i check for Gtk- and Gdk- messages and Print the error if not.

the downside is that your program will only output to StdOut with Print (not really much of a downside)


' Gambas class file

Extern dup2(OldFD As Integer, newfd As Integer) In "libc:6"
Private Writer As File
Private Reader As File
Private restore As File

Public Sub _new()
  Writer = Open Pipe "/tmp/testpipeout" For Write
  restore = File.Err
  dup2(Writer.handle, 2)
  Reader = Open Pipe "/tmp/testpipeout" For Read Watch
End

Public Sub File_read()

  Dim buffer As String

  buffer = Read #Last, -Lof(Last)
  For Each sLine As String In Split(buffer, "\n")
    If Not sLine Then Continue
    If sLine Like "(" & Application.Name & ":*): G*k-*" Then Continue
    Print sLine
  Next

End

Public Sub Form_Close()
  If Writer Then Writer.Close()
  If Reader Then Reader.Close()
  dup2(restore.handle, 2)
End

' This is a test button
Public Sub ButtonTest_Click()

  Print "Print Something"
  Debug "Debug Something"

End
Wishing well
Bruce

If at first you don't succeed , try it differently.

Post Reply