SessionInfo.cpp 42 KB

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