@@ -69,23 +69,30 @@ public LocalizedMessage (int code, string value)
6969 public static LocalizedMessage WarningBaseInterfaceNotFound => new LocalizedMessage ( 0x8C00 , Localization . Resources . Generator_BG8C00 ) ;
7070 public static LocalizedMessage WarningBaseInterfaceInvalid => new LocalizedMessage ( 0x8C01 , Localization . Resources . Generator_BG8C01 ) ;
7171
72- public static void LogCodedError ( LocalizedMessage message , params string [ ] args )
73- => LogCodedError ( message , null , null , - 1 , - 1 , args ) ;
72+ public static void LogCodedErrorAndExit ( LocalizedMessage message , params string [ ] args )
73+ => LogCodedErrorAndExit ( message , null , null , args ) ;
7474
75- public static void LogCodedError ( LocalizedMessage message , Exception ? innerException , params string [ ] args )
76- => LogCodedError ( message , innerException , null , - 1 , - 1 , args ) ;
75+ public static void LogCodedErrorAndExit ( LocalizedMessage message , Exception ? innerException , params string [ ] args )
76+ => LogCodedErrorAndExit ( message , innerException , null , args ) ;
7777
78- public static void LogCodedError ( LocalizedMessage message , Exception ? innerException , XNode node , params string ? [ ] args )
78+ public static void LogCodedErrorAndExit ( LocalizedMessage message , Exception ? innerException , XNode ? node , params string ? [ ] args )
7979 {
80- var file = Uri . TryCreate ( node . BaseUri , UriKind . Absolute , out var uri ) ? uri . LocalPath : null ;
81- var line_info = ( node as IXmlLineInfo ) ? . HasLineInfo ( ) == true ? node as IXmlLineInfo : null ;
80+ LogCodedError ( message , node , args ) ;
8281
83- LogCodedError ( message , innerException , file , line_info ? . LineNumber ?? - 1 , line_info ? . LinePosition ?? - 1 , args ) ;
82+ // Throwing a BindingGeneratorException will cause generator to terminate
83+ throw new BindingGeneratorException ( message . Code , string . Format ( message . Value , args ) , innerException ) ;
8484 }
8585
86- public static void LogCodedError ( LocalizedMessage message , Exception ? innerException , string ? sourceFile , int line , int column , params string ? [ ] args )
86+ public static void LogCodedError ( LocalizedMessage message , XNode ? node , params string ? [ ] args )
8787 {
88- throw new BindingGeneratorException ( message . Code , sourceFile , line , column , string . Format ( message . Value , args ) , innerException ) ;
88+ var ( file , line , col ) = GetLineInfo ( node ) ;
89+
90+ LogCodedError ( message , file , line , col , args ) ;
91+ }
92+
93+ public static void LogCodedError ( LocalizedMessage message , string ? sourceFile , int line , int column , params string ? [ ] args )
94+ {
95+ Console . Error . WriteLine ( Format ( true , message . Code , sourceFile , line , column , message . Value , args ) ) ;
8996 }
9097
9198 public static void LogCodedWarning ( int verbosity , LocalizedMessage message , params string ? [ ] args )
@@ -99,10 +106,9 @@ public static void LogCodedWarning (int verbosity, LocalizedMessage message, Exc
99106
100107 public static void LogCodedWarning ( int verbosity , LocalizedMessage message , Exception ? innerException , XNode node , params string ? [ ] args )
101108 {
102- var file = Uri . TryCreate ( node . BaseUri , UriKind . Absolute , out var uri ) ? uri . LocalPath : null ;
103- var line_info = ( node as IXmlLineInfo ) ? . HasLineInfo ( ) == true ? node as IXmlLineInfo : null ;
109+ var ( file , line , col ) = GetLineInfo ( node ) ;
104110
105- LogCodedWarning ( verbosity , message , innerException , file , line_info ? . LineNumber ?? - 1 , line_info ? . LinePosition ?? - 1 , args ) ;
111+ LogCodedWarning ( verbosity , message , innerException , file , line , col , args ) ;
106112 }
107113
108114 public static void LogCodedWarning ( int verbosity , LocalizedMessage message , Exception ? innerException , string ? sourceFile , int line , int column , params string ? [ ] args )
@@ -145,12 +151,26 @@ public static string Format (bool error, int errorCode, string? sourceFile, int
145151 return ret + ": " ;
146152
147153 if ( column > 0 )
148- return ret + $ "({ line } , { column } ): ";
154+ return ret + $ "({ line } ,{ column } ): ";
149155
150156 return ret + $ "({ line } ): ";
151157 }
158+
159+ static ( string ? file , int line , int col ) GetLineInfo ( XNode ? node )
160+ {
161+ if ( node is null )
162+ return ( null , - 1 , - 1 ) ;
163+
164+ var file = Uri . TryCreate ( node . BaseUri , UriKind . Absolute , out var uri ) ? uri . LocalPath : null ;
165+ var pos = ( node as IXmlLineInfo ) ? . HasLineInfo ( ) == true ? node as IXmlLineInfo : null ;
166+
167+ return ( file , pos ? . LineNumber ?? - 1 , pos ? . LinePosition ?? - 1 ) ;
168+ }
152169 }
153-
170+
171+ /// <summary>
172+ /// Throwing this exception will cause generator to exit gracefully.
173+ /// </summary>
154174 public class BindingGeneratorException : Exception
155175 {
156176 public BindingGeneratorException ( int errorCode , string message )
0 commit comments