Skip to content

Commit

Permalink
Return when the logging level does not match.
Browse files Browse the repository at this point in the history
Signed-off-by: fruffy <fruffy@nyu.edu>
  • Loading branch information
fruffy committed Jul 2, 2024
1 parent 7b183ab commit aaec7e7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions backends/p4tools/common/lib/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <fstream>
#include <unordered_map>

#include "lib/error.h"
#include "lib/log.h"
#include "lib/timer.h"

Expand Down
17 changes: 11 additions & 6 deletions backends/p4tools/common/lib/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
#define BACKENDS_P4TOOLS_COMMON_LIB_LOGGING_H_

#include <filesystem>
#include <optional>
#include <string>
#include <utility>

#include "backends/p4tools/common/lib/util.h"
#include <boost/format.hpp>

#include "lib/log.h"

namespace P4Tools {

Expand All @@ -24,11 +27,13 @@ std::string logHelper(boost::format &f, T &&t, Args &&...args) {
template <typename... Arguments>
void printFeature(const std::string &label, int level, const std::string &fmt,
Arguments &&...args) {
boost::format f(fmt);
// Do not print logging messages when logging is not enabled.
if (!Log::fileLogLevelIsAtLeast(label.c_str(), level)) {
return;
}

auto result = logHelper(f, std::forward<Arguments>(args)...);

LOG_FEATURE(label.c_str(), level, result);
boost::format f(fmt);
LOG_FEATURE(label.c_str(), level, logHelper(f, std::forward<Arguments>(args)...));
}

/// Helper functions that prints strings associated with basic tool information.
Expand All @@ -40,7 +45,7 @@ void printInfo(const std::string &fmt, Arguments &&...args) {

/// Convenience function for printing debug information.
/// Easier to use then LOG(XX) since we only need one specific log-level across all files.
/// Can be invoked with "-T 4:tools_debug".
/// Can be invoked with "-T tools_debug:4".
template <typename... Arguments>
void printDebug(const std::string &fmt, Arguments &&...args) {
printFeature("tools_debug", 4, fmt, std::forward<Arguments>(args)...);
Expand Down
2 changes: 0 additions & 2 deletions backends/p4tools/common/lib/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
#include <string>
#include <vector>

#include <boost/format.hpp>
#include <boost/random/mersenne_twister.hpp>

#include "ir/ir.h"
#include "lib/cstring.h"

namespace P4Tools {

Expand Down

0 comments on commit aaec7e7

Please sign in to comment.