Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Skip to content Skip to footer

Прозрачное журналирование с wxLog

В wxWidgets есть отличный механизм журналирования wxLog.
С его помощью можно решить проблему ведения лога одновременно в файл и, например, в текстовый контрол.

Если необходимо выводить сообщения в файл не только в ANSI, а и в Unicode,
то очень удобно использовать логгер wxLogStream.
Для его использования, потребуется собрать (если еще не собрано) wxWidgets с выставленной поддержкой std потоков:

1
#define wxUSE_STD_IOSTREAM 1

в файле setup.h. После этого, в приложении потребуется установить целевой логгер:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//wxTestApp.h
// в классе приложения добавить мембер потока
...
#include
...
 
class wxTestApp: public wxApp
{
...
private:
    std::ofstream m_logStream;
...
};
 
//wxTestApp.cpp
// в OnInit() проинициализировать целевой логгер
 
bool wxCDCreatorApp::OnInit() {
    // задаем имя лога (в каталоге приложения, файл <имя файла приложения>.log
    wxString logPath = wxwxPathOnly(wxGetApp().argv[0]) +
wxFileName::GetPathSeparator() + GetAppName() +  wxT(".log");
    m_logStream.open(logPath.GetData());
 
    // не забываем удалить предыдущий логгер
    delete wxLog::SetActiveTarget(new wxLogStream(reinterpret_cast<std::ostream*>(&m_logStream)));
 
    // можно работать
    wxLogMessage(wxT("Запуск приложения..."));
...
// инициализация приложения
}
 
// и в OnExit() закрываем поток
int wxCDCreatorApp::OnExit()
{
    wxLogMessage(wxT("Выход..."));
    m_logStream.close();
    return wxApp::OnExit();
}

После этого, в любой части приложения возмножно с помощью функций wxLogMessage[/sourcecode], wxLogDebug[/sourcecode] (только в отладочной версии),
wxLogTrace[/sourcecode] вести журналирование. В результате работы, в лог пишутся сообщения в примерно таком формате:

1
09:55:05: Запуск приложения...

Leave a comment

0.0/5