Skip to content

Commit 09c2096

Browse files
committed
Only colour log output if attached to a terminal
Detect whether stdout is terminal; if it isn't then supress ANSI escape codes in log messages.
1 parent 09775a1 commit 09c2096

File tree

8 files changed

+71
-16
lines changed

8 files changed

+71
-16
lines changed

core-program/core-program.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ cabal-version: 1.12
55
-- see: https://github.com/sol/hpack
66

77
name: core-program
8-
version: 0.4.5.0
8+
version: 0.4.5.1
99
synopsis: Opinionated Haskell Interoperability
1010
description: A library to help build command-line programs, both tools and
1111
longer-running daemons.

core-program/lib/Core/Program/Context.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import Core.Program.Metadata
5151
import Core.System.Base hiding (catch, throw)
5252
import Core.Text.Rope
5353
import Data.Foldable (foldrM)
54+
import System.IO (hIsTerminalDevice)
5455
import Data.Int (Int64)
5556
import Data.String (IsString)
5657
import Prettyprinter (LayoutOptions (..), PageWidth (..), layoutPretty)
@@ -164,6 +165,7 @@ data Context τ = Context
164165
{ -- runtime properties
165166
programNameFrom :: MVar Rope
166167
, terminalWidthFrom :: Int
168+
, terminalColouredFrom :: Bool
167169
, versionFrom :: Version
168170
, -- only used during initial setup
169171
initialConfigFrom :: Config
@@ -353,6 +355,7 @@ configure version t config = do
353355
q <- newEmptyMVar
354356
i <- newMVar start
355357
columns <- getConsoleWidth
358+
coloured <- getConsoleColoured
356359
level <- newEmptyMVar
357360
out <- newTQueueIO
358361
tel <- newTQueueIO
@@ -364,6 +367,7 @@ configure version t config = do
364367
$! Context
365368
{ programNameFrom = n
366369
, terminalWidthFrom = columns
370+
, terminalColouredFrom = coloured
367371
, versionFrom = version
368372
, initialConfigFrom = config
369373
, initialExportersFrom = []
@@ -392,7 +396,12 @@ getConsoleWidth = do
392396
Nothing -> 80
393397
return columns
394398

395-
--
399+
400+
getConsoleColoured :: IO Bool
401+
getConsoleColoured = do
402+
terminal <- hIsTerminalDevice stdout
403+
pure terminal
404+
396405

397406
{- |
398407
Process the command line options and arguments. If an invalid option is

core-program/lib/Core/Program/Execute.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ loopForever action v out queue = do
447447
formatLogMessage
448448
start
449449
now
450+
True
450451
SeverityInternal
451452
("telemetry: sent " <> desc)
452453
atomically $ do
@@ -460,6 +461,7 @@ loopForever action v out queue = do
460461
formatLogMessage
461462
start
462463
now
464+
True
463465
SeverityWarn
464466
("sending telemetry failed (Exception: " <> intoRope (show e) <> "); Restarting exporter.")
465467
atomically $ do

core-program/lib/Core/Program/Logging.hs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ putMessage context (Message now level text possiblelValue) = do
179179
let i = startTimeFrom context
180180
start <- readMVar i
181181
let output = outputChannelFrom context
182+
coloured = terminalColouredFrom context
182183

183184
let display = case possiblelValue of
184185
Just value ->
@@ -187,13 +188,13 @@ putMessage context (Message now level text possiblelValue) = do
187188
else text <> " = " <> value
188189
Nothing -> text
189190

190-
let !result = formatLogMessage start now level display
191+
let !result = formatLogMessage start now coloured level display
191192

192193
atomically $ do
193194
writeTQueue output (Just result)
194195

195-
formatLogMessage :: TimeStamp -> TimeStamp -> Severity -> Rope -> Rope
196-
formatLogMessage start now severity message =
196+
formatLogMessage :: TimeStamp -> TimeStamp -> Bool -> Severity -> Rope -> Rope
197+
formatLogMessage start now coloured severity message =
197198
let !start' = unTimeStamp start
198199
!now' = unTimeStamp now
199200
!stampZ =
@@ -219,16 +220,26 @@ formatLogMessage start now severity message =
219220
SeverityInternal -> intoEscapes dullBlue
220221

221222
!reset = intoEscapes resetColour
222-
in mconcat
223-
[ intoEscapes dullWhite
224-
, intoRope stampZ
225-
, " ("
226-
, padWithZeros 6 (show elapsed)
227-
, ") "
228-
, colour
229-
, message
230-
, reset
231-
]
223+
in case coloured of
224+
True ->
225+
mconcat
226+
[ intoEscapes dullWhite
227+
, intoRope stampZ
228+
, " ("
229+
, padWithZeros 6 (show elapsed)
230+
, ") "
231+
, colour
232+
, message
233+
, reset
234+
]
235+
False ->
236+
mconcat
237+
[ intoRope stampZ
238+
, " ("
239+
, padWithZeros 6 (show elapsed)
240+
, ") "
241+
, message
242+
]
232243

233244
{- |
234245
Utility function to prepend \'0\' characters to a string representing a

core-program/package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: core-program
2-
version: 0.4.5.0
2+
version: 0.4.5.1
33
synopsis: Opinionated Haskell Interoperability
44
description: |
55
A library to help build command-line programs, both tools and

core-telemetry/lib/Core/Telemetry/Console.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ processConsoleOutput out datums = do
8181
formatLogMessage
8282
start
8383
now
84+
True
8485
SeverityInternal
8586
text
8687
atomically $ do

package.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ dependencies:
5555

5656
executables:
5757
snippet:
58+
buildable: false
5859
dependencies:
5960
- core-webserver-warp
6061
- http-types
@@ -67,6 +68,18 @@ executables:
6768
main: WarpSnippet.hs
6869
other-modules: []
6970

71+
experiment:
72+
dependencies:
73+
- bytestring
74+
- prettyprinter
75+
- unordered-containers
76+
ghc-options:
77+
- -threaded
78+
source-dirs:
79+
- tests
80+
main: SimpleExperiment.hs
81+
other-modules: []
82+
7083
tests:
7184
check:
7285
dependencies:

unbeliever.cabal

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ source-repository head
5454
type: git
5555
location: https://github.com/aesiniath/unbeliever
5656

57+
executable experiment
58+
main-is: SimpleExperiment.hs
59+
hs-source-dirs:
60+
tests
61+
ghc-options: -Wall -Wwarn -fwarn-tabs -threaded
62+
build-depends:
63+
base >=4.11 && <5
64+
, bytestring
65+
, core-data >=0.3.0.2
66+
, core-program >=0.4.0.0
67+
, core-telemetry >=0.1.7.3
68+
, core-text >=0.3.4.0
69+
, core-webserver-servant >=0.0.1.0
70+
, core-webserver-warp >=0.1.0.0
71+
, prettyprinter
72+
, unordered-containers
73+
default-language: Haskell2010
74+
5775
executable snippet
5876
main-is: WarpSnippet.hs
5977
hs-source-dirs:
@@ -70,6 +88,7 @@ executable snippet
7088
, http-types
7189
, wai
7290
, warp
91+
buildable: False
7392
default-language: Haskell2010
7493

7594
test-suite check

0 commit comments

Comments
 (0)