logging via physfs qmlfrontend
authorunc0rr
Tue, 15 Mar 2016 22:29:40 +0300
branchqmlfrontend
changeset 11607 f0dcdbb9b2fe
parent 11606 99966b4a6e1e
child 11633 215b837c16f9
logging via physfs
hedgewars/uPhysFSLayer.pas
hedgewars/uUtils.pas
--- a/hedgewars/uPhysFSLayer.pas	Mon Mar 14 22:08:27 2016 +0300
+++ b/hedgewars/uPhysFSLayer.pas	Tue Mar 15 22:29:40 2016 +0300
@@ -22,17 +22,21 @@
 function rwopsOpenWrite(fname: shortstring): PSDL_RWops;
 
 function pfsOpenRead(fname: shortstring): PFSFile;
+function pfsOpenWrite(fname: shortstring): PFSFile;
+function pfsFlush(f: PFSFile): boolean;
 function pfsClose(f: PFSFile): boolean;
 function pfsSeek(f: PFSFile; pos: QWord): boolean;
 
 procedure pfsReadLn(f: PFSFile; var s: shortstring);
 procedure pfsReadLnA(f: PFSFile; var s: ansistring);
+procedure pfsWriteLn(f: PFSFile; s: shortstring);
 function pfsBlockRead(f: PFSFile; buf: pointer; size: Int64): Int64;
 function pfsEOF(f: PFSFile): boolean;
 function pfsEnumerateFiles(dir: shortstring): PPChar;
 procedure pfsFreeList(list: PPChar);
 
 function pfsExists(fname: shortstring): boolean;
+function pfsMakeDir(path: shortstring): boolean;
 
 function  physfsReader(L: Plua_State; f: PFSFile; sz: Psize_t) : PChar; cdecl; external PhyslayerLibName;
 procedure physfsReaderSetBuffer(buf: pointer); cdecl; external PhyslayerLibName;
@@ -49,11 +53,16 @@
 function PHYSFS_deinit(): LongInt; cdecl; external PhysfsLibName;
 function PHYSFS_mount(newDir, mountPoint: PChar; appendToPath: LongBool) : LongBool; cdecl; external PhysfsLibName;
 function PHYSFS_openRead(fname: PChar): PFSFile; cdecl; external PhysfsLibName;
+function PHYSFS_openWrite(fname: PChar): PFSFile; cdecl; external PhysfsLibName;
+function PHYSFS_setWriteDir(path: PChar): LongBool; cdecl; external PhysfsLibName;
 function PHYSFS_eof(f: PFSFile): LongBool; cdecl; external PhysfsLibName;
 function PHYSFS_readBytes(f: PFSFile; buffer: pointer; len: Int64): Int64; cdecl; external PhysfsLibName;
+function PHYSFS_writeBytes(f: PFSFile; buffer: pointer; len: Int64): Int64; cdecl; external PhysfsLibName;
 function PHYSFS_seek(f: PFSFile; pos: QWord): LongBool; cdecl; external PhysfsLibName;
+function PHYSFS_flush(f: PFSFile): LongBool; cdecl; external PhysfsLibName;
 function PHYSFS_close(f: PFSFile): LongBool; cdecl; external PhysfsLibName;
 function PHYSFS_exists(fname: PChar): LongBool; cdecl; external PhysfsLibName;
+function PHYSFS_mkdir(path: PChar): LongBool; cdecl; external PhysfsLibName;
 function PHYSFS_getLastError(): PChar; cdecl; external PhysfsLibName;
 function PHYSFS_enumerateFiles(dir: PChar): PPChar; cdecl; external PhysfsLibName;
 procedure PHYSFS_freeList(list: PPChar); cdecl; external PhysfsLibName;
@@ -79,11 +88,21 @@
     exit(PHYSFS_openRead(Str2PChar(fname)));
 end;
 
+function pfsOpenWrite(fname: shortstring): PFSFile;
+begin
+    exit(PHYSFS_openWrite(Str2PChar(fname)));
+end;
+
 function pfsEOF(f: PFSFile): boolean;
 begin
     exit(PHYSFS_eof(f))
 end;
 
+function pfsFlush(f: PFSFile): boolean;
+begin
+    exit(PHYSFS_flush(f))
+end;
+
 function pfsClose(f: PFSFile): boolean;
 begin
     exit(PHYSFS_close(f))
@@ -99,6 +118,11 @@
     exit(PHYSFS_exists(Str2PChar(fname)))
 end;
 
+function pfsMakeDir(path: shortstring): boolean;
+begin
+    exit(PHYSFS_mkdir(Str2PChar(path)))
+end;
+
 function pfsEnumerateFiles(dir: shortstring): PPChar;
 begin
     exit(PHYSFS_enumerateFiles(Str2PChar(dir)))
@@ -144,6 +168,14 @@
 s:= s + ansistring(b)
 end;
 
+procedure pfsWriteLn(f: PFSFile; s: shortstring);
+var c: char;
+begin
+    c:= #10;
+    PHYSFS_writeBytes(f, @s[1], byte(s[0]));
+    PHYSFS_writeBytes(f, @c, 1);
+end;
+
 function pfsBlockRead(f: PFSFile; buf: pointer; size: Int64): Int64;
 var r: Int64;
 begin
@@ -192,8 +224,11 @@
 {$ENDIF}
 
     pfsMountAtRoot(localPrefix);
+    pfsMount(userPrefix, PChar('/Config'));
+    pfsMakeDir('/Config/Data');
+    pfsMakeDir('/Config/Logs');
     pfsMountAtRoot(userPrefix + ansistring('/Data'));
-    pfsMount(userPrefix, PChar('/Config'));
+    PHYSFS_setWriteDir(userPrefix);
 
     hedgewarsMountPackages;
 
--- a/hedgewars/uUtils.pas	Mon Mar 14 22:08:27 2016 +0300
+++ b/hedgewars/uUtils.pas	Tue Mar 15 22:29:40 2016 +0300
@@ -95,10 +95,10 @@
 
 
 implementation
-uses {$IFNDEF PAS2C}typinfo, {$ENDIF}Math, uConsts, uVariables, SysUtils;
+uses {$IFNDEF PAS2C}typinfo, {$ENDIF}Math, uConsts, uVariables, SysUtils, uPhysFSLayer;
 
 {$IFDEF DEBUGFILE}
-var logFile: textfile;
+var logFile: PFSFile;
 {$IFDEF USE_VIDEO_RECORDING}
     logMutex: TRTLCriticalSection; // mutex for debug file
 {$ENDIF}
@@ -225,17 +225,8 @@
 end;
 
 function StrToInt(s: shortstring): LongInt;
-var c: LongInt;
 begin
-{$IFDEF PAS2C}
 val(s, StrToInt);
-{$ELSE}
-val(s, StrToInt, c);
-{$IFDEF DEBUGFILE}
-if c <> 0 then
-    writeln(logFile, 'Error at position ' + IntToStr(c) + ' : ' + s[c])
-{$ENDIF}
-{$ENDIF}
 end;
 
 function FloatToStr(n: hwFloat): shortstring;
@@ -361,8 +352,7 @@
 {$IFDEF USE_VIDEO_RECORDING}
 EnterCriticalSection(logMutex);
 {$ENDIF}
-writeln(logFile, inttostr(GameTicks)  + ': ' + s);
-flush(logFile);
+pfsWriteLn(logFile, inttostr(GameTicks)  + ': ' + s);
 
 {$IFDEF USE_VIDEO_RECORDING}
 LeaveCriticalSection(logMutex);
@@ -379,8 +369,9 @@
 {$IFDEF USE_VIDEO_RECORDING}
 EnterCriticalSection(logMutex);
 {$ENDIF}
-write(logFile, s);
-flush(logFile);
+// TODO: uncomment next two lines
+// write(logFile, s);
+// flush(logFile);
 {$IFDEF USE_VIDEO_RECORDING}
 LeaveCriticalSection(logMutex);
 {$ENDIF}
@@ -521,7 +512,6 @@
 {$IFDEF DEBUGFILE}
 var logfileBase: shortstring;
     i: LongInt;
-    rwfailed: boolean;
 {$ENDIF}
 begin
 {$IFDEF DEBUGFILE}
@@ -546,33 +536,15 @@
     InitCriticalSection(logMutex);
 {$ENDIF}
 {$I-}
-    rwfailed:= false;
-    (*if (length(UserPathPrefix) > 0) then
-        begin
-        {$IFNDEF PAS2C}
-        // create directory if it doesn't exist
-        if not FileExists(UserPathPrefix + '/Logs/') then
-            CreateDir(UserPathPrefix + '/Logs/');
-        {$ENDIF}
-        // if log is locked, write to the next one
-        i:= 0;
-        while(i < 7) do
-            begin
-            assign(logFile, shortstring(UserPathPrefix) + '/Logs/' + logfileBase + inttostr(i) + '.log');
-            Rewrite(logFile);
-            // note: IOResult is a function in pascal and a variable in pas2c
-            rwfailed:= (IOResult <> 0);
-            if (not rwfailed) then
-                break;
-            inc(i)
-            end;
-        end;
-    *)
-{$IFNDEF PAS2C}
-    // if everything fails, write to stderr
-    //if (length(UserPathPrefix) = 0) or (rwfailed) then
-        logFile:= stderr;
-{$ENDIF}
+    // if log is locked, write to the next one
+    i:= 0;
+    while(i < 7) do
+    begin
+        logFile:= pfsOpenWrite('/Logs/' + logfileBase + inttostr(i) + '.log');
+        if logFile <> nil then
+            break;
+        inc(i)
+    end;
 {$I+}
 {$ENDIF}
 
@@ -594,9 +566,9 @@
 procedure freeModule;
 begin
 {$IFDEF DEBUGFILE}
-    writeln(logFile, 'halt at ' + inttostr(GameTicks) + ' ticks. TurnTimeLeft = ' + inttostr(TurnTimeLeft));
-    flush(logFile);
-    close(logFile);
+    pfsWriteLn(logFile, 'halt at ' + inttostr(GameTicks) + ' ticks. TurnTimeLeft = ' + inttostr(TurnTimeLeft));
+    pfsFlush(logFile);
+    pfsClose(logFile);
 {$IFDEF USE_VIDEO_RECORDING}
     DoneCriticalSection(logMutex);
 {$ENDIF}