SessionInfo.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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, 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 ADF(S, F) DoAdd(llMessage, FORMAT(S, F), DoAddToSelf);
  854. if (Data == NULL)
  855. {
  856. AddSeparator();
  857. ADF(L"WinSCP %s (OS %s)", (FConfiguration->VersionStr, FConfiguration->OSVersionStr));
  858. THierarchicalStorage * Storage = FConfiguration->CreateScpStorage(false);
  859. try
  860. {
  861. ADF(L"Configuration: %s", (Storage->Source));
  862. }
  863. __finally
  864. {
  865. delete Storage;
  866. }
  867. typedef BOOL WINAPI (* TGetUserNameEx)(EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, PULONG nSize);
  868. HINSTANCE Secur32 = LoadLibrary(L"secur32.dll");
  869. TGetUserNameEx GetUserNameEx =
  870. (Secur32 != NULL) ? (TGetUserNameEx)GetProcAddress(Secur32, "GetUserNameExW") : NULL;
  871. wchar_t UserName[UNLEN + 1];
  872. unsigned long UserNameSize = LENOF(UserName);
  873. if ((GetUserNameEx == NULL) || !GetUserNameEx(NameSamCompatible, UserName, &UserNameSize))
  874. {
  875. wcscpy(UserName, L"<Failed to retrieve username>");
  876. }
  877. ADF(L"Local account: %s", (UserName));
  878. ADF(L"Working directory: %s", (GetCurrentDir()));
  879. ADF(L"Process ID: %d", (int(GetCurrentProcessId())));
  880. ADF(L"Command-line: %s", (CmdLine));
  881. ADF(L"Time zone: %s", (GetTimeZoneLogString()));
  882. ADF(L"Login time: %s", (FormatDateTime(L"dddddd tt", Now())));
  883. AddSeparator();
  884. }
  885. else
  886. {
  887. ADF(L"Session name: %s (%s)", (Data->SessionName, Data->Source));
  888. ADF(L"Host name: %s (Port: %d)", (Data->HostNameExpanded, Data->PortNumber));
  889. ADF(L"User name: %s (Password: %s, Key file: %s)",
  890. (Data->UserNameExpanded, BooleanToEngStr(!Data->Password.IsEmpty()),
  891. BooleanToEngStr(!Data->PublicKeyFile.IsEmpty())))
  892. ADF(L"Tunnel: %s", (BooleanToEngStr(Data->Tunnel)));
  893. if (Data->Tunnel)
  894. {
  895. ADF(L"Tunnel: Host name: %s (Port: %d)", (Data->TunnelHostName, Data->TunnelPortNumber));
  896. ADF(L"Tunnel: User name: %s (Password: %s, Key file: %s)",
  897. (Data->TunnelUserName, BooleanToEngStr(!Data->TunnelPassword.IsEmpty()),
  898. BooleanToEngStr(!Data->TunnelPublicKeyFile.IsEmpty())))
  899. ADF(L"Tunnel: Local port number: %d", (Data->TunnelLocalPortNumber));
  900. }
  901. ADF(L"Transfer Protocol: %s", (Data->FSProtocolStr));
  902. wchar_t * PingTypes = L"-NC";
  903. TPingType PingType;
  904. int PingInterval;
  905. if (Data->FSProtocol == fsFTP)
  906. {
  907. PingType = Data->FtpPingType;
  908. PingInterval = Data->FtpPingInterval;
  909. }
  910. else
  911. {
  912. PingType = Data->PingType;
  913. PingInterval = Data->PingInterval;
  914. }
  915. ADF(L"Ping type: %s, Ping interval: %d sec; Timeout: %d sec",
  916. (UnicodeString(PingTypes[PingType]), PingInterval, Data->Timeout));
  917. ADF(L"Proxy: %s", (ProxyMethodList[Data->ProxyMethod]));
  918. if (Data->ProxyMethod != ::pmNone)
  919. {
  920. ADF(L"HostName: %s (Port: %d); Username: %s; Passwd: %s",
  921. (Data->ProxyHost, Data->ProxyPort,
  922. Data->ProxyUsername, BooleanToEngStr(!Data->ProxyPassword.IsEmpty())));
  923. if (Data->ProxyMethod == pmTelnet)
  924. {
  925. ADF(L"Telnet command: %s", (Data->ProxyTelnetCommand));
  926. }
  927. if (Data->ProxyMethod == pmCmd)
  928. {
  929. ADF(L"Local command: %s", (Data->ProxyLocalCommand));
  930. }
  931. }
  932. wchar_t const * BugFlags = L"+-A";
  933. if (Data->UsesSsh)
  934. {
  935. ADF(L"SSH protocol version: %s; Compression: %s",
  936. (Data->SshProtStr, BooleanToEngStr(Data->Compression)));
  937. ADF(L"Bypass authentication: %s",
  938. (BooleanToEngStr(Data->SshNoUserAuth)));
  939. ADF(L"Try agent: %s; Agent forwarding: %s; TIS/CryptoCard: %s; KI: %s; GSSAPI: %s",
  940. (BooleanToEngStr(Data->TryAgent), BooleanToEngStr(Data->AgentFwd), BooleanToEngStr(Data->AuthTIS),
  941. BooleanToEngStr(Data->AuthKI), BooleanToEngStr(Data->AuthGSSAPI)));
  942. if (Data->AuthGSSAPI)
  943. {
  944. ADF(L"GSSAPI: Forwarding: %s; Server realm: %s",
  945. (BooleanToEngStr(Data->GSSAPIFwdTGT), Data->GSSAPIServerRealm));
  946. }
  947. ADF(L"Ciphers: %s; Ssh2DES: %s",
  948. (Data->CipherList, BooleanToEngStr(Data->Ssh2DES)));
  949. UnicodeString Bugs;
  950. for (int Index = 0; Index < BUG_COUNT; Index++)
  951. {
  952. Bugs += UnicodeString(BugFlags[Data->Bug[(TSshBug)Index]])+(Index<BUG_COUNT-1?L",":L"");
  953. }
  954. ADF(L"SSH Bugs: %s", (Bugs));
  955. ADF(L"Return code variable: %s; Lookup user groups: %s",
  956. ((Data->DetectReturnVar ? UnicodeString(L"Autodetect") : Data->ReturnVar),
  957. BugFlags[Data->LookupUserGroups]));
  958. ADF(L"Shell: %s", ((Data->Shell.IsEmpty()? UnicodeString(L"default") : Data->Shell)));
  959. ADF(L"EOL: %d, UTF: %d", (Data->EOLType, Data->NotUtf));
  960. ADF(L"Clear aliases: %s, Unset nat.vars: %s, Resolve symlinks: %s",
  961. (BooleanToEngStr(Data->ClearAliases), BooleanToEngStr(Data->UnsetNationalVars),
  962. BooleanToEngStr(Data->ResolveSymlinks)));
  963. ADF(L"LS: %s, Ign LS warn: %s, Scp1 Comp: %s",
  964. (Data->ListingCommand,
  965. BooleanToEngStr(Data->IgnoreLsWarnings),
  966. BooleanToEngStr(Data->Scp1Compatibility)));
  967. }
  968. if (Data->FSProtocol == fsSFTP)
  969. {
  970. UnicodeString Bugs;
  971. for (int Index = 0; Index < SFTP_BUG_COUNT; Index++)
  972. {
  973. Bugs += UnicodeString(BugFlags[Data->SFTPBug[(TSftpBug)Index]])+(Index<SFTP_BUG_COUNT-1?L",":L"");
  974. }
  975. ADF(L"SFTP Bugs: %s", (Bugs));
  976. ADF(L"SFTP Server: %s", ((Data->SftpServer.IsEmpty()? UnicodeString(L"default") : Data->SftpServer)));
  977. }
  978. if (Data->FSProtocol == fsFTP)
  979. {
  980. UnicodeString Ftps;
  981. switch (Data->Ftps)
  982. {
  983. case ftpsImplicit:
  984. Ftps = L"Implicit TLS/SSL";
  985. break;
  986. case ftpsExplicitSsl:
  987. Ftps = L"Explicit SSL";
  988. break;
  989. case ftpsExplicitTls:
  990. Ftps = L"Explicit TLS";
  991. break;
  992. default:
  993. assert(Data->Ftps == ftpsNone);
  994. Ftps = L"None";
  995. break;
  996. }
  997. ADF(L"FTP: FTPS: %s; Passive: %s [Force IP: %s]; MLSD: %s [List all: %s]",
  998. (Ftps, BooleanToEngStr(Data->FtpPasvMode),
  999. BugFlags[Data->FtpForcePasvIp],
  1000. BugFlags[Data->FtpUseMlsd],
  1001. BugFlags[Data->FtpListAll]));
  1002. if (Data->Ftps != ftpsNone)
  1003. {
  1004. ADF(L"Session reuse: %s", (BooleanToEngStr(Data->SslSessionReuse)));
  1005. ADF(L"TLS/SSL versions: %s-%s", (GetTlsVersionName(Data->MinTlsVersion), GetTlsVersionName(Data->MaxTlsVersion)));
  1006. }
  1007. }
  1008. ADF(L"Local directory: %s, Remote directory: %s, Update: %s, Cache: %s",
  1009. ((Data->LocalDirectory.IsEmpty() ? UnicodeString(L"default") : Data->LocalDirectory),
  1010. (Data->RemoteDirectory.IsEmpty() ? UnicodeString(L"home") : Data->RemoteDirectory),
  1011. BooleanToEngStr(Data->UpdateDirectories),
  1012. BooleanToEngStr(Data->CacheDirectories)));
  1013. ADF(L"Cache directory changes: %s, Permanent: %s",
  1014. (BooleanToEngStr(Data->CacheDirectoryChanges),
  1015. BooleanToEngStr(Data->PreserveDirectoryChanges)));
  1016. int TimeDifferenceMin = TimeToMinutes(Data->TimeDifference);
  1017. ADF(L"DST mode: %d; Timezone offset: %dh %dm", (int(Data->DSTMode), (TimeDifferenceMin / MinsPerHour), (TimeDifferenceMin % MinsPerHour)));
  1018. if (Data->FSProtocol == fsWebDAV)
  1019. {
  1020. ADF(L"Compression: %s",
  1021. (BooleanToEngStr(Data->Compression)));
  1022. }
  1023. AddSeparator();
  1024. }
  1025. #undef ADF
  1026. }
  1027. __finally
  1028. {
  1029. DeleteUnnecessary();
  1030. EndUpdate();
  1031. }
  1032. }
  1033. //---------------------------------------------------------------------------
  1034. void __fastcall TSessionLog::AddSeparator()
  1035. {
  1036. Add(llMessage, L"--------------------------------------------------------------------------");
  1037. }
  1038. //---------------------------------------------------------------------------
  1039. int __fastcall TSessionLog::GetBottomIndex()
  1040. {
  1041. return (Count > 0 ? (TopIndex + Count - 1) : -1);
  1042. }
  1043. //---------------------------------------------------------------------------
  1044. bool __fastcall TSessionLog::GetLoggingToFile()
  1045. {
  1046. assert((FFile == NULL) || LogToFile());
  1047. return (FFile != NULL);
  1048. }
  1049. //---------------------------------------------------------------------------
  1050. void __fastcall TSessionLog::Clear()
  1051. {
  1052. TGuard Guard(FCriticalSection);
  1053. FTopIndex += Count;
  1054. TStringList::Clear();
  1055. }
  1056. //---------------------------------------------------------------------------
  1057. //---------------------------------------------------------------------------
  1058. __fastcall TActionLog::TActionLog(TSessionUI* UI, TSessionData * SessionData,
  1059. TConfiguration * Configuration)
  1060. {
  1061. FCriticalSection = new TCriticalSection;
  1062. FConfiguration = Configuration;
  1063. FUI = UI;
  1064. FSessionData = SessionData;
  1065. FFile = NULL;
  1066. FCurrentLogFileName = L"";
  1067. FCurrentFileName = L"";
  1068. FLogging = false;
  1069. FClosed = false;
  1070. FPendingActions = new TList();
  1071. FIndent = L" ";
  1072. FInGroup = false;
  1073. FEnabled = true;
  1074. }
  1075. //---------------------------------------------------------------------------
  1076. __fastcall TActionLog::~TActionLog()
  1077. {
  1078. assert(FPendingActions->Count == 0);
  1079. delete FPendingActions;
  1080. FClosed = true;
  1081. ReflectSettings();
  1082. assert(FFile == NULL);
  1083. delete FCriticalSection;
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. void __fastcall TActionLog::Add(const UnicodeString & Line)
  1087. {
  1088. assert(FConfiguration);
  1089. if (FLogging)
  1090. {
  1091. try
  1092. {
  1093. TGuard Guard(FCriticalSection);
  1094. if (FFile == NULL)
  1095. {
  1096. OpenLogFile();
  1097. }
  1098. if (FFile != NULL)
  1099. {
  1100. UTF8String UtfLine = UTF8String(Line);
  1101. fwrite(UtfLine.c_str(), 1, UtfLine.Length(), (FILE *)FFile);
  1102. fwrite("\n", 1, 1, (FILE *)FFile);
  1103. }
  1104. }
  1105. catch (Exception &E)
  1106. {
  1107. // We failed logging, turn it off and notify user.
  1108. FConfiguration->LogActions = false;
  1109. try
  1110. {
  1111. throw ExtException(&E, LOG_GEN_ERROR);
  1112. }
  1113. catch (Exception &E)
  1114. {
  1115. FUI->HandleExtendedException(&E);
  1116. }
  1117. }
  1118. }
  1119. }
  1120. //---------------------------------------------------------------------------
  1121. void __fastcall TActionLog::AddIndented(const UnicodeString & Line)
  1122. {
  1123. Add(FIndent + Line);
  1124. }
  1125. //---------------------------------------------------------------------------
  1126. void __fastcall TActionLog::AddFailure(TStrings * Messages)
  1127. {
  1128. AddIndented(L"<failure>");
  1129. AddMessages(L" ", Messages);
  1130. AddIndented(L"</failure>");
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall TActionLog::AddFailure(Exception * E)
  1134. {
  1135. TStrings * Messages = ExceptionToMoreMessages(E);
  1136. if (Messages != NULL)
  1137. {
  1138. try
  1139. {
  1140. AddFailure(Messages);
  1141. }
  1142. __finally
  1143. {
  1144. delete Messages;
  1145. }
  1146. }
  1147. }
  1148. //---------------------------------------------------------------------------
  1149. void __fastcall TActionLog::AddMessages(UnicodeString Indent, TStrings * Messages)
  1150. {
  1151. for (int Index = 0; Index < Messages->Count; Index++)
  1152. {
  1153. AddIndented(
  1154. FORMAT(Indent + L"<message>%s</message>", (XmlEscape(Messages->Strings[Index]))));
  1155. }
  1156. }
  1157. //---------------------------------------------------------------------------
  1158. void __fastcall TActionLog::ReflectSettings()
  1159. {
  1160. TGuard Guard(FCriticalSection);
  1161. bool ALogging =
  1162. !FClosed && FConfiguration->LogActions && Enabled;
  1163. if (ALogging && !FLogging)
  1164. {
  1165. FLogging = true;
  1166. Add(L"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  1167. Add(FORMAT(L"<session xmlns=\"http://winscp.net/schema/session/1.0\" name=\"%s\" start=\"%s\">",
  1168. (XmlAttributeEscape(FSessionData->SessionName), StandardTimestamp())));
  1169. }
  1170. else if (!ALogging && FLogging)
  1171. {
  1172. if (FInGroup)
  1173. {
  1174. EndGroup();
  1175. }
  1176. // do not try to close the file, if it has not been opened, to avoid recursion
  1177. if (FFile != NULL)
  1178. {
  1179. Add(L"</session>");
  1180. }
  1181. CloseLogFile();
  1182. FLogging = false;
  1183. }
  1184. }
  1185. //---------------------------------------------------------------------------
  1186. void __fastcall TActionLog::CloseLogFile()
  1187. {
  1188. if (FFile != NULL)
  1189. {
  1190. fclose((FILE *)FFile);
  1191. FFile = NULL;
  1192. }
  1193. FCurrentLogFileName = L"";
  1194. FCurrentFileName = L"";
  1195. }
  1196. //---------------------------------------------------------------------------
  1197. void __fastcall TActionLog::OpenLogFile()
  1198. {
  1199. try
  1200. {
  1201. assert(FFile == NULL);
  1202. assert(FConfiguration != NULL);
  1203. FCurrentLogFileName = FConfiguration->ActionsLogFileName;
  1204. FFile = OpenFile(FCurrentLogFileName, FSessionData, false, FCurrentFileName);
  1205. }
  1206. catch (Exception & E)
  1207. {
  1208. // We failed logging to file, turn it off and notify user.
  1209. FCurrentLogFileName = L"";
  1210. FCurrentFileName = L"";
  1211. FConfiguration->LogActions = false;
  1212. try
  1213. {
  1214. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  1215. }
  1216. catch (Exception & E)
  1217. {
  1218. FUI->HandleExtendedException(&E);
  1219. }
  1220. }
  1221. }
  1222. //---------------------------------------------------------------------------
  1223. void __fastcall TActionLog::AddPendingAction(TSessionActionRecord * Action)
  1224. {
  1225. FPendingActions->Add(Action);
  1226. }
  1227. //---------------------------------------------------------------------------
  1228. void __fastcall TActionLog::RecordPendingActions()
  1229. {
  1230. while ((FPendingActions->Count > 0) &&
  1231. static_cast<TSessionActionRecord *>(FPendingActions->Items[0])->Record())
  1232. {
  1233. FPendingActions->Delete(0);
  1234. }
  1235. }
  1236. //---------------------------------------------------------------------------
  1237. void __fastcall TActionLog::BeginGroup(UnicodeString Name)
  1238. {
  1239. assert(!FInGroup);
  1240. FInGroup = true;
  1241. assert(FIndent == L" ");
  1242. AddIndented(FORMAT("<group name=\"%s\" start=\"%s\">",
  1243. (XmlAttributeEscape(Name), StandardTimestamp())));
  1244. FIndent = L" ";
  1245. }
  1246. //---------------------------------------------------------------------------
  1247. void __fastcall TActionLog::EndGroup()
  1248. {
  1249. assert(FInGroup);
  1250. FInGroup = false;
  1251. assert(FIndent == L" ");
  1252. FIndent = L" ";
  1253. // this is called from ReflectSettings that in turn is called when logging fails,
  1254. // so do not try to close the group, if it has not been opened, to avoid recursion
  1255. if (FFile != NULL)
  1256. {
  1257. AddIndented("</group>");
  1258. }
  1259. }
  1260. //---------------------------------------------------------------------------
  1261. void __fastcall TActionLog::SetEnabled(bool value)
  1262. {
  1263. if (Enabled != value)
  1264. {
  1265. FEnabled = value;
  1266. ReflectSettings();
  1267. }
  1268. }