Fixed logging bug with varargs and dialed back excessive trace logging in Message.

This commit is contained in:
Jason Watkins
2015-05-14 14:14:02 -07:00
parent 693c5f3d23
commit 748d37c4d9
4 changed files with 50 additions and 17 deletions

View File

@@ -29,15 +29,48 @@ namespace XPC
std::tm * tm = std::localtime(&now_tt); std::tm * tm = std::localtime(&now_tt);
std::stringstream ss; std::stringstream ss;
ss << std::setfill('0') << "[" ss << std::setfill('0')
<< std::setw(2) << tm->tm_hour << ":" << std::setw(2) << tm->tm_hour << ":"
<< std::setw(2) << tm->tm_min << ":" << std::setw(2) << tm->tm_min << ":"
<< std::setw(2) << tm->tm_sec << "." << std::setw(2) << tm->tm_sec << "."
<< std::setw(3) << ms.count() << "]"; << std::setw(3) << ms.count() << "|";
std::fprintf(fd, ss.str().c_str()); std::fprintf(fd, ss.str().c_str());
} }
static void WriteLevel(FILE* fd, int level)
{
char* str;
switch (level)
{
case LOG_OFF:
str = " OFF|";
break;
case LOG_FATAL:
str = "FATAL|";
break;
case LOG_ERROR:
str = "ERROR|";
break;
case LOG_WARN:
str = " WARN|";
break;
case LOG_INFO:
str = " INFO|";
break;
case LOG_DEBUG:
str = "DEBUG|";
break;
case LOG_TRACE:
str = "TRACE|";
break;
default:
str = " UNK|";
break;
}
std::fprintf(fd, str);
}
void Log::Initialize(const std::string& version) void Log::Initialize(const std::string& version)
{ {
if (LOG_LEVEL == LOG_OFF) if (LOG_LEVEL == LOG_OFF)
@@ -98,11 +131,12 @@ namespace XPC
} }
WriteTime(fd); WriteTime(fd);
std::fprintf(fd, "[%s] %s\n", tag.c_str(), value.c_str()); WriteLevel(fd, level);
std::fprintf(fd, "%s|%s\n", tag.c_str(), value.c_str());
std::fflush(fd); std::fflush(fd);
} }
void Log::FormatLine(int level, const std::string& tag, const std::string& format, ...) void Log::FormatLine(int level, const std::string& tag, std::string format, ...)
{ {
va_list args; va_list args;
@@ -113,7 +147,8 @@ namespace XPC
va_start(args, format); va_start(args, format);
WriteTime(fd); WriteTime(fd);
std::fprintf(fd, "[%s] ", tag.c_str()); WriteLevel(fd, level);
std::fprintf(fd, "%s| ", tag.c_str());
std::vfprintf(fd, format.c_str(), args); std::vfprintf(fd, format.c_str(), args);
std::fprintf(fd, "\n"); std::fprintf(fd, "\n");
std::fflush(fd); std::fflush(fd);

View File

@@ -22,9 +22,7 @@
#define LOG_DEBUG 5 #define LOG_DEBUG 5
#define LOG_TRACE 6 #define LOG_TRACE 6
#ifndef LOG_LEVEL #define LOG_LEVEL LOG_TRACE
#define LOG_LEVEL LOG_ERROR
#endif
namespace XPC namespace XPC
{ {
@@ -53,7 +51,10 @@ namespace XPC
/// specifiers. /// specifiers.
/// ///
/// \param format The format string appropriate for consumption by sprintf. /// \param format The format string appropriate for consumption by sprintf.
static void FormatLine(int level, const std::string& tag, const std::string& format, ...); ///
/// \remarks Note that Visual C++ silently fails va_start when the last non-varargs
/// argument is a reference, so we need a value-type format here.
static void FormatLine(int level, const std::string& tag, const std::string format, ...);
/// Writes the specified string value, followed by a line terminator /// Writes the specified string value, followed by a line terminator
/// to the XPC log file. /// to the XPC log file.

View File

@@ -13,44 +13,41 @@ namespace XPC
Message Message::ReadFrom(const UDPSocket& sock) Message Message::ReadFrom(const UDPSocket& sock)
{ {
Log::WriteLine(LOG_TRACE, "MESG", "Reading Message");
Message m; Message m;
int len = sock.Read(m.buffer, bufferSize, &m.source); int len = sock.Read(m.buffer, bufferSize, &m.source);
m.size = len < 0 ? 0 : len; m.size = len < 0 ? 0 : len;
Log::FormatLine(LOG_TRACE, "MESG", "Read result: %i", len); if (len > 0)
{
Log::FormatLine(LOG_TRACE, "MESG", "Read message with length %i", len);
}
return m; return m;
} }
unsigned long Message::GetMagicNumber() const unsigned long Message::GetMagicNumber() const
{ {
unsigned long val = size < 4 ? 0 : *((unsigned long*)buffer); unsigned long val = size < 4 ? 0 : *((unsigned long*)buffer);
Log::FormatLine(LOG_TRACE, "MESG", "Gettting magic number 0x%lX for message 0x%zX", val, this);
return val; return val;
} }
std::string Message::GetHead() const std::string Message::GetHead() const
{ {
std::string val = size < 4 ? "" : std::string((char*)buffer, 4); std::string val = size < 4 ? "" : std::string((char*)buffer, 4);
Log::FormatLine(LOG_TRACE, "MESG", "Getting head %s for message 0x%zX", val, this);
return val; return val;
} }
const unsigned char* Message::GetBuffer() const const unsigned char* Message::GetBuffer() const
{ {
const unsigned char* val = size == 0 ? NULL : buffer; const unsigned char* val = size == 0 ? NULL : buffer;
Log::FormatLine(LOG_TRACE, "MESG", "Returning buffer 0x%lX for message 0x%zX", val, this);
return val; return val;
} }
std::size_t Message::GetSize() const std::size_t Message::GetSize() const
{ {
Log::FormatLine(LOG_TRACE, "MESG", "Returning size %zu for message 0x%zX", size, this);
return size; return size;
} }
struct sockaddr Message::GetSource() const struct sockaddr Message::GetSource() const
{ {
Log::FormatLine(LOG_TRACE, "MESG", "Returning source for message 0x%zX", this);
return source; return source;
} }

View File

@@ -101,7 +101,7 @@
<PrecompiledHeader>NotUsing</PrecompiledHeader> <PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<PreprocessorDefinitions>IBM=1;XPLM200;_DEBUG;_WINDOWS;_USRDLL;XPCPLUGIN_EXPORTS;_CRT_SECURE_NO_WARNINGS;LOG_LEVEL=LOG_TRACE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>IBM=1;XPLM200;_DEBUG;_WINDOWS;_USRDLL;XPCPLUGIN_EXPORTS;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<DebugInformationFormat>OldStyle</DebugInformationFormat> <DebugInformationFormat>OldStyle</DebugInformationFormat>
<MultiProcessorCompilation>true</MultiProcessorCompilation> <MultiProcessorCompilation>true</MultiProcessorCompilation>
<OmitFramePointers /> <OmitFramePointers />