Adds the windows logger that logs via message box.

This commit is contained in:
2023-05-09 22:35:04 -07:00
parent dbf2e04e2a
commit 52de4b8bc2
3 changed files with 117 additions and 48 deletions

View File

@@ -38,7 +38,6 @@
#include <cstdlib>
#include <exception>
#include <string>
#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)

View File

@@ -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 <headhunter3@gmail.com> *
* *
* Licensed under the MIT license see below for details. *
* *
* MIT License *
* *
* Copyright (c) 2023 Tom Hicks <headhunter3@gmail.com> *
* *
* 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 <headhunter3@gmail.com>
*
**********************************************************************************************************************/
#ifndef CPPUtils__WindowsLogger_h__
#define CPPUtils__WindowsLogger_h__
/*
* Licensed under the MIT license see below for details.
*
* MIT License
*
* Copyright (c) 2023 Tom Hicks <headhunter3@gmail.com>
*
* 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 <windows.h>
#include <cstdint>
@@ -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__)

View File

@@ -29,7 +29,7 @@
* *
**********************************************************************************************************************/
#include "windows_logger.h"
#ifdef _WIN32
#include <iostream>
#include <sstream>
#include <tuple>
@@ -60,16 +60,26 @@ using TinyTest::TestResults;
string no_errors = "no errors";
typedef tuple<string, string, UINT> MessageBoxEvent;
/// @brief
class WindowsLoggerSpy : public WindowsLogger {
public:
/// @brief
mutable vector<MessageBoxEvent> 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<WindowsLoggerSpy> &spy) {
size_t actual = spy->log.size();
if (actual != expected) {
@@ -77,6 +87,10 @@ void ExpectLogSize(ostream &errors, size_t expected, const shared_ptr<WindowsLog
}
}
/// @brief
/// @param errors
/// @param expected
/// @param event
void ExpectMessage(ostream &errors, const string &expected, const MessageBoxEvent &event) {
string actual = get<0>(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<string> parts, const string &separator) {
ostringstream os;
@@ -107,6 +129,10 @@ string Join(vector<string> 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)