From 52de4b8bc2f6d8118dba94ade47f70522ba87b56 Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Tue, 9 May 2023 22:35:04 -0700 Subject: [PATCH] Adds the windows logger that logs via message box. --- windows_logger.cpp | 7 +-- windows_logger.h | 122 ++++++++++++++++++++++++++-------------- windows_logger_test.cpp | 36 +++++++++++- 3 files changed, 117 insertions(+), 48 deletions(-) diff --git a/windows_logger.cpp b/windows_logger.cpp index 550548c..47c9cb9 100644 --- a/windows_logger.cpp +++ b/windows_logger.cpp @@ -38,7 +38,6 @@ #include #include #include -#endif #pragma comment(lib, "opengl32.lib") #pragma comment(lib, "glu32.lib") @@ -49,7 +48,6 @@ #pragma comment(lib, "winmm.lib") namespace CPPUtils { -#ifdef _WIN32 namespace { using std::exception; using std::string; @@ -131,9 +129,6 @@ string WindowsLogger::GetTitle(const MessageType &type) const { return kUnknownMessageTitle; } } -#endif } // End namespace CPPUtils -#ifdef WIN32 -namespace CPPUtils {}; // namespace CPPUtils -#endif +#endif // End defined(_WIN32) diff --git a/windows_logger.h b/windows_logger.h index 6b39a04..78ed032 100644 --- a/windows_logger.h +++ b/windows_logger.h @@ -1,35 +1,40 @@ /********************************************************************************************************************** - * * - * @file windows_logger.h * - * * - * @brief Declares the WindowsLogger logging destination class declared in windows_logger.h. * - * This logging destination works with the Logger class declared in logger.h and logs messages by creating a modal * - * windows message box * - * @copyright Copyright (C) 2023 by Tom Hicks * - * * - * Licensed under the MIT license see below for details. * - * * - * MIT License * - * * - * Copyright (c) 2023 Tom Hicks * - * * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated * - * documentation files (the "Software"), to deal in the Software without restriction, including without limitation * - * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and * - * to permit persons to whom the Software is furnished to do so, subject to the following conditions: * - * * - * The above copyright notice and this permission notice shall be included in all copies or substantial portions of * - * the Software. * - * * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO * - * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * - * IN THE SOFTWARE. * - * * + * + * @file windows_logger.h + * + * @brief Declares the WindowsLogger logging destination class declared in windows_logger.h. + * + * This logging destination works with the Logger class declared in logger.h and logs messages by creating a modal + * windows message box + * + * @copyright Copyright (C) 2023 by Tom Hicks + * **********************************************************************************************************************/ -#ifndef CPPUtils__WindowsLogger_h__ -#define CPPUtils__WindowsLogger_h__ +/* + * Licensed under the MIT license see below for details. + * + * MIT License + * + * Copyright (c) 2023 Tom Hicks + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + **********************************************************************************************************************/ +#ifndef CPPUtils__windows_logger_h__ +#define CPPUtils__windows_logger_h__ +#ifdef _WIN32 #include #include @@ -39,21 +44,56 @@ #include "logger.h" namespace CPPUtils { +/// @addtogroup logger Logger +/// @{ + +/// @brief A @link Logger::Destination @endlink that logs by showing Windows message boxes. class WindowsLogger : public CPPUtils::Logger { - public: +public: + /// @brief Allocates resources used by the WindowsLogger. This is currently empty. WindowsLogger(); + + /// @brief Frees resources used by the WindowsLogger. This is currently empty. virtual ~WindowsLogger(); - virtual void LogMessage(const MessageType& type, const std::string& message) const; - virtual void LogError(const MessageType& type, const std::exception& ex) const; - virtual void LogError(const MessageType& type, const std::string& message, const std::exception& ex) const; - protected: - virtual void ShowMessageBox(const std::string& message, const std::string& title, UINT uType) const; + /// @brief Logs a string message with a specific type. + /// @param type The type of message to log. + /// @param message The message to log. + virtual void LogMessage(const MessageType &type, const std::string &message) const; - private: - uint32_t GetIcon(const MessageType& type) const; - std::string GetTitle(const MessageType& type) const; + /// @brief Logs an exception. + /// @param type The type of message to log the exception as. + /// @param ex The exception to log. + virtual void LogError(const MessageType &type, const std::exception &ex) const; + + /// @brief Logs an exception with a message. + /// @param type The type of message to log. + /// @param message The message to log. + /// @param ex The exception to log. + virtual void LogError(const MessageType &type, const std::string &message, const std::exception &ex) const; + +protected: + /// @brief Shows message boxes + /// @param message The body of the message box. + /// @param title The title of the message box. + /// @param uType The uType flags for the message box. This takes a bitmask of the MB_ defines from windows.h to + /// specify the buttons and icon to show and the behavior of the message box. + virtual void ShowMessageBox(const std::string &message, const std::string &title, UINT uType) const; + +private: + /// @brief Gets the default icon to use based on the @link MessageType @endlink. + /// @param type The @link MessageType @endlink to use. + /// @return The icon bits. + uint32_t GetIcon(const MessageType &type) const; + + /// @brief Gets the default title to use based on the @link MessageType @endlink. + /// @param type The @link MessageType @endlink to use. + /// @return The title to use for the message box. + std::string GetTitle(const MessageType &type) const; }; -} // namespace CPPUtils -#endif // End !defined(CPPUtils__WindowsLogger_h__) +/// @} +} // namespace CPPUtils + +#endif // End defined(_WIN32) +#endif // End !defined(CPPUtils__windows_logger_h__) diff --git a/windows_logger_test.cpp b/windows_logger_test.cpp index c86431a..de91d49 100644 --- a/windows_logger_test.cpp +++ b/windows_logger_test.cpp @@ -29,7 +29,7 @@ * * **********************************************************************************************************************/ #include "windows_logger.h" - +#ifdef _WIN32 #include #include #include @@ -60,16 +60,26 @@ using TinyTest::TestResults; string no_errors = "no errors"; typedef tuple MessageBoxEvent; +/// @brief class WindowsLoggerSpy : public WindowsLogger { public: + /// @brief mutable vector log; protected: + /// @brief + /// @param body + /// @param title + /// @param u_type virtual void ShowMessageBox(const string &body, const string &title, UINT u_type) const override { log.push_back(make_tuple(body, title, u_type)); } }; +/// @brief +/// @param errors +/// @param expected +/// @param spy void ExpectLogSize(ostream &errors, size_t expected, const shared_ptr &spy) { size_t actual = spy->log.size(); if (actual != expected) { @@ -77,6 +87,10 @@ void ExpectLogSize(ostream &errors, size_t expected, const shared_ptr(event); if (actual != expected) { @@ -84,6 +98,10 @@ void ExpectMessage(ostream &errors, const string &expected, const MessageBoxEven } } +/// @brief +/// @param errors +/// @param expected +/// @param event void ExpectTitle(ostream &errors, const string &expected, const MessageBoxEvent &event) { string actual = get<1>(event); if (actual != expected) { @@ -91,6 +109,10 @@ void ExpectTitle(ostream &errors, const string &expected, const MessageBoxEvent } } +/// @brief +/// @param parts +/// @param separator +/// @return string Join(vector parts, const string &separator) { ostringstream os; @@ -107,6 +129,10 @@ string Join(vector parts, const string &separator) { return os.str(); } +/// @brief +/// @param u_type +/// @param ignore_defaults +/// @return string ConvertUTypeToString(UINT u_type, bool ignore_defaults = true) { UINT u_button = u_type & MB_TYPEMASK; UINT u_icon = u_type & MB_ICONMASK; @@ -234,6 +260,10 @@ string ConvertUTypeToString(UINT u_type, bool ignore_defaults = true) { return Join(parts, " | "); } +/// @brief +/// @param errors +/// @param expected +/// @param event void ExpectUType(ostream &errors, UINT expected, const MessageBoxEvent &event) { UINT actual = get<2>(event); if (actual != expected) { @@ -242,6 +272,9 @@ void ExpectUType(ostream &errors, UINT expected, const MessageBoxEvent &event) { } } +/// @brief +/// @param error_messages +/// @return string GetErrors(ostringstream &error_messages) { string errors = error_messages.str(); if (errors.size() > 0) { @@ -491,3 +524,4 @@ int main(int argc, char *argv[]) { return results.failed() + results.errors(); } +#endif // End defined(_WIN32)