DebugLog.cpp 1.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/* Copyright 2015 the SumatraPDF project authors (see AUTHORS file).
   License: Simplified BSD (see COPYING.BSD) */

#include "BaseUtil.h"
#include "DebugLog.h"

namespace dbglog {

  // TODO: as an optimization (to avoid allocations) and to prevent potential problems
  // when formatting, when there are no args just output fmt
void LogFV(const char *fmt, va_list args)
{
  char buf[512] = { 0 };
  str::BufFmtV(buf, dimof(buf), fmt, args);
  str::BufAppend(buf, dimof(buf), "\n");
  OutputDebugStringA(buf);
}

void LogF(const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    LogFV(fmt, args);
    va_end(args);
}

// TODO: as an optimization (to avoid allocations) and to prevent potential problems
// when formatting, when there are no args just output fmt
void LogFV(const WCHAR *fmt, va_list args)
{
    WCHAR buf[256] = { 0 };
    str::BufFmtV(buf, dimof(buf), fmt, args);
    str::BufAppend(buf, dimof(buf), L"\n");
    OutputDebugStringW(buf);
}

void LogF(const WCHAR *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    LogFV(fmt, args);
    va_end(args);
}

static str::Str<char> *gCrashLog = nullptr;

void CrashLogF(const char *fmt, ...)
{
    if (!gCrashLog) {
        // this is never freed, so only call CrashLogF before a crash
        gCrashLog = new str::Str<char>(4096);
    }

    va_list args;
    va_start(args, fmt);
    gCrashLog->AppendAndFree(str::FmtV(fmt, args));
    gCrashLog->Append("\r\n");
    va_end(args);
}

const char *GetCrashLog()
{
    if (!gCrashLog)
        return nullptr;
    return gCrashLog->LendData();
}

} // namespace dbglog