Skip to content

Commit

Permalink
Merge branch 'master' into staging/warning-cleanup-1
Browse files Browse the repository at this point in the history
  • Loading branch information
kcgen authored Sep 16, 2021
2 parents 927e642 + 323d0eb commit 8e6bb49
Showing 1 changed file with 72 additions and 35 deletions.
107 changes: 72 additions & 35 deletions loguru.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef _WIN32
#if defined(__GNUC__) || defined(__clang__)
// Disable all warnings from gcc/clang:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragmas"
Expand All @@ -11,16 +11,13 @@
#pragma GCC diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
#pragma GCC diagnostic ignored "-Wpadded"
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC diagnostic ignored "-Wunused-macros"
#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#else
#ifdef _MSC_VER
#elif defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4018)
#endif // _MSC_VER
#pragma warning(disable:4365) // conversion from 'X' to 'Y', signed/unsigned mismatch
#endif

#include "loguru.hpp"
Expand Down Expand Up @@ -1244,27 +1241,47 @@ namespace loguru
{
if (out_buff_size == 0) { return; }
out_buff[0] = '\0';
long pos = 0;
if (g_preamble_date) {
pos += snprintf(out_buff + pos, out_buff_size - pos, "date ");
size_t pos = 0;
if (g_preamble_date && pos < out_buff_size) {
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "date ");
if (bytes > 0) {
pos += bytes;
}
}
if (g_preamble_time && pos < out_buff_size) {
pos += snprintf(out_buff + pos, out_buff_size - pos, "time ");
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "time ");
if (bytes > 0) {
pos += bytes;
}
}
if (g_preamble_uptime && pos < out_buff_size) {
pos += snprintf(out_buff + pos, out_buff_size - pos, "( uptime ) ");
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "( uptime ) ");
if (bytes > 0) {
pos += bytes;
}
}
if (g_preamble_thread && pos < out_buff_size) {
pos += snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", LOGURU_THREADNAME_WIDTH, " thread name/id");
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", LOGURU_THREADNAME_WIDTH, " thread name/id");
if (bytes > 0) {
pos += bytes;
}
}
if (g_preamble_file && pos < out_buff_size) {
pos += snprintf(out_buff + pos, out_buff_size - pos, "%*s:line ", LOGURU_FILENAME_WIDTH, "file");
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%*s:line ", LOGURU_FILENAME_WIDTH, "file");
if (bytes > 0) {
pos += bytes;
}
}
if (g_preamble_verbose && pos < out_buff_size) {
pos += snprintf(out_buff + pos, out_buff_size - pos, " v");
int bytes = snprintf(out_buff + pos, out_buff_size - pos, " v");
if (bytes > 0) {
pos += bytes;
}
}
if (g_preamble_pipe && pos < out_buff_size) {
/* pos += */ snprintf(out_buff + pos, out_buff_size - pos, "| ");
// Because this is the last if-statement, we avoid incrementing the
// position to clarify it's unused.
(void)snprintf(out_buff + pos, out_buff_size - pos, "| ");
}
}

Expand Down Expand Up @@ -1296,36 +1313,55 @@ namespace loguru
snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity);
}

long pos = 0;

if (g_preamble_date) {
pos += snprintf(out_buff + pos, out_buff_size - pos, "%04d-%02d-%02d ",
1900 + time_info.tm_year, 1 + time_info.tm_mon, time_info.tm_mday);
size_t pos = 0;
if (g_preamble_date && pos < out_buff_size) {
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%04d-%02d-%02d ",
1900 + time_info.tm_year, 1 + time_info.tm_mon, time_info.tm_mday);
if (bytes > 0) {
pos += bytes;
}
}
if (g_preamble_time && pos < out_buff_size) {
pos += snprintf(out_buff + pos, out_buff_size - pos, "%02d:%02d:%02d.%03lld ",
time_info.tm_hour, time_info.tm_min, time_info.tm_sec, ms_since_epoch % 1000);
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%02d:%02d:%02d.%03lld ",
time_info.tm_hour, time_info.tm_min, time_info.tm_sec, ms_since_epoch % 1000);
if (bytes > 0) {
pos += bytes;
}
}
if (g_preamble_uptime && pos < out_buff_size) {
pos += snprintf(out_buff + pos, out_buff_size - pos, "(%8.3fs) ",
uptime_sec);
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "(%8.3fs) ",
uptime_sec);
if (bytes > 0) {
pos += bytes;
}
}
if (g_preamble_thread && pos < out_buff_size) {
pos += snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]",
LOGURU_THREADNAME_WIDTH, thread_name);
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]",
LOGURU_THREADNAME_WIDTH, thread_name);
if (bytes > 0) {
pos += bytes;
}
}
if (g_preamble_file && pos < out_buff_size) {
char shortened_filename[LOGURU_FILENAME_WIDTH + 1];
snprintf(shortened_filename, LOGURU_FILENAME_WIDTH + 1, "%s", file);
pos += snprintf(out_buff + pos, out_buff_size - pos, "%*s:%-5u ",
LOGURU_FILENAME_WIDTH, shortened_filename, line);
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%*s:%-5u ",
LOGURU_FILENAME_WIDTH, shortened_filename, line);
if (bytes > 0) {
pos += bytes;
}
}
if (g_preamble_verbose && pos < out_buff_size) {
pos += snprintf(out_buff + pos, out_buff_size - pos, "%4s",
level_buff);
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%4s",
level_buff);
if (bytes > 0) {
pos += bytes;
}
}
if (g_preamble_pipe && pos < out_buff_size) {
/* pos += */ snprintf(out_buff + pos, out_buff_size - pos, "| ");
// Because this is the last if-statement, we avoid incrementing the
// position to clarify it's unused.
(void)snprintf(out_buff + pos, out_buff_size - pos, "| ");
}
}

Expand Down Expand Up @@ -1960,10 +1996,11 @@ namespace loguru

#endif // _WIN32

#ifdef _WIN32
#ifdef _MSC_VER

#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
#pragma warning(pop)
#endif // _MSC_VER
#endif // _WIN32
#endif

#endif // LOGURU_IMPLEMENTATION

0 comments on commit 8e6bb49

Please sign in to comment.