我想为我的cordova应用程序添加一些简单的日志记录功能。使用phonegap/cordova文件插件第1部分的问题
所以我添加了文件插件,实现了一个超级简单的日志方法并进行了测试。
我的配置:
$ cordova --version
3.5.0-0.2.7
$ cordova plugins
org.apache.cordova.file 1.3.0 "File"
测试设备是华为U8850,运行Android 2.3.5
记录仪:
window.MyLog = {
log: function(line){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(FS) {
FS.root.getFile('the_log1.txt', {"create":true, "exclusive":false},
function(fileEntry) {
fileEntry.createWriter(
function(writer) {
console.log(line);
writer.seek(writer.length); // append to eof
writer.write(line + '\n'); // write the line
}, fail);
}, fail);
}, fail);
}
};
测试:
MyLog.log(new Date().toLocaleTimeString());
看起来似乎没事:
- 文件被创建,管线插入
- 再次启动应用程序时,该行被追加
但后来: 我试着写一些额外的字符:
window.MyLog = {
log: function(line){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(FS) {
FS.root.getFile('the_log2.txt', {"create":true, "exclusive":false},
function(fileEntry) {
fileEntry.createWriter(
function(writer) {
console.log(line);
writer.seek(writer.length); // append to eof
writer.write(line + '\n'); // write the line
writer.write('----' + '\n'); // extra write
}, fail);
}, fail);
}, fail);
}
};
我发现这个log cat输出:
16:00:00
processMessage failed: Error: [object Object]
processMessage failed: Stack: undefined
processMessage failed: Message: S01 File218896726 {"lastModifiedDate":1409839200000,"fullPath":"\/the_log2.txt","type":"text\/plain","name":"the_log2.txt","size":0}
- >貌似是不可能使用writer.write命令不止一次
问:
- 这究竟是为什么?它是一个错误还是一个功能?
- 有没有关于这个插件的使用的任何文档/示例代码? (这里找到文档:https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md都不够全面,因为我想)
你尝试执行'onwriteend()'事件回调
writer
和write
? – benka 2014-09-05 10:06:58