SessionInfo.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <stdio.h>
  5. #include <lmcons.h>
  6. #define SECURITY_WIN32
  7. #include <sspi.h>
  8. #include <secext.h>
  9. #include "Common.h"
  10. #include "SessionInfo.h"
  11. #include "Exceptions.h"
  12. #include "TextsCore.h"
  13. //---------------------------------------------------------------------------
  14. #pragma package(smart_init)
  15. //---------------------------------------------------------------------------
  16. UnicodeString __fastcall DoXmlEscape(UnicodeString Str, bool NewLine)
  17. {
  18. for (int i = 1; i <= Str.Length(); i++)
  19. {
  20. const wchar_t * Repl = NULL;
  21. switch (Str[i])
  22. {
  23. case L'&':
  24. Repl = L"amp;";
  25. break;
  26. case L'>':
  27. Repl = L"gt;";
  28. break;
  29. case L'<':
  30. Repl = L"lt;";
  31. break;
  32. case L'"':
  33. Repl = L"quot;";
  34. break;
  35. case L'\n':
  36. if (NewLine)
  37. {
  38. Repl = L"#10;";
  39. }
  40. break;
  41. case L'\r':
  42. Str.Delete(i, 1);
  43. i--;
  44. break;
  45. }
  46. if (Repl != NULL)
  47. {
  48. Str[i] = L'&';
  49. Str.Insert(Repl, i + 1);
  50. i += wcslen(Repl);
  51. }
  52. }
  53. return Str;
  54. }
  55. //---------------------------------------------------------------------------
  56. UnicodeString __fastcall XmlEscape(UnicodeString Str)
  57. {
  58. return DoXmlEscape(Str, false);
  59. }
  60. //---------------------------------------------------------------------------
  61. UnicodeString __fastcall XmlAttributeEscape(UnicodeString Str)
  62. {
  63. return DoXmlEscape(Str, true);
  64. }
  65. //---------------------------------------------------------------------------
  66. //---------------------------------------------------------------------------
  67. #pragma warn -inl
  68. class TSessionActionRecord
  69. {
  70. public:
  71. __fastcall TSessionActionRecord(TActionLog * Log, TLogAction Action) :
  72. FLog(Log),
  73. FAction(Action),
  74. FState(Opened),
  75. FRecursive(false),
  76. FErrorMessages(NULL),
  77. FNames(new TStringList()),
  78. FValues(new TStringList()),
  79. FFileList(NULL),
  80. FFile(NULL)
  81. {
  82. FLog->AddPendingAction(this);
  83. }
  84. __fastcall ~TSessionActionRecord()
  85. {
  86. delete FErrorMessages;
  87. delete FNames;
  88. delete FValues;
  89. delete FFileList;
  90. delete FFile;
  91. }
  92. void __fastcall Restart()
  93. {
  94. FState = Opened;
  95. FRecursive = false;
  96. delete FErrorMessages;
  97. FErrorMessages = NULL;
  98. delete FFileList;
  99. FFileList = NULL;
  100. delete FFile;
  101. FFile = NULL;
  102. FNames->Clear();
  103. FValues->Clear();
  104. }
  105. bool __fastcall Record()
  106. {
  107. bool Result = (FState != Opened);
  108. if (Result)
  109. {
  110. if (FLog->FLogging && (FState != Cancelled))
  111. {
  112. const wchar_t * Name = ActionName();
  113. UnicodeString Attrs;
  114. if (FRecursive)
  115. {
  116. Attrs = L" recursive=\"true\"";
  117. }
  118. FLog->AddIndented(FORMAT(L"<%s%s>", (Name, Attrs)));
  119. for (int Index = 0; Index < FNames->Count; Index++)
  120. {
  121. UnicodeString Value = FValues->Strings[Index];
  122. if (Value.IsEmpty())
  123. {
  124. FLog->AddIndented(FORMAT(L" <%s />", (FNames->Strings[Index])));
  125. }
  126. else
  127. {
  128. FLog->AddIndented(FORMAT(L" <%s value=\"%s\" />",
  129. (FNames->Strings[Index], XmlAttributeEscape(Value))));
  130. }
  131. }
  132. if (FFileList != NULL)
  133. {
  134. FLog->AddIndented(L" <files>");
  135. for (int Index = 0; Index < FFileList->Count; Index++)
  136. {
  137. TRemoteFile * File = FFileList->Files[Index];
  138. FLog->AddIndented(L" <file>");
  139. FLog->AddIndented(FORMAT(L" <filename value=\"%s\" />", (XmlAttributeEscape(File->FileName))));
  140. FLog->AddIndented(FORMAT(L" <type value=\"%s\" />", (XmlAttributeEscape(File->Type))));
  141. if (!File->IsDirectory)
  142. {
  143. FLog->AddIndented(FORMAT(L" <size value=\"%s\" />", (IntToStr(File->Size))));
  144. }
  145. FLog->AddIndented(FORMAT(L" <modification value=\"%s\" />", (StandardTimestamp(File->Modification))));
  146. FLog->AddIndented(FORMAT(L" <permissions value=\"%s\" />", (XmlAttributeEscape(File->Rights->Text))));
  147. FLog->AddIndented(L" </file>");
  148. }
  149. FLog->AddIndented(L" </files>");
  150. }
  151. if (FFile != NULL)
  152. {
  153. FLog->AddIndented(L" <file>");
  154. FLog->AddIndented(FORMAT(L" <type value=\"%s\" />", (XmlAttributeEscape(FFile->Type))));
  155. if (!FFile->IsDirectory)
  156. {
  157. FLog->AddIndented(FORMAT(L" <size value=\"%s\" />", (IntToStr(FFile->Size))));
  158. }
  159. FLog->AddIndented(FORMAT(L" <modification value=\"%s\" />", (StandardTimestamp(FFile->Modification))));
  160. FLog->AddIndented(FORMAT(L" <permissions value=\"%s\" />", (XmlAttributeEscape(FFile->Rights->Text))));
  161. FLog->AddIndented(L" </file>");
  162. }
  163. if (FState == RolledBack)
  164. {
  165. if (FErrorMessages != NULL)
  166. {
  167. FLog->AddIndented(L" <result success=\"false\">");
  168. FLog->AddMessages(L" ", FErrorMessages);
  169. FLog->AddIndented(L" </result>");
  170. }
  171. else
  172. {
  173. FLog->AddIndented(L" <result success=\"false\" />");
  174. }
  175. }
  176. else
  177. {
  178. FLog->AddIndented(L" <result success=\"true\" />");
  179. }
  180. FLog->AddIndented(FORMAT(L"</%s>", (Name)));
  181. }
  182. delete this;
  183. }
  184. return Result;
  185. }
  186. void __fastcall Commit()
  187. {
  188. Close(Committed);
  189. }
  190. void __fastcall Rollback(Exception * E)
  191. {
  192. assert(FErrorMessages == NULL);
  193. FErrorMessages = ExceptionToMoreMessages(E);
  194. Close(RolledBack);
  195. }
  196. void __fastcall Cancel()
  197. {
  198. Close(Cancelled);
  199. }
  200. void __fastcall FileName(const UnicodeString & FileName)
  201. {
  202. Parameter(L"filename", FileName);
  203. }
  204. void __fastcall Destination(const UnicodeString & Destination)
  205. {
  206. Parameter(L"destination", Destination);
  207. }
  208. void __fastcall Rights(const TRights & Rights)
  209. {
  210. Parameter(L"permissions", Rights.Text);
  211. }
  212. void __fastcall Modification(const TDateTime & DateTime)
  213. {
  214. Parameter(L"modification", StandardTimestamp(DateTime));
  215. }
  216. void __fastcall Recursive()
  217. {
  218. FRecursive = true;
  219. }
  220. void __fastcall Command(const UnicodeString & Command)
  221. {
  222. Parameter(L"command", Command);
  223. }
  224. void __fastcall AddOutput(UnicodeString Output, bool StdError)
  225. {
  226. const wchar_t * Name = (StdError ? L"erroroutput" : L"output");
  227. int Index = FNames->IndexOf(Name);
  228. if (Index >= 0)
  229. {
  230. FValues->Strings[Index] = FValues->Strings[Index] + L"\r\n" + Output;
  231. }
  232. else
  233. {
  234. Parameter(Name, Output);
  235. }
  236. }
  237. void __fastcall FileList(TRemoteFileList * FileList)
  238. {
  239. if (FFileList == NULL)
  240. {
  241. FFileList = new TRemoteFileList();
  242. }
  243. FileList->DuplicateTo(FFileList);
  244. }
  245. void __fastcall File(TRemoteFile * File)
  246. {
  247. if (FFile != NULL)
  248. {
  249. delete FFile;
  250. }
  251. FFile = File->Duplicate(true);
  252. }
  253. protected:
  254. enum TState { Opened, Committed, RolledBack, Cancelled };
  255. inline void __fastcall Close(TState State)
  256. {
  257. assert(FState == Opened);
  258. FState = State;
  259. FLog->RecordPendingActions();
  260. }
  261. const wchar_t * __fastcall ActionName()
  262. {
  263. switch (FAction)
  264. {
  265. case laUpload: return L"upload";
  266. case laDownload: return L"download";
  267. case laTouch: return L"touch";
  268. case laChmod: return L"chmod";
  269. case laMkdir: return L"mkdir";
  270. case laRm: return L"rm";
  271. case laMv: return L"mv";
  272. case laCall: return L"call";
  273. case laLs: return L"ls";
  274. case laStat: return L"stat";
  275. default: assert(false); return L"";
  276. }
  277. }
  278. void __fastcall Parameter(const UnicodeString & Name, const UnicodeString & Value = L"")
  279. {
  280. FNames->Add(Name);
  281. FValues->Add(Value);
  282. }
  283. private:
  284. TActionLog * FLog;
  285. TLogAction FAction;
  286. TState FState;
  287. bool FRecursive;
  288. TStrings * FErrorMessages;
  289. TStrings * FNames;
  290. TStrings * FValues;
  291. TRemoteFileList * FFileList;
  292. TRemoteFile * FFile;
  293. };
  294. #pragma warn .inl
  295. //---------------------------------------------------------------------------
  296. //---------------------------------------------------------------------------
  297. __fastcall TSessionAction::TSessionAction(TActionLog * Log, TLogAction Action)
  298. {
  299. if (Log->FLogging)
  300. {
  301. FRecord = new TSessionActionRecord(Log, Action);
  302. }
  303. else
  304. {
  305. FRecord = NULL;
  306. }
  307. }
  308. //---------------------------------------------------------------------------
  309. __fastcall TSessionAction::~TSessionAction()
  310. {
  311. if (FRecord != NULL)
  312. {
  313. Commit();
  314. }
  315. }
  316. //---------------------------------------------------------------------------
  317. void __fastcall TSessionAction::Restart()
  318. {
  319. if (FRecord != NULL)
  320. {
  321. FRecord->Restart();
  322. }
  323. }
  324. //---------------------------------------------------------------------------
  325. void __fastcall TSessionAction::Commit()
  326. {
  327. if (FRecord != NULL)
  328. {
  329. TSessionActionRecord * Record = FRecord;
  330. FRecord = NULL;
  331. Record->Commit();
  332. }
  333. }
  334. //---------------------------------------------------------------------------
  335. void __fastcall TSessionAction::Rollback(Exception * E)
  336. {
  337. if (FRecord != NULL)
  338. {
  339. TSessionActionRecord * Record = FRecord;
  340. FRecord = NULL;
  341. Record->Rollback(E);
  342. }
  343. }
  344. //---------------------------------------------------------------------------
  345. void __fastcall TSessionAction::Cancel()
  346. {
  347. if (FRecord != NULL)
  348. {
  349. TSessionActionRecord * Record = FRecord;
  350. FRecord = NULL;
  351. Record->Cancel();
  352. }
  353. }
  354. //---------------------------------------------------------------------------
  355. //---------------------------------------------------------------------------
  356. __fastcall TFileSessionAction::TFileSessionAction(TActionLog * Log, TLogAction Action) :
  357. TSessionAction(Log, Action)
  358. {
  359. }
  360. //---------------------------------------------------------------------------
  361. __fastcall TFileSessionAction::TFileSessionAction(
  362. TActionLog * Log, TLogAction Action, const UnicodeString & AFileName) :
  363. TSessionAction(Log, Action)
  364. {
  365. FileName(AFileName);
  366. }
  367. //---------------------------------------------------------------------------
  368. void __fastcall TFileSessionAction::FileName(const UnicodeString & FileName)
  369. {
  370. if (FRecord != NULL)
  371. {
  372. FRecord->FileName(FileName);
  373. }
  374. }
  375. //---------------------------------------------------------------------------
  376. //---------------------------------------------------------------------------
  377. __fastcall TFileLocationSessionAction::TFileLocationSessionAction(
  378. TActionLog * Log, TLogAction Action) :
  379. TFileSessionAction(Log, Action)
  380. {
  381. }
  382. //---------------------------------------------------------------------------
  383. __fastcall TFileLocationSessionAction::TFileLocationSessionAction(
  384. TActionLog * Log, TLogAction Action, const UnicodeString & FileName) :
  385. TFileSessionAction(Log, Action, FileName)
  386. {
  387. }
  388. //---------------------------------------------------------------------------
  389. void __fastcall TFileLocationSessionAction::Destination(const UnicodeString & Destination)
  390. {
  391. if (FRecord != NULL)
  392. {
  393. FRecord->Destination(Destination);
  394. }
  395. }
  396. //---------------------------------------------------------------------------
  397. //---------------------------------------------------------------------------
  398. __fastcall TUploadSessionAction::TUploadSessionAction(TActionLog * Log) :
  399. TFileLocationSessionAction(Log, laUpload)
  400. {
  401. }
  402. //---------------------------------------------------------------------------
  403. //---------------------------------------------------------------------------
  404. __fastcall TDownloadSessionAction::TDownloadSessionAction(TActionLog * Log) :
  405. TFileLocationSessionAction(Log, laDownload)
  406. {
  407. }
  408. //---------------------------------------------------------------------------
  409. //---------------------------------------------------------------------------
  410. __fastcall TChmodSessionAction::TChmodSessionAction(
  411. TActionLog * Log, const UnicodeString & FileName) :
  412. TFileSessionAction(Log, laChmod, FileName)
  413. {
  414. }
  415. //---------------------------------------------------------------------------
  416. void __fastcall TChmodSessionAction::Recursive()
  417. {
  418. if (FRecord != NULL)
  419. {
  420. FRecord->Recursive();
  421. }
  422. }
  423. //---------------------------------------------------------------------------
  424. __fastcall TChmodSessionAction::TChmodSessionAction(
  425. TActionLog * Log, const UnicodeString & FileName, const TRights & ARights) :
  426. TFileSessionAction(Log, laChmod, FileName)
  427. {
  428. Rights(ARights);
  429. }
  430. //---------------------------------------------------------------------------
  431. void __fastcall TChmodSessionAction::Rights(const TRights & Rights)
  432. {
  433. if (FRecord != NULL)
  434. {
  435. FRecord->Rights(Rights);
  436. }
  437. }
  438. //---------------------------------------------------------------------------
  439. __fastcall TTouchSessionAction::TTouchSessionAction(
  440. TActionLog * Log, const UnicodeString & FileName, const TDateTime & Modification) :
  441. TFileSessionAction(Log, laTouch, FileName)
  442. {
  443. if (FRecord != NULL)
  444. {
  445. FRecord->Modification(Modification);
  446. }
  447. }
  448. //---------------------------------------------------------------------------
  449. __fastcall TMkdirSessionAction::TMkdirSessionAction(
  450. TActionLog * Log, const UnicodeString & FileName) :
  451. TFileSessionAction(Log, laMkdir, FileName)
  452. {
  453. }
  454. //---------------------------------------------------------------------------
  455. __fastcall TRmSessionAction::TRmSessionAction(
  456. TActionLog * Log, const UnicodeString & FileName) :
  457. TFileSessionAction(Log, laRm, FileName)
  458. {
  459. }
  460. //---------------------------------------------------------------------------
  461. void __fastcall TRmSessionAction::Recursive()
  462. {
  463. if (FRecord != NULL)
  464. {
  465. FRecord->Recursive();
  466. }
  467. }
  468. //---------------------------------------------------------------------------
  469. __fastcall TMvSessionAction::TMvSessionAction(TActionLog * Log,
  470. const UnicodeString & FileName, const UnicodeString & ADestination) :
  471. TFileLocationSessionAction(Log, laMv, FileName)
  472. {
  473. Destination(ADestination);
  474. }
  475. //---------------------------------------------------------------------------
  476. __fastcall TCallSessionAction::TCallSessionAction(TActionLog * Log,
  477. const UnicodeString & Command, const UnicodeString & Destination) :
  478. TSessionAction(Log, laCall)
  479. {
  480. if (FRecord != NULL)
  481. {
  482. FRecord->Command(Command);
  483. FRecord->Destination(Destination);
  484. }
  485. }
  486. //---------------------------------------------------------------------------
  487. void __fastcall TCallSessionAction::AddOutput(const UnicodeString & Output, bool StdError)
  488. {
  489. if (FRecord != NULL)
  490. {
  491. FRecord->AddOutput(Output, StdError);
  492. }
  493. }
  494. //---------------------------------------------------------------------------
  495. __fastcall TLsSessionAction::TLsSessionAction(TActionLog * Log,
  496. const UnicodeString & Destination) :
  497. TSessionAction(Log, laLs)
  498. {
  499. if (FRecord != NULL)
  500. {
  501. FRecord->Destination(Destination);
  502. }
  503. }
  504. //---------------------------------------------------------------------------
  505. void __fastcall TLsSessionAction::FileList(TRemoteFileList * FileList)
  506. {
  507. if (FRecord != NULL)
  508. {
  509. FRecord->FileList(FileList);
  510. }
  511. }
  512. //---------------------------------------------------------------------------
  513. //---------------------------------------------------------------------------
  514. __fastcall TStatSessionAction::TStatSessionAction(TActionLog * Log, const UnicodeString & FileName) :
  515. TFileSessionAction(Log, laStat, FileName)
  516. {
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall TStatSessionAction::File(TRemoteFile * File)
  520. {
  521. if (FRecord != NULL)
  522. {
  523. FRecord->File(File);
  524. }
  525. }
  526. //---------------------------------------------------------------------------
  527. //---------------------------------------------------------------------------
  528. TSessionInfo::TSessionInfo()
  529. {
  530. LoginTime = Now();
  531. }
  532. //---------------------------------------------------------------------------
  533. TFileSystemInfo::TFileSystemInfo()
  534. {
  535. memset(&IsCapable, false, sizeof(IsCapable));
  536. }
  537. //---------------------------------------------------------------------------
  538. //---------------------------------------------------------------------------
  539. FILE * __fastcall OpenFile(UnicodeString LogFileName, TSessionData * SessionData, bool Append, UnicodeString & NewFileName)
  540. {
  541. FILE * Result;
  542. UnicodeString ANewFileName = GetExpandedLogFileName(LogFileName, SessionData);
  543. Result = _wfopen(ANewFileName.c_str(), (Append ? L"a" : L"w"));
  544. if (Result != NULL)
  545. {
  546. setvbuf(Result, NULL, _IONBF, BUFSIZ);
  547. NewFileName = ANewFileName;
  548. }
  549. else
  550. {
  551. throw Exception(FMTLOAD(LOG_OPENERROR, (ANewFileName)));
  552. }
  553. return Result;
  554. }
  555. //---------------------------------------------------------------------------
  556. //---------------------------------------------------------------------------
  557. const wchar_t *LogLineMarks = L"<>!.*";
  558. __fastcall TSessionLog::TSessionLog(TSessionUI* UI, TSessionData * SessionData,
  559. TConfiguration * Configuration):
  560. TStringList()
  561. {
  562. FCriticalSection = new TCriticalSection;
  563. FLogging = false;
  564. FConfiguration = Configuration;
  565. FParent = NULL;
  566. FUI = UI;
  567. FSessionData = SessionData;
  568. FFile = NULL;
  569. FLoggedLines = 0;
  570. FTopIndex = -1;
  571. FCurrentLogFileName = L"";
  572. FCurrentFileName = L"";
  573. FClosed = false;
  574. }
  575. //---------------------------------------------------------------------------
  576. __fastcall TSessionLog::~TSessionLog()
  577. {
  578. FClosed = true;
  579. ReflectSettings();
  580. assert(FFile == NULL);
  581. delete FCriticalSection;
  582. }
  583. //---------------------------------------------------------------------------
  584. void __fastcall TSessionLog::Lock()
  585. {
  586. FCriticalSection->Enter();
  587. }
  588. //---------------------------------------------------------------------------
  589. void __fastcall TSessionLog::Unlock()
  590. {
  591. FCriticalSection->Leave();
  592. }
  593. //---------------------------------------------------------------------------
  594. UnicodeString __fastcall TSessionLog::GetSessionName()
  595. {
  596. assert(FSessionData != NULL);
  597. return FSessionData->SessionName;
  598. }
  599. //---------------------------------------------------------------------------
  600. UnicodeString __fastcall TSessionLog::GetLine(Integer Index)
  601. {
  602. return Strings[Index - FTopIndex];
  603. }
  604. //---------------------------------------------------------------------------
  605. TLogLineType __fastcall TSessionLog::GetType(int Index)
  606. {
  607. return (TLogLineType)Objects[Index - FTopIndex];
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TSessionLog::DoAddToParent(TLogLineType Type, const UnicodeString & Line)
  611. {
  612. assert(FParent != NULL);
  613. FParent->Add(Type, Line);
  614. }
  615. //---------------------------------------------------------------------------
  616. void __fastcall TSessionLog::DoAddToSelf(TLogLineType Type, const UnicodeString & Line)
  617. {
  618. if (FTopIndex < 0)
  619. {
  620. FTopIndex = 0;
  621. }
  622. TStringList::AddObject(Line, (TObject*)Type);
  623. FLoggedLines++;
  624. if (LogToFile())
  625. {
  626. if (FFile == NULL)
  627. {
  628. OpenLogFile();
  629. }
  630. if (FFile != NULL)
  631. {
  632. UnicodeString Timestamp = FormatDateTime(L" yyyy-mm-dd hh:nn:ss.zzz ", Now());
  633. UTF8String UtfLine = UTF8String(UnicodeString(LogLineMarks[Type]) + Timestamp + Line + "\n");
  634. fwrite(UtfLine.c_str(), UtfLine.Length(), 1, (FILE *)FFile);
  635. }
  636. }
  637. }
  638. //---------------------------------------------------------------------------
  639. void __fastcall TSessionLog::DoAdd(TLogLineType Type, UnicodeString Line,
  640. void __fastcall (__closure *f)(TLogLineType Type, const UnicodeString & Line))
  641. {
  642. UnicodeString Prefix;
  643. if (!Name.IsEmpty())
  644. {
  645. Prefix = L"[" + Name + L"] ";
  646. }
  647. while (!Line.IsEmpty())
  648. {
  649. f(Type, Prefix + CutToChar(Line, L'\n', false));
  650. }
  651. }
  652. //---------------------------------------------------------------------------
  653. void __fastcall TSessionLog::Add(TLogLineType Type, const UnicodeString & Line)
  654. {
  655. assert(FConfiguration);
  656. if (Logging)
  657. {
  658. try
  659. {
  660. if (FParent != NULL)
  661. {
  662. DoAdd(Type, Line, DoAddToParent);
  663. }
  664. else
  665. {
  666. TGuard Guard(FCriticalSection);
  667. BeginUpdate();
  668. try
  669. {
  670. DoAdd(Type, Line, DoAddToSelf);
  671. }
  672. __finally
  673. {
  674. DeleteUnnecessary();
  675. EndUpdate();
  676. }
  677. }
  678. }
  679. catch (Exception &E)
  680. {
  681. // We failed logging, turn it off and notify user.
  682. FConfiguration->Logging = false;
  683. try
  684. {
  685. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  686. }
  687. catch (Exception &E)
  688. {
  689. AddException(&E);
  690. FUI->HandleExtendedException(&E);
  691. }
  692. }
  693. }
  694. }
  695. //---------------------------------------------------------------------------
  696. void __fastcall TSessionLog::AddException(Exception * E)
  697. {
  698. if (E != NULL)
  699. {
  700. Add(llException, ExceptionLogString(E));
  701. }
  702. }
  703. //---------------------------------------------------------------------------
  704. void __fastcall TSessionLog::ReflectSettings()
  705. {
  706. TGuard Guard(FCriticalSection);
  707. bool ALogging =
  708. !FClosed &&
  709. ((FParent != NULL) || FConfiguration->Logging);
  710. if (FLogging != ALogging)
  711. {
  712. FLogging = ALogging;
  713. StateChange();
  714. }
  715. // if logging to file was turned off or log file was changed -> close current log file
  716. if ((FFile != NULL) &&
  717. (!LogToFile() || (FCurrentLogFileName != FConfiguration->LogFileName)))
  718. {
  719. CloseLogFile();
  720. }
  721. DeleteUnnecessary();
  722. }
  723. //---------------------------------------------------------------------------
  724. bool __fastcall TSessionLog::LogToFile()
  725. {
  726. return Logging && FConfiguration->LogToFile && (FParent == NULL);
  727. }
  728. //---------------------------------------------------------------------------
  729. void __fastcall TSessionLog::CloseLogFile()
  730. {
  731. if (FFile != NULL)
  732. {
  733. fclose((FILE *)FFile);
  734. FFile = NULL;
  735. }
  736. FCurrentLogFileName = L"";
  737. FCurrentFileName = L"";
  738. StateChange();
  739. }
  740. //---------------------------------------------------------------------------
  741. void __fastcall TSessionLog::OpenLogFile()
  742. {
  743. try
  744. {
  745. assert(FFile == NULL);
  746. assert(FConfiguration != NULL);
  747. FCurrentLogFileName = FConfiguration->LogFileName;
  748. FFile = OpenFile(FCurrentLogFileName, FSessionData, FConfiguration->LogFileAppend, FCurrentFileName);
  749. }
  750. catch (Exception & E)
  751. {
  752. // We failed logging to file, turn it off and notify user.
  753. FCurrentLogFileName = L"";
  754. FCurrentFileName = L"";
  755. FConfiguration->LogFileName = UnicodeString();
  756. try
  757. {
  758. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  759. }
  760. catch (Exception & E)
  761. {
  762. AddException(&E);
  763. FUI->HandleExtendedException(&E);
  764. }
  765. }
  766. StateChange();
  767. }
  768. //---------------------------------------------------------------------------
  769. void __fastcall TSessionLog::StateChange()
  770. {
  771. if (FOnStateChange != NULL)
  772. {
  773. FOnStateChange(this);
  774. }
  775. }
  776. //---------------------------------------------------------------------------
  777. void __fastcall TSessionLog::DeleteUnnecessary()
  778. {
  779. BeginUpdate();
  780. try
  781. {
  782. if (!Logging || (FParent != NULL))
  783. {
  784. Clear();
  785. }
  786. else
  787. {
  788. while (!FConfiguration->LogWindowComplete && (Count > FConfiguration->LogWindowLines))
  789. {
  790. Delete(0);
  791. FTopIndex++;
  792. }
  793. }
  794. }
  795. __finally
  796. {
  797. EndUpdate();
  798. }
  799. }
  800. //---------------------------------------------------------------------------
  801. void __fastcall TSessionLog::AddSystemInfo()
  802. {
  803. AddStartupInfo(true);
  804. }
  805. //---------------------------------------------------------------------------
  806. void __fastcall TSessionLog::AddStartupInfo()
  807. {
  808. AddStartupInfo(false);
  809. }
  810. //---------------------------------------------------------------------------
  811. void __fastcall TSessionLog::AddStartupInfo(bool System)
  812. {
  813. TSessionData * Data = (System ? NULL : FSessionData);
  814. if (Logging)
  815. {
  816. if (FParent != NULL)
  817. {
  818. // do not add session info for secondary session
  819. // (this should better be handled in the TSecondaryTerminal)
  820. }
  821. else
  822. {
  823. DoAddStartupInfo(Data);
  824. }
  825. }
  826. }
  827. //---------------------------------------------------------------------------
  828. UnicodeString __fastcall TSessionLog::GetTlsVersionName(TTlsVersion TlsVersion)
  829. {
  830. switch (TlsVersion)
  831. {
  832. default:
  833. FAIL;
  834. case ssl2:
  835. return "SSLv2";
  836. case ssl3:
  837. return "SSLv3";
  838. case tls10:
  839. return "TLSv1.0";
  840. case tls11:
  841. return "TLSv1.1";
  842. case tls12:
  843. return "TLSv1.2";
  844. }
  845. }
  846. //---------------------------------------------------------------------------
  847. void __fastcall TSessionLog::DoAddStartupInfo(TSessionData * Data)
  848. {
  849. TGuard Guard(FCriticalSection);
  850. BeginUpdate();
  851. try
  852. {
  853. #define ADSTR(S) DoAdd(llMessage, S, DoAddToSelf);
  854. #define ADF(S, F) ADSTR(FORMAT(S, F));
  855. if (Data == NULL)
  856. {
  857. AddSeparator();
  858. ADF(L"WinSCP %s (OS %s)", (FConfiguration->VersionStr, FConfiguration->OSVersionStr));
  859. THierarchicalStorage * Storage = FConfiguration->CreateScpStorage(false);
  860. try
  861. {
  862. ADF(L"Configuration: %s", (Storage->Source));
  863. }
  864. __finally
  865. {
  866. delete Storage;
  867. }
  868. wchar_t UserName[UNLEN + 1];
  869. unsigned long UserNameSize = LENOF(UserName);
  870. if (ALWAYS_FALSE(!GetUserNameEx(NameSamCompatible, UserName, &UserNameSize)))
  871. {
  872. wcscpy(UserName, L"<Failed to retrieve username>");
  873. }
  874. ADF(L"Local account: %s", (UserName));
  875. ADF(L"Working directory: %s", (GetCurrentDir()));
  876. ADF(L"Process ID: %d", (int(GetCurrentProcessId())));
  877. ADF(L"Command-line: %s", (CmdLine));
  878. ADF(L"Time zone: %s", (GetTimeZoneLogString()));
  879. if (!AdjustClockForDSTEnabled())
  880. {
  881. ADSTR(L"Warning: System option \"Automatically adjust clock for Daylight Saving Time\" is disabled, timestamps will not be represented correctly");
  882. }
  883. ADF(L"Login time: %s", (FormatDateTime(L"dddddd tt", Now())));
  884. AddSeparator();
  885. }
  886. else
  887. {
  888. ADF(L"Session name: %s (%s)", (Data->SessionName, Data->Source));
  889. ADF(L"Host name: %s (Port: %d)", (Data->HostNameExpanded, Data->PortNumber));
  890. ADF(L"User name: %s (Password: %s, Key file: %s)",
  891. (Data->UserNameExpanded, BooleanToEngStr(!Data->Password.IsEmpty()),
  892. BooleanToEngStr(!Data->PublicKeyFile.IsEmpty())))
  893. ADF(L"Tunnel: %s", (BooleanToEngStr(Data->Tunnel)));
  894. if (Data->Tunnel)
  895. {
  896. ADF(L"Tunnel: Host name: %s (Port: %d)", (Data->TunnelHostName, Data->TunnelPortNumber));
  897. ADF(L"Tunnel: User name: %s (Password: %s, Key file: %s)",
  898. (Data->TunnelUserName, BooleanToEngStr(!Data->TunnelPassword.IsEmpty()),
  899. BooleanToEngStr(!Data->TunnelPublicKeyFile.IsEmpty())))
  900. ADF(L"Tunnel: Local port number: %d", (Data->TunnelLocalPortNumber));
  901. }
  902. ADF(L"Transfer Protocol: %s", (Data->FSProtocolStr));
  903. wchar_t * PingTypes = L"-NC";
  904. TPingType PingType;
  905. int PingInterval;
  906. if (Data->FSProtocol == fsFTP)
  907. {
  908. PingType = Data->FtpPingType;
  909. PingInterval = Data->FtpPingInterval;
  910. }
  911. else
  912. {
  913. PingType = Data->PingType;
  914. PingInterval = Data->PingInterval;
  915. }
  916. ADF(L"Ping type: %s, Ping interval: %d sec; Timeout: %d sec",
  917. (UnicodeString(PingTypes[PingType]), PingInterval, Data->Timeout));
  918. ADF(L"Proxy: %s",
  919. ((Data->FtpProxyLogonType != 0) ?
  920. FORMAT(L"FTP proxy %d", (Data->FtpProxyLogonType)) :
  921. UnicodeString(ProxyMethodList[Data->ProxyMethod])));
  922. if ((Data->FtpProxyLogonType != 0) || (Data->ProxyMethod != ::pmNone))
  923. {
  924. ADF(L"HostName: %s (Port: %d); Username: %s; Passwd: %s",
  925. (Data->ProxyHost, Data->ProxyPort,
  926. Data->ProxyUsername, BooleanToEngStr(!Data->ProxyPassword.IsEmpty())));
  927. if (Data->ProxyMethod == pmTelnet)
  928. {
  929. ADF(L"Telnet command: %s", (Data->ProxyTelnetCommand));
  930. }
  931. if (Data->ProxyMethod == pmCmd)
  932. {
  933. ADF(L"Local command: %s", (Data->ProxyLocalCommand));
  934. }
  935. }
  936. ADF(L"Send buffer: %d", (Data->SendBuf));
  937. wchar_t const * BugFlags = L"+-A";
  938. if (Data->UsesSsh)
  939. {
  940. ADF(L"SSH protocol version: %s; Compression: %s",
  941. (Data->SshProtStr, BooleanToEngStr(Data->Compression)));
  942. ADF(L"Bypass authentication: %s",
  943. (BooleanToEngStr(Data->SshNoUserAuth)));
  944. ADF(L"Try agent: %s; Agent forwarding: %s; TIS/CryptoCard: %s; KI: %s; GSSAPI: %s",
  945. (BooleanToEngStr(Data->TryAgent), BooleanToEngStr(Data->AgentFwd), BooleanToEngStr(Data->AuthTIS),
  946. BooleanToEngStr(Data->AuthKI), BooleanToEngStr(Data->AuthGSSAPI)));
  947. if (Data->AuthGSSAPI)
  948. {
  949. ADF(L"GSSAPI: Forwarding: %s; Server realm: %s",
  950. (BooleanToEngStr(Data->GSSAPIFwdTGT), Data->GSSAPIServerRealm));
  951. }
  952. ADF(L"Ciphers: %s; Ssh2DES: %s",
  953. (Data->CipherList, BooleanToEngStr(Data->Ssh2DES)));
  954. UnicodeString Bugs;
  955. for (int Index = 0; Index < BUG_COUNT; Index++)
  956. {
  957. Bugs += UnicodeString(BugFlags[Data->Bug[(TSshBug)Index]])+(Index<BUG_COUNT-1?L",":L"");
  958. }
  959. ADF(L"SSH Bugs: %s", (Bugs));
  960. ADF(L"Simple channel: %s", (BooleanToEngStr(Data->SshSimple)));
  961. ADF(L"Return code variable: %s; Lookup user groups: %s",
  962. ((Data->DetectReturnVar ? UnicodeString(L"Autodetect") : Data->ReturnVar),
  963. BugFlags[Data->LookupUserGroups]));
  964. ADF(L"Shell: %s", ((Data->Shell.IsEmpty()? UnicodeString(L"default") : Data->Shell)));
  965. ADF(L"EOL: %d, UTF: %d", (Data->EOLType, Data->NotUtf)); // NotUtf duplicated in FTP branch
  966. ADF(L"Clear aliases: %s, Unset nat.vars: %s, Resolve symlinks: %s",
  967. (BooleanToEngStr(Data->ClearAliases), BooleanToEngStr(Data->UnsetNationalVars),
  968. BooleanToEngStr(Data->ResolveSymlinks)));
  969. ADF(L"LS: %s, Ign LS warn: %s, Scp1 Comp: %s",
  970. (Data->ListingCommand,
  971. BooleanToEngStr(Data->IgnoreLsWarnings),
  972. BooleanToEngStr(Data->Scp1Compatibility)));
  973. }
  974. if (Data->FSProtocol == fsSFTP)
  975. {
  976. UnicodeString Bugs;
  977. for (int Index = 0; Index < SFTP_BUG_COUNT; Index++)
  978. {
  979. Bugs += UnicodeString(BugFlags[Data->SFTPBug[(TSftpBug)Index]])+(Index<SFTP_BUG_COUNT-1?L",":L"");
  980. }
  981. ADF(L"SFTP Bugs: %s", (Bugs));
  982. ADF(L"SFTP Server: %s", ((Data->SftpServer.IsEmpty()? UnicodeString(L"default") : Data->SftpServer)));
  983. }
  984. if (Data->FSProtocol == fsFTP)
  985. {
  986. ADF(L"UTF: %d", (Data->NotUtf)); // duplicated in UsesSsh branch
  987. UnicodeString Ftps;
  988. switch (Data->Ftps)
  989. {
  990. case ftpsImplicit:
  991. Ftps = L"Implicit TLS/SSL";
  992. break;
  993. case ftpsExplicitSsl:
  994. Ftps = L"Explicit SSL";
  995. break;
  996. case ftpsExplicitTls:
  997. Ftps = L"Explicit TLS";
  998. break;
  999. default:
  1000. assert(Data->Ftps == ftpsNone);
  1001. Ftps = L"None";
  1002. break;
  1003. }
  1004. ADF(L"FTP: FTPS: %s; Passive: %s [Force IP: %s]; MLSD: %s [List all: %s]",
  1005. (Ftps, BooleanToEngStr(Data->FtpPasvMode),
  1006. BugFlags[Data->FtpForcePasvIp],
  1007. BugFlags[Data->FtpUseMlsd],
  1008. BugFlags[Data->FtpListAll]));
  1009. if (Data->Ftps != ftpsNone)
  1010. {
  1011. ADF(L"Session reuse: %s", (BooleanToEngStr(Data->SslSessionReuse)));
  1012. ADF(L"TLS/SSL versions: %s-%s", (GetTlsVersionName(Data->MinTlsVersion), GetTlsVersionName(Data->MaxTlsVersion)));
  1013. }
  1014. // kind of hidden option, so do not reveal it unless it is set
  1015. if (Data->FtpTransferActiveImmediatelly)
  1016. {
  1017. ADF(L"Transfer active immediatelly: %s", (BooleanToEngStr(Data->FtpTransferActiveImmediatelly)));
  1018. }
  1019. }
  1020. ADF(L"Local directory: %s, Remote directory: %s, Update: %s, Cache: %s",
  1021. ((Data->LocalDirectory.IsEmpty() ? UnicodeString(L"default") : Data->LocalDirectory),
  1022. (Data->RemoteDirectory.IsEmpty() ? UnicodeString(L"home") : Data->RemoteDirectory),
  1023. BooleanToEngStr(Data->UpdateDirectories),
  1024. BooleanToEngStr(Data->CacheDirectories)));
  1025. ADF(L"Cache directory changes: %s, Permanent: %s",
  1026. (BooleanToEngStr(Data->CacheDirectoryChanges),
  1027. BooleanToEngStr(Data->PreserveDirectoryChanges)));
  1028. int TimeDifferenceMin = TimeToMinutes(Data->TimeDifference);
  1029. ADF(L"DST mode: %d; Timezone offset: %dh %dm", (int(Data->DSTMode), (TimeDifferenceMin / MinsPerHour), (TimeDifferenceMin % MinsPerHour)));
  1030. if (Data->FSProtocol == fsWebDAV)
  1031. {
  1032. ADF(L"Compression: %s",
  1033. (BooleanToEngStr(Data->Compression)));
  1034. }
  1035. AddSeparator();
  1036. }
  1037. #undef ADF
  1038. #undef ADSTR
  1039. }
  1040. __finally
  1041. {
  1042. DeleteUnnecessary();
  1043. EndUpdate();
  1044. }
  1045. }
  1046. //---------------------------------------------------------------------------
  1047. void __fastcall TSessionLog::AddSeparator()
  1048. {
  1049. Add(llMessage, L"--------------------------------------------------------------------------");
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. int __fastcall TSessionLog::GetBottomIndex()
  1053. {
  1054. return (Count > 0 ? (TopIndex + Count - 1) : -1);
  1055. }
  1056. //---------------------------------------------------------------------------
  1057. bool __fastcall TSessionLog::GetLoggingToFile()
  1058. {
  1059. assert((FFile == NULL) || LogToFile());
  1060. return (FFile != NULL);
  1061. }
  1062. //---------------------------------------------------------------------------
  1063. void __fastcall TSessionLog::Clear()
  1064. {
  1065. TGuard Guard(FCriticalSection);
  1066. FTopIndex += Count;
  1067. TStringList::Clear();
  1068. }
  1069. //---------------------------------------------------------------------------
  1070. //---------------------------------------------------------------------------
  1071. __fastcall TActionLog::TActionLog(TSessionUI* UI, TSessionData * SessionData,
  1072. TConfiguration * Configuration)
  1073. {
  1074. FCriticalSection = new TCriticalSection;
  1075. FConfiguration = Configuration;
  1076. FUI = UI;
  1077. FSessionData = SessionData;
  1078. FFile = NULL;
  1079. FCurrentLogFileName = L"";
  1080. FCurrentFileName = L"";
  1081. FLogging = false;
  1082. FClosed = false;
  1083. FPendingActions = new TList();
  1084. FIndent = L" ";
  1085. FInGroup = false;
  1086. FEnabled = true;
  1087. }
  1088. //---------------------------------------------------------------------------
  1089. __fastcall TActionLog::~TActionLog()
  1090. {
  1091. assert(FPendingActions->Count == 0);
  1092. delete FPendingActions;
  1093. FClosed = true;
  1094. ReflectSettings();
  1095. assert(FFile == NULL);
  1096. delete FCriticalSection;
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. void __fastcall TActionLog::Add(const UnicodeString & Line)
  1100. {
  1101. assert(FConfiguration);
  1102. if (FLogging)
  1103. {
  1104. try
  1105. {
  1106. TGuard Guard(FCriticalSection);
  1107. if (FFile == NULL)
  1108. {
  1109. OpenLogFile();
  1110. }
  1111. if (FFile != NULL)
  1112. {
  1113. UTF8String UtfLine = UTF8String(Line);
  1114. fwrite(UtfLine.c_str(), 1, UtfLine.Length(), (FILE *)FFile);
  1115. fwrite("\n", 1, 1, (FILE *)FFile);
  1116. }
  1117. }
  1118. catch (Exception &E)
  1119. {
  1120. // We failed logging, turn it off and notify user.
  1121. FConfiguration->LogActions = false;
  1122. try
  1123. {
  1124. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  1125. }
  1126. catch (Exception &E)
  1127. {
  1128. FUI->HandleExtendedException(&E);
  1129. }
  1130. }
  1131. }
  1132. }
  1133. //---------------------------------------------------------------------------
  1134. void __fastcall TActionLog::AddIndented(const UnicodeString & Line)
  1135. {
  1136. Add(FIndent + Line);
  1137. }
  1138. //---------------------------------------------------------------------------
  1139. void __fastcall TActionLog::AddFailure(TStrings * Messages)
  1140. {
  1141. AddIndented(L"<failure>");
  1142. AddMessages(L" ", Messages);
  1143. AddIndented(L"</failure>");
  1144. }
  1145. //---------------------------------------------------------------------------
  1146. void __fastcall TActionLog::AddFailure(Exception * E)
  1147. {
  1148. TStrings * Messages = ExceptionToMoreMessages(E);
  1149. if (Messages != NULL)
  1150. {
  1151. try
  1152. {
  1153. AddFailure(Messages);
  1154. }
  1155. __finally
  1156. {
  1157. delete Messages;
  1158. }
  1159. }
  1160. }
  1161. //---------------------------------------------------------------------------
  1162. void __fastcall TActionLog::AddMessages(UnicodeString Indent, TStrings * Messages)
  1163. {
  1164. for (int Index = 0; Index < Messages->Count; Index++)
  1165. {
  1166. AddIndented(
  1167. FORMAT(Indent + L"<message>%s</message>", (XmlEscape(Messages->Strings[Index]))));
  1168. }
  1169. }
  1170. //---------------------------------------------------------------------------
  1171. void __fastcall TActionLog::ReflectSettings()
  1172. {
  1173. TGuard Guard(FCriticalSection);
  1174. bool ALogging =
  1175. !FClosed && FConfiguration->LogActions && Enabled;
  1176. if (ALogging && !FLogging)
  1177. {
  1178. FLogging = true;
  1179. Add(L"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  1180. Add(FORMAT(L"<session xmlns=\"http://winscp.net/schema/session/1.0\" name=\"%s\" start=\"%s\">",
  1181. (XmlAttributeEscape(FSessionData->SessionName), StandardTimestamp())));
  1182. }
  1183. else if (!ALogging && FLogging)
  1184. {
  1185. if (FInGroup)
  1186. {
  1187. EndGroup();
  1188. }
  1189. // do not try to close the file, if it has not been opened, to avoid recursion
  1190. if (FFile != NULL)
  1191. {
  1192. Add(L"</session>");
  1193. }
  1194. CloseLogFile();
  1195. FLogging = false;
  1196. }
  1197. }
  1198. //---------------------------------------------------------------------------
  1199. void __fastcall TActionLog::CloseLogFile()
  1200. {
  1201. if (FFile != NULL)
  1202. {
  1203. fclose((FILE *)FFile);
  1204. FFile = NULL;
  1205. }
  1206. FCurrentLogFileName = L"";
  1207. FCurrentFileName = L"";
  1208. }
  1209. //---------------------------------------------------------------------------
  1210. void __fastcall TActionLog::OpenLogFile()
  1211. {
  1212. try
  1213. {
  1214. assert(FFile == NULL);
  1215. assert(FConfiguration != NULL);
  1216. FCurrentLogFileName = FConfiguration->ActionsLogFileName;
  1217. FFile = OpenFile(FCurrentLogFileName, FSessionData, false, FCurrentFileName);
  1218. }
  1219. catch (Exception & E)
  1220. {
  1221. // We failed logging to file, turn it off and notify user.
  1222. FCurrentLogFileName = L"";
  1223. FCurrentFileName = L"";
  1224. FConfiguration->LogActions = false;
  1225. try
  1226. {
  1227. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  1228. }
  1229. catch (Exception & E)
  1230. {
  1231. FUI->HandleExtendedException(&E);
  1232. }
  1233. }
  1234. }
  1235. //---------------------------------------------------------------------------
  1236. void __fastcall TActionLog::AddPendingAction(TSessionActionRecord * Action)
  1237. {
  1238. FPendingActions->Add(Action);
  1239. }
  1240. //---------------------------------------------------------------------------
  1241. void __fastcall TActionLog::RecordPendingActions()
  1242. {
  1243. while ((FPendingActions->Count > 0) &&
  1244. static_cast<TSessionActionRecord *>(FPendingActions->Items[0])->Record())
  1245. {
  1246. FPendingActions->Delete(0);
  1247. }
  1248. }
  1249. //---------------------------------------------------------------------------
  1250. void __fastcall TActionLog::BeginGroup(UnicodeString Name)
  1251. {
  1252. assert(!FInGroup);
  1253. FInGroup = true;
  1254. assert(FIndent == L" ");
  1255. AddIndented(FORMAT("<group name=\"%s\" start=\"%s\">",
  1256. (XmlAttributeEscape(Name), StandardTimestamp())));
  1257. FIndent = L" ";
  1258. }
  1259. //---------------------------------------------------------------------------
  1260. void __fastcall TActionLog::EndGroup()
  1261. {
  1262. assert(FInGroup);
  1263. FInGroup = false;
  1264. assert(FIndent == L" ");
  1265. FIndent = L" ";
  1266. // this is called from ReflectSettings that in turn is called when logging fails,
  1267. // so do not try to close the group, if it has not been opened, to avoid recursion
  1268. if (FFile != NULL)
  1269. {
  1270. AddIndented("</group>");
  1271. }
  1272. }
  1273. //---------------------------------------------------------------------------
  1274. void __fastcall TActionLog::SetEnabled(bool value)
  1275. {
  1276. if (Enabled != value)
  1277. {
  1278. FEnabled = value;
  1279. ReflectSettings();
  1280. }
  1281. }