void TSessionLog::DoAddToSelf(TLogLineType Type, UnicodeString ALine) { if (LogToFile()) { try { if (FLogger == nullptr) { OpenLogFile(); } if (FLogger != nullptr) { UnicodeString Timestamp = FormatDateTime(L" yyyy-mm-dd hh:nn:ss.zzz ", Now()); UTF8String UtfLine = UTF8String(UnicodeString(LogLineMarks[Type]) + Timestamp + TrimRight(ALine)) + "\r\n"; #if 0 for (intptr_t Index = 1; Index <= UtfLine.Length(); Index++) { if ((UtfLine[Index] == '\n') && (UtfLine[Index - 1] != '\r')) { UtfLine.Insert(L'\r', Index); } } #endif // #if 0 intptr_t ToWrite = UtfLine.Length(); CheckSize(ToWrite); FCurrentFileSize += FLogger->Write(UtfLine.c_str(), ToWrite); }} catch (...) { // TODO: log error DEBUG_PRINTF("TSessionLog::DoAddToSelf: error"); } } }
void TraceDumpToFile() { if (TraceFile != nullptr) { TGuard Guard(TracingCriticalSection); DWORD Written; TDateTime N = Now(); #ifdef TRACE_IN_MEMORY_NO_FORMATTING DWORD Ticks = GetTickCount(); #endif const UnicodeString TimestampFormat = L"hh:mm:ss.zzz"; UnicodeString TimeString = FormatDateTime(TimestampFormat, N); UTF8String Buffer = UTF8String( FORMAT("[%s] Dumping in-memory tracing =================================\n", (TimeString))); WriteFile(TraceFile, Buffer.c_str(), Buffer.Length(), &Written, nullptr); TTracesInMemory::const_iterator i = TracesInMemory.begin(); while (i != TracesInMemory.end()) { #ifdef TRACE_IN_MEMORY_NO_FORMATTING const wchar_t * SourceFile = i->SourceFile; const wchar_t * Slash = wcsrchr(SourceFile, L'\\'); if (Slash != nullptr) { SourceFile = Slash + 1; } TimeString = FormatDateTime(TimestampFormat, IncMilliSecond(N, -static_cast<int>(Ticks - i->Ticks))); Buffer = UTF8String(FORMAT(L"[%s] [%.4X] [%s:%d:%s] %s\n", (TimeString, int(i->Thread), SourceFile, i->Line, i->Func, i->Message))); WriteFile(TraceFile, Buffer.c_str(), Buffer.Length(), &Written, nullptr); #else WriteFile(TraceFile, i->Message.c_str(), i->Message.Length(), &Written, nullptr); #endif ++i; } TracesInMemory.clear(); TimeString = FormatDateTime(TimestampFormat, Now()); Buffer = UTF8String( FORMAT("[%s] Done in-memory tracing =================================\n", (TimeString))); WriteFile(TraceFile, Buffer.c_str(), Buffer.Length(), &Written, nullptr); } }
//--------------------------------------------------------------------------- void TActionLog::Add(const UnicodeString & Line) { assert(FConfiguration); if (FLogging) { try { TGuard Guard(FCriticalSection); if (FFile == nullptr) { OpenLogFile(); } if (FFile != nullptr) { UTF8String UtfLine = UTF8String(Line); fwrite(UtfLine.c_str(), 1, UtfLine.Length(), (FILE *)FFile); fwrite("\n", 1, 1, (FILE *)FFile); } } catch (Exception &E) { // We failed logging, turn it off and notify user. FConfiguration->SetLogActions(false); try { throw ExtException(&E, LoadStr(LOG_GEN_ERROR)); } catch (Exception &E) { FUI->HandleExtendedException(&E); } } } }
void DoTrace(const wchar_t * SourceFile, const wchar_t * Func, uintptr_t Line, const wchar_t * Message) { DebugAssert(IsTracing); UnicodeString TimeString; // DateTimeToString(TimeString, L"hh:mm:ss.zzz", Now()); TODO("use Format"); const wchar_t * Slash = wcsrchr(SourceFile, L'\\'); if (Slash != nullptr) { SourceFile = Slash + 1; } UTF8String Buffer = UTF8String(FORMAT(L"[%s] [%.4X] [%s:%d:%s] %s\n", TimeString.c_str(), int(::GetCurrentThreadId()), SourceFile, Line, Func, Message)); #ifdef TRACE_IN_MEMORY if (TracingCriticalSection != nullptr) { TTraceInMemory TraceInMemory; TraceInMemory.Message = Buffer; TGuard Guard(TracingCriticalSection); if (TracesInMemory.capacity() == 0) { TracesInMemory.reserve(100000); TThreadID ThreadID; StartThread(nullptr, 0, TraceThreadProc, nullptr, 0, ThreadID); } TracesInMemory.push_back(TraceInMemory); } #else DWORD Written; WriteFile(TraceFile, Buffer.c_str(), static_cast<DWORD>(Buffer.Length()), &Written, nullptr); #endif TRACE_IN_MEMORY }
//--------------------------------------------------------------------------- void TSessionLog::DoAddToSelf(TLogLineType AType, const UnicodeString & ALine) { if (FTopIndex < 0) { FTopIndex = 0; } TStringList::AddObject(ALine, static_cast<TObject *>(reinterpret_cast<void *>(static_cast<size_t>(AType)))); FLoggedLines++; if (LogToFile()) { if (FFile == nullptr) { OpenLogFile(); } if (FFile != nullptr) { #if defined(__BORLANDC__) UnicodeString Timestamp = FormatDateTime(L" yyyy-mm-dd hh:nn:ss.zzz ", Now()); UTF8String UtfLine = UTF8String(UnicodeString(LogLineMarks[Type]) + Timestamp + Line + "\n"); fwrite(UtfLine.c_str(), UtfLine.Length(), 1, (FILE *)FFile); #else unsigned short Y, M, D, H, N, S, MS; TDateTime DateTime = Now(); DateTime.DecodeDate(Y, M, D); DateTime.DecodeTime(H, N, S, MS); UnicodeString dt = FORMAT(L" %04d-%02d-%02d %02d:%02d:%02d.%03d ", Y, M, D, H, N, S, MS); UnicodeString Timestamp = dt; UTF8String UtfLine = UTF8String(UnicodeString(LogLineMarks[AType]) + Timestamp + ALine + "\n"); fprintf_s(static_cast<FILE *>(FFile), "%s", const_cast<char *>(AnsiString(UtfLine).c_str())); #endif } } }
void TActionLog::Add(UnicodeString Line) { DebugAssert(FConfiguration); if (FLogging) { TGuard Guard(FCriticalSection); if (FLogger == nullptr) { OpenLogFile(); } if (FLogger != nullptr) { try { UTF8String UtfLine = UTF8String(Line); size_t Written = FLogger->Write(UtfLine.c_str(), UtfLine.Length()); if (Written != static_cast<size_t>(UtfLine.Length())) { throw ECRTExtException(L""); } Written = FLogger->Write("\n", 1); if (Written != 1) { throw ECRTExtException(L""); } } catch (Exception &E) { // FCriticalSection.Release(); // avoid endless loop when trying to close tags when closing log, when logging has failed if (!FFailed) { FFailed = true; // We failed logging, turn it off and notify user. FConfiguration->SetLogActions(false); if (FConfiguration->GetLogActionsRequired()) { throw EFatal(&E, LoadStr(LOG_FATAL_ERROR)); } else { try { throw ExtException(&E, LoadStr(LOG_GEN_ERROR)); } catch (Exception &E2) { if (FUI != nullptr) { FUI->HandleExtendedException(&E2); } } } } } } } }