private void ProcessOpeningBrace()
        {
            Pos++;
            if (Pos < TemplateString.Length && TemplateString[Pos] == '{')
            {
                Pos++;
                FormatBuilder.Append("{{");
                OpeningBraceIndex = TemplateString.IndexOf("{", Pos);
                return;
            }

            if (ClosingBraceIndex < 0)
            {
                FormatBuilder.Append("{{");
                OpeningBraceIndex = TemplateString.IndexOf("{", Pos);
                return;
            }

            var selectorExpression = TemplateString.Substring(Pos, ClosingBraceIndex - Pos);
            var valueSelector      = CreateValueSelector(selectorExpression);

            FormatBuilder.Append("{" + ValueSelectors.Count + "}");
            ValueSelectors.Add(valueSelector);

            Pos = ClosingBraceIndex + 1;
            OpeningBraceIndex = TemplateString.IndexOf("{", Pos);
            ClosingBraceIndex = TemplateString.IndexOf("}", Pos);
        }
        private void ProcessClosingBrace()
        {
            Pos++;
            if (Pos < TemplateString.Length && TemplateString[Pos] == '}')
            {
                Pos++;
            }

            FormatBuilder.Append("}}");
            ClosingBraceIndex = TemplateString.IndexOf("}", Pos);
        }
        private void ProcessTemplateLiteral()
        {
            var end = TemplateString.Length;

            if (OpeningBraceIndex != -1)
            {
                end = Math.Min(end, OpeningBraceIndex);
            }
            if (ClosingBraceIndex != -1)
            {
                end = Math.Min(end, ClosingBraceIndex);
            }

            var literalPart = TemplateString.Substring(Pos, end - Pos);

            FormatBuilder.Append(literalPart);

            Pos = end;
        }
Exemple #4
0
 /// <summary>
 /// Writes the specified value.
 /// </summary>
 /// <param name="value">The value.</param>
 public override void Write(bool value)
 {
     Debug.Assert(Builder.IsEmpty);
     Builder.Append(value);
     Position = Builder.WriteTo(_writer, null, Position);
     Builder.Clear();
 }
        private static FormatBuilder ValidatePathFormat(
            [NotNull] string directory,
            [NotNull] FormatBuilder fileNameFormat,
            [NotNull] ref string extension,
            [NotNull] FormatBuilder format)
        {
            if (directory == null) throw new ArgumentNullException("directory");
            if (fileNameFormat == null) throw new ArgumentNullException("fileNameFormat");
            if (extension == null) throw new ArgumentNullException("extension");
            if (format == null) throw new ArgumentNullException("format");

            if (string.IsNullOrWhiteSpace(directory))
                directory = DefaultDirectory;

            try
            {
                directory = Path.GetFullPath(directory.Trim());
                if (!System.IO.Directory.Exists(directory))
                    System.IO.Directory.CreateDirectory(directory);
            }
            catch (Exception e)
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                throw new LoggingException(
                    e,
                    LoggingLevel.Critical,
                    () => Resources.FileLogger_DirectoryAccessOrCreationError,
                    directory);
            }

            if (fileNameFormat.IsEmpty)
                // ReSharper disable once AssignNullToNotNullAttribute
                throw new LoggingException(LoggingLevel.Critical, () => Resources.FileLogger_FileNameFormatNotSpecified);


            if (string.IsNullOrWhiteSpace(extension))
                if (format == Log.XMLFormat)
                    extension = ".xml";
                else if (format == Log.JSONFormat)
                    extension = ".json";
                else
                    extension = ".log";
            else
            {
                extension = extension.Trim();
                if (extension.StartsWith("."))
                    extension = extension.Substring(1);
                if (extension.Any(c => !Char.IsLetterOrDigit(c)))
                    throw new LoggingException(
                        LoggingLevel.Critical,
                        // ReSharper disable once AssignNullToNotNullAttribute
                        () => Resources.FileLogger_Extension_Invalid_Char,
                        extension);
                if (extension.Length > 5)
                    throw new LoggingException(
                        LoggingLevel.Critical,
                        // ReSharper disable once AssignNullToNotNullAttribute
                        () => Resources.FileLogger_ExtensionLongerThanFiveCharacters,
                        extension);
                extension = "." + extension;
            }

            FormatBuilder pathBuilder = new FormatBuilder()
                .Append(directory)
                .Append('\\')
                .AppendFormat(fileNameFormat);

            // Add a dedupe tag if not already present
            // ReSharper disable once PossibleNullReferenceException
            Stack<FormatChunk> formatStack = new Stack<FormatChunk>();
            formatStack.Push(fileNameFormat.RootChunk);
            bool found = false;
            while (formatStack.Count > 0)
            {
                FormatChunk chunk = formatStack.Pop();
                // ReSharper disable once PossibleNullReferenceException
                if (string.Equals(chunk.Tag, FormatTagDedupe, StringComparison.CurrentCultureIgnoreCase))
                {
                    found = true;
                    break;
                }

                foreach (var child in chunk.Children)
                    formatStack.Push(child);
            }

            if (!found)
                pathBuilder.AppendFormat("{dedupe: ({dedupe:D})}");

            return pathBuilder
                .Append(extension)
                .MakeReadOnly();
        }
Exemple #6
0
        private static FormatBuilder ValidatePathFormat(
            [NotNull] string directory,
            [NotNull] FormatBuilder fileNameFormat,
            [NotNull] ref string extension,
            [NotNull] FormatBuilder format)
        {
            if (directory == null)
            {
                throw new ArgumentNullException("directory");
            }
            if (fileNameFormat == null)
            {
                throw new ArgumentNullException("fileNameFormat");
            }
            if (extension == null)
            {
                throw new ArgumentNullException("extension");
            }
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }

            if (string.IsNullOrWhiteSpace(directory))
            {
                directory = DefaultDirectory;
            }

            try
            {
                directory = Path.GetFullPath(directory.Trim());
                if (!System.IO.Directory.Exists(directory))
                {
                    System.IO.Directory.CreateDirectory(directory);
                }
            }
            catch (Exception e)
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                throw new LoggingException(
                          e,
                          LoggingLevel.Critical,
                          () => Resources.FileLogger_DirectoryAccessOrCreationError,
                          directory);
            }

            if (fileNameFormat.IsEmpty)
            {
                // ReSharper disable once AssignNullToNotNullAttribute
                throw new LoggingException(LoggingLevel.Critical, () => Resources.FileLogger_FileNameFormatNotSpecified);
            }


            if (string.IsNullOrWhiteSpace(extension))
            {
                if (format == Log.XMLFormat)
                {
                    extension = ".xml";
                }
                else if (format == Log.JSONFormat)
                {
                    extension = ".json";
                }
                else
                {
                    extension = ".log";
                }
            }
            else
            {
                extension = extension.Trim();
                if (extension.StartsWith("."))
                {
                    extension = extension.Substring(1);
                }
                if (extension.Any(c => !Char.IsLetterOrDigit(c)))
                {
                    throw new LoggingException(
                              LoggingLevel.Critical,
                              // ReSharper disable once AssignNullToNotNullAttribute
                              () => Resources.FileLogger_Extension_Invalid_Char,
                              extension);
                }
                if (extension.Length > 5)
                {
                    throw new LoggingException(
                              LoggingLevel.Critical,
                              // ReSharper disable once AssignNullToNotNullAttribute
                              () => Resources.FileLogger_ExtensionLongerThanFiveCharacters,
                              extension);
                }
                extension = "." + extension;
            }

            FormatBuilder pathBuilder = new FormatBuilder()
                                        .Append(directory)
                                        .Append('\\')
                                        .AppendFormat(fileNameFormat);

            // Add a dedupe tag if not already present
            // ReSharper disable once PossibleNullReferenceException
            Stack <FormatChunk> formatStack = new Stack <FormatChunk>();

            formatStack.Push(fileNameFormat.RootChunk);
            bool found = false;

            while (formatStack.Count > 0)
            {
                FormatChunk chunk = formatStack.Pop();
                // ReSharper disable once PossibleNullReferenceException
                if (string.Equals(chunk.Tag, FormatTagDedupe, StringComparison.CurrentCultureIgnoreCase))
                {
                    found = true;
                    break;
                }

                foreach (var child in chunk.Children)
                {
                    formatStack.Push(child);
                }
            }

            if (!found)
            {
                pathBuilder.AppendFormat("{dedupe: ({dedupe:D})}");
            }

            return(pathBuilder
                   .Append(extension)
                   .MakeReadOnly());
        }
Exemple #7
0
 /// <summary>
 /// Writes the specified value.
 /// </summary>
 /// <param name="value">The value.</param>
 public override void Write(bool value)
 {
     if (!_isOpen)
     {
         throw new InvalidOperationException(Resources.FormatWriter_IsClosed);
     }
     _builder.Append(value);
 }