Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
LogListener.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 
5 namespace SiliconStudio.Core.Diagnostics
6 {
7  /// <summary>
8  /// A base class to implement a log listener
9  /// </summary>
10  public abstract class LogListener : IDisposable
11  {
12  private int logCountFlushLimit;
13 
14  /// <summary>
15  /// Initializes a new instance of the <see cref="LogListener"/> class.
16  /// </summary>
17  protected LogListener()
18  {
19  LogCountFlushLimit = 1;
20  TextFormatter = msg => msg.ToString();
21  }
22 
23  /// <summary>
24  /// Gets the log message count.
25  /// </summary>
26  /// <value>The log message count.</value>
27  public int LogMessageCount { get; private set; }
28 
29  /// <summary>
30  /// Gets or sets a value indicating whether [use flush async].
31  /// </summary>
32  /// <value><c>true</c> if [use flush async]; otherwise, <c>false</c>.</value>
33  public bool UseFlushAsync { get; set; }
34 
35  /// <summary>
36  /// Gets or sets the function that convert a <see cref="ILogMessage" /> instance into a string.
37  /// </summary>
38  public Func<ILogMessage, string> TextFormatter { get; set; }
39 
40  /// <summary>
41  /// Gets or sets the log count flush limit. Default is on every message.
42  /// </summary>
43  /// <value>The log count flush limit.</value>
44  public int LogCountFlushLimit
45  {
46  get { return logCountFlushLimit; }
47  set
48  {
49  if (value <= 0)
50  {
51  throw new ArgumentException("Value must be > 0");
52  }
53 
54  logCountFlushLimit = value;
55  }
56  }
57 
58  /// <summary>
59  /// Called when a log occurred.
60  /// </summary>
61  /// <param name="logMessage">The log message.</param>
62  protected abstract void OnLog(ILogMessage logMessage);
63 
64  /// <summary>
65  /// Returns a boolean indicating whether the log should be flushed. By default, flushing is occuring if the message has a higher level than <see cref="LogMessageType.Info"/>
66  /// </summary>
67  /// <param name="logMessage">The log message.</param>
68  /// <returns><c>true</c> if the log should be flushed, <c>false</c> otherwise</returns>
69  protected virtual bool ShouldFlush(ILogMessage logMessage)
70  {
71  // By default flush if we have more than the level info (Warning, Error, Fatal)
72  return (logMessage.Type > LogMessageType.Info) || (LogMessageCount % LogCountFlushLimit) == 0;
73  }
74 
75  /// <summary>
76  /// Flush the log, method to be implemented in a subclass.
77  /// </summary>
78  protected virtual void Flush()
79  {
80  }
81 
82  /// <summary>
83  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
84  /// </summary>
85  public virtual void Dispose()
86  {
87  }
88 
89  /// <summary>
90  /// Gets the default text for a particular log message.
91  /// </summary>
92  /// <param name="logMessage">The log message.</param>
93  /// <returns>A textual representation of a message.</returns>
94  protected virtual string GetDefaultText(ILogMessage logMessage)
95  {
96  return TextFormatter(logMessage);
97  }
98 
99  /// <summary>
100  /// Gets the text that describes the exception associated to a particular log message.
101  /// </summary>
102  /// <param name="message">The log message.</param>
103  /// <returns>A textual representation of the exception, or <see cref="string.Empty"/> if no exception is associated to this log message.</returns>
104  protected virtual string GetExceptionText(ILogMessage message)
105  {
106  var serializableLogMessage = message as SerializableLogMessage;
107  if (serializableLogMessage != null)
108  {
109  return serializableLogMessage.ExceptionInfo != null ? serializableLogMessage.ExceptionInfo.ToString() : string.Empty;
110  }
111  var logMessage = message as LogMessage;
112  if (logMessage != null)
113  {
114  return logMessage.Exception != null ? logMessage.Exception.ToString() : string.Empty;
115  }
116  throw new ArgumentException("Unsupported log message.");
117  }
118 
119  /// <summary>
120  /// Performs an implicit conversion from <see cref="LogListener"/> to <see cref="Action{ILogMessage}"/>.
121  /// </summary>
122  /// <param name="logListener">The log listener.</param>
123  /// <returns>The result of the conversion.</returns>
124  public static implicit operator Action<ILogMessage>(LogListener logListener)
125  {
126  return logListener.OnLogInternal;
127  }
128 
129  private void OnLogInternal(ILogMessage logMessage)
130  {
131  OnLog(logMessage);
132  LogMessageCount++;
133  if (ShouldFlush(logMessage))
134  {
135  Flush();
136  }
137  }
138  }
139 }
virtual void Flush()
Flush the log, method to be implemented in a subclass.
Definition: LogListener.cs:78
virtual void Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resourc...
Definition: LogListener.cs:85
A class that represents a copy of a LogMessage that can be serialized.
LogMessageType
Type of a LogMessage.
virtual string GetExceptionText(ILogMessage message)
Gets the text that describes the exception associated to a particular log message.
Definition: LogListener.cs:104
virtual string GetDefaultText(ILogMessage logMessage)
Gets the default text for a particular log message.
Definition: LogListener.cs:94
A base log message used by the logging infrastructure.
Definition: LogMessage.cs:13
A base class to implement a log listener
Definition: LogListener.cs:10
The base interface for log messages used by the logging infrastructure.
Definition: ILogMessage.cs:8
LogMessageType Type
Gets or sets the type of this message.
Definition: ILogMessage.cs:23
virtual bool ShouldFlush(ILogMessage logMessage)
Returns a boolean indicating whether the log should be flushed. By default, flushing is occuring if t...
Definition: LogListener.cs:69
LogListener()
Initializes a new instance of the LogListener class.
Definition: LogListener.cs:17