Updated Log to use C++ headers cstdio and ctime instead of stdio.h and time.h.

This commit is contained in:
Jason Watkins
2015-04-30 15:11:14 -07:00
parent 37eb6b6b03
commit aaf25a9933

View File

@@ -5,64 +5,64 @@
#include "XPLMUtilities.h" #include "XPLMUtilities.h"
#include <cstdarg> #include <cstdarg>
#include <stdio.h> #include <cstdio>
#include <time.h> #include <ctime>
// Implementation note: I initial wrote this class using C++ iostreams, but I couldn't find any // Implementation note: I initial wrote this class using C++ iostreams, but I couldn't find any
// way to implement FormatLine without adding in a call to sprintf. It therefore seems more // way to implement FormatLine without adding in a call to sprintf. It therefore seems more
// efficient to me to just use C-style IO and call fprintf directly. // efficient to me to just use C-style IO and call std::fprintf directly.
namespace XPC namespace XPC
{ {
static void WriteTime(FILE* fd) static void WriteTime(FILE* fd)
{ {
time_t rawtime; std::time_t rawtime;
tm* timeinfo; std::tm* timeinfo;
time(&rawtime); std::time(&rawtime);
timeinfo = localtime(&rawtime); timeinfo = std::localtime(&rawtime);
char buffer[16] = { 0 }; char buffer[16] = { 0 };
// Format is equivalent to [%F %T], but neither of those specifiers is // Format is equivalent to [%F %T], but neither of those specifiers is
// supported on Windows as of Visual Studio 13 // supported on Windows as of Visual Studio 13
strftime(buffer, 16, "[%H:%M:%S] ", timeinfo); std::strftime(buffer, 16, "[%H:%M:%S] ", timeinfo);
fprintf(fd, buffer); std::fprintf(fd, buffer);
} }
void Log::Initialize(std::string version) void Log::Initialize(std::string version)
{ {
FILE* fd = fopen("XPCLog.txt", "w"); std::FILE* fd = std::fopen("XPCLog.txt", "w");
if (fd != NULL) if (fd != NULL)
{ {
time_t rawtime; std::time_t rawtime;
tm* timeinfo; std::tm* timeinfo;
time(&rawtime); std::time(&rawtime);
timeinfo = localtime(&rawtime); timeinfo = std::localtime(&rawtime);
char timeStr[16] = { 0 }; char timeStr[16] = { 0 };
// Format is equivalent to %F, but neither of those specifiers is // Format is equivalent to %F, but neither of those specifiers is
// supported on Windows as of Visual Studio 13 // supported on Windows as of Visual Studio 13
strftime(timeStr, 16, "%Y-%m-%d", timeinfo); std::strftime(timeStr, 16, "%Y-%m-%d", timeinfo);
fprintf(fd, "X-Plane Connect [Version %s]\n", version.c_str()); std::fprintf(fd, "X-Plane Connect [Version %s]\n", version.c_str());
fprintf(fd, "Compiled %s %s\n", __DATE__, __TIME__); std::fprintf(fd, "Compiled %s %s\n", __DATE__, __TIME__);
fprintf(fd, "Copyright (c) 2013-2015 United States Government as represented by the\n"); std::fprintf(fd, "Copyright (c) 2013-2015 United States Government as represented by the\n");
fprintf(fd, "Administrator of the National Aeronautics and Space Administration.\n"); std::fprintf(fd, "Administrator of the National Aeronautics and Space Administration.\n");
fprintf(fd, "All Rights Reserved.\n\n"); std::fprintf(fd, "All Rights Reserved.\n\n");
fprintf(fd, "This file contains debugging information about the X-Plane Connect plugin.\n"); std::fprintf(fd, "This file contains debugging information about the X-Plane Connect plugin.\n");
fprintf(fd, "If you have technical issues with the plugin, please report them by opening\n"); std::fprintf(fd, "If you have technical issues with the plugin, please report them by opening\n");
fprintf(fd, "an issue on GitHub (https://github.com/nasa/XPlaneConnect/issues) or by\n"); std::fprintf(fd, "an issue on GitHub (https://github.com/nasa/XPlaneConnect/issues) or by\n");
fprintf(fd, "emailing Christopher Teubert (christopher.a.teubert@nasa.gov).\n\n"); std::fprintf(fd, "emailing Christopher Teubert (christopher.a.teubert@nasa.gov).\n\n");
int xpVer; int xpVer;
int xplmVer; int xplmVer;
XPLMHostApplicationID hostID; XPLMHostApplicationID hostID;
XPLMGetVersions(&xpVer, &xplmVer, &hostID); XPLMGetVersions(&xpVer, &xplmVer, &hostID);
fprintf(fd, "X-Plane Version: %d\n", xpVer); std::fprintf(fd, "X-Plane Version: %d\n", xpVer);
fprintf(fd, "Plugin Manager Version: %d\n", xplmVer); std::fprintf(fd, "Plugin Manager Version: %d\n", xplmVer);
fprintf(fd, "Host Application ID: %d\n", hostID); std::fprintf(fd, "Host Application ID: %d\n", hostID);
fprintf(fd, "Log file generated on %s.\n", timeStr); std::fprintf(fd, "Log file generated on %s.\n", timeStr);
fclose(fd); std::fclose(fd);
} }
} }
@@ -73,23 +73,23 @@ namespace XPC
void Log::WriteLine(const char* value) void Log::WriteLine(const char* value)
{ {
FILE* fd = fopen("XPCLog.txt", "a"); std::FILE* fd = std::fopen("XPCLog.txt", "a");
if (!fd) if (!fd)
{ {
return; return;
} }
WriteTime(fd); WriteTime(fd);
fprintf(fd, "%s\n", value); std::fprintf(fd, "%s\n", value);
fclose(fd); std::fclose(fd);
} }
void Log::FormatLine(const char* format, ...) void Log::FormatLine(const char* format, ...)
{ {
va_list args; va_list args;
FILE* fd = fopen("XPCLog.txt", "a"); std::FILE* fd = std::fopen("XPCLog.txt", "a");
if (!fd) if (!fd)
{ {
return; return;
@@ -97,10 +97,10 @@ namespace XPC
va_start(args, format); va_start(args, format);
WriteTime(fd); WriteTime(fd);
vfprintf(fd, format, args); std::vfprintf(fd, format, args);
fprintf(fd, "\n"); std::fprintf(fd, "\n");
fclose(fd); std::fclose(fd);
va_end(args); va_end(args);
} }