diff options
Diffstat (limited to 'common')
| -rw-r--r-- | common/Logger.h | 25 | ||||
| -rw-r--r-- | common/LoggerImpl.cpp | 46 | ||||
| -rw-r--r-- | common/LoggerImpl.h | 67 |
3 files changed, 138 insertions, 0 deletions
diff --git a/common/Logger.h b/common/Logger.h new file mode 100644 index 0000000..324b71e --- /dev/null +++ b/common/Logger.h @@ -0,0 +1,25 @@ +#pragma once + +namespace nv +{ + enum struct LogSeverity + { + kInfo = 0, // Message contains information about normal and expected behavior + kWarning = 1, // Message contains information about a potentially problematic situation + kError = 2, // Message contains information about a problem + kFatal = 3 // Message contains information about a fatal problem; program should be aborted + }; + + static const char * LogSeverityStrings[] = { "INFO", "WARNING", "ERROR", "FATAL" }; + + + // Note: Implementation of this interface must be thread-safe + class ILogger + { + public: + // �filename� is NULL and �linenumber� is 0 in release builds of GameWorks + virtual void log(const char* text, LogSeverity severity, const char* filename, int linenumber) = 0; + virtual void log(const wchar_t* text, LogSeverity severity, const wchar_t* filename, int linenumber) = 0; + }; +} + diff --git a/common/LoggerImpl.cpp b/common/LoggerImpl.cpp new file mode 100644 index 0000000..9a2921d --- /dev/null +++ b/common/LoggerImpl.cpp @@ -0,0 +1,46 @@ +#include "LoggerImpl.h" +#include <iostream> +#include <sstream> +#include <windows.h> + +LoggerWWSamples* g_Logger = nullptr; + +LoggerWWSamples::LoggerWWSamples(): +LoggingLevel(nv::LogSeverity::kInfo) +{ + +} + +LoggerWWSamples::LoggerWWSamples(nv::LogSeverity loggingLevel) : + LoggingLevel(loggingLevel) +{ + +} + +nv::LogSeverity LoggerWWSamples::getLoggingLevel() +{ + return LoggingLevel; +} + +void LoggerWWSamples::setLoggingLevel(nv::LogSeverity newLevel) +{ + LoggingLevel = newLevel; +} + +void LoggerWWSamples::log(const char* text, nv::LogSeverity severity, const char* filename, int linenumber) +{ + std::ostringstream out; + + out << filename << "(" << linenumber << "): " << "[" << nv::LogSeverityStrings[(int) severity] << "] " << text << std::endl; + + OutputDebugStringA(out.str().c_str()); +} + +void LoggerWWSamples::log(const wchar_t* text, nv::LogSeverity severity, const wchar_t* filename, int linenumber) +{ + std::wstringstream out; + + out << filename << "(" << linenumber << "): " << "[" << nv::LogSeverityStrings[(int)severity] << "] " << text << std::endl; + + OutputDebugStringW(out.str().c_str()); +} diff --git a/common/LoggerImpl.h b/common/LoggerImpl.h new file mode 100644 index 0000000..a07cead --- /dev/null +++ b/common/LoggerImpl.h @@ -0,0 +1,67 @@ +#pragma once +#include "Logger.h" +#include <cstdarg> +#include <cstdio> + +class LoggerWWSamples : public nv::ILogger +{ +public: + LoggerWWSamples(); + LoggerWWSamples(nv::LogSeverity loggingLevel); + virtual ~LoggerWWSamples() = default; + + virtual void log(const char* text, nv::LogSeverity severity, const char* filename, int linenumber) override; + virtual void log(const wchar_t* text, nv::LogSeverity severity, const wchar_t* filename, int linenumber) override; + + nv::LogSeverity getLoggingLevel(); + void setLoggingLevel(nv::LogSeverity newLevel); + +private: + nv::LogSeverity LoggingLevel; +}; + +extern LoggerWWSamples* g_Logger; + +namespace wwsamples +{ + inline void log(nv::LogSeverity severity, const char* filename, int linenumber, const char* format, ...) + { + if (g_Logger == nullptr) + { + g_Logger = new LoggerWWSamples(); + } + + if (g_Logger->getLoggingLevel() > severity) + return; + + char buffer[1024]; + + va_list args; + va_start(args, format); +#if NV_GCC_FAMILY +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-nonliteral" +#endif + vsnprintf_s(buffer, sizeof(buffer), format, args); +#if NV_GCC_FAMILY +#pragma GCC diagnostic pop +#endif + va_end(args); + + g_Logger->log(buffer, severity, filename, linenumber); + } +} + + + +#ifndef NV_LOG +/// <param name="__VA_ARGS__">format, ...</param> +#define NV_LOG(...) wwsamples::log(nv::LogSeverity::kInfo, __FILE__, __LINE__, __VA_ARGS__) +/// <param name="__VA_ARGS__">format, ...</param> +#define NV_WARN(...) wwsamples::log(nv::LogSeverity::kWarning, __FILE__, __LINE__, __VA_ARGS__) +/// <param name="__VA_ARGS__">format, ...</param> +#define NV_ERROR(...) wwsamples::log(nv::LogSeverity::kError, __FILE__, __LINE__, __VA_ARGS__) +/// <param name="__VA_ARGS__">format, ...</param> +#define NV_FATAL(...) wwsamples::log(nv::LogSeverity::kFatal, __FILE__, __LINE__, __VA_ARGS__) + +#endif
\ No newline at end of file |