You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Logging.cs 4.8 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.IO;
  3. using System.Net.Sockets;
  4. using System.Net;
  5. using System.Diagnostics;
  6. using Shadowsocks.Util;
  7. namespace Shadowsocks.Controller
  8. {
  9. public class Logging
  10. {
  11. public static string LogFilePath;
  12. private static FileStream _fs;
  13. private static StreamWriterWithTimestamp _sw;
  14. public static bool OpenLogFile()
  15. {
  16. try
  17. {
  18. LogFilePath = Utils.GetTempPath("shadowsocks.log");
  19. _fs = new FileStream(LogFilePath, FileMode.Append);
  20. _sw = new StreamWriterWithTimestamp(_fs);
  21. _sw.AutoFlush = true;
  22. Console.SetOut(_sw);
  23. Console.SetError(_sw);
  24. return true;
  25. }
  26. catch (IOException e)
  27. {
  28. Console.WriteLine(e.ToString());
  29. return false;
  30. }
  31. }
  32. private static void WriteToLogFile(object o)
  33. {
  34. try {
  35. Console.WriteLine(o);
  36. } catch(ObjectDisposedException) {
  37. }
  38. }
  39. public static void Error(object o)
  40. {
  41. WriteToLogFile("[E] " + o);
  42. }
  43. public static void Info(object o)
  44. {
  45. WriteToLogFile(o);
  46. }
  47. public static void Clear() {
  48. _sw.Close();
  49. _sw.Dispose();
  50. _fs.Close();
  51. _fs.Dispose();
  52. File.Delete(LogFilePath);
  53. OpenLogFile();
  54. }
  55. [Conditional("DEBUG")]
  56. public static void Debug(object o)
  57. {
  58. WriteToLogFile("[D] " + o);
  59. }
  60. [Conditional("DEBUG")]
  61. public static void Debug(EndPoint local, EndPoint remote, int len, string header = null, string tailer = null)
  62. {
  63. if (header == null && tailer == null)
  64. Debug($"{local} => {remote} (size={len})");
  65. else if (header == null && tailer != null)
  66. Debug($"{local} => {remote} (size={len}), {tailer}");
  67. else if (header != null && tailer == null)
  68. Debug($"{header}: {local} => {remote} (size={len})");
  69. else
  70. Debug($"{header}: {local} => {remote} (size={len}), {tailer}");
  71. }
  72. [Conditional("DEBUG")]
  73. public static void Debug(Socket sock, int len, string header = null, string tailer = null)
  74. {
  75. Debug(sock.LocalEndPoint, sock.RemoteEndPoint, len, header, tailer);
  76. }
  77. public static void LogUsefulException(Exception e)
  78. {
  79. // just log useful exceptions, not all of them
  80. if (e is SocketException)
  81. {
  82. SocketException se = (SocketException)e;
  83. if (se.SocketErrorCode == SocketError.ConnectionAborted)
  84. {
  85. // closed by browser when sending
  86. // normally happens when download is canceled or a tab is closed before page is loaded
  87. }
  88. else if (se.SocketErrorCode == SocketError.ConnectionReset)
  89. {
  90. // received rst
  91. }
  92. else if (se.SocketErrorCode == SocketError.NotConnected)
  93. {
  94. // The application tried to send or receive data, and the System.Net.Sockets.Socket is not connected.
  95. }
  96. else if (se.SocketErrorCode == SocketError.HostUnreachable)
  97. {
  98. // There is no network route to the specified host.
  99. }
  100. else if (se.SocketErrorCode == SocketError.TimedOut)
  101. {
  102. // The connection attempt timed out, or the connected host has failed to respond.
  103. }
  104. else
  105. {
  106. Info(e);
  107. }
  108. }
  109. else if (e is ObjectDisposedException)
  110. {
  111. }
  112. else
  113. {
  114. Info(e);
  115. }
  116. }
  117. }
  118. // Simply extended System.IO.StreamWriter for adding timestamp workaround
  119. public class StreamWriterWithTimestamp : StreamWriter
  120. {
  121. public StreamWriterWithTimestamp(Stream stream) : base(stream)
  122. {
  123. }
  124. private string GetTimestamp()
  125. {
  126. return "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] ";
  127. }
  128. public override void WriteLine(string value)
  129. {
  130. base.WriteLine(GetTimestamp() + value);
  131. }
  132. public override void Write(string value)
  133. {
  134. base.Write(GetTimestamp() + value);
  135. }
  136. }
  137. }