00001 #ifndef RESULT_LOG_CPP
00002 #define RESULT_LOG_CPP
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "ResultLog.h"
00038 #include "HTMLHeader.h"
00039 #include "Utilities.h"
00040 #include <iostream>
00041 #include <cstdarg>
00042
00043 ResultLog::ResultLog(const std::string& fileName, const bool htmlFlag)
00044 : output(NULL), html(htmlFlag), insideTable(false) {
00045 if (fileName.size() != 0) {
00046 #ifndef _WINDOWS
00047 output = fopen(fileName.c_str(), "wt");
00048 #else
00049 fopen_s(&output, fileName.c_str(), "wt");
00050 #endif
00051 if ((output == NULL) || (ferror(output))) {
00052
00053 output = NULL;
00054 std::cerr << "Error occured when attempting to log information "
00055 << "to " << fileName << "\n"
00056 << "Results will be written to standard output.\n";
00057 }
00058 }
00059 if (output == NULL) {
00060
00061 const int StandardOutput = 1;
00062 #ifndef _WINDOWS
00063 output = fdopen(StandardOutput, "wt");
00064 #else
00065 output = _fdopen(StandardOutput, "wt");
00066 #endif
00067 }
00068
00069
00070 dumpHTMLHeaders();
00071 }
00072
00073 void
00074 ResultLog::reportLine(const char *line, ...) {
00075
00076
00077 char buffer[2048] = "\0";
00078 va_list arguments;
00079 va_start(arguments, line);
00080 vsnprintf_s(buffer, sizeof(buffer), sizeof(buffer), line, arguments);
00081 va_end(arguments);
00082
00083 if (html) {
00084
00085 endTable();
00086
00087 fprintf(output, "%s<br>\n", buffer);
00088 } else {
00089 fprintf(output, "%s\n", buffer);
00090 }
00091 }
00092
00093 void
00094 ResultLog::report(const char *col1, const char *col2, const char *col3, ...) {
00095
00096
00097 std::string tableRow;
00098 tableRow = (html ? "<tr><td>" : "");
00099 tableRow += col1;
00100 tableRow += (html ? "</td><td>" : " ");
00101 tableRow += col2;
00102 tableRow += (html ? "</td><td>" : " ");
00103 tableRow += col3;
00104 tableRow += (html ? "</td></tr>\n" : "\n");
00105
00106
00107
00108 char buffer[2048];
00109 va_list arguments;
00110 va_start(arguments, col3);
00111 vsnprintf_s(buffer, sizeof(buffer), sizeof(buffer),
00112 tableRow.c_str(), arguments);
00113 va_end(arguments);
00114
00115
00116 if (html) {
00117
00118 startTable();
00119 }
00120 fprintf(output, buffer);
00121 }
00122
00123 ResultLog::~ResultLog() {
00124
00125 endTable();
00126
00127 if (html) {
00128 fprintf(output, HTML_FOOTER);
00129 }
00130
00131 if (_fileno(output) != 1) {
00132 fclose(output);
00133 }
00134 }
00135
00136 void
00137 ResultLog::dumpHTMLHeaders() {
00138 if (!html) {
00139 return;
00140 }
00141
00142 fprintf(output, HTML_HEADER);
00143 }
00144
00145 void
00146 ResultLog::startTable(const char* titles[]) {
00147 if (!html || insideTable) {
00148
00149 return;
00150 }
00151
00152 fprintf(output, " <table border=\"0\">\n");
00153
00154 if (titles != NULL) {
00155
00156 fprintf(output, " <thead>\n <tr>");
00157 int index = 0;
00158 while (titles[index] != NULL) {
00159 fprintf(output, "<td>%s</td>", titles[index]);
00160 index++;
00161 }
00162
00163 fprintf(output, " </thead>\n");
00164 }
00165
00166 fprintf(output, " <tbody>\n");
00167 insideTable = true;
00168 }
00169
00170 void
00171 ResultLog::endTable() {
00172 if (!html || !insideTable) {
00173
00174 return;
00175 }
00176
00177 fprintf(output, " </tbody>\n");
00178 fprintf(output, " </table>\n");
00179
00180 insideTable = false;
00181 }
00182
00183 ResultLog&
00184 ResultLog::operator=(const ResultLog&) {
00185 return *this;
00186 }
00187
00188 #endif