Console: misc refactors

- use C++20 contains where appropriate
- add nolint redundant qualifier
- remove old todo
- remove redundant enum name
- add const
- remove redundant classname
- remove redundant inline
- remove redundant namespace
This commit is contained in:
bofdahof
2025-03-29 15:20:24 +10:00
committed by Kacper Donat
parent 8336d8c475
commit d4ecf95ca5
2 changed files with 232 additions and 233 deletions

View File

@@ -485,7 +485,6 @@ using PyMethodDef = struct PyMethodDef;
// clang-format on
// NOLINTEND(bugprone-reserved-identifier,bugprone-macro-parentheses,cppcoreguidelines-macro-usage,cppcoreguidelines-avoid-do-while)
// TODO: Get rid of this typedef
using ConsoleMsgFlags = unsigned int;
namespace Base
@@ -494,8 +493,8 @@ namespace Base
#ifndef FC_LOG_NO_TIMING
inline FC_DURATION GetDuration(FC_TIME_POINT& tp)
{
auto tnow = std::chrono::FC_TIME_CLOCK::now();
auto dc = std::chrono::duration_cast<FC_DURATION>(tnow - tp);
const auto tnow = std::chrono::FC_TIME_CLOCK::now();
const auto dc = std::chrono::duration_cast<FC_DURATION>(tnow - tp);
tp = tnow;
return dc;
}
@@ -580,20 +579,20 @@ public:
/**
* Returns whether a LogStyle category is active or not
*/
bool isActive(Base::LogStyle category) const
bool isActive(const LogStyle category) const
{
switch (category) {
case Base::LogStyle::Log:
case LogStyle::Log:
return bLog;
case Base::LogStyle::Warning:
case LogStyle::Warning:
return bWrn;
case Base::LogStyle::Error:
case LogStyle::Error:
return bErr;
case Base::LogStyle::Message:
case LogStyle::Message:
return bMsg;
case Base::LogStyle::Critical:
case LogStyle::Critical:
return bCritical;
case Base::LogStyle::Notification:
case LogStyle::Notification:
return bNotification;
}
@@ -712,81 +711,78 @@ public:
Notification can be direct or via queue.
*/
template<Base::LogStyle,
Base::IntendedRecipient = Base::IntendedRecipient::All,
Base::ContentType = Base::ContentType::Untranslated,
template<LogStyle,
IntendedRecipient = IntendedRecipient::All,
ContentType = ContentType::Untranslated,
typename... Args>
inline void Send(const std::string& notifiername, const char* pMsg, Args&&... args);
void Send(const std::string& notifiername, const char* pMsg, Args&&... args);
/// Prints a Message
template<typename... Args>
inline void Message(const char* pMsg, Args&&... args);
void Message(const char* pMsg, Args&&... args);
/// Prints a warning Message
template<typename... Args>
inline void Warning(const char* pMsg, Args&&... args);
void Warning(const char* pMsg, Args&&... args);
/// Prints a error Message
template<typename... Args>
inline void Error(const char* pMsg, Args&&... args);
void Error(const char* pMsg, Args&&... args);
/// Prints a log Message
template<typename... Args>
inline void Log(const char* pMsg, Args&&... args);
void Log(const char* pMsg, Args&&... args);
/// Prints a Critical Message
template<typename... Args>
inline void Critical(const char* pMsg, Args&&... args);
void Critical(const char* pMsg, Args&&... args);
/// Sends a User Notification
template<typename... Args>
inline void UserNotification(const char* pMsg, Args&&... args);
void UserNotification(const char* pMsg, Args&&... args);
/// Sends an already translated User Notification
template<typename... Args>
inline void UserTranslatedNotification(const char* pMsg, Args&&... args);
void UserTranslatedNotification(const char* pMsg, Args&&... args);
/// Prints a Message with source indication
template<typename... Args>
inline void Message(const std::string& notifier, const char* pMsg, Args&&... args);
void Message(const std::string& notifier, const char* pMsg, Args&&... args);
/// Prints a warning Message with source indication
template<typename... Args>
inline void Warning(const std::string& notifier, const char* pMsg, Args&&... args);
void Warning(const std::string& notifier, const char* pMsg, Args&&... args);
template<typename... Args>
inline void DeveloperWarning(const std::string& notifier, const char* pMsg, Args&&... args);
void DeveloperWarning(const std::string& notifier, const char* pMsg, Args&&... args);
template<typename... Args>
inline void UserWarning(const std::string& notifier, const char* pMsg, Args&&... args);
void UserWarning(const std::string& notifier, const char* pMsg, Args&&... args);
template<typename... Args>
inline void
TranslatedUserWarning(const std::string& notifier, const char* pMsg, Args&&... args);
void TranslatedUserWarning(const std::string& notifier, const char* pMsg, Args&&... args);
/// Prints a error Message with source indication
template<typename... Args>
inline void Error(const std::string& notifier, const char* pMsg, Args&&... args);
void Error(const std::string& notifier, const char* pMsg, Args&&... args);
template<typename... Args>
inline void DeveloperError(const std::string& notifier, const char* pMsg, Args&&... args);
void DeveloperError(const std::string& notifier, const char* pMsg, Args&&... args);
template<typename... Args>
/// A noexcept DeveloperError for use in destructors. When compiled in debug, terminates via an
/// assert. In release, the exception is silently caught and dropped.
inline void
DestructorError(const std::string& notifier, const char* pMsg, Args&&... args) noexcept;
void DestructorError(const std::string& notifier, const char* pMsg, Args&&... args) noexcept;
template<typename... Args>
inline void UserError(const std::string& notifier, const char* pMsg, Args&&... args);
void UserError(const std::string& notifier, const char* pMsg, Args&&... args);
template<typename... Args>
inline void TranslatedUserError(const std::string& notifier, const char* pMsg, Args&&... args);
void TranslatedUserError(const std::string& notifier, const char* pMsg, Args&&... args);
/// Prints a log Message with source indication
template<typename... Args>
inline void Log(const std::string& notifier, const char* pMsg, Args&&... args);
void Log(const std::string& notifier, const char* pMsg, Args&&... args);
/// Prints a Critical Message with source indication
template<typename... Args>
inline void Critical(const std::string& notifier, const char* pMsg, Args&&... args);
void Critical(const std::string& notifier, const char* pMsg, Args&&... args);
/// Sends a User Notification with source indication
template<typename... Args>
inline void UserNotification(const std::string& notifier, const char* pMsg, Args&&... args);
void UserNotification(const std::string& notifier, const char* pMsg, Args&&... args);
/// Sends an already translated User Notification with source indication
template<typename... Args>
inline void
UserTranslatedNotification(const std::string& notifier, const char* pMsg, Args&&... args);
void UserTranslatedNotification(const std::string& notifier, const char* pMsg, Args&&... args);
// Notify a message directly to observers
template<Base::LogStyle,
Base::IntendedRecipient = Base::IntendedRecipient::All,
Base::ContentType = Base::ContentType::Untranslated>
inline void Notify(const std::string& notifiername, const std::string& msg);
template<LogStyle,
IntendedRecipient = IntendedRecipient::All,
ContentType = ContentType::Untranslated>
void Notify(const std::string& notifiername, const std::string& msg);
/// Attaches an Observer to FCConsole
void AttachObserver(ILogger* pcObserver);
@@ -815,19 +811,19 @@ public:
};
/// Enables or disables message types of a certain console observer
ConsoleMsgFlags SetEnabledMsgType(const char* sObs, ConsoleMsgFlags type, bool on);
ConsoleMsgFlags SetEnabledMsgType(const char* sObs, ConsoleMsgFlags type, bool on) const;
/// Checks if message types of a certain console observer are enabled
bool IsMsgTypeEnabled(const char* sObs, FreeCAD_ConsoleMsgType type) const;
void SetConnectionMode(ConnectionMode mode);
int* GetLogLevel(const char* tag, bool create = true);
void SetDefaultLogLevel(int level)
void SetDefaultLogLevel(const int level)
{
_defaultLogLevel = level;
}
inline int LogLevel(int level) const
int LogLevel(const int level) const
{
return level < 0 ? _defaultLogLevel : level;
}
@@ -840,10 +836,10 @@ public:
static PyMethodDef Methods[];
void Refresh();
void Refresh() const;
void EnableRefresh(bool enable);
inline constexpr FreeCAD_ConsoleMsgType getConsoleMsg(Base::LogStyle style);
constexpr FreeCAD_ConsoleMsgType getConsoleMsg(LogStyle style);
private:
// python exports goes here +++++++++++++++++++++++++++++++++++++++++++
@@ -879,7 +875,7 @@ public:
ConsoleSingleton& operator=(ConsoleSingleton&&) = delete;
private:
void postEvent(ConsoleSingleton::FreeCAD_ConsoleMsgType type,
void postEvent(FreeCAD_ConsoleMsgType type,
IntendedRecipient recipient,
ContentType content,
const std::string& notifiername,
@@ -888,7 +884,7 @@ private:
IntendedRecipient recipient,
ContentType content,
const std::string& notifiername,
const std::string& msg);
const std::string& msg) const;
// singleton
static void Destruct();
@@ -912,16 +908,15 @@ inline ConsoleSingleton& Console()
return ConsoleSingleton::Instance();
}
inline constexpr ConsoleSingleton::FreeCAD_ConsoleMsgType
ConsoleSingleton::getConsoleMsg(Base::LogStyle style)
constexpr ConsoleSingleton::FreeCAD_ConsoleMsgType ConsoleSingleton::getConsoleMsg(LogStyle style)
{
constexpr std::array msgTypes {// In order of Base::LogStyle
FreeCAD_ConsoleMsgType::MsgType_Wrn,
FreeCAD_ConsoleMsgType::MsgType_Txt,
FreeCAD_ConsoleMsgType::MsgType_Err,
FreeCAD_ConsoleMsgType::MsgType_Log,
FreeCAD_ConsoleMsgType::MsgType_Critical,
FreeCAD_ConsoleMsgType::MsgType_Notification};
constexpr std::array msgTypes {// In order of LogStyle
MsgType_Wrn,
MsgType_Txt,
MsgType_Err,
MsgType_Log,
MsgType_Critical,
MsgType_Notification};
return msgTypes.at(static_cast<std::size_t>(style));
}
@@ -959,11 +954,11 @@ public:
bool refresh;
LogLevel(const char* tag,
bool print_tag = true,
int print_src = 0,
bool print_time = false,
bool add_eol = true,
bool refresh = false)
const bool print_tag = true,
const int print_src = 0,
const bool print_time = false,
const bool add_eol = true,
const bool refresh = false)
: tag(tag)
, lvl(*Console().GetLogLevel(tag))
, print_tag(print_tag)
@@ -973,7 +968,7 @@ public:
, refresh(refresh)
{}
bool isEnabled(int lev) const
bool isEnabled(const int lev) const
{
return lev <= level();
}
@@ -1005,94 +1000,95 @@ public:
* @see UserTranslatedNotification
*/
template<typename... Args>
inline void Base::ConsoleSingleton::Message(const char* pMsg, Args&&... args)
void Base::ConsoleSingleton::Message(const char* pMsg, Args&&... args)
{
Message(std::string(""), pMsg, std::forward<Args>(args)...);
}
template<typename... Args>
inline void
Base::ConsoleSingleton::Message(const std::string& notifier, const char* pMsg, Args&&... args)
void Base::ConsoleSingleton::Message(const std::string& notifier, const char* pMsg, Args&&... args)
{
Send<Base::LogStyle::Message>(notifier, pMsg, std::forward<Args>(args)...);
Send<LogStyle::Message>(notifier, pMsg, std::forward<Args>(args)...);
}
template<typename... Args>
inline void Base::ConsoleSingleton::Warning(const char* pMsg, Args&&... args)
void Base::ConsoleSingleton::Warning(const char* pMsg, Args&&... args)
{
Warning(std::string(""), pMsg, std::forward<Args>(args)...);
}
template<typename... Args>
inline void
Base::ConsoleSingleton::Warning(const std::string& notifier, const char* pMsg, Args&&... args)
void Base::ConsoleSingleton::Warning(const std::string& notifier, const char* pMsg, Args&&... args)
{
Send<Base::LogStyle::Warning>(notifier, pMsg, std::forward<Args>(args)...);
Send<LogStyle::Warning>(notifier, pMsg, std::forward<Args>(args)...);
}
template<typename... Args>
inline void Base::ConsoleSingleton::DeveloperWarning(const std::string& notifier,
const char* pMsg,
Args&&... args)
void Base::ConsoleSingleton::DeveloperWarning(const std::string& notifier,
const char* pMsg,
Args&&... args)
{
Send<Base::LogStyle::Warning,
Base::IntendedRecipient::Developer,
Base::ContentType::Untranslatable>(notifier, pMsg, std::forward<Args>(args)...);
}
template<typename... Args>
inline void
Base::ConsoleSingleton::UserWarning(const std::string& notifier, const char* pMsg, Args&&... args)
{
Send<Base::LogStyle::Warning, Base::IntendedRecipient::User, Base::ContentType::Untranslated>(
Send<LogStyle::Warning, IntendedRecipient::Developer, ContentType::Untranslatable>(
notifier,
pMsg,
std::forward<Args>(args)...);
}
template<typename... Args>
inline void Base::ConsoleSingleton::TranslatedUserWarning(const std::string& notifier,
const char* pMsg,
Args&&... args)
void Base::ConsoleSingleton::UserWarning(const std::string& notifier,
const char* pMsg,
Args&&... args)
{
Send<Base::LogStyle::Warning, Base::IntendedRecipient::User, Base::ContentType::Translated>(
Send<LogStyle::Warning, IntendedRecipient::User, ContentType::Untranslated>(
notifier,
pMsg,
std::forward<Args>(args)...);
}
template<typename... Args>
inline void Base::ConsoleSingleton::Error(const char* pMsg, Args&&... args)
void Base::ConsoleSingleton::TranslatedUserWarning(const std::string& notifier,
const char* pMsg,
Args&&... args)
{
Send<LogStyle::Warning, IntendedRecipient::User, ContentType::Translated>(
notifier,
pMsg,
std::forward<Args>(args)...);
}
template<typename... Args>
void Base::ConsoleSingleton::Error(const char* pMsg, Args&&... args)
{
Error(std::string(""), pMsg, std::forward<Args>(args)...);
}
template<typename... Args>
inline void
Base::ConsoleSingleton::Error(const std::string& notifier, const char* pMsg, Args&&... args)
void Base::ConsoleSingleton::Error(const std::string& notifier, const char* pMsg, Args&&... args)
{
Send<Base::LogStyle::Error>(notifier, pMsg, std::forward<Args>(args)...);
Send<LogStyle::Error>(notifier, pMsg, std::forward<Args>(args)...);
}
template<typename... Args>
inline void Base::ConsoleSingleton::DeveloperError(const std::string& notifier,
const char* pMsg,
Args&&... args)
void Base::ConsoleSingleton::DeveloperError(const std::string& notifier,
const char* pMsg,
Args&&... args)
{
Send<Base::LogStyle::Error,
Base::IntendedRecipient::Developer,
Base::ContentType::Untranslatable>(notifier, pMsg, std::forward<Args>(args)...);
Send<LogStyle::Error, IntendedRecipient::Developer, ContentType::Untranslatable>(
notifier,
pMsg,
std::forward<Args>(args)...);
}
template<typename... Args>
inline void Base::ConsoleSingleton::DestructorError(const std::string& notifier,
const char* pMsg,
Args&&... args) noexcept
void Base::ConsoleSingleton::DestructorError(const std::string& notifier,
const char* pMsg,
Args&&... args) noexcept
{
try {
Send<Base::LogStyle::Error,
Base::IntendedRecipient::Developer,
Base::ContentType::Untranslatable>(notifier, pMsg, std::forward<Args>(args)...);
Send<LogStyle::Error, IntendedRecipient::Developer, ContentType::Untranslatable>(
notifier,
pMsg,
std::forward<Args>(args)...);
}
catch (...) {
assert("An exception was thrown while attempting console output in a destructor" && false);
@@ -1100,90 +1096,90 @@ inline void Base::ConsoleSingleton::DestructorError(const std::string& notifier,
}
template<typename... Args>
inline void
Base::ConsoleSingleton::UserError(const std::string& notifier, const char* pMsg, Args&&... args)
void Base::ConsoleSingleton::UserError(const std::string& notifier,
const char* pMsg,
Args&&... args)
{
Send<Base::LogStyle::Error, Base::IntendedRecipient::User, Base::ContentType::Untranslated>(
Send<LogStyle::Error, IntendedRecipient::User, ContentType::Untranslated>(
notifier,
pMsg,
std::forward<Args>(args)...);
}
template<typename... Args>
inline void Base::ConsoleSingleton::TranslatedUserError(const std::string& notifier,
const char* pMsg,
Args&&... args)
void Base::ConsoleSingleton::TranslatedUserError(const std::string& notifier,
const char* pMsg,
Args&&... args)
{
Send<Base::LogStyle::Error, Base::IntendedRecipient::User, Base::ContentType::Translated>(
Send<LogStyle::Error, IntendedRecipient::User, ContentType::Translated>(
notifier,
pMsg,
std::forward<Args>(args)...);
}
template<typename... Args>
inline void Base::ConsoleSingleton::Critical(const char* pMsg, Args&&... args)
void Base::ConsoleSingleton::Critical(const char* pMsg, Args&&... args)
{
Critical(std::string(""), pMsg, std::forward<Args>(args)...);
}
template<typename... Args>
inline void
Base::ConsoleSingleton::Critical(const std::string& notifier, const char* pMsg, Args&&... args)
void Base::ConsoleSingleton::Critical(const std::string& notifier, const char* pMsg, Args&&... args)
{
Send<Base::LogStyle::Critical>(notifier, pMsg, std::forward<Args>(args)...);
Send<LogStyle::Critical>(notifier, pMsg, std::forward<Args>(args)...);
}
template<typename... Args>
inline void Base::ConsoleSingleton::UserNotification(const char* pMsg, Args&&... args)
void Base::ConsoleSingleton::UserNotification(const char* pMsg, Args&&... args)
{
UserNotification(std::string(""), pMsg, std::forward<Args>(args)...);
}
template<typename... Args>
inline void Base::ConsoleSingleton::UserNotification(const std::string& notifier,
const char* pMsg,
Args&&... args)
void Base::ConsoleSingleton::UserNotification(const std::string& notifier,
const char* pMsg,
Args&&... args)
{
Send<Base::LogStyle::Notification,
Base::IntendedRecipient::User,
Base::ContentType::Untranslated>(notifier, pMsg, std::forward<Args>(args)...);
Send<LogStyle::Notification, IntendedRecipient::User, ContentType::Untranslated>(
notifier,
pMsg,
std::forward<Args>(args)...);
}
template<typename... Args>
inline void Base::ConsoleSingleton::UserTranslatedNotification(const char* pMsg, Args&&... args)
void Base::ConsoleSingleton::UserTranslatedNotification(const char* pMsg, Args&&... args)
{
UserTranslatedNotification(std::string(""), pMsg, std::forward<Args>(args)...);
}
template<typename... Args>
inline void Base::ConsoleSingleton::UserTranslatedNotification(const std::string& notifier,
const char* pMsg,
Args&&... args)
void Base::ConsoleSingleton::UserTranslatedNotification(const std::string& notifier,
const char* pMsg,
Args&&... args)
{
Send<Base::LogStyle::Notification,
Base::IntendedRecipient::User,
Base::ContentType::Translated>(notifier, pMsg, std::forward<Args>(args)...);
Send<LogStyle::Notification, IntendedRecipient::User, ContentType::Translated>(
notifier,
pMsg,
std::forward<Args>(args)...);
}
template<typename... Args>
inline void Base::ConsoleSingleton::Log(const char* pMsg, Args&&... args)
void Base::ConsoleSingleton::Log(const char* pMsg, Args&&... args)
{
Log(std::string(""), pMsg, std::forward<Args>(args)...);
}
template<typename... Args>
inline void
Base::ConsoleSingleton::Log(const std::string& notifier, const char* pMsg, Args&&... args)
void Base::ConsoleSingleton::Log(const std::string& notifier, const char* pMsg, Args&&... args)
{
Send<Base::LogStyle::Log>(notifier, pMsg, std::forward<Args>(args)...);
Send<LogStyle::Log>(notifier, pMsg, std::forward<Args>(args)...);
}
template<Base::LogStyle category,
Base::IntendedRecipient recipient /*= Base::IntendedRecipient::All*/,
Base::ContentType contenttype /*= Base::ContentType::Untranslated*/,
typename... Args>
inline void
Base::ConsoleSingleton::Send(const std::string& notifiername, const char* pMsg, Args&&... args)
void Base::ConsoleSingleton::Send(const std::string& notifiername, const char* pMsg, Args&&... args)
{
std::string format;
try {
@@ -1202,7 +1198,7 @@ Base::ConsoleSingleton::Send(const std::string& notifiername, const char* pMsg,
}
else {
auto type = getConsoleMsg(category);
const auto type = getConsoleMsg(category);
postEvent(type, recipient, contenttype, notifiername, format);
}
@@ -1211,7 +1207,7 @@ Base::ConsoleSingleton::Send(const std::string& notifiername, const char* pMsg,
template<Base::LogStyle category,
Base::IntendedRecipient recipient /*= Base::IntendedRecipient::All*/,
Base::ContentType contenttype /*= Base::ContentType::Untranslated*/>
inline void Base::ConsoleSingleton::Notify(const std::string& notifiername, const std::string& msg)
void Base::ConsoleSingleton::Notify(const std::string& notifiername, const std::string& msg)
{
notifyPrivate(category, recipient, contenttype, notifiername, msg);
}