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 3.8 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 LogFile;
  11. public static bool OpenLogFile()
  12. {
  13. try
  14. {
  15. LogFile = Utils.GetTempPath("shadowsocks.log");
  16. FileStream fs = new FileStream(LogFile, 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. public static void Error(object o)
  30. {
  31. Console.WriteLine("[E] " + o);
  32. }
  33. public static void Info(object o)
  34. {
  35. Console.WriteLine(o);
  36. }
  37. public static void Debug(object o)
  38. {
  39. #if DEBUG
  40. Console.WriteLine("[D] " + o);
  41. #endif
  42. }
  43. #if DEBUG
  44. public static void Debug(EndPoint local, EndPoint remote, int len, string header = null, string tailer = null)
  45. {
  46. if (header == null && tailer == null)
  47. Debug($"{local} => {remote} (size={len})");
  48. else if (header == null && tailer != null)
  49. Debug($"{local} => {remote} (size={len}), {tailer}");
  50. else if (header != null && tailer == null)
  51. Debug($"{header}: {local} => {remote} (size={len})");
  52. else
  53. Debug($"{header}: {local} => {remote} (size={len}), {tailer}");
  54. }
  55. public static void Debug(Socket sock, int len, string header = null, string tailer = null)
  56. {
  57. Debug(sock.LocalEndPoint, sock.RemoteEndPoint, len, header, tailer);
  58. }
  59. #endif
  60. public static void LogUsefulException(Exception e)
  61. {
  62. // just log useful exceptions, not all of them
  63. if (e is SocketException)
  64. {
  65. SocketException se = (SocketException)e;
  66. if (se.SocketErrorCode == SocketError.ConnectionAborted)
  67. {
  68. // closed by browser when sending
  69. // normally happens when download is canceled or a tab is closed before page is loaded
  70. }
  71. else if (se.SocketErrorCode == SocketError.ConnectionReset)
  72. {
  73. // received rst
  74. }
  75. else if (se.SocketErrorCode == SocketError.NotConnected)
  76. {
  77. // close when not connected
  78. }
  79. else
  80. {
  81. Console.WriteLine(e);
  82. }
  83. }
  84. else if (e is ObjectDisposedException)
  85. {
  86. }
  87. else
  88. {
  89. Console.WriteLine(e);
  90. }
  91. }
  92. }
  93. // Simply extended System.IO.StreamWriter for adding timestamp workaround
  94. public class StreamWriterWithTimestamp : StreamWriter
  95. {
  96. public StreamWriterWithTimestamp(Stream stream) : base(stream)
  97. {
  98. }
  99. private string GetTimestamp()
  100. {
  101. return "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] ";
  102. }
  103. public override void WriteLine(string value)
  104. {
  105. base.WriteLine(GetTimestamp() + value);
  106. }
  107. public override void Write(string value)
  108. {
  109. base.Write(GetTimestamp() + value);
  110. }
  111. }
  112. }