【发布时间】:2014-09-05 02:01:40
【问题描述】:
我想为我的 cordova 应用添加一些简单的日志记录功能。
于是我添加了文件插件,实现了一个超级简单的日志方法并进行了测试。
我的配置:
$ 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);
}
};
我发现了这个日志猫输出:
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()事件回调?
标签: file cordova phonegap-plugins cordova-3