constexpr and static_assert when NOT on MSVC. #ifdef workaround for MSVC

This commit is contained in:
kjell@kjell-win7.hsd1.co.comcast.net. 2014-01-14 07:10:26 +01:00
parent 9f015b4216
commit 9df4fbd99c
2 changed files with 19 additions and 8 deletions

View File

@ -12,7 +12,7 @@
#include "g2loglevels.hpp"
#include "g2log.hpp"
#include <atomic>
#include <cassert>
namespace g2
{
namespace internal {
@ -21,15 +21,20 @@ namespace g2
}
// All levels are by default ON: i.e. for DEBUG, INFO, WARNING, FATAL
constexpr const int size = FATAL.value+1;
constexpr const int size{ FATAL.value + 1 };
std::atomic<bool> g_log_level_status[4]{{true}, {true}, {true},{true}};
#ifndef _MSC_VER
static_assert(4 == size, "Mismatch between number of logging levels and their use");
#endif
} // internal
#ifdef G2_DYNAMIC_LOGGING
void setLogLevel(LEVELS log_level, bool enabled)
{
// MSC: remove when constexpr available. see static_assert above
assert(size == 4 && "Mismatch between number of logging levels and their use");
int level = log_level.value;
CHECK((level >= DEBUG.value) && (level <= FATAL.value));
internal::g_log_level_status[level].store(enabled, std::memory_order_release);

View File

@ -21,8 +21,14 @@ struct LEVELS
const char* text;
};
constexpr const LEVELS hej{0, "1"};
#if _MSC_VER
// Visual Studio does not support constexpr
// what is below is ugly and not right but it works in this purpose.
// if there are any issues with it then just please remove all instances of constexpr
#ifndef constexpr
#define constexpr
#endif
#endif
constexpr const LEVELS DEBUG{0, "DEBUG"},
INFO{DEBUG.value+1, "INFO"},
WARNING{INFO.value+1, "WARNING"},
@ -33,7 +39,7 @@ constexpr const LEVELS DEBUG{0, "DEBUG"},
namespace g2 {
namespace internal {
const LEVELS CONTRACT = {100, "CONTRACT"}, FATAL_SIGNAL{101, "FATAL_SIGNAL"};
constexpr const LEVELS CONTRACT{100, "CONTRACT"}, FATAL_SIGNAL{101, "FATAL_SIGNAL"};
bool wasFatal(const LEVELS& level);
}