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 --- Primitives/interface/Errors.h | 17 +++---- Primitives/interface/FormatMessage.h | 81 --------------------------------- Primitives/interface/FormatString.h | 87 ++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 91 deletions(-) delete mode 100644 Primitives/interface/FormatMessage.h create mode 100644 Primitives/interface/FormatString.h (limited to 'Primitives/interface') 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 -- cgit v1.2.3