55 * @fileoverview Front end code relevant only to the Desktop version of
66* Ardublockly.
77 */
8- 'use strict' ;
8+ // 'use strict';
99
1010/** Create a namespace for the application. */
1111var Ardublockly = Ardublockly || { } ;
@@ -116,6 +116,65 @@ Ardublockly.bindSettingsPathInputs = function() {
116116 } ) ;
117117} ;
118118
119+ /** Wraps the console.log warn and errors to send data to logging file. */
120+ Ardublockly . redirectConsoleLogging = function ( ) {
121+ var winston = require ( 'electron' ) . remote . require ( 'winston' ) ;
122+ var consoleLog = console . log ;
123+ var consoleWarning = console . warning ;
124+ var consoleError = console . error ;
125+
126+ // This is magic from Stack Overflow
127+ // http://stackoverflow.com/questions/14172455/get-name-and-line-of-calling-function-in-node-js
128+ Object . defineProperty ( global , '__stack' , {
129+ get : function ( ) {
130+ var orig = Error . prepareStackTrace ;
131+ Error . prepareStackTrace = function ( _ , stack ) {
132+ return stack ;
133+ } ;
134+ var err = new Error ;
135+ Error . captureStackTrace ( err , arguments . callee ) ;
136+ var stack = err . stack ;
137+ Error . prepareStackTrace = orig ;
138+ return stack ;
139+ }
140+ } ) ;
141+ Object . defineProperty ( global , '__stackfilename' , {
142+ get : function ( ) {
143+ return __stack [ 2 ] . getFileName ( ) ;
144+ }
145+ } ) ;
146+ Object . defineProperty ( global , '__line' , {
147+ get : function ( ) {
148+ return __stack [ 2 ] . getLineNumber ( ) ;
149+ }
150+ } ) ;
151+ Object . defineProperty ( global , '__function' , {
152+ get : function ( ) {
153+ return __stack [ 2 ] . getFunctionName ( ) ;
154+ }
155+ } ) ;
156+
157+ // Wrapping console logging
158+ console . log = function ( logMessage ) {
159+ consoleLog . apply ( console , arguments ) ;
160+ var tagRenderer = '[Renderer "' + __stackfilename + ':' + __function +
161+ '():L' + __line + '"] ' ;
162+ winston . info ( tagRenderer + logMessage ) ;
163+ } ;
164+ console . warning = function ( warnMessage ) {
165+ consoleWarning . apply ( console , arguments ) ;
166+ var tagRenderer = '[Renderer "' + __stackfilename + ':' + __function +
167+ '():L' + __line + '"] ' ;
168+ winston . warn ( tagRenderer + warnMessage ) ;
169+ } ;
170+ console . error = function ( errMessage ) {
171+ consoleError . apply ( console , arguments ) ;
172+ var tagRenderer = '[Renderer "' + __stackfilename + ':' + __function +
173+ '():L' + __line + '"] ' ;
174+ winston . error ( tagRenderer + errMessage ) ;
175+ } ;
176+ } ;
177+
119178/** Initialize Ardublockly code required for Electron on page load. */
120179window . addEventListener ( 'load' , function load ( event ) {
121180 window . removeEventListener ( 'load' , load , false ) ;
@@ -131,6 +190,8 @@ window.addEventListener('load', function load(event) {
131190 var webFrame = require ( 'electron' ) . webFrame ;
132191 webFrame . setZoomLevelLimits ( 1 , 1 ) ;
133192
193+ Ardublockly . redirectConsoleLogging ( ) ;
194+
134195 // Electron does not offer a prompt, so replace Blocks version with modal
135196 // Original signature: function(message, opt_defaultInput, opt_callback)
136197 Blockly . prompt = Ardublockly . htmlPrompt ;
0 commit comments