From 5027f4ecc473d452e392e363af50637388f00a49 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Tue, 11 Jun 2019 14:42:14 +0200 Subject: [PATCH] Logger: moved most of the logic to C++ stream Most of the logic was moved to the C++ implementation, log_msg() is now using the ostream. This allows use of colors from rang.hpp also in log messages. --- build_aja_lib_win64.sh | 2 +- src/config_msvc.h | 10 ++++++++-- src/debug.cpp | 21 +-------------------- src/debug.h | 29 +++++++++++++++++++++++------ src/video_capture/aja.cpp | 6 +----- src/video_display/aja.cpp | 4 ---- 6 files changed, 34 insertions(+), 38 deletions(-) diff --git a/build_aja_lib_win64.sh b/build_aja_lib_win64.sh index fae76f5ff..707fec045 100755 --- a/build_aja_lib_win64.sh +++ b/build_aja_lib_win64.sh @@ -32,7 +32,7 @@ export INCLUDE='src;C:\msys64\home\toor\AJA\ajalibraries\ajantv2\includes;C:\msy cp src/video_capture/aja.cpp aja_capture.cpp cp src/video_display/aja.cpp aja_display.cpp -run_vs12 cl //LD //DAJA_WINDOWS //DMSWindows //DAJA_NTV2SDK_VERSION_MAJOR=13 aja_capture.cpp aja_display.cpp src/video_capture/aja_win32_utils.cpp src/video_capture_params.cpp src/utils/config_file.cpp ../AJA/lib/libajantv2.lib advapi32.lib user32.lib winmm.lib //Feaja +run_vs12 cl //LD //D_XKEYCHECK_H //DAJA_WINDOWS //DMSWindows //DAJA_NTV2SDK_VERSION_MAJOR=13 aja_capture.cpp aja_display.cpp src/video_capture/aja_win32_utils.cpp src/video_capture_params.cpp src/utils/config_file.cpp ../AJA/lib/libajantv2.lib advapi32.lib user32.lib winmm.lib //Feaja cp aja.lib /usr/local/lib cp aja.dll /usr/local/bin diff --git a/src/config_msvc.h b/src/config_msvc.h index 3c0eb59ff..4363f93bd 100644 --- a/src/config_msvc.h +++ b/src/config_msvc.h @@ -38,12 +38,18 @@ #ifndef CONFIG_MSVC_H #define CONFIG_MSVC_H -#if defined _MSC_VER && !defined WIN32 +#if defined _MSC_VER #include #include #include // getpid() +#if _MSC_VER <= 1800 +#define constexpr +#define noexcept +#endif + + static inline char * strtok_r(char *str, const char *delim, char **save); /* @@ -69,7 +75,7 @@ static inline char *strtok_r(char *s, const char *delimiters, char **lasts) #define ATTRIBUTE(a) -#define snprintf(x, y, ...) sprintf(x, __VA_ARGS__) +#define snprintf _snprintf #define strncasecmp(x, y, z) strcmp(x, y) #endif diff --git a/src/debug.cpp b/src/debug.cpp index 1389c08cf..2bad52fec 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -94,24 +94,6 @@ void log_msg(int level, const char *format, ...) } #endif /* WIN32 */ - rang::fg color = rang::fg::reset; - rang::style style = rang::style::reset; - - switch (level) { - case LOG_LEVEL_FATAL: color = rang::fg::red; style = rang::style::bold; break; - case LOG_LEVEL_ERROR: color = rang::fg::red; break; - case LOG_LEVEL_WARNING: color = rang::fg::yellow; break; - case LOG_LEVEL_NOTICE: color = rang::fg::green; break; - } - - auto timestamp = (char *) alloca((3 /* "[] " */ + 20 /* 64b int dec */ + 1 /* dot */ + 3 /* ms */) /* time */ + 1); - timestamp[0] = '\0'; - if (log_level >= LOG_LEVEL_VERBOSE) { - unsigned long long time_ms = time_since_epoch_in_ms(); - sprintf(timestamp, "[%llu.%03llu] ", time_ms / 1000, - time_ms % 1000); - } - // get number of required bytes va_start(ap, format); int size = vsnprintf(NULL, 0, format, ap); @@ -126,8 +108,7 @@ void log_msg(int level, const char *format, ...) } va_end(ap); - std::cerr << style << color << timestamp << - buffer << rang::style::reset << rang::fg::reset; + LOG(level) << buffer; } /** diff --git a/src/debug.h b/src/debug.h index 7041b3b20..44e9a9a9c 100644 --- a/src/debug.h +++ b/src/debug.h @@ -61,20 +61,37 @@ void log_msg(int log_level, const char *format, ...); #endif #ifdef __cplusplus -#include +#include +#include +#include "compat/platform_time.h" +#include "rang.hpp" + // Log, version 0.1: a simple logging class class Logger { public: inline Logger(int l) : level(l) {} inline ~Logger() { - log_msg(level, os.str().c_str()); + std::cerr << rang::style::reset << rang::fg::reset; } - inline std::ostringstream& Get() { - return os; + inline std::ostream& Get() { + rang::fg color = rang::fg::reset; + rang::style style = rang::style::reset; + + switch (level) { + case LOG_LEVEL_FATAL: color = rang::fg::red; style = rang::style::bold; break; + case LOG_LEVEL_ERROR: color = rang::fg::red; break; + case LOG_LEVEL_WARNING: color = rang::fg::yellow; break; + case LOG_LEVEL_NOTICE: color = rang::fg::green; break; + } + std::cerr << style << color; + if (log_level >= LOG_LEVEL_VERBOSE) { + unsigned long long time_ms = time_since_epoch_in_ms(); + std::cerr << "[" << std::fixed << std::setprecision(3) << time_ms / 1000.0 << "] "; + } + + return std::cerr; } -protected: - std::ostringstream os; private: int level; }; diff --git a/src/video_capture/aja.cpp b/src/video_capture/aja.cpp index 0c9c927b7..74cf3eeb4 100644 --- a/src/video_capture/aja.cpp +++ b/src/video_capture/aja.cpp @@ -40,10 +40,10 @@ #include "config_unix.h" #include "config_win32.h" #endif +#include "config_msvc.h" #ifdef _MSC_VER #include -#define snprintf _snprintf #endif #include "audio/audio.h" @@ -51,10 +51,6 @@ #include "debug.h" #include "host.h" #include "lib_common.h" -#if defined _MSC_VER && _MSC_VER <= 1800 // VS 2013 -#define constexpr -#define noexcept -#endif #include "rang.hpp" #include "utils/video_frame_pool.h" #include "video.h" diff --git a/src/video_display/aja.cpp b/src/video_display/aja.cpp index 22ed28327..af4111744 100644 --- a/src/video_display/aja.cpp +++ b/src/video_display/aja.cpp @@ -70,10 +70,6 @@ #include "host.h" #include "lib_common.h" #include "video_display.h" -#if defined _MSC_VER && _MSC_VER <= 1800 // VS 2013 -#define constexpr -#define noexcept -#endif #include "rang.hpp" #include "video.h"