SessionLogReader.cs 7.9 KB

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