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 »

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: 7449
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 »

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 »

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: 7449
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 »

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: 4183
Joined: Sun Jan 03, 2010 5:45 pm

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

Post by PB »

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