log : fix compile warnings

- do not use C++20 stuff
- use PRIu64 to print uint64_t
- avoid string copies by using const ref
- fix ", ##__VA_ARGS__" warnings
- compare strings with == and !=
This commit is contained in:
Georgi Gerganov 2023-08-29 13:46:43 +03:00
parent 2c1930d338
commit b97958a511
No known key found for this signature in database
GPG key ID: 449E073F9DC10735
2 changed files with 60 additions and 71 deletions

View file

@ -327,7 +327,7 @@ k_quants.o: k_quants.c k_quants.h
endif # LLAMA_NO_K_QUANTS endif # LLAMA_NO_K_QUANTS
ifdef LLAMA_DISABLE_LOGS ifdef LLAMA_DISABLE_LOGS
CFLAGS += -DLOG_DISABLE_LOGS CFLAGS += -DLOG_DISABLE_LOGS
CXXFLAGS += -DLOG_DISABLE_LOGS CXXFLAGS += -DLOG_DISABLE_LOGS
endif # LLAMA_DISABLE_LOGS endif # LLAMA_DISABLE_LOGS

View file

@ -7,6 +7,7 @@
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <cinttypes>
// -------------------------------- // --------------------------------
// //
@ -89,11 +90,11 @@
// } // }
// //
#ifndef LOG_TARGET #ifndef LOG_TARGET
/**/ #define LOG_TARGET log_handler() #define LOG_TARGET log_handler()
#endif #endif
#ifndef LOG_TEE_TARGET #ifndef LOG_TEE_TARGET
/**/ #define LOG_TEE_TARGET stderr #define LOG_TEE_TARGET stderr
#endif #endif
// Utility to obtain "pid" like unique process id and use it when creating log files. // Utility to obtain "pid" like unique process id and use it when creating log files.
@ -101,9 +102,6 @@ inline std::string log_get_pid()
{ {
static std::string pid; static std::string pid;
if (pid.empty()) if (pid.empty())
#ifndef _WIN32
[[unlikely]]
#endif
{ {
// std::this_thread::get_id() is the most portable way of obtaining a "process id" // std::this_thread::get_id() is the most portable way of obtaining a "process id"
// it's not the same as "pid" but is unique enough to solve multiple instances // it's not the same as "pid" but is unique enough to solve multiple instances
@ -123,7 +121,7 @@ inline std::string log_get_pid()
#define LOG_FILENAME_GENERATOR(log_file_basename, log_file_extension) _log_filename_generator(log_file_basename, log_file_extension) #define LOG_FILENAME_GENERATOR(log_file_basename, log_file_extension) _log_filename_generator(log_file_basename, log_file_extension)
// INTERNAL, DO NOT USE // INTERNAL, DO NOT USE
inline std::string _log_filename_generator(std::string log_file_basename, std::string log_file_extension) inline std::string _log_filename_generator(const std::string & log_file_basename, const std::string & log_file_extension)
{ {
return std::string().append(log_file_basename).append(".").append(log_get_pid()).append(".").append(log_file_extension); return std::string().append(log_file_basename).append(".").append(log_get_pid()).append(".").append(log_file_extension);
} }
@ -149,10 +147,10 @@ inline std::string _log_filename_generator(std::string log_file_basename, std::s
// //
#ifndef LOG_NO_TIMESTAMPS #ifndef LOG_NO_TIMESTAMPS
/**/#ifndef _WIN32 /**/#ifndef _WIN32
/* */#define LOG_TIMESTAMP_FMT "[%lu]" /* */#define LOG_TIMESTAMP_FMT "[%" PRIu64 "]"
/* */#define LOG_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count() /* */#define LOG_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count()
/**/#else /**/#else
/* */#define LOG_TIMESTAMP_FMT "[%llu]" /* */#define LOG_TIMESTAMP_FMT "[%" PRIu64 "]"
/* */#define LOG_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count() /* */#define LOG_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count()
/**/#endif /**/#endif
#else #else
@ -162,10 +160,10 @@ inline std::string _log_filename_generator(std::string log_file_basename, std::s
#ifdef LOG_TEE_TIMESTAMPS #ifdef LOG_TEE_TIMESTAMPS
/**/#ifndef _WIN32 /**/#ifndef _WIN32
/* */#define LOG_TEE_TIMESTAMP_FMT "[%lu]" /* */#define LOG_TEE_TIMESTAMP_FMT "[%" PRIu64 "]"
/* */#define LOG_TEE_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count() /* */#define LOG_TEE_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count()
/**/#else /**/#else
/* */#define LOG_TEE_TIMESTAMP_FMT "[%llu]" /* */#define LOG_TEE_TIMESTAMP_FMT "[%" PRIu64 "]"
/* */#define LOG_TEE_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count() /* */#define LOG_TEE_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count()
/**/#endif /**/#endif
#else #else
@ -219,22 +217,22 @@ enum LogTriState
// USE LOG() INSTEAD // USE LOG() INSTEAD
// //
#ifndef _WIN32 #ifndef _WIN32
/**/#define _LOG(str, ...) \ /**/#define _LOG(str, ...) \
{ \ { \
if (LOG_TARGET != nullptr) \ if (LOG_TARGET != nullptr) \
{ \ { \
fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL, ##__VA_ARGS__); \ fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL, __VA_ARGS__); \
fflush(LOG_TARGET); \ fflush(LOG_TARGET); \
} \ } \
} }
#else #else
/**/#define _LOG(str, ...) \ /**/#define _LOG(str, ...) \
{ \ { \
if (LOG_TARGET != nullptr) \ if (LOG_TARGET != nullptr) \
{ \ { \
fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL "", ##__VA_ARGS__); \ fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL "", __VA_ARGS__); \
fflush(LOG_TARGET); \ fflush(LOG_TARGET); \
} \ } \
} }
#endif #endif
@ -242,32 +240,32 @@ enum LogTriState
// USE LOG_TEE() INSTEAD // USE LOG_TEE() INSTEAD
// //
#ifndef _WIN32 #ifndef _WIN32
/**/#define _LOG_TEE(str, ...) \ /**/#define _LOG_TEE(str, ...) \
{ \ { \
if (LOG_TARGET != nullptr) \ if (LOG_TARGET != nullptr) \
{ \ { \
fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL, ##__VA_ARGS__); \ fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL, __VA_ARGS__); \
fflush(LOG_TARGET); \ fflush(LOG_TARGET); \
} \ } \
if (LOG_TARGET != nullptr && LOG_TARGET != stdout && LOG_TARGET != stderr && LOG_TEE_TARGET != nullptr) \ if (LOG_TARGET != nullptr && LOG_TARGET != stdout && LOG_TARGET != stderr && LOG_TEE_TARGET != nullptr) \
{ \ { \
fprintf(LOG_TEE_TARGET, LOG_TEE_TIMESTAMP_FMT LOG_TEE_FLF_FMT str "%s" LOG_TEE_TIMESTAMP_VAL LOG_TEE_FLF_VAL, ##__VA_ARGS__); \ fprintf(LOG_TEE_TARGET, LOG_TEE_TIMESTAMP_FMT LOG_TEE_FLF_FMT str "%s" LOG_TEE_TIMESTAMP_VAL LOG_TEE_FLF_VAL, __VA_ARGS__); \
fflush(LOG_TEE_TARGET); \ fflush(LOG_TEE_TARGET); \
} \ } \
} }
#else #else
/**/#define _LOG_TEE(str, ...) \ /**/#define _LOG_TEE(str, ...) \
{ \ { \
if (LOG_TARGET != nullptr) \ if (LOG_TARGET != nullptr) \
{ \ { \
fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL "", ##__VA_ARGS__); \ fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL "", __VA_ARGS__); \
fflush(LOG_TARGET); \ fflush(LOG_TARGET); \
} \ } \
if (LOG_TARGET != nullptr && LOG_TARGET != stdout && LOG_TARGET != stderr && LOG_TEE_TARGET != nullptr) \ if (LOG_TARGET != nullptr && LOG_TARGET != stdout && LOG_TARGET != stderr && LOG_TEE_TARGET != nullptr) \
{ \ { \
fprintf(LOG_TEE_TARGET, LOG_TEE_TIMESTAMP_FMT LOG_TEE_FLF_FMT str "%s" LOG_TEE_TIMESTAMP_VAL LOG_TEE_FLF_VAL "", ##__VA_ARGS__); \ fprintf(LOG_TEE_TARGET, LOG_TEE_TIMESTAMP_FMT LOG_TEE_FLF_FMT str "%s" LOG_TEE_TIMESTAMP_VAL LOG_TEE_FLF_VAL "", __VA_ARGS__); \
fflush(LOG_TEE_TARGET); \ fflush(LOG_TEE_TARGET); \
} \ } \
} }
#endif #endif
@ -308,7 +306,7 @@ enum LogTriState
#endif #endif
// INTERNAL, DO NOT USE // INTERNAL, DO NOT USE
inline FILE *_log_handler1(bool change = false, LogTriState disable = LogTriStateSame, std::string filename = LOG_DEFAULT_FILE_NAME, FILE *target = nullptr) inline FILE *_log_handler1(bool change = false, LogTriState disable = LogTriStateSame, const std::string & filename = LOG_DEFAULT_FILE_NAME, FILE *target = nullptr)
{ {
static bool _initialized{false}; static bool _initialized{false};
static bool _disabled{(filename.empty() && target == nullptr)}; static bool _disabled{(filename.empty() && target == nullptr)};
@ -317,9 +315,6 @@ inline FILE *_log_handler1(bool change = false, LogTriState disable = LogTriStat
static FILE *logfile = nullptr; static FILE *logfile = nullptr;
if (change) if (change)
#ifndef _WIN32
[[unlikely]]
#endif
{ {
if (disable == LogTriStateTrue) if (disable == LogTriStateTrue)
{ {
@ -334,7 +329,7 @@ inline FILE *_log_handler1(bool change = false, LogTriState disable = LogTriStat
// Otherwise, process the arguments // Otherwise, process the arguments
else else
{ {
if (log_current_filename.compare(filename) != 0) if (log_current_filename != filename)
{ {
_initialized = false; _initialized = false;
} }
@ -347,16 +342,13 @@ inline FILE *_log_handler1(bool change = false, LogTriState disable = LogTriStat
} }
if (_initialized) if (_initialized)
#ifndef _WIN32
[[likely]]
#endif
{ {
if (_disabled) if (_disabled)
{ {
// Log is disabled // Log is disabled
return nullptr; return nullptr;
} }
else else // NOLINT
{ {
// with fallback in case something went wrong // with fallback in case something went wrong
return logfile ? logfile : stderr; return logfile ? logfile : stderr;
@ -378,10 +370,7 @@ inline FILE *_log_handler1(bool change = false, LogTriState disable = LogTriStat
} }
else else
{ {
if (log_current_filename.compare(filename) != 0) if (log_current_filename != filename)
#ifndef _WIN32
[[likely]]
#endif
{ {
if (logfile != nullptr && logfile != stdout && logfile != stderr) if (logfile != nullptr && logfile != stdout && logfile != stderr)
{ {
@ -415,7 +404,7 @@ inline FILE *_log_handler1(bool change = false, LogTriState disable = LogTriStat
} }
// INTERNAL, DO NOT USE // INTERNAL, DO NOT USE
inline FILE *_log_handler2(bool change = false, LogTriState disable = LogTriStateSame, FILE *target = nullptr, std::string filename = LOG_DEFAULT_FILE_NAME) inline FILE *_log_handler2(bool change = false, LogTriState disable = LogTriStateSame, FILE *target = nullptr, const std::string & filename = LOG_DEFAULT_FILE_NAME)
{ {
return _log_handler1(change, disable, filename, target); return _log_handler1(change, disable, filename, target);
} }
@ -444,7 +433,7 @@ inline FILE *_log_enable()
#define LOG_SET_TARGET(target) _log_set_target(target) #define LOG_SET_TARGET(target) _log_set_target(target)
// INTERNAL, DO NOT USE // INTERNAL, DO NOT USE
inline FILE *_log_set_target(std::string filename) { return _log_handler1(true, LogTriStateSame, filename); } inline FILE *_log_set_target(const std::string & filename) { return _log_handler1(true, LogTriStateSame, filename); }
inline FILE *_log_set_target(FILE *target) { return _log_handler2(true, LogTriStateSame, target); } inline FILE *_log_set_target(FILE *target) { return _log_handler2(true, LogTriStateSame, target); }
// INTERNAL, DO NOT USE // INTERNAL, DO NOT USE
@ -491,19 +480,19 @@ inline void log_test()
#endif #endif
} }
inline bool log_param_single_parse(std::string param) inline bool log_param_single_parse(const std::string & param)
{ {
if (std::string("--log-test").compare(param) == 0) if (std::string("--log-test") == param)
{ {
log_test(); log_test();
return true; return true;
} }
else if (std::string("--log-disable").compare(param) == 0) else if (std::string("--log-disable") == param) // NOLINT
{ {
LOG_DISABLE(); LOG_DISABLE();
return true; return true;
} }
else if (std::string("--log-enable").compare(param) == 0) else if (std::string("--log-enable") == param)
{ {
LOG_ENABLE(); LOG_ENABLE();
return true; return true;
@ -512,9 +501,9 @@ inline bool log_param_single_parse(std::string param)
return false; return false;
} }
inline bool log_param_pair_parse(bool check_but_dont_parse, std::string param, std::string next = std::string()) inline bool log_param_pair_parse(bool check_but_dont_parse, const std::string & param, const std::string & next = std::string())
{ {
if (std::string("--log-file").compare(param) == 0) if (std::string("--log-file") == param)
{ {
if (check_but_dont_parse) if (check_but_dont_parse)
{ {
@ -573,7 +562,7 @@ inline std::string _log_var_to_string(std::string var)
return var; return var;
} }
inline std::string _log_var_to_string(std::vector<int> var) inline std::string _log_var_to_string(const std::vector<int> & var)
{ {
std::string buf; std::string buf;
buf.append("[ "); buf.append("[ ");