Browse Source

*Use convert_cast instead of static_cast where feasible

master
bergmann 4 years ago
parent
commit
ebad81657f
4 changed files with 24 additions and 11 deletions
  1. +8
    -0
      cmake/config.h.in
  2. +5
    -0
      src/CMakeLists.txt
  3. +10
    -10
      src/cpplogging/manager/consumer/formatting.cpp
  4. +1
    -1
      test/CMakeLists.txt

+ 8
- 0
cmake/config.h.in View File

@@ -1,3 +1,11 @@
#pragma once

#cmakedefine CPPLOGGING_HAS_CPPCORE
#cmakedefine CPPLOGGING_HAS_NLOHMANN_JSON

#ifdef CPPCURL_HAS_CPPCORE
#include <cppcore/conversion/convert_cast.h>
#define cpplogging_convert_cast ::cppcore::convert_cast
#else
#define cpplogging_convert_cast static_cast
#endif

+ 5
- 0
src/CMakeLists.txt View File

@@ -4,6 +4,11 @@ Include ( cotire OPTIONAL RESULT_VARIABLE HAS_COTIRE
Include ( pedantic OPTIONAL RESULT_VARIABLE HAS_PEDANTIC )
Include ( strip_symbols OPTIONAL RESULT_VARIABLE HAS_STRIP_SYMBOLS )

Find_Package ( cppcore QUIET )
If ( cppcore )
Set ( CPPLOGGING_HAS_CPPCORE true )
EndIf ( )

Find_Package ( nlohmann_json QUIET )
If ( nlohmann_json_FOUND )
Set ( CPPLOGGING_HAS_NLOHMANN_JSON true )


+ 10
- 10
src/cpplogging/manager/consumer/formatting.cpp View File

@@ -144,7 +144,7 @@ consumer::format_type consumer::parse_format(const std::string& format)
if (s < c)
{
if (!ret.empty() && !ret.back().empty() && *ret.back().c_str() != '\0')
ret.back() += std::string(s, static_cast<size_t>(c - s - 1));
ret.back() += std::string(s, cpplogging_convert_cast<size_t>(c - s - 1));
else
ret.emplace_back(s, c - s);
}
@@ -174,7 +174,7 @@ consumer::format_type consumer::parse_format(const std::string& format)
if (s + 1 < c)
{
if (!ret.empty() && !ret.back().empty() && *ret.back().c_str() != '\0')
ret.back() += std::string(s, static_cast<size_t>(c - s - 1));
ret.back() += std::string(s, cpplogging_convert_cast<size_t>(c - s - 1));
else
ret.emplace_back(s, c - s - 1);
}
@@ -197,7 +197,7 @@ consumer::format_type consumer::parse_format(const std::string& format)
/* if we are in 'is_in_token' we extract the token and switch to 'is_in_format' */
case parser_state::is_in_token:
state = parser_state::is_in_format;
item.token = value_token_map.get_token(std::string(x, static_cast<size_t>(c - x)));
item.token = value_token_map.get_token(std::string(x, cpplogging_convert_cast<size_t>(c - x)));
if (item.token == value_token::unknown)
{
state = parser_state::is_text;
@@ -270,7 +270,7 @@ consumer::format_type consumer::parse_format(const std::string& format)
}
else
{
item.width = static_cast<uint8_t>(u);
item.width = cpplogging_convert_cast<uint8_t>(u);
x = c + 1;
state = parser_state::is_in_format_dot;
}
@@ -326,9 +326,9 @@ consumer::format_type consumer::parse_format(const std::string& format)
else
{
if (state == parser_state::is_in_format_dot)
item.precision = static_cast<uint8_t>(u);
item.precision = cpplogging_convert_cast<uint8_t>(u);
else
item.width = static_cast<uint8_t>(u);
item.width = cpplogging_convert_cast<uint8_t>(u);

/* set the type and switch to 'is_in_format_type' */
x = c + 1;
@@ -360,7 +360,7 @@ consumer::format_type consumer::parse_format(const std::string& format)
/* if we are in 'is_in_token' we extract the token, add the item and switch to 'is_text' */
case parser_state::is_in_token:
state = parser_state::is_text;
item.token = value_token_map.get_token(std::string(x, static_cast<size_t>(c - x)));
item.token = value_token_map.get_token(std::string(x, cpplogging_convert_cast<size_t>(c - x)));
if (item.token == value_token::unknown)
{
state = parser_state::is_text;
@@ -390,9 +390,9 @@ consumer::format_type consumer::parse_format(const std::string& format)
if (x != tmp && x <= c)
{
if (state == parser_state::is_in_format_dot)
item.precision = static_cast<uint8_t>(u);
item.precision = cpplogging_convert_cast<uint8_t>(u);
else
item.width = static_cast<uint8_t>(u);
item.width = cpplogging_convert_cast<uint8_t>(u);
ret.emplace_back(item.as_string());
}
item.reset();
@@ -482,7 +482,7 @@ void consumer::write_formatted(const log_entry& e, const format_type& f, std::os
switch (i.format)
{
case value_format::decimal:
os << static_cast<int>(e.level);
os << cpplogging_convert_cast<int>(e.level);
break;
case value_format::string_upper:
switch (e.level)


+ 1
- 1
test/CMakeLists.txt View File

@@ -45,7 +45,7 @@ ForEach ( FILE IN LISTS CPPLOGGING_TEST_SOURCE_FILES )

# test
If ( HAS_CMAKE_TESTS )
Add_CMake_Test ( NAME ${TEST_NAME} TARGET ${TEST_NAME} )
Add_CMake_Test ( NAME ${TEST_NAME} TARGET ${TEST_NAME} GROUP cpplogging )
Else ( )
Add_Test ( NAME ${TEST_NAME} COMMAND ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
EndIf ( )


Loading…
Cancel
Save