From 10f235b43c9a5e5c23f94140c03c54b6ce24af50 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 30 Jan 2024 18:45:13 +0100 Subject: [PATCH 1/2] Avoid implicit wxString conversions in wxLog{Status,SysError}() They don't compile when wxNO_IMPLICIT_WXSTRING_ENCODING is defined. Closes #24267. --- include/wx/log.h | 4 ++-- tests/allheaders.cpp | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/wx/log.h b/include/wx/log.h index 83e08f7e10..12e287a542 100644 --- a/include/wx/log.h +++ b/include/wx/log.h @@ -1208,7 +1208,7 @@ WXDLLIMPEXP_BASE wxString wxSysErrorMsgStr(unsigned long nErrCode = 0); // and it will have changed already by then (in fact it even changes when // wxString::Format() is called because of vsnprintf() inside it so it can // change even much sooner) -#define wxLOG_KEY_SYS_ERROR_CODE "wx.sys_error" +#define wxLOG_KEY_SYS_ERROR_CODE wxASCII_STR("wx.sys_error") #define wxLogSysError \ wxDO_LOG_IF_ENABLED_WITH_FUNC(Error, MaybeStore(wxLOG_KEY_SYS_ERROR_CODE, \ @@ -1224,7 +1224,7 @@ WXDLLIMPEXP_BASE wxString wxSysErrorMsgStr(unsigned long nErrCode = 0); #if wxUSE_GUI // wxLogStatus() is similar to wxLogSysError() as it allows to optionally // specify the frame to which the message should go - #define wxLOG_KEY_FRAME "wx.frame" + #define wxLOG_KEY_FRAME wxASCII_STR("wx.frame") #define wxLogStatus \ wxDO_LOG_IF_ENABLED_WITH_FUNC(Status, MaybeStore(wxLOG_KEY_FRAME).Log) diff --git a/tests/allheaders.cpp b/tests/allheaders.cpp index 0d30d1c4a6..5b5527f455 100644 --- a/tests/allheaders.cpp +++ b/tests/allheaders.cpp @@ -411,4 +411,6 @@ TEST_CASE("wxNO_IMPLICIT_WXSTRING_ENCODING", "[string]") // wxNO_IMPLICIT_WXSTRING_ENCODING must be set s = "Hello, implicit encoding"; #endif + + wxLogSysError(wxASCII_STR("Bogus error for testing")); } From fb8d7b7de0f2576c1d16d989e7d497671390b865 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 30 Jan 2024 18:47:06 +0100 Subject: [PATCH 2/2] Take C string in wxLogger::MaybeStore() to simplify We don't need to take wxString here, when we only ever call this function ourselves with ASCII strings only. This should result in slightly shorter generated code too, as the conversion from C string to wxString now happens only in a single place inside MaybeStore() instead of being done in all places calling wxLogSysError() or wxLogStatus(). --- include/wx/log.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/include/wx/log.h b/include/wx/log.h index 12e287a542..fa54e9243f 100644 --- a/include/wx/log.h +++ b/include/wx/log.h @@ -921,12 +921,15 @@ public: // indicates that we may have an extra first argument preceding the format // string and that if we do have it, we should store it in m_info using the // given key (while by default 0 value will be used) - wxLogger& MaybeStore(const wxString& key, wxUIntPtr value = 0) + wxLogger& MaybeStore(const char* key, wxUIntPtr value = 0) { wxASSERT_MSG( m_optKey.empty(), "can only have one optional value" ); - m_optKey = key; - m_info.StoreValue(key, value); + // We only use keys defined in this file and we can be sure they + // contain ASCII characters only. + m_optKey = wxString::FromAscii(key); + + m_info.StoreValue(m_optKey, value); return *this; } @@ -1208,7 +1211,7 @@ WXDLLIMPEXP_BASE wxString wxSysErrorMsgStr(unsigned long nErrCode = 0); // and it will have changed already by then (in fact it even changes when // wxString::Format() is called because of vsnprintf() inside it so it can // change even much sooner) -#define wxLOG_KEY_SYS_ERROR_CODE wxASCII_STR("wx.sys_error") +#define wxLOG_KEY_SYS_ERROR_CODE "wx.sys_error" #define wxLogSysError \ wxDO_LOG_IF_ENABLED_WITH_FUNC(Error, MaybeStore(wxLOG_KEY_SYS_ERROR_CODE, \ @@ -1224,7 +1227,7 @@ WXDLLIMPEXP_BASE wxString wxSysErrorMsgStr(unsigned long nErrCode = 0); #if wxUSE_GUI // wxLogStatus() is similar to wxLogSysError() as it allows to optionally // specify the frame to which the message should go - #define wxLOG_KEY_FRAME wxASCII_STR("wx.frame") + #define wxLOG_KEY_FRAME "wx.frame" #define wxLogStatus \ wxDO_LOG_IF_ENABLED_WITH_FUNC(Status, MaybeStore(wxLOG_KEY_FRAME).Log)