Skip to content

Commit

Permalink
Remove duplicate of StringUtil::format from Debug::log
Browse files Browse the repository at this point in the history
  • Loading branch information
ongardie committed Jun 8, 2016
1 parent 0c58ec1 commit 2f12dc3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 40 deletions.
36 changes: 6 additions & 30 deletions Core/Debug.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ logLevelToString(LogLevel level)
case LogLevel::VERBOSE: return "VERBOSE";
}
log(LogLevel::ERROR, __FILE__, __LINE__, __FUNCTION__,
"%d is not a valid log level.\n",
static_cast<int>(level));
Core::StringUtil::format("%d is not a valid log level",
static_cast<int>(level)).c_str());
abort();
}

Expand All @@ -185,7 +185,8 @@ logLevelFromString(const std::string& level)
if (strcasecmp(level.c_str(), "NOTICE") == 0) return LogLevel::NOTICE;
if (strcasecmp(level.c_str(), "VERBOSE") == 0) return LogLevel::VERBOSE;
log(LogLevel::ERROR, __FILE__, __LINE__, __FUNCTION__,
"'%s' is not a valid log level.\n", level.c_str());
Core::StringUtil::format("'%s' is not a valid log level",
level.c_str()).c_str());
abort();
}

Expand Down Expand Up @@ -416,33 +417,8 @@ isLogging(LogLevel level, const char* fileName)
void
log(LogLevel level,
const char* fileName, uint32_t lineNum, const char* functionName,
const char* format, ...)
const char* message)
{
va_list ap;

std::string message;
// this part is copied from Core::StringUtil::toString.
va_start(ap, format);
// We're not really sure how big of a buffer will be necessary.
// Try 1K, if not the return value will tell us how much is necessary.
size_t bufSize = 1024;
while (true) {
char buf[bufSize];
// vsnprintf trashes the va_list, so copy it first
va_list aq;
va_copy(aq, ap);
int r = vsnprintf(buf, bufSize, format, aq);
va_end(aq);
assert(r >= 0); // old glibc versions returned -1
size_t r2 = size_t(r);
if (r2 < bufSize) {
message = buf; // copy string
break;
}
bufSize = size_t(r2) + 1;
}
va_end(ap);

if (logHandler) {
DebugMessage d;
d.filename = relativeFileName(fileName);
Expand Down Expand Up @@ -487,7 +463,7 @@ log(LogLevel level,
relativeFileName(fileName), lineNum, functionName,
logLevelToString(level),
processName.c_str(), ThreadId::getName().c_str(),
message.c_str());
message);

fflush(stream);
}
Expand Down
24 changes: 14 additions & 10 deletions Core/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@

namespace LogCabin {
namespace Core {

namespace StringUtil {
std::string format(const char* format, ...)
__attribute__((format(printf, 1, 2)));
}

namespace Debug {

// Configuring logging is exposed to clients as well as servers,
Expand Down Expand Up @@ -62,17 +68,14 @@ isLogging(LogLevel level, const char* fileName);
* The output of __LINE__.
* \param functionName
* The output of __FUNCTION__.
* \param format
* A printf-style format string for the message. It should include a line
* break at the end.
* \param ...
* The arguments to the format string, as in printf.
* \param message
* A descriptive message to print, which should not include a line break
* at the end.
*/
void
log(LogLevel level,
const char* fileName, uint32_t lineNum, const char* functionName,
const char* format, ...)
__attribute__((format(printf, 5, 6)));
const char* message);

/**
* A short name to be used in log messages to identify this process.
Expand All @@ -89,17 +92,18 @@ extern std::string processName;
* This is normally called by ERROR(), WARNING(), NOTICE(), or VERBOSE().
* \param level
* The level of importance of the message.
* \param format
* \param _format
* A printf-style format string for the message. It should not include a
* line break at the end, as LOG will add one.
* \param ...
* The arguments to the format string, as in printf.
*/
#define LOG(level, format, ...) do { \
#define LOG(level, _format, ...) do { \
if (::LogCabin::Core::Debug::isLogging(level, __FILE__)) { \
::LogCabin::Core::Debug::log(level, \
__FILE__, __LINE__, __FUNCTION__, \
format, ##__VA_ARGS__); \
::LogCabin::Core::StringUtil::format( \
_format, ##__VA_ARGS__).c_str()); \
} \
} while (0)

Expand Down

0 comments on commit 2f12dc3

Please sign in to comment.