From 2f5b2dd6bb2db2464d9b9c9d95fc0a57559fa331 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sat, 22 Sep 2018 20:51:25 -0700 Subject: Reworked message formatting --- Graphics/GraphicsEngineD3DBase/include/D3DErrors.h | 7 +- .../src/VulkanUtilities/VulkanMemoryManager.cpp | 5 +- .../src/HLSL2GLSLConverterImpl.cpp | 9 +-- Platforms/Android/src/AndroidDebug.cpp | 2 +- Platforms/Basic/interface/DebugUtilities.h | 7 +- Platforms/Basic/src/BasicPlatformDebug.cpp | 6 +- Platforms/Linux/src/LinuxDebug.cpp | 2 +- Platforms/UWP/src/UWPDebug.cpp | 2 +- Platforms/Win32/src/Win32Debug.cpp | 2 +- Primitives/CMakeLists.txt | 2 +- Primitives/interface/Errors.h | 17 ++--- Primitives/interface/FormatMessage.h | 81 -------------------- Primitives/interface/FormatString.h | 87 ++++++++++++++++++++++ Primitives/src/test.cpp | 2 +- 14 files changed, 114 insertions(+), 117 deletions(-) delete mode 100644 Primitives/interface/FormatMessage.h create mode 100644 Primitives/interface/FormatString.h diff --git a/Graphics/GraphicsEngineD3DBase/include/D3DErrors.h b/Graphics/GraphicsEngineD3DBase/include/D3DErrors.h index b1d1e068..d3d3792f 100644 --- a/Graphics/GraphicsEngineD3DBase/include/D3DErrors.h +++ b/Graphics/GraphicsEngineD3DBase/include/D3DErrors.h @@ -81,10 +81,9 @@ do{ \ HRESULT _hr_ = Expr; \ if(FAILED(_hr_)) \ { \ - Diligent::MsgStream ms; \ - Diligent::FormatMsg(ms, __VA_ARGS__); \ - ComErrorDesc ErrDesc( _hr_ ); \ - LOG_ERROR_AND_THROW( ms.str(), "\nHRESULT Desc: ", ErrDesc.Get());\ + auto msg = Diligent::FormatString(__VA_ARGS__); \ + ComErrorDesc ErrDesc( _hr_ ); \ + LOG_ERROR_AND_THROW( msg, "\nHRESULT Desc: ", ErrDesc.Get());\ } \ }while(false) diff --git a/Graphics/GraphicsEngineVulkan/src/VulkanUtilities/VulkanMemoryManager.cpp b/Graphics/GraphicsEngineVulkan/src/VulkanUtilities/VulkanMemoryManager.cpp index 973b1add..65e138b6 100644 --- a/Graphics/GraphicsEngineVulkan/src/VulkanUtilities/VulkanMemoryManager.cpp +++ b/Graphics/GraphicsEngineVulkan/src/VulkanUtilities/VulkanMemoryManager.cpp @@ -49,9 +49,8 @@ VulkanMemoryPage::VulkanMemoryPage(VulkanMemoryManager& ParentMemoryMgr, MemAlloc.allocationSize = PageSize; MemAlloc.memoryTypeIndex = MemoryTypeIndex; - std::stringstream ss; - Diligent::FormatMsg(ss, "Device memory page. Size: ", Diligent::FormatMemorySize(PageSize, 2), ", type: ", MemoryTypeIndex); - m_VkMemory = ParentMemoryMgr.m_LogicalDevice.AllocateDeviceMemory(MemAlloc, ss.str().c_str()); + auto MemoryName = Diligent::FormatString("Device memory page. Size: ", Diligent::FormatMemorySize(PageSize, 2), ", type: ", MemoryTypeIndex); + m_VkMemory = ParentMemoryMgr.m_LogicalDevice.AllocateDeviceMemory(MemAlloc, MemoryName.c_str()); if (IsHostVisible) { diff --git a/Graphics/HLSL2GLSLConverterLib/src/HLSL2GLSLConverterImpl.cpp b/Graphics/HLSL2GLSLConverterLib/src/HLSL2GLSLConverterImpl.cpp index 6eaa5497..bd032a06 100644 --- a/Graphics/HLSL2GLSLConverterLib/src/HLSL2GLSLConverterImpl.cpp +++ b/Graphics/HLSL2GLSLConverterLib/src/HLSL2GLSLConverterImpl.cpp @@ -916,11 +916,10 @@ String HLSL2GLSLConverterImpl::ConversionStream::PrintTokenContext( IteratorType #define VERIFY_PARSER_STATE( Token, Condition, ... )\ - if( !(Condition) ) \ - { \ - MsgStream ss; \ - FormatMsg( ss, __VA_ARGS__ ); \ - LOG_ERROR_AND_THROW( ss.str(), "\n", PrintTokenContext(Token, 4) );\ + if( !(Condition) ) \ + { \ + auto err = FormatString(__VA_ARGS__ ); \ + LOG_ERROR_AND_THROW( err, "\n", PrintTokenContext(Token, 4) );\ } template diff --git a/Platforms/Android/src/AndroidDebug.cpp b/Platforms/Android/src/AndroidDebug.cpp index 6480b614..52658ba0 100644 --- a/Platforms/Android/src/AndroidDebug.cpp +++ b/Platforms/Android/src/AndroidDebug.cpp @@ -22,7 +22,7 @@ */ #include "AndroidDebug.h" -#include "FormatMessage.h" +#include "FormatString.h" #include #include diff --git a/Platforms/Basic/interface/DebugUtilities.h b/Platforms/Basic/interface/DebugUtilities.h index 0ecc6c81..20eee447 100644 --- a/Platforms/Basic/interface/DebugUtilities.h +++ b/Platforms/Basic/interface/DebugUtilities.h @@ -23,7 +23,7 @@ #pragma once -#include "../../../Primitives/interface/FormatMessage.h" +#include "../../../Primitives/interface/FormatString.h" #include "../../../Primitives/interface/Errors.h" #include "BasicPlatformDebug.h" @@ -33,9 +33,8 @@ #define ASSERTION_FAILED(Message, ...)\ do{ \ - Diligent::MsgStream ms; \ - Diligent::FormatMsg( ms, Message, ##__VA_ARGS__);\ - DebugAssertionFailed( ms.str().c_str(), __FUNCTION__, __FILE__, __LINE__); \ + auto msg = Diligent::FormatString(Message, ##__VA_ARGS__);\ + DebugAssertionFailed( msg.c_str(), __FUNCTION__, __FILE__, __LINE__); \ }while(false) # define VERIFY(Expr, Message, ...)\ diff --git a/Platforms/Basic/src/BasicPlatformDebug.cpp b/Platforms/Basic/src/BasicPlatformDebug.cpp index 31e46692..0d649070 100644 --- a/Platforms/Basic/src/BasicPlatformDebug.cpp +++ b/Platforms/Basic/src/BasicPlatformDebug.cpp @@ -22,7 +22,7 @@ */ #include "BasicPlatformDebug.h" -#include "FormatMessage.h" +#include "FormatString.h" #include "BasicFileSystem.h" #include @@ -35,9 +35,7 @@ String BasicPlatformDebug :: FormatAssertionFailedMessage( const Char *Message, { String FileName; BasicFileSystem::SplitFilePath( File, nullptr, &FileName ); - std::stringstream msgss; - Diligent::FormatMsg( msgss, "Debug assertion failed in ", Function, "(), file ", FileName, ", line ", Line, ":\n", Message); - return msgss.str(); + return Diligent::FormatString("Debug assertion failed in ", Function, "(), file ", FileName, ", line ", Line, ":\n", Message); } String BasicPlatformDebug::FormatDebugMessage(DebugMessageSeverity Severity, diff --git a/Platforms/Linux/src/LinuxDebug.cpp b/Platforms/Linux/src/LinuxDebug.cpp index 00a6554d..4ab433a4 100644 --- a/Platforms/Linux/src/LinuxDebug.cpp +++ b/Platforms/Linux/src/LinuxDebug.cpp @@ -25,7 +25,7 @@ #include #include "LinuxDebug.h" -#include "FormatMessage.h" +#include "FormatString.h" using namespace Diligent; diff --git a/Platforms/UWP/src/UWPDebug.cpp b/Platforms/UWP/src/UWPDebug.cpp index ed78587e..3dca028e 100644 --- a/Platforms/UWP/src/UWPDebug.cpp +++ b/Platforms/UWP/src/UWPDebug.cpp @@ -24,7 +24,7 @@ #include "pch.h" #include "UWPDebug.h" -#include "FormatMessage.h" +#include "FormatString.h" #include #define NOMINMAX diff --git a/Platforms/Win32/src/Win32Debug.cpp b/Platforms/Win32/src/Win32Debug.cpp index c1483c4a..7c8aa2b4 100644 --- a/Platforms/Win32/src/Win32Debug.cpp +++ b/Platforms/Win32/src/Win32Debug.cpp @@ -24,7 +24,7 @@ #include "pch.h" #include "Win32Debug.h" -#include "FormatMessage.h" +#include "FormatString.h" #include #include #include diff --git a/Primitives/CMakeLists.txt b/Primitives/CMakeLists.txt index 82e0fefb..2f87b4cb 100644 --- a/Primitives/CMakeLists.txt +++ b/Primitives/CMakeLists.txt @@ -13,7 +13,7 @@ set(INTERFACE interface/DebugOutput.h interface/Errors.h interface/FileStream.h - interface/FormatMessage.h + interface/FormatString.h interface/InterfaceID.h interface/MemoryAllocator.h interface/Object.h diff --git a/Primitives/interface/Errors.h b/Primitives/interface/Errors.h index 187a55ce..d38d4cc5 100644 --- a/Primitives/interface/Errors.h +++ b/Primitives/interface/Errors.h @@ -28,7 +28,7 @@ #include #include "DebugOutput.h" -#include "FormatMessage.h" +#include "FormatString.h" namespace Diligent { @@ -44,16 +44,14 @@ inline void ThrowIf(std::string &&msg) throw std::runtime_error( std::move(msg) ); } -template -void LogError( const char *Function, const char *FullFilePath, int Line, const FirstArgType& first, const RestArgsType&... RestArgs ) +template +void LogError( const char *Function, const char *FullFilePath, int Line, const ArgsType&... Args ) { std::string FileName(FullFilePath); auto LastSlashPos = FileName.find_last_of("/\\"); if(LastSlashPos != std::string::npos) FileName.erase(0, LastSlashPos+1); - Diligent::MsgStream ss; - Diligent::FormatMsg( ss, first, RestArgs... ); - auto Msg = ss.str(); + auto Msg = Diligent::FormatString(Args...); if(DebugMessageCallback != nullptr) { DebugMessageCallback( bThrowException ? DebugMessageSeverity::FatalError : DebugMessageSeverity::Error, Msg.c_str(), Function, FileName.c_str(), Line); @@ -94,10 +92,9 @@ do{ \ #define LOG_DEBUG_MESSAGE(Severity, ...)\ -do{ \ - Diligent::MsgStream _msg_ss; \ - Diligent::FormatMsg( _msg_ss, ##__VA_ARGS__ );\ - if(Diligent::DebugMessageCallback != nullptr) Diligent::DebugMessageCallback( Severity, _msg_ss.str().c_str(), nullptr, nullptr, 0 );\ +do{ \ + auto _msg = Diligent::FormatString(##__VA_ARGS__ );\ + if(Diligent::DebugMessageCallback != nullptr) Diligent::DebugMessageCallback( Severity, _msg.c_str(), nullptr, nullptr, 0 );\ }while(false) #define LOG_ERROR_MESSAGE(...) LOG_DEBUG_MESSAGE(Diligent::DebugMessageSeverity::Error, ##__VA_ARGS__) diff --git a/Primitives/interface/FormatMessage.h b/Primitives/interface/FormatMessage.h deleted file mode 100644 index bd4c20c7..00000000 --- a/Primitives/interface/FormatMessage.h +++ /dev/null @@ -1,81 +0,0 @@ -/* Copyright 2015-2018 Egor Yusov - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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 OF ANY PROPRIETARY RIGHTS. - * - * In no event and under no legal theory, whether in tort (including negligence), - * contract, or otherwise, unless required by applicable law (such as deliberate - * and grossly negligent acts) or agreed to in writing, shall any Contributor be - * liable for any damages, including any direct, indirect, special, incidental, - * or consequential damages of any character arising as a result of this License or - * out of the use or inability to use the software (including but not limited to damages - * for loss of goodwill, work stoppage, computer failure or malfunction, or any and - * all other commercial damages or losses), even if such Contributor has been advised - * of the possibility of such damages. - */ - -#pragma once - -#include -#include - -namespace Diligent -{ - typedef std::stringstream MsgStream; - - template - void FormatMsg( SSType &ss, const ArgType& Arg ) - { - ss << Arg; - } - - template - void FormatMsg( SSType &ss, const FirstArgType& FirstArg, const RestArgsType&... RestArgs ) - { - FormatMsg( ss, FirstArg ); - FormatMsg( ss, RestArgs... ); // recursive call using pack expansion syntax - } - - template - struct MemorySizeFormatter - { - Type size = 0; - size_t precision = 0; - Type ref_size = 0; - }; - - template - MemorySizeFormatter FormatMemorySize(Type _size, size_t _precision = 0, Type _ref_size = 0) - { - return MemorySizeFormatter{_size, _precision, _ref_size}; - } - - template - void FormatMsg(SSType& ss, const MemorySizeFormatter& Arg) - { - auto ref_size = Arg.ref_size != 0 ? Arg.ref_size : Arg.size; - if (ref_size >= (1 << 30)) - { - ss << std::fixed << std::setprecision(Arg.precision) << static_cast(Arg.size) / double{ 1 << 30 } << " GB"; - } - else if(ref_size >= (1 << 20)) - { - ss << std::fixed << std::setprecision(Arg.precision) << static_cast(Arg.size) / double{ 1 << 20 } << " MB"; - } - else if (ref_size >= (1 << 10)) - { - ss << std::fixed << std::setprecision(Arg.precision) << static_cast(Arg.size) / double{ 1 << 10 } << " KB"; - } - else - { - ss << Arg.size << ( ((Arg.size & 0x01) == 0x01) ? " Byte" : " Bytes" ); - } - } -} \ No newline at end of file diff --git a/Primitives/interface/FormatString.h b/Primitives/interface/FormatString.h new file mode 100644 index 00000000..08d1ca85 --- /dev/null +++ b/Primitives/interface/FormatString.h @@ -0,0 +1,87 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#pragma once + +#include +#include + +namespace Diligent +{ + template + void FormatStrSS(SSType &ss, const ArgType& Arg) + { + ss << Arg; + } + + template + void FormatStrSS(SSType &ss, const FirstArgType& FirstArg, const RestArgsType&... RestArgs) + { + FormatStrSS( ss, FirstArg ); + FormatStrSS( ss, RestArgs... ); // recursive call using pack expansion syntax + } + + template + std::string FormatString(const RestArgsType&... Args) + { + std::stringstream ss; + FormatStrSS(ss, Args...); + return ss.str(); + } + + template + struct MemorySizeFormatter + { + Type size = 0; + size_t precision = 0; + Type ref_size = 0; + }; + + template + MemorySizeFormatter FormatMemorySize(Type _size, size_t _precision = 0, Type _ref_size = 0) + { + return MemorySizeFormatter{_size, _precision, _ref_size}; + } + + template + void FormatStrSS(SSType& ss, const MemorySizeFormatter& Arg) + { + auto ref_size = Arg.ref_size != 0 ? Arg.ref_size : Arg.size; + if (ref_size >= (1 << 30)) + { + ss << std::fixed << std::setprecision(Arg.precision) << static_cast(Arg.size) / double{ 1 << 30 } << " GB"; + } + else if(ref_size >= (1 << 20)) + { + ss << std::fixed << std::setprecision(Arg.precision) << static_cast(Arg.size) / double{ 1 << 20 } << " MB"; + } + else if (ref_size >= (1 << 10)) + { + ss << std::fixed << std::setprecision(Arg.precision) << static_cast(Arg.size) / double{ 1 << 10 } << " KB"; + } + else + { + ss << Arg.size << ( ((Arg.size & 0x01) == 0x01) ? " Byte" : " Bytes" ); + } + } +} \ No newline at end of file diff --git a/Primitives/src/test.cpp b/Primitives/src/test.cpp index c578e76f..67a6eb34 100644 --- a/Primitives/src/test.cpp +++ b/Primitives/src/test.cpp @@ -26,6 +26,6 @@ #include "ReferenceCounters.h" #include "Object.h" #include "MemoryAllocator.h" -#include "FormatMessage.h" +#include "FormatString.h" #include "FileStream.h" #include "DataBlob.h" -- cgit v1.2.3