From f623acf0f1e6565c581e7f3c0dd51920bc2520ce Mon Sep 17 00:00:00 2001 From: Dmitry Radkovskiy Date: Wed, 19 Jul 2017 20:04:11 +0300 Subject: [PATCH 1/6] Fix Unity 2017 compiler define --- src/Request.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Request.cs b/src/Request.cs index 2d86473..e61bdfa 100644 --- a/src/Request.cs +++ b/src/Request.cs @@ -89,7 +89,7 @@ public Request( string method, string uri, WWWForm form ) this.method = method; this.uri = new Uri (uri); this.byteStream = new MemoryStream(form.data); -#if UNITY_5 +#if UNITY_5 || UNITY_5_3_OR_NEWER foreach ( var entry in form.headers ) { this.AddHeader( entry.Key, entry.Value ); From bf07dfaa34b9f1ccdb548298e1d44a62db673e17 Mon Sep 17 00:00:00 2001 From: "d.radkovskiy" Date: Fri, 8 Sep 2017 17:03:33 +0300 Subject: [PATCH 2/6] Added ILogger, ConsoleLogger, UnityLogger --- src/Logger.cs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/Logger.cs diff --git a/src/Logger.cs b/src/Logger.cs new file mode 100644 index 0000000..cdf9f0e --- /dev/null +++ b/src/Logger.cs @@ -0,0 +1,52 @@ +using System; + +namespace UnityHTTP +{ + public interface ILogger + { + void Log(string msg); + void LogError(string msg); + void LogException(Exception e); + void LogWarning(string msg); + } +#if UNITY_EDITOR + public class UnityLogger : ILogger + { + public void Log(string msg) + { + UnityEngine.Debug.Log(msg); + } + public void LogError(string msg) + { + UnityEngine.Debug.LogError(msg); + } + public void LogException(Exception e) + { + UnityEngine.Debug.LogException(msg); + } + public void LogWarning(string msg) + { + UnityEngine.Debug.LogWarning(msg); + } + } +#endif + public class ConsoleLogger : ILogger + { + public void Log(string msg) + { + Console.WriteLine(msg); + } + public void LogError(string msg) + { + Console.WriteLine(msg); + } + public void LogException(Exception e) + { + Console.WriteLine(msg); + } + public void LogWarning(string msg) + { + Console.WriteLine(msg); + } + } +} \ No newline at end of file From 5663f4a0e5b8d17a22e74291bb8d776fc40ba25a Mon Sep 17 00:00:00 2001 From: "d.radkovskiy" Date: Fri, 8 Sep 2017 17:04:03 +0300 Subject: [PATCH 3/6] refactored Request to use UnityLogger/ConsoleLogger where appropriate --- src/Request.cs | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/Request.cs b/src/Request.cs index e61bdfa..38970b1 100644 --- a/src/Request.cs +++ b/src/Request.cs @@ -30,7 +30,14 @@ public class Request public static bool LogAllRequests = false; public static bool VerboseLogging = false; public static string unityVersion = Application.unityVersion; - public static string operatingSystem = SystemInfo.operatingSystem; + public static string operatingSystem = SystemInfo.operatingSystem; + public static ILogger logger = +#if !UNITY_EDITOR + new ConsoleLogger() +#else + new UnityLogger() +#endif + ; public CookieJar cookieJar = CookieJar.Instance; public string method = "GET"; @@ -184,13 +191,8 @@ private void GetResponse() { var ssl = ostream as SslStream; ssl.AuthenticateAsClient (uri.Host); } catch (Exception e) { -#if !UNITY_EDITOR - Console.WriteLine ("SSL authentication failed."); - Console.WriteLine (e); -#else - Debug.LogError ("SSL authentication failed."); - Debug.LogException(e); -#endif + logger.LogError ("SSL authentication failed."); + logger.LogException(e); return; } } @@ -220,13 +222,8 @@ private void GetResponse() { } } catch (Exception e) { -#if !UNITY_EDITOR - Console.WriteLine ("Unhandled Exception, aborting request."); - Console.WriteLine (e); -#else - Debug.LogError("Unhandled Exception, aborting request."); - Debug.LogException(e); -#endif + logger.LogError("Unhandled Exception, aborting request."); + logger.LogException(e); exception = e; response = null; } @@ -253,19 +250,19 @@ private void GetResponse() { if ( LogAllRequests ) { #if !UNITY_EDITOR - System.Console.WriteLine("NET: " + InfoString( VerboseLogging )); + logger.Log("NET: " + InfoString( VerboseLogging )); #else if ( response != null && response.status >= 200 && response.status < 300 ) { - Debug.Log( InfoString( VerboseLogging ) ); + logger.Log( InfoString( VerboseLogging ) ); } else if ( response != null && response.status >= 400 ) { - Debug.LogError( InfoString( VerboseLogging ) ); + logger.LogError( InfoString( VerboseLogging ) ); } else { - Debug.LogWarning( InfoString( VerboseLogging ) ); + logger.LogWarning( InfoString( VerboseLogging ) ); } #endif } @@ -340,9 +337,9 @@ public string Text { public static bool ValidateServerCertificate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { #if !UNITY_EDITOR - System.Console.WriteLine( "NET: SSL Cert: " + sslPolicyErrors.ToString() ); + logger.LogWarning( "NET: SSL Cert: " + sslPolicyErrors.ToString() ); #else - Debug.LogWarning("SSL Cert Error: " + sslPolicyErrors.ToString ()); + logger.LogWarning("SSL Cert Error: " + sslPolicyErrors.ToString ()); #endif return true; } From 51b0b3da5d348ab783aaeffac2844981aa25dc60 Mon Sep 17 00:00:00 2001 From: "d.radkovskiy" Date: Fri, 8 Sep 2017 17:05:59 +0300 Subject: [PATCH 4/6] added loggers for every request --- src/Request.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Request.cs b/src/Request.cs index 38970b1..78a9478 100644 --- a/src/Request.cs +++ b/src/Request.cs @@ -55,6 +55,8 @@ public class Request public long responseTime = 0; // in milliseconds public bool synchronous = false; public int bufferSize = 4 * 1024; + + public ILogger logger = Request.logger; public Action< UnityHTTP.Request > completedCallback = null; From 2ba38524bdc5cd46134389f63d4e6361a8717a25 Mon Sep 17 00:00:00 2001 From: "d.radkovskiy" Date: Fri, 8 Sep 2017 17:19:25 +0300 Subject: [PATCH 5/6] compiler errors fixes --- src/Logger.cs | 4 ++-- src/Request.cs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Logger.cs b/src/Logger.cs index cdf9f0e..193096f 100644 --- a/src/Logger.cs +++ b/src/Logger.cs @@ -22,7 +22,7 @@ public void LogError(string msg) } public void LogException(Exception e) { - UnityEngine.Debug.LogException(msg); + UnityEngine.Debug.LogException(e); } public void LogWarning(string msg) { @@ -42,7 +42,7 @@ public void LogError(string msg) } public void LogException(Exception e) { - Console.WriteLine(msg); + Console.WriteLine(e); } public void LogWarning(string msg) { diff --git a/src/Request.cs b/src/Request.cs index 78a9478..429a52d 100644 --- a/src/Request.cs +++ b/src/Request.cs @@ -31,7 +31,7 @@ public class Request public static bool VerboseLogging = false; public static string unityVersion = Application.unityVersion; public static string operatingSystem = SystemInfo.operatingSystem; - public static ILogger logger = + public static ILogger Logger = #if !UNITY_EDITOR new ConsoleLogger() #else @@ -56,7 +56,7 @@ public class Request public bool synchronous = false; public int bufferSize = 4 * 1024; - public ILogger logger = Request.logger; + public ILogger logger = Request.Logger; public Action< UnityHTTP.Request > completedCallback = null; @@ -339,9 +339,9 @@ public string Text { public static bool ValidateServerCertificate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { #if !UNITY_EDITOR - logger.LogWarning( "NET: SSL Cert: " + sslPolicyErrors.ToString() ); + Logger.LogWarning( "NET: SSL Cert: " + sslPolicyErrors.ToString() ); #else - logger.LogWarning("SSL Cert Error: " + sslPolicyErrors.ToString ()); + Logger.LogWarning("SSL Cert Error: " + sslPolicyErrors.ToString ()); #endif return true; } From f41bad5587ab6fdb7703c1c60e4bd5490f638665 Mon Sep 17 00:00:00 2001 From: "d.radkovskiy" Date: Fri, 8 Sep 2017 17:21:30 +0300 Subject: [PATCH 6/6] added DiscardLogger --- src/Logger.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Logger.cs b/src/Logger.cs index 193096f..d466a6f 100644 --- a/src/Logger.cs +++ b/src/Logger.cs @@ -49,4 +49,23 @@ public void LogWarning(string msg) Console.WriteLine(msg); } } + public class DiscardLogger : ILogger + { + public void Log(string msg) + { + // discard logs + } + public void LogError(string msg) + { + // discard logs + } + public void LogException(Exception e) + { + // discard logs + } + public void LogWarning(string msg) + { + // discard logs + } + } } \ No newline at end of file