|
15 | 15 |
|
16 | 16 | #include "Logger.h" |
17 | 17 | #include <execinfo.h> |
| 18 | +#include "PerfUtils/Cycles.h" |
| 19 | + |
| 20 | +using PerfUtils::Cycles; |
18 | 21 |
|
19 | 22 | namespace Arachne { |
20 | 23 |
|
21 | 24 | extern FILE* errorStream; |
22 | | -LogLevel Logger::displayMinLevel = WARNING; |
| 25 | +LogLevel Logger::displayMinLevel = DEBUG; |
23 | 26 | 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 | + |
24 | 37 |
|
| 38 | +void |
| 39 | +Logger::init() |
| 40 | +{ |
| 41 | +startingTsc = Cycles::rdtsc(); |
| 42 | +} |
25 | 43 | void |
26 | 44 | Logger::log(LogLevel level, const char* fmt, ...) { |
27 | 45 | if (level < displayMinLevel) { |
28 | 46 | return; |
29 | 47 | } |
30 | 48 |
|
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 |
32 | 65 | va_list args; |
33 | 66 | 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); |
35 | 72 | fflush(errorStream); |
36 | 73 | va_end(args); |
37 | 74 | } |
|
0 commit comments