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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net.Sockets;
  5. using System.Text;
  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. string temppath = Path.GetTempPath();
  16. LogFile = Path.Combine(temppath, "shadowsocks.log");
  17. FileStream fs = new FileStream(LogFile, FileMode.Append);
  18. TextWriter tmp = Console.Out;
  19. StreamWriter sw = new StreamWriter(fs);
  20. sw.AutoFlush = true;
  21. Console.SetOut(sw);
  22. Console.SetError(sw);
  23. return true;
  24. }
  25. catch (IOException e)
  26. {
  27. Console.WriteLine(e.ToString());
  28. return false;
  29. }
  30. }
  31. public static void LogUsefulException(Exception e)
  32. {
  33. // just log useful exceptions, not all of them
  34. if (e is SocketException)
  35. {
  36. SocketException se = (SocketException)e;
  37. if (se.SocketErrorCode == SocketError.ConnectionAborted)
  38. {
  39. // closed by browser when sending
  40. // normally happens when download is canceled or a tab is closed before page is loaded
  41. }
  42. else if (se.SocketErrorCode == SocketError.ConnectionReset)
  43. {
  44. // received rst
  45. }
  46. else
  47. {
  48. Console.WriteLine(e);
  49. }
  50. }
  51. else
  52. {
  53. Console.WriteLine(e);
  54. }
  55. }
  56. }
  57. }