SessionLogReader.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Xml;
  5. using System.Text;
  6. namespace WinSCP
  7. {
  8. internal class SessionLogReader : CustomLogReader
  9. {
  10. public SessionLogReader(Session session) :
  11. base(session)
  12. {
  13. _position = 0;
  14. }
  15. public override void Dispose()
  16. {
  17. using (Session.Logger.CreateCallstack())
  18. {
  19. Cleanup();
  20. }
  21. base.Dispose();
  22. }
  23. private void Cleanup()
  24. {
  25. if (_stream != null)
  26. {
  27. Session.Logger.WriteLine("Closing log");
  28. _stream.Dispose();
  29. _stream = null;
  30. }
  31. if (_reader != null)
  32. {
  33. ((IDisposable)_reader).Dispose();
  34. _reader = null;
  35. }
  36. }
  37. public override bool Read(LogReadFlags flags)
  38. {
  39. using (Session.Logger.CreateCallstack())
  40. {
  41. bool result;
  42. bool retry;
  43. do
  44. {
  45. result = DoRead();
  46. retry = false;
  47. if (result &&
  48. IsNonEmptyElement("failure"))
  49. {
  50. SessionRemoteException e = SessionRemoteException.ReadFailure(this);
  51. Session.RaiseFailed(e);
  52. if ((flags & LogReadFlags.ThrowFailures) == 0)
  53. {
  54. retry = true;
  55. }
  56. else
  57. {
  58. throw e;
  59. }
  60. }
  61. }
  62. while (retry);
  63. return result;
  64. }
  65. }
  66. private bool DoRead()
  67. {
  68. int interval = 50;
  69. bool result;
  70. do
  71. {
  72. if (_reader == null)
  73. {
  74. OpenLog();
  75. }
  76. Debug.Assert(_reader != null);
  77. try
  78. {
  79. result = _reader.Read();
  80. if (result)
  81. {
  82. ++_position;
  83. Session.Logger.WriteLine("Read node {0}: {1} {2}{3}{4}",
  84. _position, _reader.NodeType, _reader.Name,
  85. (_reader.HasValue && !string.IsNullOrEmpty(_reader.Name) && !string.IsNullOrEmpty(_reader.Value) ? "=" : string.Empty),
  86. _reader.Value);
  87. Session.GotOutput();
  88. }
  89. else
  90. {
  91. Session.Logger.WriteLine("Cannot read");
  92. if (!_closed)
  93. {
  94. // this should not happen as when the log is not closed,
  95. // we should get XmlException on reaching the end
  96. _closed = true;
  97. Cleanup();
  98. }
  99. Session.CheckForTimeout();
  100. }
  101. }
  102. catch (XmlException e)
  103. {
  104. Cleanup();
  105. // We hope this code is not needed anymore.
  106. // keeping it just in case the XmlLogReader by passes
  107. // our override of PatientFileStream.Read uing other read method.
  108. #if !DEBUG
  109. if (!_closed)
  110. {
  111. // If log was not closed, it is likely the XML is not well-formed
  112. // (at least top-level <session/> tag is not closed),
  113. // so we swallow the parsing errors here.
  114. Session.Logger.WriteLine("Error parsing session log file, but it is not closed yet, will retry");
  115. result = false;
  116. }
  117. else
  118. #endif
  119. {
  120. // check if the the root cause was session abort
  121. Session.CheckForTimeout();
  122. LogContents();
  123. throw new SessionLocalException(Session, "Error parsing session log file", e);
  124. }
  125. }
  126. if (!result && !_closed)
  127. {
  128. Session.Logger.WriteLine("Waiting for log update and dispatching events for {0}", interval);
  129. Session.DispatchEvents(interval);
  130. if (interval < 500)
  131. {
  132. interval *= 2;
  133. }
  134. }
  135. }
  136. while (!result && !_closed);
  137. if (result)
  138. {
  139. LogContents();
  140. }
  141. return result;
  142. }
  143. private void LogContents()
  144. {
  145. if (Session.Logger.Logging)
  146. {
  147. try
  148. {
  149. // alterative to File.ReadAllText with write-sharing
  150. // (note that the StreamReader disposes the Stream)
  151. using (StreamReader reader = new StreamReader(new FileStream(Session.XmlLogPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8))
  152. {
  153. string contents = reader.ReadToEnd();
  154. if ((_logged == null) || (_logged != contents))
  155. {
  156. Session.Logger.WriteLine("Log contents:\n{0}", contents);
  157. _logged = contents;
  158. }
  159. else
  160. {
  161. Session.Logger.WriteLine("Log contents has not changed");
  162. }
  163. }
  164. }
  165. catch (Exception e)
  166. {
  167. Session.Logger.WriteLine("Error logging log contents [{0}]", e.Message);
  168. }
  169. }
  170. }
  171. private void OpenLog()
  172. {
  173. if (_closed)
  174. {
  175. throw new InvalidOperationException("Log was closed already");
  176. }
  177. try
  178. {
  179. Session.Logger.WriteLine("Opening log without write sharing");
  180. // First try to open file without write sharing.
  181. // This fails, if WinSCP is still writing to the log file.
  182. // This is done only as a way to detect that log file is not complete yet.
  183. _stream = new PatientFileStream(Session, Session.XmlLogPath, FileMode.Open, FileAccess.Read, FileShare.Read);
  184. _closed = true;
  185. LogContents();
  186. }
  187. catch (IOException)
  188. {
  189. Session.Logger.WriteLine("Opening log with write sharing");
  190. // If log file is still being written to, open it with write sharing
  191. _stream = new PatientFileStream(Session, Session.XmlLogPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  192. _closed = false;
  193. }
  194. Session.Logger.WriteLine("Log opened");
  195. _reader = XmlReader.Create(_stream);
  196. int skip = _position;
  197. Session.Logger.WriteLine("Skipping {0} nodes", skip);
  198. while (skip > 0)
  199. {
  200. if (!_reader.Read())
  201. {
  202. throw new SessionLocalException(Session, "Read less nodes than in previous log parsing");
  203. }
  204. --skip;
  205. }
  206. }
  207. internal override XmlReader Reader
  208. {
  209. get
  210. {
  211. if (_reader == null)
  212. {
  213. throw new SessionLocalException(Session, "Reading has not commenced yet");
  214. }
  215. return _reader;
  216. }
  217. }
  218. private int _position;
  219. private XmlReader _reader;
  220. private PatientFileStream _stream;
  221. private bool _closed;
  222. private string _logged;
  223. }
  224. }