From 446d407e7837b1e20ad30a357a8985d65c305b29 Mon Sep 17 00:00:00 2001 From: Jonas Stienen Date: Sat, 13 Aug 2016 23:16:11 +0200 Subject: [PATCH] Adding ITALog --- CMakeLists.txt | 2 ++ include/ITALog.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ src/ITALog.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 include/ITALog.h create mode 100644 src/ITALog.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 97e9115..dce1ee6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,7 @@ set( ITABaseHeader "include/ITAHDFTSpectrum.h" "include/ITAFunctors.h" "include/ITAHPT.h" + "include/ITALog.h" "include/ITAMutex.h" "include/ITANumericUtils.h" "include/ITASampleBuffer.h" @@ -86,6 +87,7 @@ set( ITABaseSources "src/ITAFileSystemUtils.cpp" "src/ITAHDFTSpectrum.cpp" "src/ITAHPT.cpp" + "src/ITALog.cpp" "src/ITAMutex.cpp" "src/ITANumericUtils.cpp" "src/ITASampleBuffer.cpp" diff --git a/include/ITALog.h b/include/ITALog.h new file mode 100644 index 0000000..897d4f5 --- /dev/null +++ b/include/ITALog.h @@ -0,0 +1,62 @@ +/* +* ---------------------------------------------------------------- +* +* ITA core libs +* (c) Copyright Institute of Technical Acoustics (ITA) +* RWTH Aachen University, Germany, 2015-2016 +* +* ---------------------------------------------------------------- +* ____ __________ _______ +* // / //__ ___/ // _ | +* // / // / // /_| | +* // / // / // ___ | +* //__/ //__/ //__/ |__| +* +* ---------------------------------------------------------------- +* +*/ + +#ifndef INCLUDE_WATCHER_ITA_LOG +#define INCLUDE_WATCHER_ITA_LOG + +//! Ausgabe-Direktiven +enum +{ + ITALOG_OUTPUT_CONSOLE = 0, //!< Log to stdout, stderr + ITALOG_OUTPUT_MSVC = 1 //!< Log to the Visual Studio console ("OutputDebugString") +}; + +//! Log-Stufen +enum +{ + ITALOG_QUIET = 0, //!< Stumm + ITALOG_ERROR = 1, //!< Fehler + ITALOG_WARN = 2, //!< Warnung + ITALOG_INFO = 3, //!< Information + ITALOG_TRACE = 4 //!< Gesprächig +}; + +//! Flags +const int ITALOG_MODULENAME = 1<<0; +const int ITALOG_SHORTNAME = 1<<1; +const int ITALOG_LEVELINFO = 1<<2; +const int ITALOG_TIMESTAMP = 1<<3; + + +//! Ausgabe-Funktionen +void ITA_BASE_API italog_printf(const char* pszModuleName, int iFlags, const char* format ...); +void ITA_BASE_API italog_msvc( const char* pszModuleName, int iFlags, const char* format ... ); + + +// --= Makros für verschiedene Ausgabeversionen mit mehr oder weniger Parametern =-- + +//! Einfache Ausgabe ohne weitere Argumente +#define ITALOG_PRINTF0(...) { italog_printf("", 0, __VA_ARGS__); } +#define ITALOG_PRINTF1(SHORTNAME, ...) { italog_printf(SHORTNAME, ITALOG_SHORTNAME, __VA_ARGS__); } +#define ITALOG_PRINTF2(MODULENAME, ...) { italog_printf(MODULENAME, ITALOG_MODULENAME, __VA_ARGS__); } +#define ITALOG_PRINTF9(MODULE, NAME, SHORTNAME, FLAGS, LOGLEVEL, LOGTYPE, ...) { if(LOGLEVEL >= LOGTYPE) { italog_printf(MODULENAME, FLAGS, __VA_ARGS__); } } + +#define ITALOG_MSVC0(...) { italog_msvc("", 0, __VA_ARGS__); } +#define ITALOG_MSVC1(SHORTNAME, ...) { italog_msvc(SHORTNAME, ITALOG_SHORTNAME, __VA_ARGS__); } + +#endif // INCLUDE_WATCHER_ITA_LOG diff --git a/src/ITALog.cpp b/src/ITALog.cpp new file mode 100644 index 0000000..b5b4fb1 --- /dev/null +++ b/src/ITALog.cpp @@ -0,0 +1,59 @@ +#include + +#include + +#include +#include +#include + +#ifdef WIN32 +#include +#endif + +#define DEBUG_PRINTF_BUFSIZE 16384 + +static ITACriticalSection g_csDebugPrintf; +static char g_pszDebugPrintfBuf[DEBUG_PRINTF_BUFSIZE]; + + +void italog_printf(const char* pszModuleName, int iFlags, const char* format ...) { + + std::string sCombinedPreamble = ""; + if (iFlags & (ITALOG_MODULENAME + ITALOG_SHORTNAME)) { + sCombinedPreamble += pszModuleName; + sCombinedPreamble += ": "; + } + + // Source: VADebug by fwe + va_list args; + va_start(args, format); +#ifdef WIN32 + printf_s("%s", sCombinedPreamble.c_str()); +#else + printf("%s", sCombinedPreamble.c_str()); +#endif + vfprintf(stdout, format, args); + va_end(args); + + return; +}; + +#ifdef WIN32 +void italog_msvc(const char* pszModuleName, int iFlags, const char* format ...) { + g_csDebugPrintf.enter(); + + int pos = 0; + if (iFlags & (ITALOG_MODULENAME + ITALOG_SHORTNAME)) + pos = sprintf_s(g_pszDebugPrintfBuf, DEBUG_PRINTF_BUFSIZE, "%s: ", pszModuleName); + + va_list args; + va_start(args, format); + vsprintf_s(g_pszDebugPrintfBuf+pos, DEBUG_PRINTF_BUFSIZE-pos, format, args); + va_end(args); + + OutputDebugStringA(g_pszDebugPrintfBuf); + g_csDebugPrintf.leave(); + + return; +} +#endif \ No newline at end of file -- GitLab