Skip to content

Commit 3ce620a

Browse files
committed
Add timestamps to logger
1 parent 2f66340 commit 3ce620a

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

src/Logger.cc

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,60 @@
1515

1616
#include "Logger.h"
1717
#include <execinfo.h>
18+
#include "PerfUtils/Cycles.h"
19+
20+
using PerfUtils::Cycles;
1821

1922
namespace Arachne {
2023

2124
extern FILE* errorStream;
22-
LogLevel Logger::displayMinLevel = WARNING;
25+
LogLevel Logger::displayMinLevel = DEBUG;
2326
std::mutex Logger::mutex;
27+
PerfUtils::Initialize Logger::_(Logger::init);
28+
uint64_t Logger::startingTsc;
29+
30+
/**
31+
* Friendly names for each #LogLevel value.
32+
* Keep this in sync with the LogLevel enum.
33+
*/
34+
static const char* logLevelNames[] = {"VERBOSE", "DEBUG", "NOTICE", "WARNING",
35+
"ERROR", "SILENT"};
36+
2437

38+
void
39+
Logger::init()
40+
{
41+
startingTsc = Cycles::rdtsc();
42+
}
2543
void
2644
Logger::log(LogLevel level, const char* fmt, ...) {
2745
if (level < displayMinLevel) {
2846
return;
2947
}
3048

31-
Lock lock(mutex);
49+
// First format the message; timestamps are in seconds since the first log
50+
#define MAX_MESSAGE_CHARS 2000
51+
// Construct a message on the stack and then print it out with the lock
52+
char buffer[MAX_MESSAGE_CHARS];
53+
int spaceLeft = MAX_MESSAGE_CHARS;
54+
int charsWritten = 0;
55+
int actual;
56+
double time = Cycles::toSeconds(Cycles::rdtsc() - startingTsc);
57+
58+
// Add a header including rdtsc time and location in the file.
59+
actual = snprintf(buffer + charsWritten, spaceLeft,
60+
"%.10lf: %s: ", time, logLevelNames[level]);
61+
charsWritten += actual;
62+
spaceLeft -= actual;
63+
64+
// Add the intended message
3265
va_list args;
3366
va_start(args, fmt);
34-
vfprintf(errorStream, fmt, args);
67+
actual = vsnprintf(buffer + charsWritten, spaceLeft, fmt, args);
68+
va_end(args);
69+
70+
Lock lock(mutex);
71+
fprintf(errorStream, "%s\n", buffer);
3572
fflush(errorStream);
3673
va_end(args);
3774
}

src/Logger.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <stdarg.h>
2020
#include <stdint.h>
2121
#include <mutex>
22+
#include <PerfUtils/Initialize.h>
2223

2324
#define ARACHNE_LOG Logger::log
2425
#define ARACHNE_BACKTRACE Logger::logBacktrace
@@ -52,12 +53,17 @@ class Logger {
5253
static void logBacktrace(LogLevel level);
5354

5455
private:
56+
static void init();
5557
// The minimum severity level to print.
5658
static LogLevel displayMinLevel;
5759

5860
// Lock around printing since Arachne is multithreaded.
5961
typedef std::unique_lock<std::mutex> Lock;
6062
static std::mutex mutex;
63+
64+
// Initialize
65+
static PerfUtils::Initialize _;
66+
static uint64_t startingTsc;
6167
};
6268

6369
} // namespace Arachne

0 commit comments

Comments
 (0)