From 7d882ac01b981c31729c9193ba8829beed2a4f06 Mon Sep 17 00:00:00 2001 From: Evan Czaplicki Date: Fri, 21 Feb 2014 11:57:14 -0500 Subject: [PATCH] Add library for simple logging, specifically for debugging. --- changelog.txt | 3 ++- libraries/Debug.elm | 17 +++++++++++++++++ libraries/Native/Debug.js | 22 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 libraries/Debug.elm create mode 100644 libraries/Native/Debug.js diff --git a/changelog.txt b/changelog.txt index 2be88c0..e9269b3 100644 --- a/changelog.txt +++ b/changelog.txt @@ -13,7 +13,8 @@ Breaking Changes: Improvements: - * Support Trampolining (thanks to @maxsnew and @timthelion) + * Add Trampoline library (thanks to @maxsnew and @timthelion) + * Add Debug library (inspired by @timthelion) * Drastically improved performance on markdown parsing (thanks to @Dandandan) * Add Date.fromTime function * Use pointer-events to detect hovers on layered elements (thanks to @Xashili) diff --git a/libraries/Debug.elm b/libraries/Debug.elm new file mode 100644 index 0000000..96d29ab --- /dev/null +++ b/libraries/Debug.elm @@ -0,0 +1,17 @@ +module Debug where +{-| This library is for investigating bugs or performance problems. It should +*not* be used in production code. +-} + +import Native.Debug + +{-| Log a tagged value on the developer console, and then return the value. + + 1 + log "number" 1 -- equals 2, logs "number: 1" + length (log "start" []) -- equals 0, logs "start: []" + +Notice that `log` is not a pure function! It should *only* be used for +investigating bugs or performance problems. +-} +log : String -> a -> a +log = Native.Debug.log \ No newline at end of file diff --git a/libraries/Native/Debug.js b/libraries/Native/Debug.js new file mode 100644 index 0000000..373e3e6 --- /dev/null +++ b/libraries/Native/Debug.js @@ -0,0 +1,22 @@ +Elm.Native.Debug = {}; +Elm.Native.Debug.make = function(elm) { + elm.Native = elm.Native || {}; + elm.Native.Debug = elm.Native.Debug || {}; + if (elm.Native.Debug.values) return elm.Native.Debug.values; + + var show = Elm.Native.Show.make(elm).show; + + function log(tag,value) { + if (process && process.stdout) { + process.stdout.write(tag + ': ' + show(value)); + } else { + console.log(tag + ': ' + show(value)); + } + return value; + } + + return elm.Native.Debug.values = { + log: F2(log) + }; + +};