Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add diagnostic for Logging Generator: can't have malformed format strings #52040

Closed
wants to merge 2 commits into from

Conversation

maryamariyan
Copy link
Member

@maryamariyan maryamariyan commented Apr 29, 2021

@ghost
Copy link

ghost commented Apr 29, 2021

Tagging subscribers to this area: @maryamariyan
See info in area-owners.md if you want to be subscribed.

Issue Details

cc: @geeknoid @eerhardt

Author: maryamariyan
Assignees: maryamariyan
Labels:

area-Extensions-Logging

Milestone: -

else
{
// dangling }, fail
throw new ArgumentException($"Dangling }} in format string at position {pos}", nameof(format));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume these strings will be shown to the developer. If so, we should put them in a resource so they can be localized.

ReadOnlySpan<char> format = msg.AsSpan();
var builder = new StringBuilder(msg.Length);
var pos = 0;
var len = format.Length;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len

i think caching the sapn length will prevent the jit from optimizaing the usage. maybe you can just always call format.Length instead.

// classic composite format string

pos++;
if (pos == len || (ch = format[pos]) < '0' || ch > '9')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pos

is it possible to have spaces after {?

{
// we need a template name
throw new ArgumentException($"Missing template name in format string at position {pos}", nameof(format));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can move this block before the condition
if (templates == null) so you don't have to repeat the same code

}

// skip whitespace
while (pos < len && (ch = format[pos]) == ' ')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format

does the format can have different space characters? I mean shoud you use Char.IsWhiteSpace instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to do that in other places to check space char?

}
}

private static bool ValidTemplateNameChar(char ch, bool first)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private static bool ValidTemplateNameChar(char ch, bool first)

private static bool ValidTemplateNameChar(char ch, bool first) => first ? char.IsLetter(ch) || ch == '' : char.IsLetterOrDigit(ch) || ch == '';

/// <summary>
/// Parses the message template and throws exception for malformed messages.
/// </summary>
internal static string Parse(this string msg, List<string>? templates)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parse

nit: I am seeing this method is too long and will be hard to maintain. I would suggest split the body of this method into smaller methods and keep the body simile doing the outerloop and just calling the smaller methods.

@ericstj
Copy link
Member

ericstj commented May 24, 2021

@maryamariyan is the PR still active, or is it stale now due to conflicts?

@ghost ghost locked as resolved and limited conversation to collaborators Jun 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[LSG] LoggerMessage - Add diagnostic - Can't have malformed format strings
4 participants