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 @@ ...@@ -6,6 +6,7 @@
static FILE * logFile; static FILE * logFile;
static int logFlags; static int logFlags;
static int logTimestamp; static int logTimestamp;
static int startSeconds;
void logInit(FILE * destFile, int flags, int useTimestamp) void logInit(FILE * destFile, int flags, int useTimestamp)
{ {
...@@ -20,6 +21,9 @@ void logInit(FILE * destFile, int flags, int useTimestamp) ...@@ -20,6 +21,9 @@ void logInit(FILE * destFile, int flags, int useTimestamp)
logFile = destFile; logFile = destFile;
logFlags = flags; logFlags = flags;
logTimestamp = useTimestamp; logTimestamp = useTimestamp;
struct timeval now;
gettimeofday(&now, NULL);
startSeconds = now.tv_sec;
} }
} }
...@@ -30,53 +34,58 @@ void logCleanup(void) ...@@ -30,53 +34,58 @@ void logCleanup(void)
// Print the timestamp and type of logging to the logFile. // Print the timestamp and type of logging to the logFile.
static void logPrefix(int which) static void logPrefix(int which)
{ {
char * messageClass = "LOG_ERROR ";
switch(which) switch(which)
{ {
case ERROR: case ERROR:
fprintf(logFile, "ERROR "); messageClass = "ERROR ";
break; break;
case EXCEPTION: case EXCEPTION:
fprintf(logFile, "EXCEPTION "); messageClass = "EXCEPTION ";
break; break;
case PEER_CYCLE: case PEER_CYCLE:
fprintf(logFile, "PEER_CYCLE "); messageClass = "PEER_CYCLE ";
break; break;
case SENSOR: case SENSOR:
fprintf(logFile, "SENSOR "); messageClass = "SENSOR ";
break; break;
case CONNECTION_MODEL: case CONNECTION_MODEL:
fprintf(logFile, "CONNECTION_MODEL "); messageClass = "CONNECTION_MODEL ";
break; break;
case ROBUST: case ROBUST:
fprintf(logFile, "ROBUST "); messageClass = "ROBUST ";
break; break;
case MAIN_LOOP: case MAIN_LOOP:
fprintf(logFile, "MAIN_LOOP "); messageClass = "MAIN_LOOP ";
break; break;
case COMMAND_INPUT: case COMMAND_INPUT:
fprintf(logFile, "COMMAND_INPUT "); messageClass = "COMMAND_INPUT ";
break; break;
case CONNECTION: case CONNECTION:
fprintf(logFile, "CONNECTION "); messageClass = "CONNECTION ";
break; break;
case PCAP: case PCAP:
fprintf(logFile, "PCAP "); messageClass = "PCAP ";
break; break;
case COMMAND_OUTPUT: case COMMAND_OUTPUT:
fprintf(logFile, "COMMAND_OUTPUT "); messageClass = "COMMAND_OUTPUT ";
break; break;
default: default:
fprintf(logFile, "LOG_ERROR "); messageClass = "LOG_ERROR ";
break; break;
} }
if (logTimestamp) if (logTimestamp)
{ {
struct timeval now; struct timeval now;
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
fprintf(logFile, "%f ", (double)(now.tv_sec) + now.tv_sec -= startSeconds;
((now.tv_usec)/1000)/1000.0); 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, ...) void logWrite(int flags, char const * format, ...)
...@@ -87,7 +96,6 @@ void logWrite(int flags, char const * format, ...) ...@@ -87,7 +96,6 @@ void logWrite(int flags, char const * format, ...)
{ {
logPrefix(flags & logFlags); logPrefix(flags & logFlags);
vfprintf(logFile, format, va); vfprintf(logFile, format, va);
fprintf(logFile, "\n");
} }
va_end(va); 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