Browse Source

Add unused logger::isTruncated().

tags/v2.0.0
Andrew Belt 5 years ago
parent
commit
8edf67b058
2 changed files with 35 additions and 6 deletions
  1. +4
    -0
      include/logger.hpp
  2. +31
    -6
      src/logger.cpp

+ 4
- 0
include/logger.hpp View File

@@ -36,6 +36,10 @@ void destroy();
Thread-safe, meaning messages cannot overlap each other in the log. Thread-safe, meaning messages cannot overlap each other in the log.
*/ */
void log(Level level, const char* filename, int line, const char* format, ...); void log(Level level, const char* filename, int line, const char* format, ...);
/** Returns whether the current log file failed to end properly, due to a possible crash.
Must be called *before* init().
*/
bool isTruncated();




} // namespace logger } // namespace logger


+ 31
- 6
src/logger.cpp View File

@@ -18,17 +18,19 @@ void init() {
startTime = system::getNanoseconds(); startTime = system::getNanoseconds();
if (settings::devMode) { if (settings::devMode) {
outputFile = stderr; outputFile = stderr;
return;
} }

outputFile = fopen(asset::logPath.c_str(), "w");
if (!outputFile) {
fprintf(stderr, "Could not open log at %s\n", asset::logPath.c_str());
else {
outputFile = fopen(asset::logPath.c_str(), "w");
if (!outputFile) {
fprintf(stderr, "Could not open log at %s\n", asset::logPath.c_str());
}
} }
} }


void destroy() { void destroy() {
if (outputFile != stderr) {
if (outputFile && outputFile != stderr) {
// Print end token so we know if the logger exited cleanly.
fprintf(outputFile, "END");
fclose(outputFile); fclose(outputFile);
} }
} }
@@ -49,6 +51,8 @@ static const int levelColors[] = {


static void logVa(Level level, const char* filename, int line, const char* format, va_list args) { static void logVa(Level level, const char* filename, int line, const char* format, va_list args) {
std::lock_guard<std::mutex> lock(logMutex); std::lock_guard<std::mutex> lock(logMutex);
if (!outputFile)
return;


int64_t nowTime = system::getNanoseconds(); int64_t nowTime = system::getNanoseconds();
double duration = (nowTime - startTime) / 1e9; double duration = (nowTime - startTime) / 1e9;
@@ -69,6 +73,27 @@ void log(Level level, const char* filename, int line, const char* format, ...) {
va_end(args); va_end(args);
} }


bool isTruncated() {
if (settings::devMode)
return false;

// Open existing log file
FILE* file = fopen(asset::logPath.c_str(), "r");
if (!file)
return false;
DEFER({
fclose(file);
});

// Seek to last 3 characters
fseek(file, -3, SEEK_END);
char str[4];
if (fread(str, 1, 3, file) != 3)
return true;
if (memcmp(str, "END", 3) != 0)
return true;
}



} // namespace logger } // namespace logger
} // namespace rack } // namespace rack

Loading…
Cancel
Save