Advertisement
Guest User

Untitled

a guest
May 9th, 2020
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. #pragma once
  2. #include <string_view>
  3. #include <memory>
  4. #include <charconv>
  5. #include <atomic>
  6.  
  7. #include "FastPimpl.hpp"
  8. #include "Platform.hpp"
  9.  
  10. class Logger
  11. {
  12.  
  13. public:
  14.  
  15. static Logger& Instance();
  16.  
  17. void logDebugEndl( std::string_view _toLog );
  18.  
  19. void logDebug( std::string_view _toLog );
  20.  
  21. template< typename TToLog >
  22. static constexpr bool IsStringType( TToLog&& _toLog )
  23. {
  24. static_cast<void>(_toLog);
  25.  
  26. using TClearType = typename std::decay<TToLog>::type;
  27. constexpr bool isStringType = std::is_same_v<TClearType,std::string >
  28. || std::is_same_v<TClearType,std::string_view >
  29. || std::is_same_v<TClearType,const char *>;
  30. return isStringType;
  31. }
  32.  
  33. template< typename TToLog >
  34. void logDebugEndl( TToLog&& _toLog )
  35. {
  36. constexpr bool isString = IsStringType( _toLog );
  37. if constexpr ( !isString )
  38. {
  39. std::array<char, sizeof (TToLog)* 8> str{};
  40. if( auto [p, ec] = std::to_chars(str.data(), str.data() + str.size(),_toLog); ec == std::errc() )
  41. {
  42. logDebugEndl( std::string_view( str.data(), p - str.data() ) );
  43. }
  44. }
  45. else{
  46. logDebugEndl( static_cast<std::string_view>(_toLog) );
  47. }
  48.  
  49. }
  50.  
  51. template< typename TToLog>
  52. void logDebug( TToLog&& _toLog )
  53. {
  54. if constexpr (!IsStringType(_toLog))
  55. {
  56. std::array<char, sizeof (TToLog)* 8> str{};
  57. if( auto [p, ec] = std::to_chars(str.data(), str.data() + str.size(),_toLog); ec == std::errc() )
  58. {
  59. logDebug( std::string_view( str.data(), p - str.data() ) );
  60. }
  61. }
  62. else{
  63. logDebug( static_cast<std::string_view>(_toLog) );
  64. }
  65. }
  66.  
  67. private:
  68.  
  69. Logger();
  70. ~Logger();
  71.  
  72. private:
  73.  
  74. static constexpr inline std::size_t kImplSize = Platform::LogerImplSize;
  75. static constexpr inline std::size_t kImplAlignment = Platform::LogerImplAlignment;
  76.  
  77. private:
  78.  
  79. mutable std::atomic_flag m_loggerReady;
  80.  
  81. class LoggerImpl;
  82. Utils::FastPimpl<LoggerImpl,kImplSize,kImplAlignment> m_pLoggerImpl;
  83. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement