diff --git a/src/Logger.cs b/src/Logger.cs new file mode 100644 index 0000000..d466a6f --- /dev/null +++ b/src/Logger.cs @@ -0,0 +1,71 @@ +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(e); + } + 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(e); + } + 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 diff --git a/src/Request.cs b/src/Request.cs index 2d86473..429a52d 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"; @@ -48,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; @@ -89,7 +98,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 ); @@ -184,13 +193,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 +224,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 +252,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 +339,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; }