logging via physfs
authorunc0rr
Tue, 15 Mar 2016 22:29:40 +0300
changeset 11632 cac74d9075be
parent 11631 b3db79b56f28
child 11633 215b837c16f9
child 11635 2a53e219bc1a
logging via physfs (grafted from f0dcdbb9b2fef96188c5d90547b12201228ce878)
hedgewars/uPhysFSLayer.pas
hedgewars/uUtils.pas
--- a/hedgewars/uPhysFSLayer.pas	Tue Mar 29 22:00:34 2016 +0300
+++ b/hedgewars/uPhysFSLayer.pas	Tue Mar 15 22:29:40 2016 +0300
@@ -22,14 +22,18 @@
 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;
 
 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 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;
@@ -46,11 +50,19 @@
 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;
 {$ELSE}
 function PHYSFS_readBytes(f: PFSFile; buffer: pointer; len: Int64): Int64;
 begin
@@ -73,11 +85,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))
@@ -88,6 +110,20 @@
     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)))
+end;
+
+procedure pfsFreeList(list: PPChar);
+begin
+    PHYSFS_freeList(list)
+end;
 
 procedure pfsReadLn(f: PFSFile; var s: shortstring);
 var c: char;
@@ -124,6 +160,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
@@ -176,7 +220,11 @@
 {$ENDIF}
 
     pfsMountAtRoot(localPrefix);
+    pfsMount(userPrefix, PChar('/Config'));
+    pfsMakeDir('/Config/Data');
+    pfsMakeDir('/Config/Logs');
     pfsMountAtRoot(userPrefix + ansistring('/Data'));
+    PHYSFS_setWriteDir(userPrefix);
 
     hedgewarsMountPackages;
 
--- a/hedgewars/uUtils.pas	Tue Mar 29 22:00:34 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}
@@ -545,35 +535,15 @@
 {$IFDEF USE_VIDEO_RECORDING}
     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}
-{$I+}
+    // 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;
 {$ENDIF}
 
     //mobile stuff
@@ -594,9 +564,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}