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

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