我想爲我的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