Is there a way to configure some log level go to file and others to GUI? Topic is solved

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
User avatar
JohnKnow
Knows some wx things
Knows some wx things
Posts: 41
Joined: Tue Aug 31, 2021 4:14 am

Is there a way to configure some log level go to file and others to GUI?

Post by JohnKnow »

For example, I want make verbose log go to file.log. And status, message, error go GUI.
No, I know everything.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Is there a way to configure some log level go to file and others to GUI?

Post by ONEEYEMAN »

Hi,
Why not check the docs?
Use different classes derived from wxLog.

Thank you.
User avatar
JohnKnow
Knows some wx things
Knows some wx things
Posts: 41
Joined: Tue Aug 31, 2021 4:14 am

Re: Is there a way to configure some log level go to file and others to GUI?

Post by JohnKnow »

Hi ONEEYEMAN,
Thank you for the reply.

I have read the document, but it says:
"Only one log target is active at any moment"

So that it is impossible to create multiple target and using them at the same time.

I also tried subclass wxLog, and dispatch logs to wxLogGUI and wxLogStderr.
But nothing come after calling LogGUI's LogRecord(level, log, info), while LogStderr's LogRecord(level, log, info) works.

Code: Select all

CommonLogTarget::CommonLogTarget(wxLog *pGuiLog) : m_pGuiLog(pGuiLog)
{
	m_pLogFile = fopen("log.txt", "a+");
	m_pFileLog = new wxLogStderr(m_pLogFile);
	if(m_pGuiLog == NULL)
	{
		m_pGuiLog = new wxLogGui();
	}
}

void CommonLogTarget::DoLogRecord(wxLogLevel level, const wxString &msg, const wxLogRecordInfo &info)
{
	wxString log;
	wxDateTime time(info.timestamp);
	log.Printf("%s:%hu %s: %s", time.FormatISOCombined(' '), time.GetMillisecond(), info.func, msg);

	if(level < wxLOG_Info)
	{
		m_pGuiLog->LogRecord(level, log, info);
	}

	m_pFileLog->LogRecord(level, log, info);
}
In wxApp:

Code: Select all

	
	wxLog::SetActiveTarget(new CommonLogTarget(new wxLogGui()));
	wxLog::SetVerbose();
Last edited by JohnKnow on Sat Oct 16, 2021 9:59 am, edited 1 time in total.
No, I know everything.
User avatar
JohnKnow
Knows some wx things
Knows some wx things
Posts: 41
Joined: Tue Aug 31, 2021 4:14 am

Re: Is there a way to configure some log level go to file and others to GUI?

Post by JohnKnow »

Hi PB,
Thank you for the reply.

All logs will go to both GUI and file by using wxLogChain.

I'm trying to make verbose log go to file and message,warning,error,status go GUI.
No, I know everything.
User avatar
JohnKnow
Knows some wx things
Knows some wx things
Posts: 41
Joined: Tue Aug 31, 2021 4:14 am

Re: Is there a way to configure some log level go to file and others to GUI?

Post by JohnKnow »

I found the reason.
It is because wxLogGui needs a flush to show logs.
Add below code to CommonLog then it works

Code: Select all

void CommonLogTarget::Flush()
{
	m_pGuiLog->Flush();
	m_pFileLog->Flush();
}
No, I know everything.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Is there a way to configure some log level go to file and others to GUI?

Post by PB »

JohnKnow wrote: Sat Oct 16, 2021 9:57 am All logs will go to both GUI and file by using wxLogChain.
Sorry, I misread your post. Adding a "filter" log target while using the default GUI target indeed seems impossible.
User avatar
JohnKnow
Knows some wx things
Knows some wx things
Posts: 41
Joined: Tue Aug 31, 2021 4:14 am

Re: Is there a way to configure some log level go to file and others to GUI?

Post by JohnKnow »

Thank you for telling me filter, PB.
I think filter can achieve my precious goal.

Besides previous goal, I find subclassing wxlog can also change log format handy, I plan to keep current implementation.
No, I know everything.
Post Reply