Skip to content

Commit 8e6bb49

Browse files
authored
Merge branch 'master' into staging/warning-cleanup-1
2 parents 927e642 + 323d0eb commit 8e6bb49

File tree

1 file changed

+72
-35
lines changed

1 file changed

+72
-35
lines changed

loguru.cpp

Lines changed: 72 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#ifndef _WIN32
1+
#if defined(__GNUC__) || defined(__clang__)
22
// Disable all warnings from gcc/clang:
33
#pragma GCC diagnostic push
44
#pragma GCC diagnostic ignored "-Wpragmas"
@@ -11,16 +11,13 @@
1111
#pragma GCC diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
1212
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
1313
#pragma GCC diagnostic ignored "-Wpadded"
14-
#pragma GCC diagnostic ignored "-Wsign-compare"
1514
#pragma GCC diagnostic ignored "-Wsign-conversion"
1615
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
1716
#pragma GCC diagnostic ignored "-Wunused-macros"
1817
#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
19-
#else
20-
#ifdef _MSC_VER
18+
#elif defined(_MSC_VER)
2119
#pragma warning(push)
22-
#pragma warning(disable:4018)
23-
#endif // _MSC_VER
20+
#pragma warning(disable:4365) // conversion from 'X' to 'Y', signed/unsigned mismatch
2421
#endif
2522

2623
#include "loguru.hpp"
@@ -1244,27 +1241,47 @@ namespace loguru
12441241
{
12451242
if (out_buff_size == 0) { return; }
12461243
out_buff[0] = '\0';
1247-
long pos = 0;
1248-
if (g_preamble_date) {
1249-
pos += snprintf(out_buff + pos, out_buff_size - pos, "date ");
1244+
size_t pos = 0;
1245+
if (g_preamble_date && pos < out_buff_size) {
1246+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "date ");
1247+
if (bytes > 0) {
1248+
pos += bytes;
1249+
}
12501250
}
12511251
if (g_preamble_time && pos < out_buff_size) {
1252-
pos += snprintf(out_buff + pos, out_buff_size - pos, "time ");
1252+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "time ");
1253+
if (bytes > 0) {
1254+
pos += bytes;
1255+
}
12531256
}
12541257
if (g_preamble_uptime && pos < out_buff_size) {
1255-
pos += snprintf(out_buff + pos, out_buff_size - pos, "( uptime ) ");
1258+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "( uptime ) ");
1259+
if (bytes > 0) {
1260+
pos += bytes;
1261+
}
12561262
}
12571263
if (g_preamble_thread && pos < out_buff_size) {
1258-
pos += snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", LOGURU_THREADNAME_WIDTH, " thread name/id");
1264+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", LOGURU_THREADNAME_WIDTH, " thread name/id");
1265+
if (bytes > 0) {
1266+
pos += bytes;
1267+
}
12591268
}
12601269
if (g_preamble_file && pos < out_buff_size) {
1261-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%*s:line ", LOGURU_FILENAME_WIDTH, "file");
1270+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%*s:line ", LOGURU_FILENAME_WIDTH, "file");
1271+
if (bytes > 0) {
1272+
pos += bytes;
1273+
}
12621274
}
12631275
if (g_preamble_verbose && pos < out_buff_size) {
1264-
pos += snprintf(out_buff + pos, out_buff_size - pos, " v");
1276+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, " v");
1277+
if (bytes > 0) {
1278+
pos += bytes;
1279+
}
12651280
}
12661281
if (g_preamble_pipe && pos < out_buff_size) {
1267-
/* pos += */ snprintf(out_buff + pos, out_buff_size - pos, "| ");
1282+
// Because this is the last if-statement, we avoid incrementing the
1283+
// position to clarify it's unused.
1284+
(void)snprintf(out_buff + pos, out_buff_size - pos, "| ");
12681285
}
12691286
}
12701287

@@ -1296,36 +1313,55 @@ namespace loguru
12961313
snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity);
12971314
}
12981315

1299-
long pos = 0;
1300-
1301-
if (g_preamble_date) {
1302-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%04d-%02d-%02d ",
1303-
1900 + time_info.tm_year, 1 + time_info.tm_mon, time_info.tm_mday);
1316+
size_t pos = 0;
1317+
if (g_preamble_date && pos < out_buff_size) {
1318+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%04d-%02d-%02d ",
1319+
1900 + time_info.tm_year, 1 + time_info.tm_mon, time_info.tm_mday);
1320+
if (bytes > 0) {
1321+
pos += bytes;
1322+
}
13041323
}
13051324
if (g_preamble_time && pos < out_buff_size) {
1306-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%02d:%02d:%02d.%03lld ",
1307-
time_info.tm_hour, time_info.tm_min, time_info.tm_sec, ms_since_epoch % 1000);
1325+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%02d:%02d:%02d.%03lld ",
1326+
time_info.tm_hour, time_info.tm_min, time_info.tm_sec, ms_since_epoch % 1000);
1327+
if (bytes > 0) {
1328+
pos += bytes;
1329+
}
13081330
}
13091331
if (g_preamble_uptime && pos < out_buff_size) {
1310-
pos += snprintf(out_buff + pos, out_buff_size - pos, "(%8.3fs) ",
1311-
uptime_sec);
1332+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "(%8.3fs) ",
1333+
uptime_sec);
1334+
if (bytes > 0) {
1335+
pos += bytes;
1336+
}
13121337
}
13131338
if (g_preamble_thread && pos < out_buff_size) {
1314-
pos += snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]",
1315-
LOGURU_THREADNAME_WIDTH, thread_name);
1339+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]",
1340+
LOGURU_THREADNAME_WIDTH, thread_name);
1341+
if (bytes > 0) {
1342+
pos += bytes;
1343+
}
13161344
}
13171345
if (g_preamble_file && pos < out_buff_size) {
13181346
char shortened_filename[LOGURU_FILENAME_WIDTH + 1];
13191347
snprintf(shortened_filename, LOGURU_FILENAME_WIDTH + 1, "%s", file);
1320-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%*s:%-5u ",
1321-
LOGURU_FILENAME_WIDTH, shortened_filename, line);
1348+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%*s:%-5u ",
1349+
LOGURU_FILENAME_WIDTH, shortened_filename, line);
1350+
if (bytes > 0) {
1351+
pos += bytes;
1352+
}
13221353
}
13231354
if (g_preamble_verbose && pos < out_buff_size) {
1324-
pos += snprintf(out_buff + pos, out_buff_size - pos, "%4s",
1325-
level_buff);
1355+
int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%4s",
1356+
level_buff);
1357+
if (bytes > 0) {
1358+
pos += bytes;
1359+
}
13261360
}
13271361
if (g_preamble_pipe && pos < out_buff_size) {
1328-
/* pos += */ snprintf(out_buff + pos, out_buff_size - pos, "| ");
1362+
// Because this is the last if-statement, we avoid incrementing the
1363+
// position to clarify it's unused.
1364+
(void)snprintf(out_buff + pos, out_buff_size - pos, "| ");
13291365
}
13301366
}
13311367

@@ -1960,10 +1996,11 @@ namespace loguru
19601996

19611997
#endif // _WIN32
19621998

1963-
#ifdef _WIN32
1964-
#ifdef _MSC_VER
1999+
2000+
#if defined(__GNUC__) || defined(__clang__)
2001+
#pragma GCC diagnostic pop
2002+
#elif defined(_MSC_VER)
19652003
#pragma warning(pop)
1966-
#endif // _MSC_VER
1967-
#endif // _WIN32
2004+
#endif
19682005

19692006
#endif // LOGURU_IMPLEMENTATION

0 commit comments

Comments
 (0)