Last active
October 12, 2015 21:48
-
-
Save pec1985/4091949 to your computer and use it in GitHub Desktop.
Login utility to save to file for titanium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* To be used this way | |
*/ | |
var Log = require('log'); | |
Log.Info('This is an info log'); | |
Log.Error('This is an error log'); | |
Log.Debug('This is a debug log'); | |
// ... more code ... | |
Log.emailLogFile({ | |
developer: '[email protected]', | |
message: 'Something intelligent', | |
subject: 'Here is the log' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var DEBUG = true; | |
var WRITES = true; | |
function logFile() { | |
return Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'titanium_log.txt'); | |
} | |
function logMsg(type, str) { | |
if (typeof str == 'object') { | |
try { | |
str = JSON.parse(str); | |
} catch(e) { | |
str = JSON.stringify(str); | |
} | |
} | |
return '['+type+' ' + new Date().getTime() + '] ' + str; | |
} | |
exports.Info = function(str) { | |
if(DEBUG){ | |
if(WRITES) | |
logFile().write(logMsg('INFO', str), true); | |
Ti.API.info(str); | |
} | |
}; | |
exports.Debug = function(str) { | |
if(DEBUG){ | |
if(WRITES) | |
logFile().write(logMsg('DEBUG', str), true); | |
Ti.API.debug(str); | |
} | |
}; | |
exports.Error = function(str) { | |
if(DEBUG){ | |
if(WRITES) | |
logFile().write(logMsg('ERROR', str), true); | |
Ti.API.error(str); | |
} | |
}; | |
/** | |
* Gets the actual log file | |
*/ | |
exports.getLogFile = function() { | |
return logFile(); | |
}; | |
/** | |
* Gets the log file path | |
*/ | |
exports.getLogFile = function() { | |
return logFile().nativePath; | |
}; | |
/** | |
* Returns the text of the log file | |
*/ | |
exports.getLogText = function() { | |
return logFile().read(); | |
}; | |
/** | |
* Opens an email dialog to send the log to the developer | |
* @param {Object} params { | |
* @param developer Email to be sent to | |
* @param message Message to send | |
* @param subject Subject in the message | |
* } | |
*/ | |
exports.emailLogFile = function(params) { | |
var developer = params.developer || ''; | |
var message = params.message || 'Please see attached'; | |
var subject = params.subject || 'Log file from app'; | |
var email = Ti.UI.createEmailDialog({ | |
subject: 'Log file from app', | |
messageBody: 'Please see attached', | |
toRecipients: [developer] | |
}); | |
email.addAttachment(logFile()); | |
}; | |
exports.deleteLog = function() { | |
logFile().deleteFile(); | |
}; | |
exports.setDebug = function(_bool) { | |
DEBUG = _bool; | |
}; | |
exports.setWritesToFile = function(_bool) { | |
WRITES = _bool; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, it could optimized, I could have create a couple of variables or functions to avoid code repetition, but I put this together quickly as an example... meh.