Skip to content

[api-xml-adjuster] give chance to set logging verbosity in ApiXmlAdju… #104

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

Merged
merged 1 commit into from
Nov 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static void FindParametersDefects (this JavaMethodBase methodBase)
int dummy;
foreach (var p in methodBase.Parameters) {
if (p.Name.StartsWith ("p", StringComparison.Ordinal) && int.TryParse (p.Name.Substring (1), out dummy)) {
Console.Error.WriteLine ("Warning: {0} in {1} has 'unnamed' parameters", methodBase.Parent, methodBase);
Log.LogWarning ("Warning: {0} in {1} has 'unnamed' parameters", methodBase.Parent, methodBase);
break; // reporting once is enough.
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static void PrepareGenericInheritanceMapping (this JavaClass cls)
cls.GenericInheritanceMapping = empty;
else if (cls.ResolvedExtends.ReferencedType.TypeParameters == null) {
// FIXME: I guess this should not happen. But this still happens.
Console.WriteLine ("Warning: '{0}' is referenced as base type of '{1}' and expected to have generic type parameters, but it does not.", cls.ExtendsGeneric, cls.FullName);
Log.LogWarning ("Warning: '{0}' is referenced as base type of '{1}' and expected to have generic type parameters, but it does not.", cls.ExtendsGeneric, cls.FullName);
cls.GenericInheritanceMapping = empty;
} else {
if (cls.ResolvedExtends.ReferencedType.TypeParameters.TypeParameters.Count != cls.ResolvedExtends.TypeParameters.Count)
Expand All @@ -44,8 +44,6 @@ static void PrepareGenericInheritanceMapping (this JavaClass cls)
dic.Add (new JavaTypeReference (kvp.Key, null), kvp.Value);
if (dic.Any ()) {
cls.GenericInheritanceMapping = dic;
//Console.WriteLine ("in {0}.{1}: {2}", cls.Parent.Name, cls.Name,
// string.Join (", ", dic.Select (p => string.Format ("base {0} becomes {1}", p.Key.ReferencedTypeParameter.Name, p.Value.ToString ()))));
}
else
cls.GenericInheritanceMapping = empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public static void Resolve (this JavaApi api)
while (true) {
bool errors = false;
foreach (var t in api.Packages.SelectMany (p => p.Types).OfType<JavaClass> ().ToArray ())
try { t.Resolve (); } catch (JavaTypeResolutionException ex) { Console.Error.WriteLine (string.Format ("Error while processing type '{0}': {1}", t, ex.Message)); errors = true; t.Parent.Types.Remove (t); }
try { t.Resolve (); } catch (JavaTypeResolutionException ex) { Log.LogError ("Error while processing type '{0}': {1}", t, ex.Message); errors = true; t.Parent.Types.Remove (t); }
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we change this line to follow "normal" code conventions, e.g. multiple lines?

try {
    t.Resolve ();
} catch (JavaTypeResolutionException ex) {
    Log.LogError ("Error while processing type '{0}': {1}", t, ex.Message); errors = true; t.Parent.Types.Remove (t);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That had better be done but this commit is not about formatting. Mixing different changes is bad.

foreach (var t in api.Packages.SelectMany (p => p.Types).OfType<JavaInterface> ().ToArray ())
try { t.Resolve (); } catch (JavaTypeResolutionException ex) { Console.Error.WriteLine (string.Format ("Error while processing type '{0}': {1}", t, ex.Message)); errors = true; t.Parent.Types.Remove (t); }
try { t.Resolve (); } catch (JavaTypeResolutionException ex) { Log.LogError ("Error while processing type '{0}': {1}", t, ex.Message); errors = true; t.Parent.Types.Remove (t); }
if (!errors)
break;
}
Expand Down Expand Up @@ -89,7 +89,7 @@ static void ResolveWithTryCatch (Action resolve, JavaMember m)
try {
resolve ();
} catch (JavaTypeResolutionException ex) {
Console.Error.WriteLine (string.Format ("Error while processing '{0}' in '{1}': {2}", m, m.Parent, ex.Message));
Log.LogError ("Error while processing '{0}' in '{1}': {2}", m, m.Parent, ex.Message);
m.Parent.Members.Remove (m);
}
}
Expand Down Expand Up @@ -128,7 +128,7 @@ static void Resolve (this JavaTypeParameters tp, JavaApi api, params JavaTypePar
foreach (var t in tp.TypeParameters)
if (t.GenericConstraints != null)
foreach (var g in t.GenericConstraints.GenericConstraints)
try { g.ResolvedType = api.Parse (g.Type, additionalTypeParameters); } catch (JavaTypeResolutionException ex) { Console.Error.WriteLine (string.Format ("Warning: failed to resolve generic constraint: '{0}': {1}", g.Type, ex.Message)); }
try { g.ResolvedType = api.Parse (g.Type, additionalTypeParameters); } catch (JavaTypeResolutionException ex) { Log.LogWarning ("Warning: failed to resolve generic constraint: '{0}': {1}", g.Type, ex.Message); }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ static bool IsConformantType (this JavaTypeParameter typeParameter, JavaTypeRefe
if (typeParameter.GenericConstraints == null)
return true;
// FIXME: implement correct generic constraint conformance check.
Console.Error.WriteLine ("WARNING: generic constraint conformance check is not implemented, so the type might be actually compatible. Type parameter: {0}{1}, examined type: {2}",
typeParameter.Name, typeParameter.Parent.ParentMethod?.Name ?? typeParameter.Parent.ParentType?.Name, examinedType.ToString ());
Log.LogDebug ("NOTICE: generic constraint conformance check is not implemented, so the type might be actually compatible. Type parameter: {0}{1}, examined type: {2}",
typeParameter.Name, typeParameter.Parent.ParentMethod?.Name ?? typeParameter.Parent.ParentType?.Name, examinedType);
return false;
}
}
Expand Down
55 changes: 55 additions & 0 deletions src/Xamarin.Android.Tools.ApiXmlAdjuster/Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.IO;

namespace Xamarin.Android.Tools.ApiXmlAdjuster
{
public static class Log
{
public enum LoggingLevel
{
None = 0,
Error = 1,
Warning = 2,
Debug = 3,
}

static Action<string> write_default = s => (DefaultWriter ?? Console.Out).WriteLine (s);

static Action<string> e, w, d;

public static TextWriter DefaultWriter { get; set; }

public static LoggingLevel Verbosity { get; set; } = LoggingLevel.Error;

public static Action<string> LogErrorAction {
get { return e ?? write_default; }
set { e = value; }
}
public static Action<string> LogWarningAction {
get { return w ?? write_default; }
set { w = value; }
}
public static Action<string> LogDebugAction {
get { return d ?? write_default; }
set { d = value; }
}

public static void LogError (string format, params object [] args)
{
if ((int) Verbosity >= (int) LoggingLevel.Error)
LogErrorAction (args.Length > 0 ? string.Format (format, args) : format);
}

public static void LogWarning (string format, params object [] args)
{
if ((int)Verbosity >= (int)LoggingLevel.Warning)
LogWarningAction (args.Length > 0 ? string.Format (format, args) : format);
}

public static void LogDebug (string format, params object [] args)
{
if ((int)Verbosity >= (int)LoggingLevel.Debug)
LogDebugAction (args.Length > 0 ? string.Format (format, args) : format);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Compile Include="JavaApiDefectFinderExtensions.cs" />
<Compile Include="JavaTypeResolutionUtil.cs" />
<Compile Include="JavaApiFixVisibilityExtensions.cs" />
<Compile Include="Log.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
15 changes: 14 additions & 1 deletion tools/generator/ApiXmlAdjuster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,21 @@ namespace Xamarin.Android.Tools.ApiXmlAdjuster
{
public class Adjuster
{
public void Process (string inputXmlFile, GenBase [] gens, string outputXmlFile)
public void Process (string inputXmlFile, GenBase [] gens, string outputXmlFile, int reportVerbosity)
{
switch (reportVerbosity) {
case 0:
break;
case 1:
Log.Verbosity = Log.LoggingLevel.Error;
break;
case 2:
Log.Verbosity = Log.LoggingLevel.Warning;
break;
default:
Log.Verbosity = Log.LoggingLevel.Debug;
break;
}
var api = new JavaApi ();
api.LoadReferences (gens);
api.Load (inputXmlFile);
Expand Down
2 changes: 1 addition & 1 deletion tools/generator/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ static void Run (CodeGeneratorOptions options, DirectoryAssemblyResolver resolve
}
if (apiSourceAttr == "class-parse") {
apiXmlFile = api_xml_adjuster_output ?? Path.Combine (Path.GetDirectoryName (filename), Path.GetFileName (filename) + ".adjusted");
new Adjuster ().Process (filename, SymbolTable.AllRegisteredSymbols ().OfType<GenBase> ().ToArray (), apiXmlFile);
new Adjuster ().Process (filename, SymbolTable.AllRegisteredSymbols ().OfType<GenBase> ().ToArray (), apiXmlFile, Report.Verbosity ?? 0);
}
if (only_xml_adjuster)
return;
Expand Down