wxLogStream doesn't work on macOS and Linux but on Windows

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
hneubauer
Earned a small fee
Earned a small fee
Posts: 20
Joined: Wed May 13, 2020 4:08 pm

wxLogStream doesn't work on macOS and Linux but on Windows

Post by hneubauer » Mon Mar 15, 2021 9:27 am

Possibly someone has the same problem?

So far we have created our own log target using wxLogStream.
In the current wxWidgets version (3.1.4) this no longer works for macOS and Linux. On Windows everything runs smoothly.

Code: Select all

   wxString path = wxS("/Users/<username>/Library/Preferences/<softwarename>/") + wxS( "log.txt" );
   std::string logpath = ( const char* )path.char_str();
   wxLog* logger=new wxLogStream( new std::ofstream( logpath.data() ) );
   wxLog::SetActiveTarget( logger );
   wxLogMessage(wxS("Hello World"))
The log file will be created but it stays empty. On macOS it gets a "1" after exit. On Windows all is ok.

There is a workaround using wxLogStderr and works on all three platforms.

Code: Select all

   wxString path = wxS("/Users/<username>/Library/Preferences/<softwarename>/") + wxS( "log.txt" );
   std::string logpath = ( const char* )path.char_str();
   FILE* fp = fopen(logpath.data(), "w");
   wxLog *logger = new wxLogStderr(fp);
   wxLog::SetActiveTarget( logger );
   wxLogMessage(wxS("Hello World"));
The "open" seems relevant. Can anyone reproduce the issue or am I doing something wrong?

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 5154
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxLogStream doesn't work on macOS and Linux but on Windows

Post by ONEEYEMAN » Mon Mar 15, 2021 3:53 pm

Hi,
Do you have the rights to write to the directory where you create the log?

Thank you.

hneubauer
Earned a small fee
Earned a small fee
Posts: 20
Joined: Wed May 13, 2020 4:08 pm

Re: wxLogStream doesn't work on macOS and Linux but on Windows

Post by hneubauer » Wed Mar 17, 2021 12:16 pm

Yes. If there is no log file, it will be created, but it stays empty.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 5154
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxLogStream doesn't work on macOS and Linux but on Windows

Post by ONEEYEMAN » Wed Mar 17, 2021 3:17 pm

Hi,
What is the latest wxWidgets version where it was working fine?
Did you try to use different directory for logging?
Also - is there reason you wanted to create your own log target? Is wxWidgets wxLOg-based classes not enough?

Thank you.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 3042
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxLogStream doesn't work on macOS and Linux but on Windows

Post by PB » Wed Mar 17, 2021 5:11 pm

I do not use Linux but tried to run this code on Ubuntu 20.04 in VirtualBox and it works as expected (wxWidgets 3.1.4 compiled for GTK3):

Code: Select all

#include <fstream>

#include <wx/wx.h>

class MyApp : public wxApp
{
public:
    bool OnInit() override
    {
        std::ofstream ofs("mylog.txt");
        wxLogStream   logger(&ofs);
        wxLog*        oldLogger = wxLog::SetActiveTarget(&logger);

        wxLogMessage("Hello World");
        wxLog::SetActiveTarget(oldLogger);

        return false;
    }
}; wxIMPLEMENT_APP(MyApp);
Can you reproduce the issue with the verbatim code from above? If not, are you sure your file/stream/log is properly closed and thus flushed?

Post Reply