Skip to content

Commit

Permalink
Moved log functions inside logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimi1010 committed Oct 14, 2024
1 parent 87042f5 commit be95ab8
Showing 1 changed file with 45 additions and 54 deletions.
99 changes: 45 additions & 54 deletions Common++/header/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,6 @@ namespace pcpp
return s << static_cast<std::underlying_type<LogLevel>::type>(v);
}

// Forward declaration
template <class T> void log(LogSource source, LogLevel level, T const& message);
template <> void log(LogSource source, LogLevel level, std::string const& message);
template <> void log(LogSource source, LogLevel level, const char* const& message);

/**
* @class Logger
* PcapPlusPlus logger manager.
Expand Down Expand Up @@ -316,7 +311,48 @@ namespace pcpp
return instance;
}

template <class T> friend void pcpp::log(LogSource source, LogLevel level, T const& message);
template <class T> void log(LogSource source, LogLevel level, T const& message)
{
if (shouldLog(level, source.logModule))
{
std::ostringstream sstream;
sstream << message;
printLogMessage(source, level, sstream.str());
}
};

// Specialization for string to skip the stringstream
template <> void log(LogSource source, LogLevel level, std::string const& message)
{
if (shouldLog(level, source.logModule))
{
printLogMessage(source, level, message);
}
};

// Specialization for const char* to skip the stringstream
template <> void log(LogSource source, LogLevel level, const char* const& message)
{
if (shouldLog(level, source.logModule))
{
printLogMessage(source, level, message);
}
};

template <class T> void logError(LogSource source, T const& message)
{
log(source, LogLevel::Error, message);
};

template <class T> void logInfo(LogSource source, T const& message)
{
log(source, LogLevel::Info, message);
};

template <class T> void logDebug(LogSource source, T const& message)
{
log(source, LogLevel::Debug, message);
};

private:
bool m_LogsEnabled;
Expand All @@ -331,62 +367,17 @@ namespace pcpp
static void defaultLogPrinter(LogLevel logLevel, const std::string& logMessage, const std::string& file,
const std::string& method, const int line);
};

template <class T> inline void log(LogSource source, LogLevel level, T const& message)
{
auto& logger = Logger::getInstance();
if (logger.shouldLog(level, source.logModule))
{
std::ostringstream sstream;
sstream << message;
logger.printLogMessage(source, level, sstream.str());
}
};

// Specialization for string to skip the stringstream
template <> inline void log(LogSource source, LogLevel level, std::string const& message)
{
auto& logger = Logger::getInstance();
if (logger.shouldLog(level, source.logModule))
{
logger.printLogMessage(source, level, message);
}
};

// Specialization for const char* to skip the stringstream
template <> inline void log(LogSource source, LogLevel level, const char* const& message)
{
auto& logger = Logger::getInstance();
if (logger.shouldLog(level, source.logModule))
{
logger.printLogMessage(source, level, message);
}
};

template <class T> inline void logError(LogSource source, T const& message)
{
log(source, LogLevel::Error, message);
};

template <class T> inline void logInfo(LogSource source, T const& message)
{
log(source, LogLevel::Info, message);
};

template <class T> inline void logDebug(LogSource source, T const& message)
{
log(source, LogLevel::Debug, message);
};
} // namespace pcpp

#define PCPP_LOG(level, message) \
do \
{ \
if (pcpp::Logger::getInstance().shouldLog(level, LOG_MODULE)) \
auto& logger = Logger::getInstance(); \
if (logger.shouldLog(level, LOG_MODULE)) \
{ \
std::ostringstream sstream; \
sstream << message; \
pcpp::log(pcpp::LogSource(LOG_MODULE, PCAPPP_FILENAME, __FUNCTION__, __LINE__), level, sstream.str()); \
logger.log(pcpp::LogSource(LOG_MODULE, PCAPPP_FILENAME, __FUNCTION__, __LINE__), level, sstream.str()); \
} \
} while (0)

Expand Down

0 comments on commit be95ab8

Please sign in to comment.