diff --git a/src/Fable.Cli/Main.fs b/src/Fable.Cli/Main.fs index 422729a94d..134b7ac7a9 100644 --- a/src/Fable.Cli/Main.fs +++ b/src/Fable.Cli/Main.fs @@ -98,7 +98,7 @@ module private Util = let stack = innerStack ex $"[ERROR] %s{file}{Log.newLine}%s{ex.Message}{Log.newLine}%s{stack}" - let formatLog rootDir (log: Log) = + let formatLog rootDir (log: LogEntry) = match log.FileName with | None -> log.Message | Some file -> @@ -120,7 +120,7 @@ module private Util = | None -> $"%s{file}(1,1): %s{severity} %s{log.Tag}: %s{log.Message}" - let logErrors rootDir (logs: Log seq) = + let logErrors rootDir (logs: LogEntry seq) = logs |> Seq.filter (fun log -> log.Severity = Severity.Error) |> Seq.iter (fun log -> @@ -153,7 +153,7 @@ module private Util = let msg = $"%s{er.Message} (code %i{er.ErrorNumber})" - Log.Make( + LogEntry.Make( severity, msg, fileName = er.FileName, @@ -529,7 +529,7 @@ type FableCompileResult = Result<{| File: string OutPath: string - Logs: Log[] + Logs: LogEntry[] InlineExprs: (string * InlineExpr)[] WatchDependencies: string[] |}, {| @@ -538,7 +538,8 @@ type FableCompileResult = |}> type ReplyChannel = - AsyncReplyChannel> + AsyncReplyChannel> type FableCompilerMsg = | GetFableProject of replyChannel: AsyncReplyChannel @@ -565,7 +566,7 @@ type FableCompilerState = FilesCheckedButNotCompiled: Set FableFilesToCompileExpectedCount: int FableFilesCompiledCount: int - FSharpLogs: Log[] + FSharpLogs: LogEntry[] FableResults: FableCompileResult list HasFSharpCompilationFinished: bool ReplyChannel: ReplyChannel option @@ -915,6 +916,7 @@ and FableCompiler projCracked.ProjectOptions.SourceFiles, [], assemblies, + Log.log, ?precompiledInfo = (projCracked.PrecompiledInfo |> Option.map (fun i -> i :> _)), @@ -1330,12 +1332,12 @@ let private compilationCycle (state: State) (changes: ISet) = let log = match e.Exception with | Fable.FableError msg -> - Log.MakeError(msg, fileName = e.File) + LogEntry.MakeError(msg, fileName = e.File) | ex -> let msg = ex.Message + Log.newLine + ex.StackTrace - Log.MakeError( + LogEntry.MakeError( msg, fileName = e.File, tag = "EXCEPTION" diff --git a/src/Fable.Compiler/Library.fs b/src/Fable.Compiler/Library.fs index 799003610e..1ff8c3cca4 100644 --- a/src/Fable.Compiler/Library.fs +++ b/src/Fable.Compiler/Library.fs @@ -5,10 +5,10 @@ open System.IO open FSharp.Compiler.CodeAnalysis open FSharp.Compiler.SourceCodeServices open Fable -open Fable.Compiler.Util open Fable.Transforms.State open Fable.Transforms open Fable.Compiler.ProjectCracker +open Fable.Compiler.Util type BabelWriter ( @@ -154,7 +154,8 @@ let compileProjectToJavaScript cliArgs.ProjectFile, crackerResponse.ProjectOptions.SourceFiles, checkProjectResult.AssemblyContents.ImplementationFiles, - assemblies + assemblies, + Log.log // ?precompiledInfo = // (projCracked.PrecompiledInfo |> Option.map (fun i -> i :> _)), // getPlugin = loadType projCracked.CliArgs @@ -233,7 +234,8 @@ let compileFileToJavaScript cliArgs.ProjectFile, crackerResponse.ProjectOptions.SourceFiles, checkProjectResult.AssemblyContents.ImplementationFiles, - assemblies + assemblies, + Log.log ) let opts = cliArgs.CompilerOptions diff --git a/src/Fable.Compiler/Util.fs b/src/Fable.Compiler/Util.fs index dd3a6a85c1..b47815b152 100644 --- a/src/Fable.Compiler/Util.fs +++ b/src/Fable.Compiler/Util.fs @@ -153,6 +153,18 @@ module Log = Console.Error.WriteLine(msg) Console.ResetColor() + let info (msg: string) = + if canLog msg then + Console.ForegroundColor <- ConsoleColor.Gray + Console.Out.WriteLine(msg) + Console.ResetColor() + + let log (sev: Fable.Severity) (msg: string) = + match sev with + | Fable.Severity.Info -> info msg + | Fable.Severity.Warning -> warning msg + | Fable.Severity.Error -> error msg + let mutable private femtoMsgShown = false let showFemtoMsg (show: unit -> bool) : unit = diff --git a/src/Fable.Compiler/Util.fsi b/src/Fable.Compiler/Util.fsi index 70123a5f09..af64528359 100644 --- a/src/Fable.Compiler/Util.fsi +++ b/src/Fable.Compiler/Util.fsi @@ -48,6 +48,8 @@ module Log = val verbose: msg: Lazy -> unit val warning: msg: string -> unit val error: msg: string -> unit + val info: msg: string -> unit + val log: sev: Fable.Severity -> msg: string -> unit val showFemtoMsg: show: (unit -> bool) -> unit module File = diff --git a/src/Fable.Transforms/Rust/AST/Rust.AST.Adapters.fs b/src/Fable.Transforms/Rust/AST/Rust.AST.Adapters.fs index 421734c3dd..7ef58ead72 100644 --- a/src/Fable.Transforms/Rust/AST/Rust.AST.Adapters.fs +++ b/src/Fable.Transforms/Rust/AST/Rust.AST.Adapters.fs @@ -248,12 +248,10 @@ type Macros = static member write(buf: String, fmt: string, [] args) = buf.push_str (Macros.format (fmt, args)) -#if false //DEBUG - static member debug(fmt: string, [] args) = - System.Console.WriteLine(fmt, args) -#else + + // static member debug(fmt: string, [] args) = + // System.Console.WriteLine(fmt, args) static member debug _ = () -#endif [] module ArrayHelpers = diff --git a/src/Fable.Transforms/State.fs b/src/Fable.Transforms/State.fs index 111480b03a..b07b8badd0 100644 --- a/src/Fable.Transforms/State.fs +++ b/src/Fable.Transforms/State.fs @@ -13,7 +13,13 @@ type PluginRef = TypeFullName: string } -type Assemblies(getPlugin, fsharpAssemblies: FSharpAssembly list) = +type Assemblies + ( + getPlugin, + fsharpAssemblies: FSharpAssembly list, + addLog: Severity -> string -> unit + ) + = let assemblies = Dictionary() let coreAssemblies = Dictionary() let entities = ConcurrentDictionary() @@ -46,13 +52,7 @@ type Assemblies(getPlugin, fsharpAssemblies: FSharpAssembly list) = let errorMessage = $"Could not scan {path} for Fable plugins, skipping this assembly" -#if !FABLE_COMPILER - Console.ForegroundColor <- ConsoleColor.Gray -#endif - Console.WriteLine(errorMessage) -#if !FABLE_COMPILER - Console.ResetColor() -#endif + addLog Severity.Info errorMessage hasSkippedAssembly <- true false @@ -83,20 +83,12 @@ type Assemblies(getPlugin, fsharpAssemblies: FSharpAssembly list) = $"Error while loading plugin: {e.FullName}" "" "This error often happens if you are trying to use a plugin that is not compatible with the current version of Fable." - "If you see this error please open an issue at https://github.com/fable-compiler/Fable/" "so we can check if we can improve the plugin detection mechanism." ] - |> String.concat "\n" + |> String.concat Environment.NewLine -#if !FABLE_COMPILER - Console.ForegroundColor <- - ConsoleColor.DarkRed -#endif - Console.WriteLine(errorMessage) -#if !FABLE_COMPILER - Console.ResetColor() -#endif + addLog Severity.Error errorMessage raise ex @@ -105,7 +97,7 @@ type Assemblies(getPlugin, fsharpAssemblies: FSharpAssembly list) = // Add a blank line to separate the error message from the rest of the output if hasSkippedAssembly then - Console.WriteLine() + addLog Severity.Info "" // or Environment.NewLine ({ MemberDeclarationPlugins = Map.empty }, plugins) ||> Seq.fold (fun acc kv -> @@ -219,6 +211,7 @@ type Project sourceFiles: string[], fsharpFiles: FSharpImplementationFileContents list, fsharpAssemblies: FSharpAssembly list, + addLog: Severity -> string -> unit, ?getPlugin: PluginRef -> System.Type, ?precompiledInfo: PrecompiledInfo ) @@ -227,7 +220,7 @@ type Project let getPlugin = defaultArg getPlugin (fun _ -> failwith "Plugins are not supported") - let assemblies = Assemblies(getPlugin, fsharpAssemblies) + let assemblies = Assemblies(getPlugin, fsharpAssemblies, addLog) let implFilesMap = fsharpFiles @@ -283,7 +276,7 @@ type Project member _.Assemblies = assemblies member _.PrecompiledInfo = precompiledInfo -type Log = +type LogEntry = { Message: string Tag: string @@ -302,7 +295,7 @@ type Log = } static member MakeError(msg, ?fileName, ?range, ?tag) = - Log.Make( + LogEntry.Make( Severity.Error, msg, ?fileName = fileName, @@ -321,7 +314,7 @@ type CompilerImpl ?outType: OutputType, ?outDir: string, ?watchDependencies: HashSet, - ?logs: ResizeArray, + ?logs: ResizeArray, ?isPrecompilingInlineFunction: bool ) = @@ -456,7 +449,7 @@ type CompilerImpl ?tag: string ) = - Log.Make( + LogEntry.Make( severity, msg, ?range = range, diff --git a/src/fable-standalone/src/Main.fs b/src/fable-standalone/src/Main.fs index df90b2d2dc..f5196225d1 100644 --- a/src/fable-standalone/src/Main.fs +++ b/src/fable-standalone/src/Main.fs @@ -179,11 +179,14 @@ let makeProject else checkResults.AssemblyContents.ImplementationFiles + let addLog (severity: Fable.Severity) (msg: string) = () //TODO: log loading errors + Project.From( projectOptions.ProjectFileName, projectOptions.SourceFiles, implFiles, - checkResults.ProjectContext.GetReferencedAssemblies() + checkResults.ProjectContext.GetReferencedAssemblies(), + addLog ) let parseAndCheckProject @@ -339,7 +342,7 @@ let getCompletionsAtLocation ) | None -> [||] -let mapFableError (com: Compiler) (log: Log) = +let mapFableError (com: Compiler) (log: LogEntry) = let r = defaultArg log.Range Fable.AST.SourceLocation.Empty { diff --git a/tests/Integration/Compiler/Util/Compiler.fs b/tests/Integration/Compiler/Util/Compiler.fs index 26b1bc3453..4bc21bf1c9 100644 --- a/tests/Integration/Compiler/Util/Compiler.fs +++ b/tests/Integration/Compiler/Util/Compiler.fs @@ -8,10 +8,10 @@ open Fable.Compiler.Util module Compiler = - type Result = Log list + type Result = LogEntry list module Result = - let errors = List.filter (fun (m: Log) -> m.Severity = Severity.Error) - let warnings = List.filter (fun (m: Log) -> m.Severity = Severity.Warning) + let errors = List.filter (fun (m: LogEntry) -> m.Severity = Severity.Error) + let warnings = List.filter (fun (m: LogEntry) -> m.Severity = Severity.Warning) let wasFailure = errors >> List.isEmpty >> not type Settings = { @@ -123,8 +123,8 @@ module Compiler = module private Test = module Is = - let error (msg: Log) = msg.Severity = Severity.Error - let warning (msg: Log) = msg.Severity = Severity.Warning + let error (msg: LogEntry) = msg.Severity = Severity.Error + let warning (msg: LogEntry) = msg.Severity = Severity.Warning let single = function | [_] -> true | _ -> false let zero = List.isEmpty let count n = List.length >> (=) n @@ -142,7 +142,7 @@ module Compiler = let withMsg (txt: string) = List.exists (fun m -> m.Message.Contains txt) module Text = let contains (txt: string) msg = msg.Message.Contains(txt, StringComparison.InvariantCultureIgnoreCase) - let isMatch (regex: System.Text.RegularExpressions.Regex) (msg: Log) = + let isMatch (regex: System.Text.RegularExpressions.Regex) (msg: LogEntry) = regex.IsMatch msg.Message let private (>&>) a b = fun actual -> a actual && b actual