SessionInfo.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  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. #include "CoreMain.h"
  14. #include "Script.h"
  15. #include <System.IOUtils.hpp>
  16. //---------------------------------------------------------------------------
  17. #pragma package(smart_init)
  18. //---------------------------------------------------------------------------
  19. UnicodeString __fastcall DoXmlEscape(UnicodeString Str, bool NewLine)
  20. {
  21. for (int i = 1; i <= Str.Length(); i++)
  22. {
  23. UnicodeString Repl;
  24. switch (Str[i])
  25. {
  26. case L'\x00': // \0 Is not valid in XML anyway
  27. case L'\x01':
  28. case L'\x02':
  29. case L'\x03':
  30. case L'\x04':
  31. case L'\x05':
  32. case L'\x06':
  33. case L'\x07':
  34. case L'\x08':
  35. // \n is handled below
  36. case L'\x0B':
  37. case L'\x0C':
  38. // \r is handled below
  39. case L'\x0E':
  40. case L'\x0F':
  41. case L'\x10':
  42. case L'\x11':
  43. case L'\x12':
  44. case L'\x13':
  45. case L'\x14':
  46. case L'\x15':
  47. case L'\x16':
  48. case L'\x17':
  49. case L'\x18':
  50. case L'\x19':
  51. case L'\x1A':
  52. case L'\x1B':
  53. case L'\x1C':
  54. case L'\x1D':
  55. case L'\x1E':
  56. case L'\x1F':
  57. Repl = L"#x" + ByteToHex((unsigned char)Str[i]) + L";";
  58. break;
  59. case L'&':
  60. Repl = L"amp;";
  61. break;
  62. case L'>':
  63. Repl = L"gt;";
  64. break;
  65. case L'<':
  66. Repl = L"lt;";
  67. break;
  68. case L'"':
  69. Repl = L"quot;";
  70. break;
  71. case L'\n':
  72. if (NewLine)
  73. {
  74. Repl = L"#10;";
  75. }
  76. break;
  77. case L'\r':
  78. Str.Delete(i, 1);
  79. i--;
  80. break;
  81. }
  82. if (!Repl.IsEmpty())
  83. {
  84. Str[i] = L'&';
  85. Str.Insert(Repl, i + 1);
  86. i += Repl.Length();
  87. }
  88. }
  89. return Str;
  90. }
  91. //---------------------------------------------------------------------------
  92. UnicodeString __fastcall XmlEscape(UnicodeString Str)
  93. {
  94. return DoXmlEscape(Str, false);
  95. }
  96. //---------------------------------------------------------------------------
  97. UnicodeString __fastcall XmlAttributeEscape(UnicodeString Str)
  98. {
  99. return DoXmlEscape(Str, true);
  100. }
  101. //---------------------------------------------------------------------------
  102. //---------------------------------------------------------------------------
  103. #pragma warn -inl
  104. class TSessionActionRecord
  105. {
  106. public:
  107. __fastcall TSessionActionRecord(TActionLog * Log, TLogAction Action) :
  108. FLog(Log),
  109. FAction(Action),
  110. FState(Opened),
  111. FRecursive(false),
  112. FErrorMessages(NULL),
  113. FNames(new TStringList()),
  114. FValues(new TStringList()),
  115. FFileList(NULL),
  116. FFile(NULL)
  117. {
  118. FLog->AddPendingAction(this);
  119. }
  120. __fastcall ~TSessionActionRecord()
  121. {
  122. delete FErrorMessages;
  123. delete FNames;
  124. delete FValues;
  125. delete FFileList;
  126. delete FFile;
  127. }
  128. void __fastcall Restart()
  129. {
  130. FState = Opened;
  131. FRecursive = false;
  132. delete FErrorMessages;
  133. FErrorMessages = NULL;
  134. delete FFileList;
  135. FFileList = NULL;
  136. delete FFile;
  137. FFile = NULL;
  138. FNames->Clear();
  139. FValues->Clear();
  140. }
  141. bool __fastcall Record()
  142. {
  143. bool Result = (FState != Opened);
  144. if (Result)
  145. {
  146. if (FLog->FLogging && (FState != Cancelled))
  147. {
  148. const wchar_t * Name = ActionName();
  149. UnicodeString Attrs;
  150. if (FRecursive)
  151. {
  152. Attrs = L" recursive=\"true\"";
  153. }
  154. FLog->AddIndented(FORMAT(L"<%s%s>", (Name, Attrs)));
  155. for (int Index = 0; Index < FNames->Count; Index++)
  156. {
  157. UnicodeString Value = FValues->Strings[Index];
  158. if (Value.IsEmpty())
  159. {
  160. FLog->AddIndented(FORMAT(L" <%s />", (FNames->Strings[Index])));
  161. }
  162. else
  163. {
  164. FLog->AddIndented(FORMAT(L" <%s value=\"%s\" />",
  165. (FNames->Strings[Index], XmlAttributeEscape(Value))));
  166. }
  167. }
  168. if (FFileList != NULL)
  169. {
  170. FLog->AddIndented(L" <files>");
  171. for (int Index = 0; Index < FFileList->Count; Index++)
  172. {
  173. TRemoteFile * File = FFileList->Files[Index];
  174. RecordFile(L" ", File, true);
  175. }
  176. FLog->AddIndented(L" </files>");
  177. }
  178. if (FFile != NULL)
  179. {
  180. RecordFile(L" ", FFile, false);
  181. }
  182. if (FState == RolledBack)
  183. {
  184. if (FErrorMessages != NULL)
  185. {
  186. FLog->AddIndented(L" <result success=\"false\">");
  187. FLog->AddMessages(L" ", FErrorMessages);
  188. FLog->AddIndented(L" </result>");
  189. }
  190. else
  191. {
  192. FLog->AddIndented(L" <result success=\"false\" />");
  193. }
  194. }
  195. else
  196. {
  197. FLog->AddIndented(L" <result success=\"true\" />");
  198. }
  199. FLog->AddIndented(FORMAT(L"</%s>", (Name)));
  200. }
  201. delete this;
  202. }
  203. return Result;
  204. }
  205. void __fastcall Commit()
  206. {
  207. Close(Committed);
  208. }
  209. void __fastcall Rollback(Exception * E)
  210. {
  211. DebugAssert(FErrorMessages == NULL);
  212. FErrorMessages = ExceptionToMoreMessages(E);
  213. Close(RolledBack);
  214. }
  215. void __fastcall Cancel()
  216. {
  217. Close(Cancelled);
  218. }
  219. void __fastcall FileName(const UnicodeString & FileName)
  220. {
  221. Parameter(L"filename", FileName);
  222. }
  223. void __fastcall Destination(const UnicodeString & Destination)
  224. {
  225. Parameter(L"destination", Destination);
  226. }
  227. void Size(__int64 Size)
  228. {
  229. Parameter(L"size", Size);
  230. }
  231. void __fastcall Rights(const TRights & Rights)
  232. {
  233. Parameter(L"permissions", Rights.Text);
  234. }
  235. void __fastcall Modification(const TDateTime & DateTime)
  236. {
  237. Parameter(L"modification", StandardTimestamp(DateTime));
  238. }
  239. void __fastcall Recursive()
  240. {
  241. FRecursive = true;
  242. }
  243. void __fastcall Command(const UnicodeString & Command)
  244. {
  245. Parameter(L"command", Command);
  246. }
  247. void __fastcall AddOutput(UnicodeString Output, bool StdError)
  248. {
  249. const wchar_t * Name = (StdError ? L"erroroutput" : L"output");
  250. int Index = FNames->IndexOf(Name);
  251. if (Index >= 0)
  252. {
  253. FValues->Strings[Index] = FValues->Strings[Index] + L"\r\n" + Output;
  254. }
  255. else
  256. {
  257. Parameter(Name, Output);
  258. }
  259. }
  260. void __fastcall ExitCode(int ExitCode)
  261. {
  262. Parameter(L"exitcode", IntToStr(ExitCode));
  263. }
  264. void __fastcall Checksum(const UnicodeString & Alg, const UnicodeString & Checksum)
  265. {
  266. Parameter(L"algorithm", Alg);
  267. Parameter(L"checksum", Checksum);
  268. }
  269. void __fastcall Cwd(const UnicodeString & Path)
  270. {
  271. Parameter(L"cwd", Path);
  272. }
  273. void __fastcall FileList(TRemoteFileList * FileList)
  274. {
  275. if (FFileList == NULL)
  276. {
  277. FFileList = new TRemoteFileList();
  278. }
  279. FileList->DuplicateTo(FFileList);
  280. }
  281. void __fastcall File(TRemoteFile * File)
  282. {
  283. if (FFile != NULL)
  284. {
  285. delete FFile;
  286. }
  287. FFile = File->Duplicate(true);
  288. }
  289. void __fastcall SynchronizeChecklistItem(const TSynchronizeChecklist::TItem * Item)
  290. {
  291. UnicodeString Action;
  292. bool RecordLocal = false;
  293. bool RecordRemote = false;
  294. switch (Item->Action)
  295. {
  296. case TSynchronizeChecklist::saUploadNew:
  297. Action = L"uploadnew";
  298. RecordLocal = true;
  299. break;
  300. case TSynchronizeChecklist::saDownloadNew:
  301. Action = L"downloadnew";
  302. RecordRemote = true;
  303. break;
  304. case TSynchronizeChecklist::saUploadUpdate:
  305. Action = L"uploadupdate";
  306. RecordLocal = true;
  307. RecordRemote = true;
  308. break;
  309. case TSynchronizeChecklist::saDownloadUpdate:
  310. Action = L"downloadupdate";
  311. RecordLocal = true;
  312. RecordRemote = true;
  313. break;
  314. case TSynchronizeChecklist::saDeleteRemote:
  315. Action = L"deleteremote";
  316. RecordRemote = true;
  317. break;
  318. case TSynchronizeChecklist::saDeleteLocal:
  319. Action = L"deletelocal";
  320. RecordLocal = true;
  321. break;
  322. default:
  323. DebugFail();
  324. break;
  325. }
  326. Parameter(L"action", Action);
  327. if (RecordLocal)
  328. {
  329. UnicodeString FileName = CombinePaths(Item->Local.Directory, Item->Local.FileName);
  330. SynchronizeChecklistItemFileInfo(FileName, Item->IsDirectory, Item->Local);
  331. }
  332. if (RecordRemote)
  333. {
  334. UnicodeString FileName = UnixCombinePaths(Item->Remote.Directory, Item->Remote.FileName);
  335. SynchronizeChecklistItemFileInfo(FileName, Item->IsDirectory, Item->Remote);
  336. }
  337. }
  338. protected:
  339. enum TState { Opened, Committed, RolledBack, Cancelled };
  340. inline void __fastcall Close(TState State)
  341. {
  342. DebugAssert(FState == Opened);
  343. FState = State;
  344. FLog->RecordPendingActions();
  345. }
  346. const wchar_t * __fastcall ActionName()
  347. {
  348. switch (FAction)
  349. {
  350. case laUpload: return L"upload";
  351. case laDownload: return L"download";
  352. case laTouch: return L"touch";
  353. case laChmod: return L"chmod";
  354. case laMkdir: return L"mkdir";
  355. case laRm: return L"rm";
  356. case laMv: return L"mv";
  357. case laCp: return L"cp";
  358. case laCall: return L"call";
  359. case laLs: return L"ls";
  360. case laStat: return L"stat";
  361. case laChecksum: return L"checksum";
  362. case laCwd: return L"cwd";
  363. case laDifference: return L"difference";
  364. default: DebugFail(); return L"";
  365. }
  366. }
  367. void __fastcall Parameter(const UnicodeString & Name, const UnicodeString & Value = L"")
  368. {
  369. FNames->Add(Name);
  370. FValues->Add(Value);
  371. }
  372. void __fastcall RecordFile(const UnicodeString & Indent, TRemoteFile * File, bool IncludeFileName)
  373. {
  374. FLog->AddIndented(Indent + L"<file>");
  375. FLog->AddIndented(Indent + FORMAT(L" <filename value=\"%s\" />", (XmlAttributeEscape(File->FileName))));
  376. FLog->AddIndented(Indent + FORMAT(L" <type value=\"%s\" />", (XmlAttributeEscape(towupper(File->Type)))));
  377. if (!File->IsDirectory)
  378. {
  379. FLog->AddIndented(Indent + FORMAT(L" <size value=\"%s\" />", (IntToStr(File->Size))));
  380. }
  381. if (File->ModificationFmt != mfNone)
  382. {
  383. FLog->AddIndented(Indent + FORMAT(L" <modification value=\"%s\" />", (StandardTimestamp(File->Modification))));
  384. }
  385. if (!File->Rights->Unknown)
  386. {
  387. FLog->AddIndented(Indent + FORMAT(L" <permissions value=\"%s\" />", (XmlAttributeEscape(File->Rights->Text))));
  388. }
  389. if (File->Owner.IsSet)
  390. {
  391. FLog->AddIndented(Indent + FORMAT(L" <owner value=\"%s\" />", (XmlAttributeEscape(File->Owner.DisplayText))));
  392. }
  393. if (File->Group.IsSet)
  394. {
  395. FLog->AddIndented(Indent + FORMAT(L" <group value=\"%s\" />", (XmlAttributeEscape(File->Group.DisplayText))));
  396. }
  397. FLog->AddIndented(Indent + L"</file>");
  398. }
  399. void __fastcall SynchronizeChecklistItemFileInfo(
  400. const UnicodeString & AFileName, bool IsDirectory, const TSynchronizeChecklist::TItem::TFileInfo FileInfo)
  401. {
  402. Parameter(L"type", (IsDirectory ? L'D' : L'-'));
  403. FileName(AFileName);
  404. if (!IsDirectory)
  405. {
  406. Parameter(L"size", IntToStr(FileInfo.Size));
  407. }
  408. if (FileInfo.ModificationFmt != mfNone)
  409. {
  410. Modification(FileInfo.Modification);
  411. }
  412. }
  413. private:
  414. TActionLog * FLog;
  415. TLogAction FAction;
  416. TState FState;
  417. bool FRecursive;
  418. TStrings * FErrorMessages;
  419. TStrings * FNames;
  420. TStrings * FValues;
  421. TRemoteFileList * FFileList;
  422. TRemoteFile * FFile;
  423. };
  424. #pragma warn .inl
  425. //---------------------------------------------------------------------------
  426. //---------------------------------------------------------------------------
  427. __fastcall TSessionAction::TSessionAction(TActionLog * Log, TLogAction Action)
  428. {
  429. FCancelled = false;
  430. if (Log->FLogging)
  431. {
  432. FRecord = new TSessionActionRecord(Log, Action);
  433. }
  434. else
  435. {
  436. FRecord = NULL;
  437. }
  438. }
  439. //---------------------------------------------------------------------------
  440. __fastcall TSessionAction::~TSessionAction()
  441. {
  442. if (FRecord != NULL)
  443. {
  444. TSessionActionRecord * Record = FRecord;
  445. FRecord = NULL;
  446. Record->Commit();
  447. }
  448. }
  449. //---------------------------------------------------------------------------
  450. void __fastcall TSessionAction::Restart()
  451. {
  452. if (FRecord != NULL)
  453. {
  454. FRecord->Restart();
  455. }
  456. }
  457. //---------------------------------------------------------------------------
  458. void __fastcall TSessionAction::Rollback(Exception * E)
  459. {
  460. if (FRecord != NULL)
  461. {
  462. TSessionActionRecord * Record = FRecord;
  463. FRecord = NULL;
  464. Record->Rollback(E);
  465. }
  466. }
  467. //---------------------------------------------------------------------------
  468. void __fastcall TSessionAction::Cancel()
  469. {
  470. if (FRecord != NULL)
  471. {
  472. TSessionActionRecord * Record = FRecord;
  473. FRecord = NULL;
  474. FCancelled = true;
  475. Record->Cancel();
  476. }
  477. }
  478. //---------------------------------------------------------------------------
  479. bool __fastcall TSessionAction::IsValid()
  480. {
  481. return !FCancelled;
  482. }
  483. //---------------------------------------------------------------------------
  484. //---------------------------------------------------------------------------
  485. __fastcall TFileSessionAction::TFileSessionAction(TActionLog * Log, TLogAction Action) :
  486. TSessionAction(Log, Action)
  487. {
  488. }
  489. //---------------------------------------------------------------------------
  490. __fastcall TFileSessionAction::TFileSessionAction(
  491. TActionLog * Log, TLogAction Action, const UnicodeString & AFileName) :
  492. TSessionAction(Log, Action)
  493. {
  494. FileName(AFileName);
  495. }
  496. //---------------------------------------------------------------------------
  497. void __fastcall TFileSessionAction::FileName(const UnicodeString & FileName)
  498. {
  499. if (FRecord != NULL)
  500. {
  501. FRecord->FileName(FileName);
  502. }
  503. }
  504. //---------------------------------------------------------------------------
  505. //---------------------------------------------------------------------------
  506. __fastcall TFileLocationSessionAction::TFileLocationSessionAction(
  507. TActionLog * Log, TLogAction Action) :
  508. TFileSessionAction(Log, Action)
  509. {
  510. }
  511. //---------------------------------------------------------------------------
  512. __fastcall TFileLocationSessionAction::TFileLocationSessionAction(
  513. TActionLog * Log, TLogAction Action, const UnicodeString & FileName) :
  514. TFileSessionAction(Log, Action, FileName)
  515. {
  516. }
  517. //---------------------------------------------------------------------------
  518. void __fastcall TFileLocationSessionAction::Destination(const UnicodeString & Destination)
  519. {
  520. if (FRecord != NULL)
  521. {
  522. FRecord->Destination(Destination);
  523. }
  524. }
  525. //---------------------------------------------------------------------------
  526. //---------------------------------------------------------------------------
  527. TTransferSessionAction::TTransferSessionAction(TActionLog * Log, TLogAction Action) :
  528. TFileLocationSessionAction(Log, Action)
  529. {
  530. }
  531. //---------------------------------------------------------------------------
  532. void TTransferSessionAction::Size(__int64 Size)
  533. {
  534. if (FRecord != NULL)
  535. {
  536. FRecord->Size(Size);
  537. }
  538. }
  539. //---------------------------------------------------------------------------
  540. //---------------------------------------------------------------------------
  541. __fastcall TUploadSessionAction::TUploadSessionAction(TActionLog * Log) :
  542. TTransferSessionAction(Log, laUpload)
  543. {
  544. }
  545. //---------------------------------------------------------------------------
  546. //---------------------------------------------------------------------------
  547. __fastcall TDownloadSessionAction::TDownloadSessionAction(TActionLog * Log) :
  548. TTransferSessionAction(Log, laDownload)
  549. {
  550. }
  551. //---------------------------------------------------------------------------
  552. //---------------------------------------------------------------------------
  553. __fastcall TChmodSessionAction::TChmodSessionAction(
  554. TActionLog * Log, const UnicodeString & FileName) :
  555. TFileSessionAction(Log, laChmod, FileName)
  556. {
  557. }
  558. //---------------------------------------------------------------------------
  559. void __fastcall TChmodSessionAction::Recursive()
  560. {
  561. if (FRecord != NULL)
  562. {
  563. FRecord->Recursive();
  564. }
  565. }
  566. //---------------------------------------------------------------------------
  567. __fastcall TChmodSessionAction::TChmodSessionAction(
  568. TActionLog * Log, const UnicodeString & FileName, const TRights & ARights) :
  569. TFileSessionAction(Log, laChmod, FileName)
  570. {
  571. Rights(ARights);
  572. }
  573. //---------------------------------------------------------------------------
  574. void __fastcall TChmodSessionAction::Rights(const TRights & Rights)
  575. {
  576. if (FRecord != NULL)
  577. {
  578. FRecord->Rights(Rights);
  579. }
  580. }
  581. //---------------------------------------------------------------------------
  582. __fastcall TTouchSessionAction::TTouchSessionAction(
  583. TActionLog * Log, const UnicodeString & FileName, const TDateTime & Modification) :
  584. TFileSessionAction(Log, laTouch, FileName)
  585. {
  586. if (FRecord != NULL)
  587. {
  588. FRecord->Modification(Modification);
  589. }
  590. }
  591. //---------------------------------------------------------------------------
  592. __fastcall TMkdirSessionAction::TMkdirSessionAction(
  593. TActionLog * Log, const UnicodeString & FileName) :
  594. TFileSessionAction(Log, laMkdir, FileName)
  595. {
  596. }
  597. //---------------------------------------------------------------------------
  598. __fastcall TRmSessionAction::TRmSessionAction(
  599. TActionLog * Log, const UnicodeString & FileName) :
  600. TFileSessionAction(Log, laRm, FileName)
  601. {
  602. }
  603. //---------------------------------------------------------------------------
  604. void __fastcall TRmSessionAction::Recursive()
  605. {
  606. if (FRecord != NULL)
  607. {
  608. FRecord->Recursive();
  609. }
  610. }
  611. //---------------------------------------------------------------------------
  612. __fastcall TMvSessionAction::TMvSessionAction(TActionLog * Log,
  613. const UnicodeString & FileName, const UnicodeString & ADestination) :
  614. TFileLocationSessionAction(Log, laMv, FileName)
  615. {
  616. Destination(ADestination);
  617. }
  618. //---------------------------------------------------------------------------
  619. __fastcall TCpSessionAction::TCpSessionAction(TActionLog * Log,
  620. const UnicodeString & FileName, const UnicodeString & ADestination) :
  621. TFileLocationSessionAction(Log, laCp, FileName)
  622. {
  623. Destination(ADestination);
  624. }
  625. //---------------------------------------------------------------------------
  626. __fastcall TCallSessionAction::TCallSessionAction(TActionLog * Log,
  627. const UnicodeString & Command, const UnicodeString & Destination) :
  628. TSessionAction(Log, laCall)
  629. {
  630. if (FRecord != NULL)
  631. {
  632. FRecord->Command(Command);
  633. FRecord->Destination(Destination);
  634. }
  635. }
  636. //---------------------------------------------------------------------------
  637. void __fastcall TCallSessionAction::AddOutput(const UnicodeString & Output, bool StdError)
  638. {
  639. if (FRecord != NULL)
  640. {
  641. FRecord->AddOutput(Output, StdError);
  642. }
  643. }
  644. //---------------------------------------------------------------------------
  645. void __fastcall TCallSessionAction::ExitCode(int ExitCode)
  646. {
  647. if (FRecord != NULL)
  648. {
  649. FRecord->ExitCode(ExitCode);
  650. }
  651. }
  652. //---------------------------------------------------------------------------
  653. __fastcall TLsSessionAction::TLsSessionAction(TActionLog * Log,
  654. const UnicodeString & Destination) :
  655. TSessionAction(Log, laLs)
  656. {
  657. if (FRecord != NULL)
  658. {
  659. FRecord->Destination(Destination);
  660. }
  661. }
  662. //---------------------------------------------------------------------------
  663. void __fastcall TLsSessionAction::FileList(TRemoteFileList * FileList)
  664. {
  665. if (FRecord != NULL)
  666. {
  667. FRecord->FileList(FileList);
  668. }
  669. }
  670. //---------------------------------------------------------------------------
  671. //---------------------------------------------------------------------------
  672. __fastcall TStatSessionAction::TStatSessionAction(TActionLog * Log, const UnicodeString & FileName) :
  673. TFileSessionAction(Log, laStat, FileName)
  674. {
  675. }
  676. //---------------------------------------------------------------------------
  677. void __fastcall TStatSessionAction::File(TRemoteFile * File)
  678. {
  679. if (FRecord != NULL)
  680. {
  681. FRecord->File(File);
  682. }
  683. }
  684. //---------------------------------------------------------------------------
  685. //---------------------------------------------------------------------------
  686. __fastcall TChecksumSessionAction::TChecksumSessionAction(TActionLog * Log) :
  687. TFileSessionAction(Log, laChecksum)
  688. {
  689. }
  690. //---------------------------------------------------------------------------
  691. void __fastcall TChecksumSessionAction::Checksum(const UnicodeString & Alg, const UnicodeString & Checksum)
  692. {
  693. if (FRecord != NULL)
  694. {
  695. FRecord->Checksum(Alg, Checksum);
  696. }
  697. }
  698. //---------------------------------------------------------------------------
  699. //---------------------------------------------------------------------------
  700. __fastcall TCwdSessionAction::TCwdSessionAction(TActionLog * Log, const UnicodeString & Path) :
  701. TSessionAction(Log, laCwd)
  702. {
  703. if (FRecord != NULL)
  704. {
  705. FRecord->Cwd(Path);
  706. }
  707. }
  708. //---------------------------------------------------------------------------
  709. //---------------------------------------------------------------------------
  710. __fastcall TDifferenceSessionAction::TDifferenceSessionAction(TActionLog * Log, const TSynchronizeChecklist::TItem * Item) :
  711. TSessionAction(Log, laDifference)
  712. {
  713. if (FRecord != NULL)
  714. {
  715. FRecord->SynchronizeChecklistItem(Item);
  716. }
  717. }
  718. //---------------------------------------------------------------------------
  719. //---------------------------------------------------------------------------
  720. TSessionInfo::TSessionInfo()
  721. {
  722. LoginTime = Now();
  723. CertificateVerifiedManually = false;
  724. }
  725. //---------------------------------------------------------------------------
  726. TFileSystemInfo::TFileSystemInfo()
  727. {
  728. memset(&IsCapable, false, sizeof(IsCapable));
  729. }
  730. //---------------------------------------------------------------------------
  731. //---------------------------------------------------------------------------
  732. FILE * __fastcall OpenFile(UnicodeString LogFileName, TDateTime Started, TSessionData * SessionData, bool Append, UnicodeString & NewFileName)
  733. {
  734. FILE * Result;
  735. UnicodeString ANewFileName = GetExpandedLogFileName(LogFileName, Started, SessionData);
  736. Result = _wfopen(ApiPath(ANewFileName).c_str(), (Append ? L"ab" : L"wb"));
  737. if (Result != NULL)
  738. {
  739. setvbuf(Result, NULL, _IONBF, BUFSIZ);
  740. NewFileName = ANewFileName;
  741. }
  742. else
  743. {
  744. throw ECRTExtException(FMTLOAD(LOG_OPENERROR, (ANewFileName)));
  745. }
  746. return Result;
  747. }
  748. //---------------------------------------------------------------------------
  749. //---------------------------------------------------------------------------
  750. const wchar_t *LogLineMarks = L"<>!.*";
  751. __fastcall TSessionLog::TSessionLog(TSessionUI* UI, TDateTime Started, TSessionData * SessionData,
  752. TConfiguration * Configuration)
  753. {
  754. FCriticalSection = new TCriticalSection;
  755. FLogging = false;
  756. FConfiguration = Configuration;
  757. FParent = NULL;
  758. FUI = UI;
  759. FSessionData = SessionData;
  760. FStarted = Started;
  761. FFile = NULL;
  762. FCurrentLogFileName = L"";
  763. FCurrentFileName = L"";
  764. FClosed = false;
  765. }
  766. //---------------------------------------------------------------------------
  767. __fastcall TSessionLog::~TSessionLog()
  768. {
  769. FClosed = true;
  770. ReflectSettings();
  771. DebugAssert(FFile == NULL);
  772. delete FCriticalSection;
  773. }
  774. //---------------------------------------------------------------------------
  775. void __fastcall TSessionLog::SetParent(TSessionLog * Parent, const UnicodeString & Name)
  776. {
  777. FParent = Parent;
  778. FName = Name;
  779. }
  780. //---------------------------------------------------------------------------
  781. void __fastcall TSessionLog::DoAddToParent(TLogLineType Type, const UnicodeString & Line)
  782. {
  783. DebugAssert(FParent != NULL);
  784. FParent->Add(Type, Line);
  785. }
  786. //---------------------------------------------------------------------------
  787. void __fastcall TSessionLog::DoAddToSelf(TLogLineType Type, const UnicodeString & Line)
  788. {
  789. if (LogToFile())
  790. {
  791. if (FFile == NULL)
  792. {
  793. OpenLogFile();
  794. }
  795. if (FFile != NULL)
  796. {
  797. UnicodeString Timestamp = FormatDateTime(L" yyyy-mm-dd hh:nn:ss.zzz ", Now());
  798. UTF8String UtfLine = UTF8String(UnicodeString(LogLineMarks[Type]) + Timestamp + Line + L"\r\n");
  799. for (int Index = 1; Index <= UtfLine.Length(); Index++)
  800. {
  801. if ((UtfLine[Index] == '\n') &&
  802. ((Index == 1) || (UtfLine[Index - 1] != '\r')))
  803. {
  804. UtfLine.Insert('\r', Index);
  805. }
  806. }
  807. int Writting = UtfLine.Length();
  808. CheckSize(Writting);
  809. FCurrentFileSize += fwrite(UtfLine.c_str(), 1, Writting, (FILE *)FFile);
  810. }
  811. }
  812. }
  813. //---------------------------------------------------------------------------
  814. UnicodeString __fastcall TSessionLog::LogPartFileName(const UnicodeString & BaseName, int Index)
  815. {
  816. UnicodeString Result;
  817. if (Index >= 1)
  818. {
  819. Result = FORMAT(L"%s.%d", (BaseName, Index));
  820. }
  821. else
  822. {
  823. Result = BaseName;
  824. }
  825. return Result;
  826. }
  827. //---------------------------------------------------------------------------
  828. void __fastcall TSessionLog::CheckSize(__int64 Addition)
  829. {
  830. __int64 MaxSize = FConfiguration->LogMaxSize;
  831. if ((MaxSize > 0) && (FCurrentFileSize + Addition >= MaxSize))
  832. {
  833. // Before we close it
  834. UnicodeString BaseName = FCurrentFileName;
  835. CloseLogFile();
  836. FCurrentFileSize = 0;
  837. int Index = 0;
  838. while (FileExists(LogPartFileName(BaseName, Index + 1)))
  839. {
  840. Index++;
  841. }
  842. int MaxCount = FConfiguration->LogMaxCount;
  843. do
  844. {
  845. UnicodeString LogPart = LogPartFileName(BaseName, Index);
  846. if ((MaxCount > 0) && (Index >= MaxCount))
  847. {
  848. DeleteFileChecked(LogPart);
  849. }
  850. else
  851. {
  852. THROWOSIFFALSE(RenameFile(LogPart, LogPartFileName(BaseName, Index + 1)));
  853. }
  854. Index--;
  855. }
  856. while (Index >= 0);
  857. OpenLogFile();
  858. }
  859. }
  860. //---------------------------------------------------------------------------
  861. void __fastcall TSessionLog::DoAdd(TLogLineType Type, UnicodeString Line,
  862. void __fastcall (__closure *f)(TLogLineType Type, const UnicodeString & Line))
  863. {
  864. UnicodeString Prefix;
  865. if (!FName.IsEmpty())
  866. {
  867. Prefix = L"[" + FName + L"] ";
  868. }
  869. while (!Line.IsEmpty())
  870. {
  871. f(Type, Prefix + CutToChar(Line, L'\n', false));
  872. }
  873. }
  874. //---------------------------------------------------------------------------
  875. void __fastcall TSessionLog::Add(TLogLineType Type, const UnicodeString & Line)
  876. {
  877. DebugAssert(FConfiguration);
  878. if (Logging)
  879. {
  880. try
  881. {
  882. if (FParent != NULL)
  883. {
  884. DoAdd(Type, Line, DoAddToParent);
  885. }
  886. else
  887. {
  888. TGuard Guard(FCriticalSection);
  889. DoAdd(Type, Line, DoAddToSelf);
  890. }
  891. }
  892. catch (Exception &E)
  893. {
  894. // We failed logging, turn it off and notify user.
  895. FConfiguration->Logging = false;
  896. try
  897. {
  898. throw ExtException(&E, MainInstructions(LoadStr(LOG_GEN_ERROR)));
  899. }
  900. catch (Exception &E)
  901. {
  902. AddException(&E);
  903. FUI->HandleExtendedException(&E);
  904. }
  905. }
  906. }
  907. }
  908. //---------------------------------------------------------------------------
  909. void __fastcall TSessionLog::AddException(Exception * E)
  910. {
  911. if (E != NULL)
  912. {
  913. Add(llException, ExceptionLogString(E));
  914. }
  915. }
  916. //---------------------------------------------------------------------------
  917. void __fastcall TSessionLog::ReflectSettings()
  918. {
  919. TGuard Guard(FCriticalSection);
  920. FLogging =
  921. !FClosed &&
  922. ((FParent != NULL) || FConfiguration->Logging);
  923. // if logging to file was turned off or log file was changed -> close current log file
  924. if ((FFile != NULL) &&
  925. (!LogToFile() || (FCurrentLogFileName != FConfiguration->LogFileName)))
  926. {
  927. CloseLogFile();
  928. }
  929. if (FFile != NULL)
  930. {
  931. CheckSize(0);
  932. }
  933. }
  934. //---------------------------------------------------------------------------
  935. bool __fastcall TSessionLog::LogToFile()
  936. {
  937. return Logging && FConfiguration->LogToFile && (FParent == NULL);
  938. }
  939. //---------------------------------------------------------------------------
  940. void __fastcall TSessionLog::CloseLogFile()
  941. {
  942. if (FFile != NULL)
  943. {
  944. fclose((FILE *)FFile);
  945. FFile = NULL;
  946. }
  947. FCurrentLogFileName = L"";
  948. FCurrentFileName = L"";
  949. }
  950. //---------------------------------------------------------------------------
  951. void __fastcall TSessionLog::OpenLogFile()
  952. {
  953. try
  954. {
  955. DebugAssert(FFile == NULL);
  956. DebugAssert(FConfiguration != NULL);
  957. FCurrentLogFileName = FConfiguration->LogFileName;
  958. FFile = OpenFile(FCurrentLogFileName, FStarted, FSessionData, FConfiguration->LogFileAppend, FCurrentFileName);
  959. TSearchRec SearchRec;
  960. if (FileSearchRec(FCurrentFileName, SearchRec))
  961. {
  962. FCurrentFileSize = SearchRec.Size;
  963. }
  964. else
  965. {
  966. FCurrentFileSize = 0;
  967. }
  968. }
  969. catch (Exception & E)
  970. {
  971. // We failed logging to file, turn it off and notify user.
  972. FCurrentLogFileName = L"";
  973. FCurrentFileName = L"";
  974. FConfiguration->LogFileName = UnicodeString();
  975. try
  976. {
  977. throw ExtException(&E, MainInstructions(LoadStr(LOG_GEN_ERROR)));
  978. }
  979. catch (Exception & E)
  980. {
  981. AddException(&E);
  982. // not to deadlock with TSessionLog::ReflectSettings invoked by FConfiguration->LogFileName setter above
  983. TUnguard Unguard(FCriticalSection);
  984. FUI->HandleExtendedException(&E);
  985. }
  986. }
  987. // in case we are appending and the existing log file is already too large
  988. if (FFile != NULL)
  989. {
  990. CheckSize(0);
  991. }
  992. }
  993. //---------------------------------------------------------------------------
  994. void __fastcall TSessionLog::AddSystemInfo()
  995. {
  996. AddStartupInfo(true);
  997. }
  998. //---------------------------------------------------------------------------
  999. void __fastcall TSessionLog::AddStartupInfo()
  1000. {
  1001. AddStartupInfo(false);
  1002. }
  1003. //---------------------------------------------------------------------------
  1004. void __fastcall TSessionLog::AddStartupInfo(bool System)
  1005. {
  1006. TSessionData * Data = (System ? NULL : FSessionData);
  1007. if (Logging)
  1008. {
  1009. if (FParent != NULL)
  1010. {
  1011. // do not add session info for secondary session
  1012. // (this should better be handled in the TSecondaryTerminal)
  1013. }
  1014. else
  1015. {
  1016. DoAddStartupInfo(Data);
  1017. }
  1018. }
  1019. }
  1020. //---------------------------------------------------------------------------
  1021. UnicodeString __fastcall TSessionLog::GetTlsVersionName(TTlsVersion TlsVersion)
  1022. {
  1023. switch (TlsVersion)
  1024. {
  1025. default:
  1026. DebugFail();
  1027. case ssl2:
  1028. case ssl3:
  1029. case tls10:
  1030. return "TLSv1.0";
  1031. case tls11:
  1032. return "TLSv1.1";
  1033. case tls12:
  1034. return "TLSv1.2";
  1035. case tls13:
  1036. return "TLSv1.3";
  1037. }
  1038. }
  1039. //---------------------------------------------------------------------------
  1040. UnicodeString __fastcall TSessionLog::LogSensitive(const UnicodeString & Str)
  1041. {
  1042. if (FConfiguration->LogSensitive && !Str.IsEmpty())
  1043. {
  1044. return NormalizeString(Str);
  1045. }
  1046. else
  1047. {
  1048. return BooleanToEngStr(!Str.IsEmpty());
  1049. }
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. UnicodeString __fastcall TSessionLog::GetCmdLineLog(TConfiguration * AConfiguration)
  1053. {
  1054. UnicodeString Result = CmdLine;
  1055. if (!AConfiguration->LogSensitive)
  1056. {
  1057. TManagementScript Script(StoredSessions, false);
  1058. Script.MaskPasswordInCommandLine(Result, true);
  1059. }
  1060. return Result;
  1061. }
  1062. //---------------------------------------------------------------------------
  1063. template <typename T>
  1064. UnicodeString __fastcall EnumName(T Value, UnicodeString Names)
  1065. {
  1066. int N = int(Value);
  1067. do
  1068. {
  1069. UnicodeString Name = CutToChar(Names, L';', true);
  1070. if (N == 0)
  1071. {
  1072. return Name;
  1073. }
  1074. N--;
  1075. }
  1076. while ((N >= 0) && !Names.IsEmpty());
  1077. return L"(unknown)";
  1078. }
  1079. #define ADSTR(S) AddLogEntry(S)
  1080. #define ADF(S, F) ADSTR(FORMAT(S, F));
  1081. //---------------------------------------------------------------------------
  1082. void __fastcall TSessionLog::DoAddStartupInfo(TAddLogEntryEvent AddLogEntry, TConfiguration * AConfiguration, bool DoNotMaskPaswords)
  1083. {
  1084. ADSTR(GetEnvironmentInfo());
  1085. THierarchicalStorage * Storage = AConfiguration->CreateConfigStorage();
  1086. try
  1087. {
  1088. ADF(L"Configuration: %s", (Storage->Source));
  1089. }
  1090. __finally
  1091. {
  1092. delete Storage;
  1093. }
  1094. wchar_t UserName[UNLEN + 1];
  1095. unsigned long UserNameSize = LENOF(UserName);
  1096. if (DebugAlwaysFalse(!GetUserNameEx(NameSamCompatible, UserName, &UserNameSize)))
  1097. {
  1098. wcscpy(UserName, L"<Failed to retrieve username>");
  1099. }
  1100. UnicodeString LogStr;
  1101. if (AConfiguration->LogProtocol <= -1)
  1102. {
  1103. LogStr = L"Reduced";
  1104. }
  1105. else if (AConfiguration->LogProtocol <= 0)
  1106. {
  1107. LogStr = L"Normal";
  1108. }
  1109. else if (AConfiguration->LogProtocol == 1)
  1110. {
  1111. LogStr = L"Debug 1";
  1112. }
  1113. else if (AConfiguration->LogProtocol >= 2)
  1114. {
  1115. LogStr = L"Debug 2";
  1116. }
  1117. if (AConfiguration->LogSensitive)
  1118. {
  1119. LogStr += L", Logging passwords";
  1120. }
  1121. if (AConfiguration->LogMaxSize > 0)
  1122. {
  1123. LogStr += FORMAT(L", Rotating after: %s", (SizeToStr(AConfiguration->LogMaxSize)));
  1124. if (AConfiguration->LogMaxCount > 0)
  1125. {
  1126. LogStr += FORMAT(L", Keeping at most %d logs", (AConfiguration->LogMaxCount));
  1127. }
  1128. }
  1129. ADF(L"Log level: %s", (LogStr));
  1130. ADF(L"Local account: %s", (UserName));
  1131. ADF(L"Working directory: %s", (GetCurrentDir()));
  1132. ADF(L"Process ID: %d", (int(GetCurrentProcessId())));
  1133. ADF(L"Ancestor processes: %s", (GetAncestorProcessNames()));
  1134. // This logs even passwords, contrary to a session log.
  1135. // GetCmdLineLog requires master password, but we do not know it yet atm.
  1136. UnicodeString ACmdLine;
  1137. if (DoNotMaskPaswords)
  1138. {
  1139. ACmdLine = CmdLine;
  1140. }
  1141. else
  1142. {
  1143. ACmdLine = GetCmdLineLog(AConfiguration);
  1144. }
  1145. ADF(L"Command-line: %s", (ACmdLine));
  1146. if (AConfiguration->ActualLogProtocol >= 1)
  1147. {
  1148. GetGlobalOptions()->LogOptions(AddLogEntry);
  1149. }
  1150. ADF(L"Time zone: %s", (GetTimeZoneLogString()));
  1151. if (!AdjustClockForDSTEnabled())
  1152. {
  1153. ADSTR(L"Warning: System option \"Automatically adjust clock for Daylight Saving Time\" is disabled, timestamps will not be represented correctly");
  1154. }
  1155. }
  1156. //---------------------------------------------------------------------------
  1157. #undef ADSTR
  1158. #define ADSTR(S) DoAdd(llMessage, S, DoAddToSelf);
  1159. //---------------------------------------------------------------------------
  1160. void __fastcall TSessionLog::DoAddStartupInfoEntry(const UnicodeString & S)
  1161. {
  1162. ADSTR(S);
  1163. }
  1164. //---------------------------------------------------------------------------
  1165. void __fastcall TSessionLog::DoAddStartupInfo(TSessionData * Data)
  1166. {
  1167. if (Data == NULL)
  1168. {
  1169. AddSeparator();
  1170. DoAddStartupInfo(DoAddStartupInfoEntry, FConfiguration, false);
  1171. ADF(L"Login time: %s", (FormatDateTime(L"dddddd tt", Now())));
  1172. AddSeparator();
  1173. }
  1174. else
  1175. {
  1176. ADF(L"Session name: %s (%s)", (Data->SessionName, Data->SourceName));
  1177. UnicodeString AddressFamily;
  1178. if (Data->AddressFamily != afAuto)
  1179. {
  1180. AddressFamily = FORMAT(L"%s, ", (Data->AddressFamily == afIPv4 ? L"IPv4" : L"IPv6"));
  1181. }
  1182. UnicodeString HostName = Data->HostNameExpanded;
  1183. if (!Data->HostNameSource.IsEmpty())
  1184. {
  1185. HostName = FORMAT(L"%s [%s]", (HostName, Data->HostNameSource));
  1186. }
  1187. ADF(L"Host name: %s (%sPort: %d)", (HostName, AddressFamily, Data->PortNumber));
  1188. UnicodeString UserName = Data->UserNameExpanded;
  1189. if (!Data->UserNameSource.IsEmpty())
  1190. {
  1191. UserName = FORMAT(L"%s [%s]", (UserName, Data->UserNameSource));
  1192. }
  1193. ADF(L"User name: %s (Password: %s, Key file: %s, Passphrase: %s)",
  1194. (UserName, LogSensitive(Data->Password),
  1195. LogSensitive(Data->ResolvePublicKeyFile()), LogSensitive(Data->Passphrase)));
  1196. if (Data->UsesSsh)
  1197. {
  1198. ADF(L"Tunnel: %s", (BooleanToEngStr(Data->Tunnel)));
  1199. if (Data->Tunnel)
  1200. {
  1201. ADF(L"Tunnel: Host name: %s (Port: %d)", (Data->TunnelHostName, Data->TunnelPortNumber));
  1202. ADF(L"Tunnel: User name: %s (Password: %s, Key file: %s)",
  1203. (Data->TunnelUserName,
  1204. LogSensitive(Data->TunnelPassword),
  1205. LogSensitive(Data->TunnelPublicKeyFile)));
  1206. ADF(L"Tunnel: Local port number: %d", (Data->TunnelLocalPortNumber));
  1207. }
  1208. }
  1209. ADF(L"Transfer Protocol: %s", (Data->FSProtocolStr));
  1210. if (Data->UsesSsh || (Data->FSProtocol == fsFTP))
  1211. {
  1212. UnicodeString PingType;
  1213. int PingInterval;
  1214. if (Data->FSProtocol == fsFTP)
  1215. {
  1216. PingType = EnumName(Data->FtpPingType, FtpPingTypeNames);
  1217. PingInterval = Data->FtpPingInterval;
  1218. }
  1219. else
  1220. {
  1221. PingType = EnumName(Data->PingType, PingTypeNames);
  1222. PingInterval = Data->PingInterval;
  1223. }
  1224. ADF(L"Ping type: %s, Ping interval: %d sec; Timeout: %d sec",
  1225. (PingType, PingInterval, Data->Timeout));
  1226. ADF(L"Disable Nagle: %s",
  1227. (BooleanToEngStr(Data->TcpNoDelay)));
  1228. }
  1229. ADF(L"Proxy: %s",
  1230. ((Data->FtpProxyLogonType != 0) ?
  1231. FORMAT(L"FTP proxy %d", (Data->FtpProxyLogonType)) :
  1232. EnumName(Data->ProxyMethod, ProxyMethodNames)));
  1233. if ((Data->FtpProxyLogonType != 0) || (Data->ProxyMethod != ::pmNone))
  1234. {
  1235. ADF(L"HostName: %s (Port: %d); Username: %s; Passwd: %s",
  1236. (Data->ProxyHost, Data->ProxyPort,
  1237. Data->ProxyUsername, LogSensitive(Data->ProxyPassword)));
  1238. if (Data->ProxyMethod == pmTelnet)
  1239. {
  1240. ADF(L"Telnet command: %s", (Data->ProxyTelnetCommand));
  1241. }
  1242. if (Data->ProxyMethod == pmCmd)
  1243. {
  1244. ADF(L"Local command: %s", (Data->ProxyLocalCommand));
  1245. }
  1246. }
  1247. if (Data->UsesSsh || (Data->FSProtocol == fsFTP) || (Data->FSProtocol == fsS3))
  1248. {
  1249. ADF(L"Send buffer: %d", (Data->SendBuf));
  1250. }
  1251. if (Data->UsesSsh && !Data->SourceAddress.IsEmpty())
  1252. {
  1253. ADF(L"Source address: %s", (Data->SourceAddress));
  1254. }
  1255. if (Data->UsesSsh)
  1256. {
  1257. ADF(L"Compression: %s", (BooleanToEngStr(Data->Compression)));
  1258. ADF(L"Bypass authentication: %s",
  1259. (BooleanToEngStr(Data->SshNoUserAuth)));
  1260. ADF(L"Try agent: %s; Agent forwarding: %s; KI: %s; GSSAPI: %s",
  1261. (BooleanToEngStr(Data->TryAgent), BooleanToEngStr(Data->AgentFwd),
  1262. BooleanToEngStr(Data->AuthKI), BooleanToEngStr(Data->AuthGSSAPI)));
  1263. if (Data->AuthGSSAPI)
  1264. {
  1265. ADF(L"GSSAPI: KEX: %s; Forwarding: %s; Libs: %s; Custom: %s",
  1266. (BooleanToEngStr(Data->AuthGSSAPIKEX), BooleanToEngStr(Data->GSSAPIFwdTGT), Data->GssLibList, Data->GssLibCustom));
  1267. }
  1268. ADF(L"Ciphers: %s; Ssh2DES: %s",
  1269. (Data->CipherList, BooleanToEngStr(Data->Ssh2DES)));
  1270. ADF(L"KEX: %s", (Data->KexList));
  1271. UnicodeString Bugs;
  1272. for (int Index = 0; Index < BUG_COUNT; Index++)
  1273. {
  1274. AddToList(Bugs, EnumName(Data->Bug[(TSshBug)Index], AutoSwitchNames), L",");
  1275. }
  1276. ADF(L"SSH Bugs: %s", (Bugs));
  1277. ADF(L"Simple channel: %s", (BooleanToEngStr(Data->SshSimple)));
  1278. ADF(L"Return code variable: %s; Lookup user groups: %s",
  1279. ((Data->DetectReturnVar ? UnicodeString(L"Autodetect") : Data->ReturnVar),
  1280. EnumName(Data->LookupUserGroups, AutoSwitchNames)));
  1281. ADF(L"Shell: %s", ((Data->Shell.IsEmpty()? UnicodeString(L"default") : Data->Shell)));
  1282. ADF(L"EOL: %s, UTF: %s", (EnumName(Data->EOLType, EOLTypeNames), EnumName(Data->NotUtf, NotAutoSwitchNames))); // NotUtf duplicated in FTP branch
  1283. ADF(L"Clear aliases: %s, Unset nat.vars: %s, Resolve symlinks: %s; Follow directory symlinks: %s",
  1284. (BooleanToEngStr(Data->ClearAliases), BooleanToEngStr(Data->UnsetNationalVars),
  1285. BooleanToEngStr(Data->ResolveSymlinks), BooleanToEngStr(Data->FollowDirectorySymlinks)));
  1286. ADF(L"LS: %s, Ign LS warn: %s, Scp1 Comp: %s; Exit code 1 is error: %s",
  1287. (Data->ListingCommand,
  1288. BooleanToEngStr(Data->IgnoreLsWarnings),
  1289. BooleanToEngStr(Data->Scp1Compatibility),
  1290. BooleanToEngStr(Data->ExitCode1IsError)));
  1291. }
  1292. if ((Data->FSProtocol == fsSFTP) || (Data->FSProtocol == fsSFTPonly))
  1293. {
  1294. UnicodeString Bugs;
  1295. for (int Index = 0; Index < SFTP_BUG_COUNT; Index++)
  1296. {
  1297. AddToList(Bugs, EnumName(Data->SFTPBug[(TSftpBug)Index], AutoSwitchNames), L",");
  1298. }
  1299. ADF(L"SFTP Bugs: %s", (Bugs));
  1300. ADF(L"SFTP Server: %s", ((Data->SftpServer.IsEmpty()? UnicodeString(L"default") : Data->SftpServer)));
  1301. if (Data->SFTPRealPath != asAuto)
  1302. {
  1303. ADF(L"SFTP Real path: %s", (EnumName(Data->SFTPRealPath, AutoSwitchNames)));
  1304. }
  1305. if (Data->UsePosixRename)
  1306. {
  1307. ADF(L"Use POSIX rename: %s", (BooleanToEngStr(Data->UsePosixRename)));
  1308. }
  1309. }
  1310. bool FtpsOn = false;
  1311. if (Data->FSProtocol == fsFTP)
  1312. {
  1313. ADF(L"UTF: %s", (EnumName(Data->NotUtf, NotAutoSwitchNames))); // duplicated in UsesSsh branch
  1314. UnicodeString Ftps;
  1315. switch (Data->Ftps)
  1316. {
  1317. case ftpsImplicit:
  1318. Ftps = L"Implicit TLS/SSL";
  1319. FtpsOn = true;
  1320. break;
  1321. case ftpsExplicitSsl:
  1322. Ftps = L"Explicit SSL/TLS";
  1323. FtpsOn = true;
  1324. break;
  1325. case ftpsExplicitTls:
  1326. Ftps = L"Explicit TLS/SSL";
  1327. FtpsOn = true;
  1328. break;
  1329. default:
  1330. DebugAssert(Data->Ftps == ftpsNone);
  1331. Ftps = L"None";
  1332. break;
  1333. }
  1334. // kind of hidden option, so do not reveal it unless it is set
  1335. if (Data->FtpTransferActiveImmediately != asAuto)
  1336. {
  1337. ADF(L"Transfer active immediately: %s", (EnumName(Data->FtpTransferActiveImmediately, AutoSwitchNames)));
  1338. }
  1339. ADF(L"FTPS: %s [Client certificate: %s]",
  1340. (Ftps, LogSensitive(Data->TlsCertificateFile)));
  1341. ADF(L"FTP: Passive: %s [Force IP: %s]; MLSD: %s [List all: %s]; HOST: %s",
  1342. (BooleanToEngStr(Data->FtpPasvMode),
  1343. EnumName(Data->FtpForcePasvIp, AutoSwitchNames),
  1344. EnumName(Data->FtpUseMlsd, AutoSwitchNames),
  1345. EnumName(Data->FtpListAll, AutoSwitchNames),
  1346. EnumName(Data->FtpHost, AutoSwitchNames)));
  1347. if (Data->FtpWorkFromCwd != asAuto)
  1348. {
  1349. ADF(L"FTP: Relative paths: %s", (EnumName(Data->FtpWorkFromCwd, AutoSwitchNames)));
  1350. }
  1351. }
  1352. if (Data->FSProtocol == fsWebDAV)
  1353. {
  1354. FtpsOn = (Data->Ftps != ftpsNone);
  1355. ADF(L"HTTPS: %s [Client certificate: %s]",
  1356. (BooleanToEngStr(FtpsOn), LogSensitive(Data->TlsCertificateFile)));
  1357. ADF(L"WebDAV: Tolerate non-encoded: %s", (BooleanToEngStr(Data->WebDavLiberalEscaping)));
  1358. }
  1359. if (Data->FSProtocol == fsS3)
  1360. {
  1361. FtpsOn = (Data->Ftps != ftpsNone);
  1362. ADF(L"HTTPS: %s", (BooleanToEngStr(FtpsOn)));
  1363. ADF(L"S3: URL Style: %s", (EnumName(Data->S3UrlStyle, L"Virtual Host;Path")));
  1364. if (!Data->S3DefaultRegion.IsEmpty())
  1365. {
  1366. ADF(L"S3: Default region: %s", (Data->S3DefaultRegion));
  1367. }
  1368. if (!Data->S3SessionToken.IsEmpty())
  1369. {
  1370. ADF(L"S3: Session token: %s", (Data->S3SessionToken));
  1371. }
  1372. if (Data->S3CredentialsEnv)
  1373. {
  1374. ADF(L"S3: Credentials from AWS environment: %s", (DefaultStr(Data->S3Profile, L"General")));
  1375. }
  1376. }
  1377. if (FtpsOn)
  1378. {
  1379. if (Data->FSProtocol == fsFTP)
  1380. {
  1381. ADF(L"Session reuse: %s", (BooleanToEngStr(Data->SslSessionReuse)));
  1382. }
  1383. ADF(L"TLS/SSL versions: %s-%s", (GetTlsVersionName(Data->MinTlsVersion), GetTlsVersionName(Data->MaxTlsVersion)));
  1384. }
  1385. ADF(L"Local directory: %s, Remote directory: %s, Update: %s, Cache: %s",
  1386. ((Data->LocalDirectory.IsEmpty() ? UnicodeString(L"default") : Data->LocalDirectory),
  1387. (Data->RemoteDirectory.IsEmpty() ? UnicodeString(L"home") : Data->RemoteDirectory),
  1388. BooleanToEngStr(Data->UpdateDirectories),
  1389. BooleanToEngStr(Data->CacheDirectories)));
  1390. ADF(L"Cache directory changes: %s, Permanent: %s",
  1391. (BooleanToEngStr(Data->CacheDirectoryChanges),
  1392. BooleanToEngStr(Data->PreserveDirectoryChanges)));
  1393. ADF(L"Recycle bin: Delete to: %s, Overwritten to: %s, Bin path: %s",
  1394. (BooleanToEngStr(Data->DeleteToRecycleBin),
  1395. BooleanToEngStr(Data->OverwrittenToRecycleBin),
  1396. Data->RecycleBinPath));
  1397. if (Data->TrimVMSVersions || Data->VMSAllRevisions)
  1398. {
  1399. ADF(L"Trim VMS versions: %s; VMS all revisions: %s",
  1400. (BooleanToEngStr(Data->TrimVMSVersions), BooleanToEngStr(Data->VMSAllRevisions)));
  1401. }
  1402. UnicodeString TimeInfo;
  1403. if ((Data->FSProtocol == fsSFTP) || (Data->FSProtocol == fsSFTPonly) || (Data->FSProtocol == fsSCPonly) || (Data->FSProtocol == fsWebDAV))
  1404. {
  1405. AddToList(TimeInfo, FORMAT(L"DST mode: %s", (EnumName(Data->DSTMode, DSTModeNames))), L";");
  1406. }
  1407. if ((Data->FSProtocol == fsSCPonly) || (Data->FSProtocol == fsFTP))
  1408. {
  1409. int TimeDifferenceMin = TimeToMinutes(Data->TimeDifference);
  1410. AddToList(TimeInfo, FORMAT(L"Timezone offset: %dh %dm", ((TimeDifferenceMin / MinsPerHour), (TimeDifferenceMin % MinsPerHour))), L";");
  1411. }
  1412. ADSTR(TimeInfo);
  1413. if (Data->FSProtocol == fsWebDAV)
  1414. {
  1415. ADF(L"Compression: %s",
  1416. (BooleanToEngStr(Data->Compression)));
  1417. }
  1418. if ((Data->FSProtocol == fsFTP) && !Data->FtpPasvMode)
  1419. {
  1420. if (!Configuration->ExternalIpAddress.IsEmpty() || Configuration->HasLocalPortNumberLimits())
  1421. {
  1422. AddSeparator();
  1423. ADF(L"FTP active mode interface: %s:%d-%d", (DefaultStr(Configuration->ExternalIpAddress, L"<system address>"), Configuration->LocalPortNumberMin, Configuration->LocalPortNumberMax));
  1424. }
  1425. }
  1426. AddSeparator();
  1427. }
  1428. }
  1429. //---------------------------------------------------------------------------
  1430. #undef ADF
  1431. #undef ADSTR
  1432. //---------------------------------------------------------------------------
  1433. void __fastcall TSessionLog::AddSeparator()
  1434. {
  1435. Add(llMessage, L"--------------------------------------------------------------------------");
  1436. }
  1437. //---------------------------------------------------------------------------
  1438. //---------------------------------------------------------------------------
  1439. __fastcall TActionLog::TActionLog(TSessionUI * UI, TDateTime Started, TSessionData * SessionData,
  1440. TConfiguration * Configuration)
  1441. {
  1442. DebugAssert(UI != NULL);
  1443. DebugAssert(SessionData != NULL);
  1444. Init(UI, Started, SessionData, Configuration);
  1445. }
  1446. //---------------------------------------------------------------------------
  1447. __fastcall TActionLog::TActionLog(TDateTime Started, TConfiguration * Configuration)
  1448. {
  1449. Init(NULL, Started, NULL, Configuration);
  1450. // not associated with session, so no need to waiting for anything
  1451. ReflectSettings();
  1452. }
  1453. //---------------------------------------------------------------------------
  1454. void __fastcall TActionLog::Init(TSessionUI * UI, TDateTime Started, TSessionData * SessionData,
  1455. TConfiguration * Configuration)
  1456. {
  1457. FCriticalSection = new TCriticalSection;
  1458. FConfiguration = Configuration;
  1459. FUI = UI;
  1460. FSessionData = SessionData;
  1461. FStarted = Started;
  1462. FFile = NULL;
  1463. FCurrentLogFileName = L"";
  1464. FCurrentFileName = L"";
  1465. FLogging = false;
  1466. FClosed = false;
  1467. FFailed = false;
  1468. FPendingActions = new TList();
  1469. FIndent = L" ";
  1470. FInGroup = false;
  1471. FEnabled = true;
  1472. }
  1473. //---------------------------------------------------------------------------
  1474. __fastcall TActionLog::~TActionLog()
  1475. {
  1476. DebugAssert(FPendingActions->Count == 0);
  1477. delete FPendingActions;
  1478. FClosed = true;
  1479. ReflectSettings();
  1480. DebugAssert(FFile == NULL);
  1481. delete FCriticalSection;
  1482. }
  1483. //---------------------------------------------------------------------------
  1484. void __fastcall TActionLog::Add(const UnicodeString & Line)
  1485. {
  1486. DebugAssert(FConfiguration);
  1487. if (FLogging)
  1488. {
  1489. TGuard Guard(FCriticalSection);
  1490. if (FFile == NULL)
  1491. {
  1492. OpenLogFile();
  1493. }
  1494. if (FFile != NULL)
  1495. {
  1496. try
  1497. {
  1498. UTF8String UtfLine = UTF8String(Line);
  1499. size_t Written =
  1500. fwrite(UtfLine.c_str(), 1, UtfLine.Length(), (FILE *)FFile);
  1501. if (Written != static_cast<size_t>(UtfLine.Length()))
  1502. {
  1503. throw ECRTExtException(L"");
  1504. }
  1505. #ifdef _DEBUG
  1506. #endif
  1507. Written =
  1508. fwrite("\n", 1, 1, (FILE *)FFile);
  1509. if (Written != 1)
  1510. {
  1511. throw ECRTExtException(L"");
  1512. }
  1513. }
  1514. catch (Exception &E)
  1515. {
  1516. FCriticalSection->Release();
  1517. // avoid endless loop when trying to close tags when closing log, when logging has failed
  1518. if (!FFailed)
  1519. {
  1520. FFailed = true;
  1521. // We failed logging, turn it off and notify user.
  1522. FConfiguration->LogActions = false;
  1523. if (FConfiguration->LogActionsRequired)
  1524. {
  1525. throw EFatal(&E, LoadStr(LOG_FATAL_ERROR));
  1526. }
  1527. else
  1528. {
  1529. try
  1530. {
  1531. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  1532. }
  1533. catch (Exception &E)
  1534. {
  1535. if (FUI != NULL)
  1536. {
  1537. FUI->HandleExtendedException(&E);
  1538. }
  1539. }
  1540. }
  1541. }
  1542. }
  1543. }
  1544. }
  1545. }
  1546. //---------------------------------------------------------------------------
  1547. void __fastcall TActionLog::AddIndented(const UnicodeString & Line)
  1548. {
  1549. Add(FIndent + Line);
  1550. }
  1551. //---------------------------------------------------------------------------
  1552. void __fastcall TActionLog::AddFailure(TStrings * Messages)
  1553. {
  1554. AddIndented(L"<failure>");
  1555. AddMessages(L" ", Messages);
  1556. AddIndented(L"</failure>");
  1557. }
  1558. //---------------------------------------------------------------------------
  1559. void __fastcall TActionLog::AddFailure(Exception * E)
  1560. {
  1561. TStrings * Messages = ExceptionToMoreMessages(E);
  1562. if (Messages != NULL)
  1563. {
  1564. try
  1565. {
  1566. AddFailure(Messages);
  1567. }
  1568. __finally
  1569. {
  1570. delete Messages;
  1571. }
  1572. }
  1573. }
  1574. //---------------------------------------------------------------------------
  1575. void __fastcall TActionLog::AddMessages(UnicodeString Indent, TStrings * Messages)
  1576. {
  1577. for (int Index = 0; Index < Messages->Count; Index++)
  1578. {
  1579. AddIndented(
  1580. FORMAT(Indent + L"<message>%s</message>", (XmlEscape(Messages->Strings[Index]))));
  1581. }
  1582. }
  1583. //---------------------------------------------------------------------------
  1584. void __fastcall TActionLog::ReflectSettings()
  1585. {
  1586. TGuard Guard(FCriticalSection);
  1587. bool ALogging =
  1588. !FClosed && FConfiguration->LogActions && Enabled;
  1589. if (ALogging && !FLogging)
  1590. {
  1591. FLogging = true;
  1592. Add(L"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  1593. UnicodeString SessionName =
  1594. (FSessionData != NULL) ? XmlAttributeEscape(FSessionData->SessionName) : UnicodeString(L"nosession");
  1595. Add(FORMAT(L"<session xmlns=\"http://winscp.net/schema/session/1.0\" name=\"%s\" start=\"%s\">",
  1596. (SessionName, StandardTimestamp())));
  1597. }
  1598. else if (!ALogging && FLogging)
  1599. {
  1600. if (FInGroup)
  1601. {
  1602. EndGroup();
  1603. }
  1604. // do not try to close the file, if it has not been opened, to avoid recursion
  1605. if (FFile != NULL)
  1606. {
  1607. Add(L"</session>");
  1608. }
  1609. CloseLogFile();
  1610. FLogging = false;
  1611. }
  1612. }
  1613. //---------------------------------------------------------------------------
  1614. void __fastcall TActionLog::CloseLogFile()
  1615. {
  1616. if (FFile != NULL)
  1617. {
  1618. fclose((FILE *)FFile);
  1619. FFile = NULL;
  1620. }
  1621. FCurrentLogFileName = L"";
  1622. FCurrentFileName = L"";
  1623. }
  1624. //---------------------------------------------------------------------------
  1625. void __fastcall TActionLog::OpenLogFile()
  1626. {
  1627. try
  1628. {
  1629. DebugAssert(FFile == NULL);
  1630. DebugAssert(FConfiguration != NULL);
  1631. FCurrentLogFileName = FConfiguration->ActionsLogFileName;
  1632. FFile = OpenFile(FCurrentLogFileName, FStarted, FSessionData, false, FCurrentFileName);
  1633. }
  1634. catch (Exception & E)
  1635. {
  1636. // We failed logging to file, turn it off and notify user.
  1637. FCurrentLogFileName = L"";
  1638. FCurrentFileName = L"";
  1639. FConfiguration->LogActions = false;
  1640. if (FConfiguration->LogActionsRequired)
  1641. {
  1642. throw EFatal(&E, LoadStr(LOG_FATAL_ERROR));
  1643. }
  1644. else
  1645. {
  1646. try
  1647. {
  1648. throw ExtException(&E, LoadStr(LOG_GEN_ERROR));
  1649. }
  1650. catch (Exception & E)
  1651. {
  1652. if (FUI != NULL)
  1653. {
  1654. // not to deadlock with TSessionLog::ReflectSettings invoked by FConfiguration->LogFileName setter above
  1655. TUnguard Unguard(FCriticalSection);
  1656. FUI->HandleExtendedException(&E);
  1657. }
  1658. }
  1659. }
  1660. }
  1661. }
  1662. //---------------------------------------------------------------------------
  1663. void __fastcall TActionLog::AddPendingAction(TSessionActionRecord * Action)
  1664. {
  1665. FPendingActions->Add(Action);
  1666. }
  1667. //---------------------------------------------------------------------------
  1668. void __fastcall TActionLog::RecordPendingActions()
  1669. {
  1670. while ((FPendingActions->Count > 0) &&
  1671. static_cast<TSessionActionRecord *>(FPendingActions->Items[0])->Record())
  1672. {
  1673. FPendingActions->Delete(0);
  1674. }
  1675. }
  1676. //---------------------------------------------------------------------------
  1677. void __fastcall TActionLog::BeginGroup(UnicodeString Name)
  1678. {
  1679. DebugAssert(!FInGroup);
  1680. FInGroup = true;
  1681. DebugAssert(FIndent == L" ");
  1682. AddIndented(FORMAT(L"<group name=\"%s\" start=\"%s\">",
  1683. (XmlAttributeEscape(Name), StandardTimestamp())));
  1684. FIndent = L" ";
  1685. }
  1686. //---------------------------------------------------------------------------
  1687. void __fastcall TActionLog::EndGroup()
  1688. {
  1689. DebugAssert(FInGroup);
  1690. FInGroup = false;
  1691. DebugAssert(FIndent == L" ");
  1692. FIndent = L" ";
  1693. // this is called from ReflectSettings that in turn is called when logging fails,
  1694. // so do not try to close the group, if it has not been opened, to avoid recursion
  1695. if (FFile != NULL)
  1696. {
  1697. AddIndented(L"</group>");
  1698. }
  1699. }
  1700. //---------------------------------------------------------------------------
  1701. void __fastcall TActionLog::SetEnabled(bool value)
  1702. {
  1703. if (Enabled != value)
  1704. {
  1705. FEnabled = value;
  1706. ReflectSettings();
  1707. }
  1708. }
  1709. //---------------------------------------------------------------------------
  1710. //---------------------------------------------------------------------------
  1711. TApplicationLog::TApplicationLog()
  1712. {
  1713. FFile = NULL;
  1714. FLogging = false;
  1715. FCriticalSection.reset(new TCriticalSection());
  1716. }
  1717. //---------------------------------------------------------------------------
  1718. TApplicationLog::~TApplicationLog()
  1719. {
  1720. if (FFile != NULL)
  1721. {
  1722. Log(L"Closing log");
  1723. fclose(static_cast<FILE *>(FFile));
  1724. FFile = NULL;
  1725. }
  1726. FLogging = false;
  1727. }
  1728. //---------------------------------------------------------------------------
  1729. void TApplicationLog::Enable(const UnicodeString & Path)
  1730. {
  1731. UnicodeString Dummy;
  1732. FPath = Path;
  1733. FFile = OpenFile(FPath, Now(), NULL, false, Dummy);
  1734. FLogging = true;
  1735. }
  1736. //---------------------------------------------------------------------------
  1737. void TApplicationLog::AddStartupInfo()
  1738. {
  1739. if (Logging)
  1740. {
  1741. TSessionLog::DoAddStartupInfo(Log, Configuration, true);
  1742. }
  1743. }
  1744. //---------------------------------------------------------------------------
  1745. void __fastcall TApplicationLog::Log(const UnicodeString & S)
  1746. {
  1747. if (FFile != NULL)
  1748. {
  1749. UnicodeString Timestamp = FormatDateTime(L"yyyy-mm-dd hh:nn:ss.zzz", Now());
  1750. UnicodeString Line = FORMAT(L"[%s] [%x] %s\r\n", (Timestamp, static_cast<int>(GetCurrentThreadId()), S));
  1751. UTF8String UtfLine = UTF8String(Line);
  1752. int Writting = UtfLine.Length();
  1753. TGuard Guard(FCriticalSection.get());
  1754. fwrite(UtfLine.c_str(), 1, Writting, static_cast<FILE *>(FFile));
  1755. }
  1756. }