Commit 63abdeb8 authored by Jonathon Duerig's avatar Jonathon Duerig
Browse files

Streamlined logging so that only two fprintf statements are necessary. One for...

Streamlined logging so that only two fprintf statements are necessary. One for the prefix and one for the variable argument body. This is down from the 5 it was earlier. Also changed the format of timestamps to me more useful. Timestamps are now in a secs:millis format rather than a floating point number. And secs is seconds since the beginning of the run (rounded to the nearest second) rather than seconds since epoch.
parent a77aedb7
......@@ -6,6 +6,7 @@
static FILE * logFile;
static int logFlags;
static int logTimestamp;
static int startSeconds;
void logInit(FILE * destFile, int flags, int useTimestamp)
{
......@@ -20,6 +21,9 @@ void logInit(FILE * destFile, int flags, int useTimestamp)
logFile = destFile;
logFlags = flags;
logTimestamp = useTimestamp;
struct timeval now;
gettimeofday(&now, NULL);
startSeconds = now.tv_sec;
}
}
......@@ -30,53 +34,58 @@ void logCleanup(void)
// Print the timestamp and type of logging to the logFile.
static void logPrefix(int which)
{
char * messageClass = "LOG_ERROR ";
switch(which)
{
case ERROR:
fprintf(logFile, "ERROR ");
messageClass = "ERROR ";
break;
case EXCEPTION:
fprintf(logFile, "EXCEPTION ");
messageClass = "EXCEPTION ";
break;
case PEER_CYCLE:
fprintf(logFile, "PEER_CYCLE ");
messageClass = "PEER_CYCLE ";
break;
case SENSOR:
fprintf(logFile, "SENSOR ");
messageClass = "SENSOR ";
break;
case CONNECTION_MODEL:
fprintf(logFile, "CONNECTION_MODEL ");
messageClass = "CONNECTION_MODEL ";
break;
case ROBUST:
fprintf(logFile, "ROBUST ");
messageClass = "ROBUST ";
break;
case MAIN_LOOP:
fprintf(logFile, "MAIN_LOOP ");
messageClass = "MAIN_LOOP ";
break;
case COMMAND_INPUT:
fprintf(logFile, "COMMAND_INPUT ");
messageClass = "COMMAND_INPUT ";
break;
case CONNECTION:
fprintf(logFile, "CONNECTION ");
messageClass = "CONNECTION ";
break;
case PCAP:
fprintf(logFile, "PCAP ");
messageClass = "PCAP ";
break;
case COMMAND_OUTPUT:
fprintf(logFile, "COMMAND_OUTPUT ");
messageClass = "COMMAND_OUTPUT ";
break;
default:
fprintf(logFile, "LOG_ERROR ");
messageClass = "LOG_ERROR ";
break;
}
if (logTimestamp)
{
struct timeval now;
gettimeofday(&now, NULL);
fprintf(logFile, "%f ", (double)(now.tv_sec) +
((now.tv_usec)/1000)/1000.0);
now.tv_sec -= startSeconds;
fprintf(logFile, "\n%s %u:%u: ", messageClass,
now.tv_sec - startSeconds, (now.tv_usec)/1000);
}
else
{
fprintf(logFile, "\n%s: ", messageClass);
}
fprintf(logFile, ": ");
}
void logWrite(int flags, char const * format, ...)
......@@ -87,7 +96,6 @@ void logWrite(int flags, char const * format, ...)
{
logPrefix(flags & logFlags);
vfprintf(logFile, format, va);
fprintf(logFile, "\n");
}
va_end(va);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment