From 493231410314512d3bd3009b4b5e4fab7cff11b8 Mon Sep 17 00:00:00 2001 From: Virgile Bello Date: Thu, 9 Jul 2015 00:51:38 +0900 Subject: [PATCH] Add warning logging. --- SharpYaml/Serialization/Logging/ILogger.cs | 12 ++++++ SharpYaml/Serialization/Logging/LogLevel.cs | 11 +++++ SharpYaml/Serialization/Serializer.cs | 40 +++++++++++-------- SharpYaml/Serialization/SerializerContext.cs | 16 +++++++- .../SerializerContextSettings.cs | 20 ++++++++++ .../Serializers/CollectionSerializer.cs | 7 +++- .../Serializers/DictionarySerializer.cs | 9 +++-- .../Serializers/ObjectSerializer.cs | 7 +++- SharpYaml/SharpYAml.csproj | 3 ++ 9 files changed, 101 insertions(+), 24 deletions(-) create mode 100644 SharpYaml/Serialization/Logging/ILogger.cs create mode 100644 SharpYaml/Serialization/Logging/LogLevel.cs create mode 100644 SharpYaml/Serialization/SerializerContextSettings.cs diff --git a/SharpYaml/Serialization/Logging/ILogger.cs b/SharpYaml/Serialization/Logging/ILogger.cs new file mode 100644 index 0000000..ef64fce --- /dev/null +++ b/SharpYaml/Serialization/Logging/ILogger.cs @@ -0,0 +1,12 @@ +using System; + +namespace SharpYaml.Serialization.Logging +{ + /// + /// Logger interface. + /// + public interface ILogger + { + void Log(LogLevel level, Exception ex, string message); + } +} \ No newline at end of file diff --git a/SharpYaml/Serialization/Logging/LogLevel.cs b/SharpYaml/Serialization/Logging/LogLevel.cs new file mode 100644 index 0000000..8e8dc98 --- /dev/null +++ b/SharpYaml/Serialization/Logging/LogLevel.cs @@ -0,0 +1,11 @@ +namespace SharpYaml.Serialization.Logging +{ + /// + /// Severity log level. + /// + public enum LogLevel + { + Error = 0, + Warning = 1, + } +} \ No newline at end of file diff --git a/SharpYaml/Serialization/Serializer.cs b/SharpYaml/Serialization/Serializer.cs index af3e03d..ccfc6f8 100644 --- a/SharpYaml/Serialization/Serializer.cs +++ b/SharpYaml/Serialization/Serializer.cs @@ -110,11 +110,12 @@ namespace SharpYaml.Serialization /// /// The graph. /// The expected type. + /// The context settings. /// A YAML string of the object. - public string Serialize(object graph, Type expectedType) + public string Serialize(object graph, Type expectedType, SerializerContextSettings contextSettings = null) { var stringWriter = new StringWriter(); - Serialize(stringWriter, graph, expectedType); + Serialize(stringWriter, graph, expectedType, contextSettings); return stringWriter.ToString(); } @@ -133,12 +134,13 @@ namespace SharpYaml.Serialization /// /// The stream. /// The object to serialize. - public void Serialize(Stream stream, object graph, Type expectedType) + /// The context settings. + public void Serialize(Stream stream, object graph, Type expectedType, SerializerContextSettings contextSettings = null) { var writer = new StreamWriter(stream); try { - Serialize(writer, graph, expectedType); + Serialize(writer, graph, expectedType, contextSettings); } finally { @@ -168,9 +170,10 @@ namespace SharpYaml.Serialization /// The where to serialize the object. /// The object to serialize. /// The static type of the object to serialize. - public void Serialize(TextWriter writer, object graph, Type type) + /// The context settings. + public void Serialize(TextWriter writer, object graph, Type type, SerializerContextSettings contextSettings = null) { - Serialize(new Emitter(writer, Settings.PreferredIndent), graph, type); + Serialize(new Emitter(writer, Settings.PreferredIndent), graph, type, contextSettings); } /// @@ -189,7 +192,8 @@ namespace SharpYaml.Serialization /// The where to serialize the object. /// The object to serialize. /// The static type of the object to serialize. - public void Serialize(IEmitter emitter, object graph, Type type) + /// The context settings. + public void Serialize(IEmitter emitter, object graph, Type type, SerializerContextSettings contextSettings = null) { if (emitter == null) { @@ -210,7 +214,7 @@ namespace SharpYaml.Serialization defaultEmitter.ForceIndentLess = settings.IndentLess; } - var context = new SerializerContext(this) {Emitter = emitter, Writer = CreateEmitter(emitter)}; + var context = new SerializerContext(this, contextSettings) { Emitter = emitter, Writer = CreateEmitter(emitter) }; // Serialize the document context.Writer.StreamStart(); @@ -245,13 +249,14 @@ namespace SharpYaml.Serialization /// /// The stream. /// The expected type. + /// The context settings. /// A deserialized object. /// stream - public object Deserialize(Stream stream, Type expectedType) + public object Deserialize(Stream stream, Type expectedType, SerializerContextSettings contextSettings = null) { if (stream == null) throw new ArgumentNullException("stream"); - return Deserialize(new StreamReader(stream), expectedType); + return Deserialize(new StreamReader(stream), expectedType, contextSettings); } /// @@ -271,12 +276,13 @@ namespace SharpYaml.Serialization /// /// The reader. /// The expected type. + /// The context settings. /// A deserialized object. /// reader - public object Deserialize(TextReader reader, Type expectedType) + public object Deserialize(TextReader reader, Type expectedType, SerializerContextSettings contextSettings = null) { if (reader == null) throw new ArgumentNullException("reader"); - return Deserialize(new EventReader(new Parser(reader)), expectedType); + return Deserialize(new EventReader(new Parser(reader)), expectedType, contextSettings); } /// @@ -343,11 +349,12 @@ namespace SharpYaml.Serialization /// /// The reader. /// The expected type. + /// The context settings. /// A deserialized object. /// reader - public object Deserialize(EventReader reader, Type expectedType) + public object Deserialize(EventReader reader, Type expectedType, SerializerContextSettings contextSettings = null) { - return Deserialize(reader, null, expectedType); + return Deserialize(reader, null, expectedType, contextSettings); } /// @@ -356,9 +363,10 @@ namespace SharpYaml.Serialization /// The reader. /// The value. /// The expected type. + /// The context settings. /// A deserialized object. /// reader - public object Deserialize(EventReader reader, object value, Type expectedType) + public object Deserialize(EventReader reader, object value, Type expectedType, SerializerContextSettings contextSettings = null) { if (reader == null) throw new ArgumentNullException("reader"); @@ -368,7 +376,7 @@ namespace SharpYaml.Serialization object result = null; if (!reader.Accept() && !reader.Accept()) { - var context = new SerializerContext(this) {Reader = reader}; + var context = new SerializerContext(this, contextSettings) { Reader = reader }; result = context.ReadYaml(value, expectedType); } diff --git a/SharpYaml/Serialization/SerializerContext.cs b/SharpYaml/Serialization/SerializerContext.cs index 05e7df8..d4d69de 100644 --- a/SharpYaml/Serialization/SerializerContext.cs +++ b/SharpYaml/Serialization/SerializerContext.cs @@ -59,13 +59,15 @@ namespace SharpYaml.Serialization private readonly ITagTypeRegistry tagTypeRegistry; private readonly ITypeDescriptorFactory typeDescriptorFactory; private IEmitter emitter; + private readonly SerializerContextSettings contextSettings; internal int AnchorCount; /// /// Initializes a new instance of the class. /// /// The serializer. - internal SerializerContext(Serializer serializer) + /// The serializer context settings. + internal SerializerContext(Serializer serializer, SerializerContextSettings serializerContextSettings) { Serializer = serializer; settings = serializer.Settings; @@ -75,6 +77,7 @@ namespace SharpYaml.Serialization Schema = Settings.Schema; ObjectSerializer = serializer.ObjectSerializer; typeDescriptorFactory = serializer.TypeDescriptorFactory; + contextSettings = serializerContextSettings ?? SerializerContextSettings.Default; } /// @@ -86,6 +89,17 @@ namespace SharpYaml.Serialization get { return Writer != null; } } + /// + /// Gets the context settings. + /// + /// + /// The context settings. + /// + public SerializerContextSettings ContextSettings + { + get { return contextSettings; } + } + /// /// Gets the settings. /// diff --git a/SharpYaml/Serialization/SerializerContextSettings.cs b/SharpYaml/Serialization/SerializerContextSettings.cs new file mode 100644 index 0000000..19f6987 --- /dev/null +++ b/SharpYaml/Serialization/SerializerContextSettings.cs @@ -0,0 +1,20 @@ +using SharpYaml.Serialization.Logging; + +namespace SharpYaml.Serialization +{ + /// + /// Some parameters that can be transmitted from caller + /// + public class SerializerContextSettings + { + public static readonly SerializerContextSettings Default = new SerializerContextSettings(); + + /// + /// Gets the logger. + /// + /// + /// The logger. + /// + public ILogger Logger { get; set; } + } +} \ No newline at end of file diff --git a/SharpYaml/Serialization/Serializers/CollectionSerializer.cs b/SharpYaml/Serialization/Serializers/CollectionSerializer.cs index 6e912a1..fbd14ac 100644 --- a/SharpYaml/Serialization/Serializers/CollectionSerializer.cs +++ b/SharpYaml/Serialization/Serializers/CollectionSerializer.cs @@ -47,6 +47,7 @@ using System.Collections; using System.Collections.Generic; using SharpYaml.Events; using SharpYaml.Serialization.Descriptors; +using SharpYaml.Serialization.Logging; namespace SharpYaml.Serialization.Serializers { @@ -165,9 +166,11 @@ namespace SharpYaml.Serialization.Serializers { ReadAddCollectionItem(ref objectContext, elementType, collectionDescriptor, thisObject, index); } - catch (YamlException) + catch (YamlException ex) { - // TODO: Warning? + var logger = objectContext.SerializerContext.ContextSettings.Logger; + if (logger != null) + logger.Log(LogLevel.Warning, ex, "Ignored collection item that could not be deserialized"); objectContext.Reader.Skip(currentDepth); } } diff --git a/SharpYaml/Serialization/Serializers/DictionarySerializer.cs b/SharpYaml/Serialization/Serializers/DictionarySerializer.cs index 7952267..72c0fba 100644 --- a/SharpYaml/Serialization/Serializers/DictionarySerializer.cs +++ b/SharpYaml/Serialization/Serializers/DictionarySerializer.cs @@ -47,6 +47,7 @@ using System.Collections.Generic; using System.Linq; using SharpYaml.Events; using SharpYaml.Serialization.Descriptors; +using SharpYaml.Serialization.Logging; using SharpYaml.Tokens; using Scalar = SharpYaml.Events.Scalar; @@ -140,10 +141,12 @@ namespace SharpYaml.Serialization.Serializers var keyValue = ReadDictionaryItem(ref objectContext, new KeyValuePair(dictionaryDescriptor.KeyType, dictionaryDescriptor.ValueType)); dictionaryDescriptor.AddToDictionary(objectContext.Instance, keyValue.Key, keyValue.Value); } - catch (YamlException) + catch (YamlException ex) { - // TODO: Warning? - objectContext.Reader.Skip(currentDepth); + var logger = objectContext.SerializerContext.ContextSettings.Logger; + if (logger != null) + logger.Log(LogLevel.Warning, ex, "Ignored dictionary item that could not be deserialized"); + objectContext.Reader.Skip(currentDepth); } } else diff --git a/SharpYaml/Serialization/Serializers/ObjectSerializer.cs b/SharpYaml/Serialization/Serializers/ObjectSerializer.cs index e15cde3..39c4732 100644 --- a/SharpYaml/Serialization/Serializers/ObjectSerializer.cs +++ b/SharpYaml/Serialization/Serializers/ObjectSerializer.cs @@ -45,6 +45,7 @@ using System; using SharpYaml.Events; +using SharpYaml.Serialization.Logging; namespace SharpYaml.Serialization.Serializers { @@ -195,9 +196,11 @@ namespace SharpYaml.Serialization.Serializers { ReadMemberCore(ref objectContext); } - catch (YamlException) + catch (YamlException ex) { - // TODO: Warning? + var logger = objectContext.SerializerContext.ContextSettings.Logger; + if (logger != null) + logger.Log(LogLevel.Warning, ex, "Ignored object member that could not be deserialized"); objectContext.Reader.Skip(currentDepth); } } diff --git a/SharpYaml/SharpYAml.csproj b/SharpYaml/SharpYAml.csproj index baf63ec..bf7fb34 100644 --- a/SharpYaml/SharpYAml.csproj +++ b/SharpYaml/SharpYAml.csproj @@ -59,10 +59,13 @@ + + +