提交 482ae41b 编写于 作者: H Heikki Linnakangas

Silence compiler's suggestions about marking functions with printf-attribute.

I was getting these compiler warnings:

src/s3log.cpp: In function ‘void _LogMessage(const char*, __va_list_tag*)’:
src/s3log.cpp:17:42: warning: function might be possible candidate for ‘gnu_printf’ format attribute [-Wsuggest-attribute=format]
     vsnprintf(buf, sizeof(buf), fmt, args);
                                          ^
src/s3log.cpp: In function ‘void _send_to_remote(const char*, __va_list_tag*)’:
src/s3log.cpp:27:55: warning: function might be possible candidate for ‘gnu_printf’ format attribute [-Wsuggest-attribute=format]
     size_t len = vsnprintf(buf, sizeof(buf), fmt, args);
                                                       ^
src/s3log.cpp: In function ‘void LogMessage(LOGLEVEL, const char*, ...)’:
src/s3log.cpp:41:39: warning: function might be possible candidate for ‘gnu_printf’ format attribute [-Wsuggest-attribute=format]
             vfprintf(stderr, fmt, args);
                                       ^

Those are good suggestions. I couldn't figure out the correct way to mark
the _LogMessage() and _send_to_remote() local functions, so I decided to
inline them into the caller, LogMessage(), instead. They were almost
one-liners, and LogMessage() is still very small, too, so I don't think
there's any significant loss to readability.

A few format strings in debugging messages were treating pthread_self() as
a pointer, while others were treating it as a wrong kind of integer.
Harmonize by casting it to "uint64_t", and using PRIX64 as the format
string. This isn't totally portable: pthread_t can be an arithmetic type,
or a struct, and casting a struct to unsigned int won't work. In principle,
that was a problem before this patch already, but now you should get a
compiler error, if you try to compile on a platform where pthread_t is not
an arithmetic type. I think that's better than silent type confusion.
Reviewed-by: NPaul Guo <pguo@pivotal.io>
Reviewed-by: NDaniel Gustafsson <dgustafsson@pivotal.io>
上级 0a7ea4ea
......@@ -16,15 +16,15 @@ enum LOGTYPE {
STDERR_LOG // use STDERR
};
void LogMessage(LOGLEVEL level, const char* fmt, ...);
void LogMessage(LOGLEVEL level, const char* fmt, ...) __attribute__((format(printf, 2, 3)));
LOGTYPE getLogType(const char* v);
LOGLEVEL getLogLevel(const char* v);
#define PRINTFUNCTION(i, format, ...) LogMessage(i, format, __VA_ARGS__)
#define LOG_FMT "[%s]#%d#(%0X)%s:%d "
#define LOG_ARGS(LOGLEVELSTR) LOGLEVELSTR, s3ext_segid, pthread_self(), __FILE__, __LINE__
#define LOG_FMT "[%s]#%d#(%" PRIX64 ")%s:%d "
#define LOG_ARGS(LOGLEVELSTR) LOGLEVELSTR, s3ext_segid, (uint64_t) pthread_self(), __FILE__, __LINE__
#define NEWLINE "\n"
#define S3DEBUG(message, args...) \
......
......@@ -86,7 +86,8 @@ void DecompressReader::decompress() {
S3DEBUG(
"No more data to decompress: avail_in = %u, avail_out = %u, total_in = %u, "
"total_out = %u",
zstream.avail_in, zstream.avail_out, zstream.total_in, zstream.total_out);
zstream.avail_in, zstream.avail_out,
(unsigned int) zstream.total_in, (unsigned int) zstream.total_out);
return;
}
......
......@@ -89,8 +89,8 @@ void* S3KeyWriter::UploadThreadFunc(void* data) {
S3KeyWriter* writer = params->keyWriter;
try {
S3DEBUG("Upload thread start: %p, part number: %" PRIu64 ", data size: %" PRIu64,
pthread_self(), params->currentNumber, params->data.size());
S3DEBUG("Upload thread start: %" PRIX64 ", part number: %" PRIu64 ", data size: %" PRIu64,
(uint64_t) pthread_self(), params->currentNumber, params->data.size());
string etag = writer->s3Interface->uploadPartOfData(
params->data, writer->params.getS3Url(), params->currentNumber, writer->uploadId);
......@@ -103,7 +103,7 @@ void* S3KeyWriter::UploadThreadFunc(void* data) {
}
writer->activeThreads--;
pthread_cond_broadcast(&writer->cv);
S3DEBUG("Upload part finish: %p, eTag: %s, part number: %" PRIu64, pthread_self(),
S3DEBUG("Upload part finish: %" PRIX64 ", eTag: %s, part number: %" PRIu64, (uint64_t) pthread_self(),
etag.c_str(), params->currentNumber);
} catch (S3Exception& e) {
S3ERROR("Upload thread error: %s", e.getMessage().c_str());
......
......@@ -12,37 +12,33 @@ void write_log(const char* fmt, ...) __attribute__((format(printf, 1, 2)));
}
#endif
static void _LogMessage(const char* fmt, va_list args) {
char buf[MAX_MESSAGE_LINE_LENGTH];
vsnprintf(buf, sizeof(buf), fmt, args);
#ifdef S3_STANDALONE
fprintf(stderr, "%s", buf);
#else
write_log("%s", buf);
#endif
}
static void _send_to_remote(const char* fmt, va_list args) {
char buf[MAX_MESSAGE_LINE_LENGTH];
size_t len = vsnprintf(buf, sizeof(buf), fmt, args);
sendto(s3ext_logsock_udp, buf, len, 0, (struct sockaddr*)&s3ext_logserveraddr,
sizeof(struct sockaddr_in));
}
void LogMessage(LOGLEVEL loglevel, const char* fmt, ...) {
if (loglevel > s3ext_loglevel) return;
char buf[MAX_MESSAGE_LINE_LENGTH];
size_t len;
va_list args;
va_start(args, fmt);
switch (s3ext_logtype) {
switch (s3ext_logtype)
{
case INTERNAL_LOG:
_LogMessage(fmt, args);
vsnprintf(buf, sizeof(buf), fmt, args);
#ifdef S3_STANDALONE
fprintf(stderr, "%s", buf);
#else
write_log("%s", buf);
#endif
break;
case STDERR_LOG:
vfprintf(stderr, fmt, args);
break;
case REMOTE_LOG: // `socat UDP-RECV:[port] STDOUT` to listen
_send_to_remote(fmt, args);
len = vsnprintf(buf, sizeof(buf), fmt, args);
sendto(s3ext_logsock_udp, buf, len, 0, (struct sockaddr*)&s3ext_logserveraddr,
sizeof(struct sockaddr_in));
break;
default:
break;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册