Skip to content

Instantly share code, notes, and snippets.

@pec1985
Last active October 12, 2015 21:48
Show Gist options
  • Save pec1985/4091949 to your computer and use it in GitHub Desktop.
Save pec1985/4091949 to your computer and use it in GitHub Desktop.
Login utility to save to file for titanium
/**
* 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'
});
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;
}
@pec1985
Copy link
Author

pec1985 commented Nov 17, 2012

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment