Skip to content

Commit

Permalink
Merge pull request #362 from avdgrinten/quiet-logging
Browse files Browse the repository at this point in the history
logging: Replace NOLOGGING
  • Loading branch information
avdgrinten committed Jul 11, 2019
2 parents 86bc785 + a5636f1 commit 6059174
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 123 deletions.
21 changes: 18 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ endif("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")
# BUILD OPTIONS
option(NETWORKIT_BUILD_CORE "Build NetworKit core library" ON)
option(NETWORKIT_BUILD_TESTS "Build NetworKit C++ tests" OFF)
option(NETWORKIT_LOGGING "Build with logging support" ON)
option(NETWORKIT_QUIET_LOGGING "Set log level to QUIET by default (can still be changed at run time)" OFF)
option(NETWORKIT_STATIC "Build static libraries" OFF)
option(NETWORKIT_MONOLITH "Build single library (and tests is requested; required for shared lib)" ON)
option(NETWORKIT_NATIVE "Optimize for native architecture (often better performance)" OFF)
Expand All @@ -18,6 +18,7 @@ option(NETWORKIT_COVERAGE "Build with support for coverage" OFF)
set(NETWORKIT_PYTHON "" CACHE STRING "Directory containing Python.h. Implies MONOLITH=TRUE")
set(NETWORKIT_PYTHON_SOABI "" CACHE STRING "Platform specific file extension. Implies MONOLITH=TRUE")
set(NETWORKIT_WITH_SANITIZERS "" CACHE STRING "Uses sanitizers during the compilation")
set(NETWORKIT_RELEASE_LOGGING "AUTO" CACHE STRING "Do not compile log messages at levels TRACE or DEBUG (AUTO|ON|OFF)")

set (NETWORKIT_CXX_STANDARD "11" CACHE STRING "CXX Version to compile with. Currently fixed to 11")

Expand Down Expand Up @@ -51,8 +52,22 @@ endif()
set(NETWORKIT_CXX_FLAGS "")
set(NETWORKIT_LINK_FLAGS "")

if (NOT NETWORKIT_LOGGING)
set(NETWORKIT_CXX_FLAGS "-DNOLOGGING")
if (NETWORKIT_QUIET_LOGGING)
set(NETWORKIT_CXX_FLAGS "-DNETWORKIT_QUIET_LOGGING")
endif()

if (NETWORKIT_RELEASE_LOGGING STREQUAL "AUTO")
if (CMAKE_BUILD_TYPE STREQUAL "Release")
set(NETWORKIT_RELEASE_LOGGING "ON")
else()
set(NETWORKIT_RELEASE_LOGGING "OFF")
endif()
endif()

if (NETWORKIT_RELEASE_LOGGING STREQUAL "ON")
set(NETWORKIT_CXX_FLAGS "${NETWORKIT_CXX_FLAGS} -DNETWORKIT_RELEASE_LOGGING")
elseif(NOT NETWORKIT_RELEASE_LOGGING STREQUAL "OFF")
message(FATAL_ERROR "Unsupported setting ${NETWORKIT_RELEASE_LOGGING} for NETWORKIT_RELEASE_LOGGING")
endif()

if (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") OR
Expand Down
106 changes: 26 additions & 80 deletions include/networkit/auxiliary/Log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,108 +6,53 @@

#include <networkit/auxiliary/StringBuilder.hpp>

#ifdef NOLOGGING

#define FATAL(...) do{}while(false)
#define ERROR(...) do{}while(false)
#define WARN(...) do{}while(false)
#define INFO(...) do{}while(false)
#define DEBUG(...) do{}while(false)
#define TRACE(...) do{}while(false)

#define FATALF(...) do{}while(false)
#define ERRORF(...) do{}while(false)
#define WARNF(...) do{}while(false)
#define INFOF(...) do{}while(false)
#define DEBUGF(...) do{}while(false)
#define TRACEF(...) do{}while(false)

#define TRACEPOINT do{}while(false)

#else // NOLOGGING

#ifdef _MSC_VER
#define NETWORKT_PRETTY_FUNCTION __FUNCSIG__
#else
#define NETWORKT_PRETTY_FUNCTION __PRETTY_FUNCTION__
#endif

#define LOG_LEVEL_FATAL 0
#define LOG_LEVEL_ERROR 1
#define LOG_LEVEL_WARN 2
#define LOG_LEVEL_INFO 3
#define LOG_LEVEL_DEBUG 4
#define LOG_LEVEL_TRACE 5


#if !defined(LOG_LEVEL)
#define LOG_LEVEL LOG_LEVEL_TRACE
#endif

#if LOG_LEVEL >= LOG_LEVEL_FATAL
#define FATAL(...) ::Aux::Log::log({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::fatal, __VA_ARGS__)
#define FATALF(...) ::Aux::Log::logF({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::fatal, __VA_ARGS__)
#else
#define FATAL(...) do{}while(false)
#define FATALF(...) do{}while(false)
#endif

#if LOG_LEVEL >= LOG_LEVEL_ERROR
#define ERROR(...) ::Aux::Log::log({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::error, __VA_ARGS__)
#define ERRORF(...) ::Aux::Log::logF({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::error, __VA_ARGS__)
#else
#define ERROR(...) do{}while(false)
#define ERRORF(...) do{}while(false)
#endif

#if LOG_LEVEL >= LOG_LEVEL_WARN
#define WARN(...) ::Aux::Log::log({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::warn, __VA_ARGS__)
#define WARNF(...) ::Aux::Log::logF({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::warn, __VA_ARGS__)
#else
#define WARN(...) do{}while(false)
#define WARNF(...) do{}while(false)
#endif

#if LOG_LEVEL >= LOG_LEVEL_INFO
#define INFO(...) ::Aux::Log::log({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::info, __VA_ARGS__)
#define INFOF(...) ::Aux::Log::logF({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::info, __VA_ARGS__)
#else
#define INFO(...) do{}while(false)
#define INFOF(...) do{}while(false)
#endif

#if LOG_LEVEL >= LOG_LEVEL_DEBUG
#define DEBUG(...) ::Aux::Log::log({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::debug, __VA_ARGS__)
#define DEBUGF(...) ::Aux::Log::logF({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::debug, __VA_ARGS__)
#else
#define DEBUG(...) do{}while(false)
#define DEBUGF(...) do{}while(false)
#endif
// DEBUG and TRACE are no-ops if NETWORKIT_RELEASE_LOGGING is defined.
#if defined(NETWORKIT_RELEASE_LOGGING)
# define DEBUG(...) do {} while(false)
# define DEBUGF(...) do {} while(false)

#if LOG_LEVEL >= LOG_LEVEL_TRACE
#define TRACE(...) ::Aux::Log::log({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::trace, __VA_ARGS__)
#define TRACEF(...) ::Aux::Log::logF({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::trace, __VA_ARGS__)
#define TRACEPOINT ::Aux::Log::log({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::trace, "tracepoint")
# define TRACE(...) do {} while(false)
# define TRACEF(...) do {} while(false)
# define TRACEPOINT do {} while(false)
#else
#define TRACE(...) do{}while(false)
#define TRACEF(...) do{}while(false)
#define TRACEPOINT do{}while(false)
#endif

#endif // NOLOGGING
# define DEBUG(...) ::Aux::Log::log({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::debug, __VA_ARGS__)
# define DEBUGF(...) ::Aux::Log::logF({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::debug, __VA_ARGS__)

# define TRACE(...) ::Aux::Log::log({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::trace, __VA_ARGS__)
# define TRACEF(...) ::Aux::Log::logF({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::trace, __VA_ARGS__)
# define TRACEPOINT ::Aux::Log::log({__FILE__, NETWORKT_PRETTY_FUNCTION, __LINE__},\
::Aux::Log::LogLevel::trace, "tracepoint")
#endif // defined(NETWORKIT_RELEASE_LOGGING)

namespace Aux { namespace Log {

Expand All @@ -123,14 +68,15 @@ enum class LogLevel {
info,
warn,
error,
fatal
fatal,
quiet // Emits no log messages at all.
};

/**
* Accept loglevel as string and set.
* @param logLevel as string
*/
void setLogLevel(std::string logLevel);
void setLogLevel(const std::string &logLevel);

/**
* @return current loglevel as string
Expand All @@ -148,15 +94,15 @@ bool getPrintTime();
void setPrintLocation(bool b);
bool getPrintLocation();

void setLogfile(const std::string& filename);
void setLogfile(const std::string &filename);
}

namespace Impl {
void log(const Location& loc, LogLevel p, const std::string msg);
void log(const Location &loc, LogLevel p, const std::string &msg);
} //namespace impl

template<typename...T>
void log(const Location& loc, LogLevel p, const T&...args) {
void log(const Location &loc, LogLevel p, const T &...args) {
if(p >= Settings::getLogLevel()) {
std::stringstream stream;
printToStream(stream, args...);
Expand All @@ -165,7 +111,7 @@ void log(const Location& loc, LogLevel p, const T&...args) {
}

template<typename...T>
void logF(const Location& loc, LogLevel p, const std::string& format, const T&...args) {
void logF(const Location &loc, LogLevel p, const std::string &format, const T &...args) {
if(p >= Settings::getLogLevel()) {
std::stringstream stream;
printToStreamF(stream, format, args...);
Expand Down
10 changes: 4 additions & 6 deletions networkit/cpp/Unittests-X.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,13 @@ int main(int argc, char *argv[]) {
}

// Configure logging
#ifndef NOLOGGING
Aux::Log::setLogLevel(options.loglevel);
Aux::Log::Settings::setPrintLocation(options.sourceLocation);
std::cout << "Loglevel: " << Aux::Log::getLogLevel() << "\n";
#else
if (options.loglevel != "ERROR")
std::cout << "WARNING: --loglevel is ignored in NOLOGGING builds" << std::endl;
if (options.sourceLocation)
std::cout << "WARNING: --srcloc is ignored in NOLOGGING builds" << std::endl;
#ifdef NETWORKIT_RELEASE_LOGGING
if (options.loglevel == "TRACE" || options.loglevel == "DEBUG")
std::cout << "WARNING: TRACE and DEBUG messages are missing"
" in NETWORKIT_RELEASE_LOGGING builds" << std::endl;
#endif

// Configure parallelism
Expand Down
55 changes: 32 additions & 23 deletions networkit/cpp/auxiliary/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Aux { namespace Log {

void setLogLevel(std::string logLevel) {
void setLogLevel(const std::string &logLevel) {
if (logLevel == "TRACE") {
Settings::setLogLevel(LogLevel::trace);
} else if (logLevel == "DEBUG") {
Expand All @@ -22,29 +22,32 @@ void setLogLevel(std::string logLevel) {
Settings::setLogLevel(LogLevel::error);
} else if (logLevel == "FATAL") {
Settings::setLogLevel(LogLevel::fatal);
} else if (logLevel == "QUIET") {
Settings::setLogLevel(LogLevel::quiet);
} else {
throw std::runtime_error("unknown loglevel");
}
}

std::string getLogLevel() {
LogLevel current = Settings::getLogLevel();
if (current == LogLevel::trace) {
switch (current) {
case LogLevel::trace:
return "TRACE";
} else if (current == LogLevel::debug) {
case LogLevel::debug:
return "DEBUG";
} else if (current == LogLevel::info) {
case LogLevel::info:
return "INFO";
} else if (current == LogLevel::warn) {
case LogLevel::warn:
return "WARN";
} else if (current == LogLevel::error) {
case LogLevel::error:
return "ERROR";
} else if (current == LogLevel::fatal) {
case LogLevel::fatal:
return "FATAL";
} else {
// this only exists to silence a warning:
// TODO: consider replacing it with __builtin_unreachable();
throw std::logic_error{"invalid loglevel. This should NEVER happen"};
case LogLevel::quiet:
return "QUIET";
default:
throw std::logic_error{"invalid loglevel in getLogLevel()"};
}
}

Expand All @@ -53,7 +56,13 @@ namespace Settings {
namespace {
bool printTime = false;
bool printLocation = false;

#ifdef NETWORKIT_QUIET_LOGGING
LogLevel loglevel = LogLevel::quiet;
#else
LogLevel loglevel = LogLevel::info;
#endif

std::ofstream logfile;

std::atomic_bool logfileIsOpen{false};
Expand All @@ -69,7 +78,7 @@ bool getPrintTime() {return printTime;}
void setPrintLocation(bool b) {printLocation = b;}
bool getPrintLocation() {return printLocation;}

void setLogfile(const std::string& filename) {
void setLogfile(const std::string &filename) {
std::lock_guard<std::mutex> guard{logfileMutex};
if(logfile.is_open()) {
logfile.close();
Expand All @@ -83,7 +92,7 @@ void setLogfile(const std::string& filename) {
}
} // namespace Settings

void printLogLevel(std::ostream& stream, LogLevel p) {
void printLogLevel(std::ostream &stream, LogLevel p) {
switch(p) {
case LogLevel::fatal:
stream << "[FATAL]"; break;
Expand All @@ -100,12 +109,12 @@ void printLogLevel(std::ostream& stream, LogLevel p) {
}
}

void printTime(std::ostream& stream,
const std::chrono::time_point<std::chrono::system_clock>& timePoint) {
void printTime(std::ostream &stream,
const std::chrono::time_point<std::chrono::system_clock> &timePoint) {
stream << "[" << timePoint.time_since_epoch().count() << "]";
}

void printLocation(std::ostream& stream, const Location& loc) {
void printLocation(std::ostream &stream, const Location &loc) {
stream << "[" << loc.file << ", " << loc.line << ": " << loc.function << "]";
}

Expand All @@ -128,9 +137,9 @@ std::tuple<std::string, std::string> getTerminalFormat(LogLevel p) {
}
}

static void logToTerminal(const Location& loc, LogLevel p,
const std::chrono::time_point<std::chrono::system_clock>& timePoint,
const std::string msg) {
static void logToTerminal(const Location &loc, LogLevel p,
const std::chrono::time_point<std::chrono::system_clock> &timePoint,
const std::string &msg) {
std::stringstream stream;

if(Settings::getPrintTime()) {
Expand Down Expand Up @@ -171,9 +180,9 @@ static void logToTerminal(const Location& loc, LogLevel p,
}
}

static void logToFile(const Location& loc, LogLevel p,
const std::chrono::time_point<std::chrono::system_clock>& timePoint,
const std::string& msg) {
static void logToFile(const Location &loc, LogLevel p,
const std::chrono::time_point<std::chrono::system_clock> &timePoint,
const std::string &msg) {
if(!Settings::logfileIsOpen) {
return;
}
Expand All @@ -199,7 +208,7 @@ static void logToFile(const Location& loc, LogLevel p,

namespace Impl {

void log(const Location& loc, LogLevel p, const std::string msg) {
void log(const Location &loc, LogLevel p, const std::string &msg) {
auto time =std::chrono::system_clock::now();

logToTerminal(loc, p, time, msg);
Expand Down
2 changes: 1 addition & 1 deletion networkit/cpp/auxiliary/test/AuxGTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AuxGTest: public testing::Test{};

TEST_F(AuxGTest, produceRandomIntegers) {
Aux::Random::setSeed(1, false);
#if (LOG_LEVEL == LOG_LEVEL_TRACE)
#ifndef NETWORKIT_RELEASE_LOGGING
int64_t l = 0; // lower bound
int64_t u = 100; // upper bound

Expand Down
2 changes: 1 addition & 1 deletion networkit/cpp/centrality/TopCloseness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ void TopCloseness::run() {
count *distances = NULL;
node *pred = NULL;
count visEdges = 0;
#if LOG_LEVEL >= LOG_LEVEL_DEBUG
#ifndef NETWORKIT_RELEASE_LOGGING
count iters = 0;
#endif
double farnessS;
Expand Down
2 changes: 1 addition & 1 deletion networkit/cpp/generators/quadtree/test/QuadTreeGTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ TEST_F(QuadTreeGTest, testQuadTreeThresholdGrowth) {
vector<index> neighbours = quad.getElementsInHyperbolicCircle(HyperbolicSpace::polarToCartesian(angles[query], radii[query]), threshold);
EXPECT_GE(neighbours.size(), lastNeighbours.size());
if (neighbours.size() < lastNeighbours.size()) {
#if LOG_LEVEL >= LOG_LEVEL_DEBUG
#ifndef NETWORKIT_RELEASE_LOGGING
DEBUG("Previous Neighbours: ");
for (index neighbour : lastNeighbours) {
DEBUG(neighbour);
Expand Down
Loading

0 comments on commit 6059174

Please sign in to comment.