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

10 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 if (se.SocketErrorCode == SocketError.NotConnected)
  47. {
  48. // close when not connected
  49. }
  50. else
  51. {
  52. Console.WriteLine(e);
  53. }
  54. }
  55. else
  56. {
  57. Console.WriteLine(e);
  58. }
  59. }
  60. }
  61. }