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.4 kB

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