diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-02-07 13:51:08 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-02-07 13:51:08 +0200 |
| commit | 6e082b1d9d7b4c5888e7e7345e148eb944b8c624 (patch) | |
| tree | 583efc08780196dbb3ae5fac714125b8e41387ae /Software/Visual_Studio | |
| parent | 942a428f90587a9ffdafaa593b780fb6ad06aabc (diff) | |
| download | Tango-6e082b1d9d7b4c5888e7e7345e148eb944b8c624.tar.gz Tango-6e082b1d9d7b4c5888e7e7345e148eb944b8c624.zip | |
Added logging categories!!!
Diffstat (limited to 'Software/Visual_Studio')
9 files changed, 116 insertions, 36 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModelLocator.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModelLocator.cs index 7ceab8268..597dfd819 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModelLocator.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModelLocator.cs @@ -17,6 +17,7 @@ using Tango.MachineStudio.UI.StudioApplication; using Tango.MachineStudio.UI.SupervisingController; using Tango.MachineStudio.UI.ViewModels; using Tango.MachineStudio.UI.Views; +using Tango.Settings; namespace Tango.MachineStudio.UI { @@ -67,7 +68,10 @@ namespace Tango.MachineStudio.UI if (!ViewModelBase.IsInDesignModeStatic) { - //LogManager.RegisterLogger(new VSOutputLogger()); + LogManager.Categories.Clear(); + LogManager.Categories.AddRange(SettingsManager.Default.MachineStudio.LoggingCategories); + + LogManager.RegisterLogger(new VSOutputLogger()); LogManager.RegisterLogger(new FileLogger()); } diff --git a/Software/Visual_Studio/Tango.Logging/ExceptionLogItem.cs b/Software/Visual_Studio/Tango.Logging/ExceptionLogItem.cs index acebc2856..d9bb8b6a7 100644 --- a/Software/Visual_Studio/Tango.Logging/ExceptionLogItem.cs +++ b/Software/Visual_Studio/Tango.Logging/ExceptionLogItem.cs @@ -36,7 +36,7 @@ namespace Tango.Logging /// </summary> public override string ToString() { - return String.Format("[{0}] [{1}] [{2}] [Line {3}]: {4}{5}", TimeStamp.ToString("HH:mm:ss.ff"), Path.GetFileNameWithoutExtension(CallerFile), CallerMethodName, CallerLineNumber, Description, Environment.NewLine + InnerException.Message); + return String.Format("[{0}] [{6}] [{1}] [{2}] [Line {3}]: {4}{5}", TimeStamp.ToString("HH:mm:ss.ff"), Path.GetFileNameWithoutExtension(CallerFile), CallerMethodName, CallerLineNumber, Description, Environment.NewLine + InnerException.Message, Category); } } diff --git a/Software/Visual_Studio/Tango.Logging/LogCategory.cs b/Software/Visual_Studio/Tango.Logging/LogCategory.cs new file mode 100644 index 000000000..f33190e2c --- /dev/null +++ b/Software/Visual_Studio/Tango.Logging/LogCategory.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Logging +{ + /// <summary> + /// Represents the different logging categories. + /// </summary> + public enum LogCategory + { + General = 1, + Debug = 2, + Warning = 4, + Error = 8, + Critical = 16, + } +} diff --git a/Software/Visual_Studio/Tango.Logging/LogItemBase.cs b/Software/Visual_Studio/Tango.Logging/LogItemBase.cs index aab78c63d..85451488a 100644 --- a/Software/Visual_Studio/Tango.Logging/LogItemBase.cs +++ b/Software/Visual_Studio/Tango.Logging/LogItemBase.cs @@ -32,6 +32,11 @@ namespace Tango.Logging public DateTime TimeStamp { get; set; } /// <summary> + /// Gets or sets the log category. + /// </summary> + public LogCategory Category { get; set; } + + /// <summary> /// Gets the log message. /// </summary> /// <returns></returns> diff --git a/Software/Visual_Studio/Tango.Logging/LogManager.cs b/Software/Visual_Studio/Tango.Logging/LogManager.cs index 3ada76104..0fe1e11b3 100644 --- a/Software/Visual_Studio/Tango.Logging/LogManager.cs +++ b/Software/Visual_Studio/Tango.Logging/LogManager.cs @@ -33,9 +33,21 @@ namespace Tango.Logging { _loggers = new List<ILogger>(); _logs = new ConcurrentQueue<LogItemBase>(); + Categories = new List<LogCategory>(); + + Categories.Add(LogCategory.Critical); + Categories.Add(LogCategory.Debug); + Categories.Add(LogCategory.Error); + Categories.Add(LogCategory.General); + Categories.Add(LogCategory.Warning); } /// <summary> + /// Gets or sets the logging categories filter. + /// </summary> + public static List<LogCategory> Categories { get; private set; } + + /// <summary> /// Registers a logger. /// </summary> /// <param name="logger">The logger.</param> @@ -66,12 +78,25 @@ namespace Tango.Logging /// <param name="description">Error description.</param> public static Exception Log(Exception e, String description = null, [CallerMemberName] string caller = null, [CallerFilePath] string file = null, [CallerLineNumber] int lineNumber = 0) { + return Log(e, LogCategory.Error, description, caller, file, lineNumber); + } + + /// <summary> + /// Add new exception log item. + /// </summary> + /// <param name="e">Exception.</param> + /// <param name="description">Error description.</param> + public static Exception Log(Exception e, LogCategory category, String description = null, [CallerMemberName] string caller = null, [CallerFilePath] string file = null, [CallerLineNumber] int lineNumber = 0) + { + if (!Categories.Contains(category)) return e; + ExceptionLogItem log = new ExceptionLogItem(); log.CallerMethodName = caller; log.CallerFile = file; log.CallerLineNumber = lineNumber; log.TimeStamp = DateTime.Now; log.InnerException = e; + log.Category = category; log.Description = description != null ? description : e.ToString(); if (!OverrideQueue) @@ -92,11 +117,23 @@ namespace Tango.Logging /// <param name="message">Message.</param> public static void Log(String message, [CallerMemberName] string caller = null, [CallerFilePath] string file = null, [CallerLineNumber] int lineNumber = 0) { + Log(message, LogCategory.General, caller, file, lineNumber); + } + + /// <summary> + /// Add new message log item. + /// </summary> + /// <param name="message">Message.</param> + public static void Log(String message, LogCategory category, [CallerMemberName] string caller = null, [CallerFilePath] string file = null, [CallerLineNumber] int lineNumber = 0) + { + if (!Categories.Contains(category)) return; + MessageLogItem log = new MessageLogItem(); log.CallerMethodName = caller; log.CallerFile = file; log.CallerLineNumber = lineNumber; log.TimeStamp = DateTime.Now; + log.Category = category; log.Message = message; if (!OverrideQueue) @@ -210,7 +247,7 @@ namespace Tango.Logging wroteLog = true; } - catch + catch { Thread.Sleep(5); } diff --git a/Software/Visual_Studio/Tango.Logging/MessageLogItem.cs b/Software/Visual_Studio/Tango.Logging/MessageLogItem.cs index 445706779..f7d7e004c 100644 --- a/Software/Visual_Studio/Tango.Logging/MessageLogItem.cs +++ b/Software/Visual_Studio/Tango.Logging/MessageLogItem.cs @@ -31,7 +31,7 @@ namespace Tango.Logging /// </summary> public override string ToString() { - return String.Format("[{0}] [{1}] [{2}] [Line {3}]: {4}", TimeStamp.ToString("HH:mm:ss.ff"), Path.GetFileNameWithoutExtension(CallerFile), CallerMethodName, CallerLineNumber, Message); + return String.Format("[{0}] [{5}] [{1}] [{2}] [Line {3}]: {4}", TimeStamp.ToString("HH:mm:ss.ff"), Path.GetFileNameWithoutExtension(CallerFile), CallerMethodName, CallerLineNumber, Message, Category); } } } diff --git a/Software/Visual_Studio/Tango.Logging/Tango.Logging.csproj b/Software/Visual_Studio/Tango.Logging/Tango.Logging.csproj index 93d0cd4ea..1e70e4cb1 100644 --- a/Software/Visual_Studio/Tango.Logging/Tango.Logging.csproj +++ b/Software/Visual_Studio/Tango.Logging/Tango.Logging.csproj @@ -59,6 +59,7 @@ <Compile Include="GlobalExceptionTrapper.cs" /> <Compile Include="IGlobalExceptionTrapper.cs" /> <Compile Include="ILogger.cs" /> + <Compile Include="LogCategory.cs" /> <Compile Include="LogItemBase.cs" /> <Compile Include="LogManager.cs" /> <Compile Include="MessageLogItem.cs" /> diff --git a/Software/Visual_Studio/Tango.Settings/MachineStudioSettings/MachineStudio.cs b/Software/Visual_Studio/Tango.Settings/MachineStudioSettings/MachineStudio.cs index 951d4a407..70199d0ef 100644 --- a/Software/Visual_Studio/Tango.Settings/MachineStudioSettings/MachineStudio.cs +++ b/Software/Visual_Studio/Tango.Settings/MachineStudioSettings/MachineStudio.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core.Cryptography; +using Tango.Logging; namespace Tango.Settings.MachineStudioSettings { @@ -28,6 +29,11 @@ namespace Tango.Settings.MachineStudioSettings public bool RememberMe { get; set; } /// <summary> + /// Gets or sets the logging categories. + /// </summary> + public List<LogCategory> LoggingCategories { get; set; } + + /// <summary> /// Gets or sets the synchronization module settings. /// </summary> public SynchronizationModule SynchronizationModule { get; set; } @@ -52,6 +58,13 @@ namespace Tango.Settings.MachineStudioSettings /// </summary> public MachineStudio() { + LoggingCategories = new List<LogCategory>(); + + LoggingCategories.Add(LogCategory.Critical); + LoggingCategories.Add(LogCategory.Error); + LoggingCategories.Add(LogCategory.General); + LoggingCategories.Add(LogCategory.Warning); + SynchronizationModule = new SynchronizationModule(); StubsModule = new StubsModule(); TechnicianModule = new TechnicianModule(); diff --git a/Software/Visual_Studio/Tango.Transport/TransporterBase.cs b/Software/Visual_Studio/Tango.Transport/TransporterBase.cs index c1d0ba368..c4232e5b2 100644 --- a/Software/Visual_Studio/Tango.Transport/TransporterBase.cs +++ b/Software/Visual_Studio/Tango.Transport/TransporterBase.cs @@ -260,8 +260,8 @@ namespace Tango.Transport /// <returns></returns> public Task<TangoMessage<Response>> SendRequest<Request, Response>(TangoMessage<Request> request, TimeSpan? timeout = null) where Request : IMessage<Request> where Response : IMessage<Response> { - LogManager.Log("Queuing request message: " + typeof(Request).Name + " Token: " + request.Container.Token); - LogManager.Log("Expected response: " + typeof(Response).Name); + LogManager.Log("Queuing request message: " + typeof(Request).Name + " Token: " + request.Container.Token, LogCategory.Debug); + LogManager.Log("Expected response: " + typeof(Response).Name, LogCategory.Debug); TaskCompletionSource<TangoMessage<Response>> source = new TaskCompletionSource<TangoMessage<Response>>(); TransportMessage<TangoMessage<Response>> message = new TransportMessage<TangoMessage<Response>>(request.Container.Token, request, TransportMessageDirection.Request, () => Encoder.Encode(request), source); @@ -270,8 +270,8 @@ namespace Tango.Transport { if (!source.Task.IsCompleted) { - LogManager.Log("Request message: " + typeof(Request).Name + " had timed out after " + (timeout != null ? timeout.Value.TotalSeconds : RequestTimeout.TotalSeconds) + " seconds."); - LogManager.Log("Setting request task exception..."); + LogManager.Log(new TimeoutException("Request message: " + typeof(Request).Name + " had timed out after " + (timeout != null ? timeout.Value.TotalSeconds : RequestTimeout.TotalSeconds) + " seconds.")); + LogManager.Log("Setting request task exception...", LogCategory.Debug); source.SetException(new TimeoutException()); } }); @@ -280,11 +280,11 @@ namespace Tango.Transport public IObservable<TangoMessage<Response>> SendContinuousRequest<Request, Response>(TangoMessage<Request> request, TimeSpan? timeout = default(TimeSpan?)) where Request : IMessage<Request> where Response : IMessage<Response> { - LogManager.Log("Queuing continuous request message: " + typeof(Request).Name + " Token: " + request.Container.Token); + LogManager.Log("Queuing continuous request message: " + typeof(Request).Name + " Token: " + request.Container.Token, LogCategory.Debug); Subject<TangoMessage<Response>> subject = new Subject<TangoMessage<Response>>(); - LogManager.Log("Expected response: " + typeof(Response).Name); + LogManager.Log("Expected response: " + typeof(Response).Name, LogCategory.Debug); request.Container.Continuous = true; request.Container.Completed = false; @@ -300,8 +300,8 @@ namespace Tango.Transport { if (!message.AtLeastOneResponseReceived) { - LogManager.Log("Request message: " + typeof(Request).Name + " had timed out after " + (timeout != null ? timeout.Value.TotalSeconds : RequestTimeout.TotalSeconds) + " seconds."); - LogManager.Log("Setting request exception..."); + LogManager.Log(new TimeoutException("Request message: " + typeof(Request).Name + " had timed out after " + (timeout != null ? timeout.Value.TotalSeconds : RequestTimeout.TotalSeconds) + " seconds.")); + LogManager.Log("Setting request exception...", LogCategory.Debug); message.SetException(new TimeoutException()); } }); @@ -330,37 +330,37 @@ namespace Tango.Transport public Task SendResponse<Response>(TangoMessage<Response> response, String token, bool? completed = null) where Response : IMessage<Response> { response.Container.Token = token; - + if (completed.HasValue) { response.Container.Completed = completed.Value; } - LogManager.Log("Queuing response message: " + typeof(Response).Name); + LogManager.Log("Queuing response message: " + typeof(Response).Name, LogCategory.Debug); PendingResponse pendingResponse = null; - LogManager.Log("Searching for matching request token: " + token); + LogManager.Log("Searching for matching request token: " + token, LogCategory.Debug); if (_pendingResponses.TryGetValue(token, out pendingResponse)) { - LogManager.Log("Found matching request token: " + token); + LogManager.Log("Found matching request token: " + token, LogCategory.Debug); if (!pendingResponse.IsContinuous) { - LogManager.Log("Removing matching request token."); + LogManager.Log("Removing matching request token.", LogCategory.Debug); _pendingResponses.Remove(token); } else if (response.Container.Completed) { - LogManager.Log("Response completed. Removing matching request token."); + LogManager.Log("Response completed. Removing matching request token.", LogCategory.Debug); _pendingResponses.Remove(token); } } else { //This should never happen. - throw LogManager.Log(new InvalidOperationException("Matching request token was not found!")); + throw LogManager.Log(new InvalidOperationException("Matching request token was not found!"), LogCategory.Critical); } TaskCompletionSource<object> source = new TaskCompletionSource<object>(); @@ -417,7 +417,7 @@ namespace Tango.Transport if (Adapter.State == TransportComponentState.Connected) { Adapter.Write(message.Serialize()); - LogManager.Log("Message sent on adapter: " + Adapter.Address + "..."); + LogManager.Log("Message sent on adapter: " + Adapter.Address + "...", LogCategory.Debug); } if (message.Direction == TransportMessageDirection.Request) @@ -464,20 +464,20 @@ namespace Tango.Transport { if (_arrivedResponses.TryDequeue(out data)) { - LogManager.Log("Message received on adapter: " + Adapter.Address); + LogManager.Log("Message received on adapter: " + Adapter.Address, LogCategory.Debug); - LogManager.Log("Parsing message container..."); + LogManager.Log("Parsing message container...", LogCategory.Debug); MessageContainer container = Encoder.DecodeContainer(data); - LogManager.Log("Searching for pending request token: " + container.Token); + LogManager.Log("Searching for pending request token: " + container.Token, LogCategory.Debug); TransportMessageBase request = _pendingRequests.SingleOrDefault(x => x.Token == container.Token); if (request != null) { - LogManager.Log("Found pending request: " + request.Message.GetType().GetGenericArguments()[0].Name); + LogManager.Log("Found pending request: " + request.Message.GetType().GetGenericArguments()[0].Name, LogCategory.Debug); if (!request.IsContinuous) { - LogManager.Log("Pending request was identified as 'single response'. Removing pending request."); + LogManager.Log("Pending request was identified as 'single response'. Removing pending request.", LogCategory.Debug); _pendingRequests.Remove(request); @@ -485,13 +485,13 @@ namespace Tango.Transport { if (container.Error == ErrorCode.None) { - LogManager.Log("Parsing inner response message and setting pending request task result..."); + LogManager.Log("Parsing inner response message and setting pending request task result...", LogCategory.Debug); request.SetResult(Encoder.Decode(data), true); - LogManager.Log("Message enquirer released..."); + LogManager.Log("Message enquirer released...", LogCategory.Debug); } else { - LogManager.Log("Response has returned with error: " + container.Error.ToString()); + LogManager.Log("Response has returned with error: " + container.Error.ToString(), LogCategory.Warning); request.SetException(new ResponseErrorException(container.Error)); } } @@ -502,22 +502,22 @@ namespace Tango.Transport } else { - LogManager.Log("Pending request was identified as 'continuous response'. keeping pending request."); + LogManager.Log("Pending request was identified as 'continuous response'. keeping pending request.", LogCategory.Debug); try { if (container.Error == ErrorCode.None) { - LogManager.Log("Parsing inner response message and invoking continuous response callback..."); + LogManager.Log("Parsing inner response message and invoking continuous response callback...", LogCategory.Debug); if (container.Completed) { - LogManager.Log("Continuous sequence completed."); + LogManager.Log("Continuous sequence completed.", LogCategory.Debug); } request.SetResult(Encoder.Decode(data), container.Completed); } else { - LogManager.Log("Response has returned with error: " + container.Error.ToString()); + LogManager.Log("Response has returned with error: " + container.Error.ToString(), LogCategory.Warning); request.SetException(new ResponseErrorException(container.Error)); } } @@ -529,21 +529,21 @@ namespace Tango.Transport } else { - LogManager.Log("Message was identified as a new request message: " + container.Type.ToString()); + LogManager.Log("Message was identified as a new request message: " + container.Type.ToString(), LogCategory.Debug); try { - LogManager.Log("Saving request token: " + container.Token); + LogManager.Log("Saving request token: " + container.Token, LogCategory.Debug); _pendingResponses.Add(container.Token, new PendingResponse(container.Continuous)); if (container.Type == MessageType.KeepAliveRequest) { - LogManager.Log("Submitting keep alive response..."); + LogManager.Log("Submitting keep alive response...", LogCategory.Debug); SendResponse<KeepAliveResponse>(new KeepAliveResponse(), container.Token); } else { - LogManager.Log("Invoking RequestReceived event..."); + LogManager.Log("Invoking RequestReceived event...", LogCategory.Debug); Task.Factory.StartNew(() => OnRequestReceived(container)); } } |
