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

Paket.PowerShell support for Package Manager Console #875

Merged
merged 6 commits into from
Jun 18, 2015
Merged
Show file tree
Hide file tree
Changes from 2 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
115 changes: 68 additions & 47 deletions src/Paket.Core/Logging.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,103 @@

open System
open System.IO
open System.Diagnostics

/// [omit]
let mutable verbose = false

let mutable logFile : string option = None
let traceFunctions = System.Collections.Generic.List<_>()
/// [omit]
type Trace = {
Level: TraceLevel
Text: string
NewLine: bool }

let setLogFile fileName =
let fi = FileInfo fileName
logFile <- Some fi.FullName
if fi.Exists then
fi.Delete()
else
if fi.Directory.Exists |> not then
fi.Directory.Create()
/// [omit]
let event = Event<Trace>()

let inline traceToFile (text:string) =
match logFile with
| Some fileName -> try File.AppendAllLines(fileName,[text]) with | _ -> ()
| _ -> ()
/// [omit]
let subscribe callback = Observable.subscribe callback event.Publish

let RegisterTraceFunction(traceFunction:Action<string>) =
traceFunctions.Add(traceFunction)

let RemoveTraceFunction(traceFunction:Action<string>) =
traceFunctions.Remove(traceFunction)
/// [omit]
let tracen s = event.Trigger { Level = TraceLevel.Info; Text = s; NewLine = true }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Who was using this? Will Logging.subscribe work for them or does this need to be added back (may be call it subscribeWithAction). It would return a IDisposable instead of having RemoveTraceFunction.

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 Paket.VisualStudio is using this. But we can fix breaking changes
there
On Jun 15, 2015 10:13, "Cameron Taggart" [email protected] wrote:

In src/Paket.Core/Logging.fs
#875 (comment):

-let RegisterTraceFunction(traceFunction:Action) =

Who was using this? Will Logging.subscribe work for them or does this
need to be added back (may be call subscribeWithAction). It would return
a IDisposable instead of having RemoveTraceFunction.


Reply to this email directly or view it on GitHub
https://github.com/fsprojects/Paket/pull/875/files#r32398873.

let inline traceToRegisteredFunctions (text:string) =
traceToFile text
for f in traceFunctions do
f.Invoke(text)
/// [omit]
let tracefn fmt = Printf.ksprintf tracen fmt

/// [omit]
let monitor = new Object()
let trace s = event.Trigger { Level = TraceLevel.Info; Text = s; NewLine = false }

/// [omit]
let inline tracen (s : string) = lock monitor (fun () -> traceToRegisteredFunctions s; Console.WriteLine s)
let tracef fmt = Printf.ksprintf trace fmt

/// [omit]
let inline tracefn fmt = Printf.ksprintf tracen fmt
let traceVerbose s =
if verbose then
event.Trigger { Level = TraceLevel.Verbose; Text = s; NewLine = true }

/// [omit]
let inline trace (s : string) = lock monitor (fun () -> traceToRegisteredFunctions s; Console.Write s)
let verbosefn fmt = Printf.ksprintf traceVerbose fmt

/// [omit]
let inline tracef fmt = Printf.ksprintf trace fmt
let traceError s = event.Trigger { Level = TraceLevel.Error; Text = s; NewLine = true }

/// [omit]
let inline traceVerbose (s : string) =
if verbose then
lock monitor (fun () -> traceToRegisteredFunctions s; Console.WriteLine s)
let traceWarn s = event.Trigger { Level = TraceLevel.Warning; Text = s; NewLine = true }

/// [omit]
let inline verbosefn fmt = Printf.ksprintf traceVerbose fmt
let traceErrorfn fmt = Printf.ksprintf traceError fmt

/// [omit]
let inline traceColored color (s: string) =
lock monitor
(fun () ->
let curColor = Console.ForegroundColor
if curColor <> color then Console.ForegroundColor <- color
traceToRegisteredFunctions s
use textWriter =
match color with
| ConsoleColor.Red -> Console.Error
| _ -> Console.Out
textWriter.WriteLine s
if curColor <> color then Console.ForegroundColor <- curColor)
let traceWarnfn fmt = Printf.ksprintf traceWarn fmt


// Console Trace

/// [omit]
let inline traceError s = traceColored ConsoleColor.Red s
let traceColored color (s:string) =
let curColor = Console.ForegroundColor
if curColor <> color then Console.ForegroundColor <- color
use textWriter =
match color with
| ConsoleColor.Red -> Console.Error
| _ -> Console.Out
textWriter.WriteLine s
if curColor <> color then Console.ForegroundColor <- curColor

/// [omit]
let inline traceWarn s = traceColored ConsoleColor.Yellow s
let monitor = new Object()

/// [omit]
let inline traceErrorfn fmt = Printf.ksprintf traceError fmt
let traceToConsole (trace:Trace) =
lock monitor
(fun () ->
match trace.Level with
| TraceLevel.Warning -> traceColored ConsoleColor.Yellow trace.Text
| TraceLevel.Error -> traceColored ConsoleColor.Red trace.Text
| _ ->
if trace.NewLine then Console.WriteLine trace.Text
else Console.Write trace.Text )


// Log File Trace

/// [omit]
let inline traceWarnfn fmt = Printf.ksprintf traceWarn fmt
let mutable logFile : string option = None

/// [omit]
let traceToFile (trace:Trace) =
match logFile with
| Some fileName -> try File.AppendAllLines(fileName,[trace.Text]) with | _ -> ()
| _ -> ()

/// [omit]
let setLogFile fileName =
let fi = FileInfo fileName
logFile <- Some fi.FullName
if fi.Exists then
fi.Delete()
else
if fi.Directory.Exists |> not then
fi.Directory.Create()
subscribe traceToFile
39 changes: 39 additions & 0 deletions src/Paket.Core/Logging.fsi
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Paket.Logging

open System
open System.Diagnostics

val mutable verbose : bool


val tracen : string -> unit

val tracefn : Printf.StringFormat<'a,unit> -> 'a

val trace : string -> unit

val tracef : Printf.StringFormat<'a,unit> -> 'a

val traceVerbose : string -> unit

val verbosefn : Printf.StringFormat<'a,unit> -> 'a

val traceError : string -> unit

val traceWarn : string -> unit

val traceErrorfn : Printf.StringFormat<'a,unit> -> 'a

val traceWarnfn : Printf.StringFormat<'a,unit> -> 'a


type Trace = {
Level: TraceLevel
Text: string
NewLine: bool }

val subscribe : (Trace -> unit) -> IDisposable

val traceToConsole : Trace -> unit

val setLogFile : string -> IDisposable
3 changes: 2 additions & 1 deletion src/Paket.Core/Paket.Core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<Compile Include="Domain.fs" />
<Compile Include="SemVer.fs" />
<Compile Include="VersionRange.fs" />
<Compile Include="Logging.fsi" />
<Compile Include="Logging.fs" />
<Compile Include="Xml.fs" />
<Compile Include="Utils.fs" />
Expand Down Expand Up @@ -267,4 +268,4 @@
</ItemGroup>
</When>
</Choose>
</Project>
</Project>
18 changes: 17 additions & 1 deletion src/Paket.PowerShell/CmdletExt.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
module Paket.PowerShell.CmdletExt

open System.Management.Automation
open System
open System.Diagnostics
open Paket

// add F# printf write extensions
type Cmdlet with
Expand Down Expand Up @@ -37,4 +40,17 @@ type PSCmdlet with
else false

member x.SetCurrentDirectoryToLocation() =
System.Environment.CurrentDirectory <- x.SessionState.Path.CurrentFileSystemLocation.Path
Environment.CurrentDirectory <- x.SessionState.Path.CurrentFileSystemLocation.Path

member x.RegisterTrace() =
Logging.verbose <- x.Verbose
let id = Threading.Thread.CurrentThread.ManagedThreadId
Logging.subscribe (fun trace ->
if id = Threading.Thread.CurrentThread.ManagedThreadId then
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This thread ID check appears it will work. Messages in async will not print, but also will not error out the program now.

match trace.Level with
| TraceLevel.Warning -> x.WriteWarning trace.Text
| TraceLevel.Error -> x.WriteWarning trace.Text
| _ -> x.WriteObject trace.Text
else
Diagnostics.Debug.Write(sprintf "not on main PS thread: %A" trace)
)
5 changes: 5 additions & 0 deletions src/Paket.PowerShell/Paket.PowerShell.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<WarningLevel>3</WarningLevel>
<DocumentationFile>
</DocumentationFile>
<StartAction>Project</StartAction>
<StartProgram>
</StartProgram>
<StartArguments>
</StartArguments>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
Loading