SessionLogReader.cs 8.1 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 = 125;
  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. if (!_closed)
  106. {
  107. // If log was not closed, it is likely the XML is not well-formed
  108. // (at least top-level <session/> tag is not closed),
  109. // so we swallow the parsing errors here.
  110. Session.Logger.WriteLine("Error parsing session log file, but it is not closed yet, will retry");
  111. result = false;
  112. }
  113. else
  114. {
  115. // check if the the root cause was session abort
  116. Session.CheckForTimeout();
  117. LogContents();
  118. throw new SessionLocalException(Session, "Error parsing session log file", e);
  119. }
  120. }
  121. if (!result && !_closed)
  122. {
  123. Session.Logger.WriteLine("Waiting for log update and dispatching events for {0}", interval);
  124. Session.DispatchEvents(interval);
  125. if (interval < 500)
  126. {
  127. interval *= 2;
  128. }
  129. }
  130. }
  131. while (!result && !_closed);
  132. if (result)
  133. {
  134. LogContents();
  135. }
  136. return result;
  137. }
  138. private void LogContents()
  139. {
  140. if (Session.Logger.Logging)
  141. {
  142. try
  143. {
  144. // alterative to File.ReadAllText with write-sharing
  145. // (note that the StreamReader disposes the Stream)
  146. using (StreamReader reader = new StreamReader(OpenLogFileWithWriteSharing(), Encoding.UTF8))
  147. {
  148. string contents = reader.ReadToEnd();
  149. if ((_logged == null) || (_logged != contents))
  150. {
  151. Session.Logger.WriteLine("Log contents:\n{0}", contents);
  152. _logged = contents;
  153. }
  154. else
  155. {
  156. Session.Logger.WriteLine("Log contents has not changed");
  157. }
  158. }
  159. }
  160. catch (Exception e)
  161. {
  162. Session.Logger.WriteLine("Error logging log contents [{0}]", e.Message);
  163. }
  164. }
  165. }
  166. private void OpenLog()
  167. {
  168. if (_closed)
  169. {
  170. throw new InvalidOperationException("Log was closed already");
  171. }
  172. try
  173. {
  174. Session.Logger.WriteLine("Opening log without write sharing");
  175. // First try to open file without write sharing.
  176. // This fails, if WinSCP is still writing to the log file.
  177. // This is done only as a way to detect that log file is not complete yet.
  178. _stream = File.Open(Session.XmlLogPath, FileMode.Open, FileAccess.Read, FileShare.Read);
  179. _closed = true;
  180. LogContents();
  181. }
  182. catch (IOException)
  183. {
  184. Session.Logger.WriteLine("Opening log with write sharing");
  185. // If log file is still being written to, open it with write sharing
  186. _stream = OpenLogFileWithWriteSharing();
  187. _closed = false;
  188. }
  189. Session.Logger.WriteLine("Log opened");
  190. _reader = XmlReader.Create(_stream);
  191. int skip = _position;
  192. Session.Logger.WriteLine("Skipping {0} nodes", skip);
  193. while (skip > 0)
  194. {
  195. if (!_reader.Read())
  196. {
  197. throw new SessionLocalException(Session, "Read less nodes than in previous log parsing");
  198. }
  199. --skip;
  200. }
  201. }
  202. private FileStream OpenLogFileWithWriteSharing()
  203. {
  204. return File.Open(Session.XmlLogPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  205. }
  206. internal override XmlReader Reader
  207. {
  208. get
  209. {
  210. if (_reader == null)
  211. {
  212. throw new SessionLocalException(Session, "Reading has not commenced yet");
  213. }
  214. return _reader;
  215. }
  216. }
  217. private int _position;
  218. private XmlReader _reader;
  219. private FileStream _stream;
  220. private bool _closed;
  221. private string _logged;
  222. }
  223. }