Android Question Append to text file issues

flyingpole

Member
Licensed User
Longtime User
Having an issue with text file writing. Below is my code to write a string to a log file for diagnosing customer issues. Every time I restart the app, the program will start a new file, even though I am opening my file for append. Subsequent calls to this routine will append to the file properly. If I run in debug mode and step through the routine, it will append properly to the pre-existing file with the same name. To summarize, if the user closes the app, then reopens the app, a new log file for that day is created, where I want to have it append to that days log file if there already is one. Again, if I step through this routine the first time it is called in the app, it works properly, but will not append if I let it run at full program speed.

Any Ideas please!

Sub LogEvent(TextToLog As String)
Try
Dim FW1 As TextWriter
Dim FileName As String
Dim Now As Long
Dim Month As Int
Dim Day As Int
Dim Year As Int
Dim LogEntry As String

Now = DateTime.Now
Month = DateTime.GetMonth(Now)
Day = DateTime.GetDayOfMonth (Now)
Year = DateTime.GetYear(Now)

FileName = NumberFormat(Month,2,0) & "-" & NumberFormat(Day,2,0) & "-" & Year & ".log"


FW1.Initialize(File.OpenOutput (LogFilePath, FileName, True))
LogEntry = NumberFormat(DateTime.GetHour(Now),2,0) & ":" & NumberFormat(DateTime.GetMinute(Now),2,0)& ":" & NumberFormat(DateTime.GetSecond (Now),2,0)
LogEntry =LogEntry & " " & TextToLog
FW1.WriteLine(LogEntry)

FW1.Close

Catch
Log("Error in Sub LogEvent")
End Try

End Sub
 

flyingpole

Member
Licensed User
Longtime User
B4X:
Sub LogEvent(TextToLog As String)
Try
Dim FW1 As TextWriter
Dim FileName As String
Dim Now As Long
Dim Month As Int
Dim Day As Int
Dim Year As Int
Dim LogEntry As String

Now = DateTime.Now
Month = DateTime.GetMonth(Now)
Day = DateTime.GetDayOfMonth (Now)
Year = DateTime.GetYear(Now)

FileName = NumberFormat(Month,2,0) & "-" & NumberFormat(Day,2,0) & "-" & Year & ".log"


FW1.Initialize(File.OpenOutput (LogFilePath, FileName, True))
LogEntry = NumberFormat(DateTime.GetHour(Now),2,0) & ":" & NumberFormat(DateTime.GetMinute(Now),2,0)& ":" & NumberFormat(DateTime.GetSecond (Now),2,0)
LogEntry =LogEntry & " " & TextToLog
FW1.WriteLine(LogEntry)

FW1.Close

Catch
Log("Error in Sub LogEvent")
End Try

End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The code is correct. It will not overwrite an existing file.

Add this to test it:
B4X:
If File.Exists(LogFilePath, FileName) Then
 Log("File exists")
 Log(File.ReadString(LogFilePath, FileName))
 Log("**********************")
Else
 Log("File does not exist")
End If
FW1.Initialize(...)
...
FW1.Close
Log(File.ReadString(LogFilePath, FileName))
 
Upvote 0

flyingpole

Member
Licensed User
Longtime User
I had actually added that code myself to test it as well. It shows the file existing, but it still opens it as new. Only when I run it full speed. If I put a breakpoint before the file open and step through (F8) it appends as it should. My problem is that I'm trying to find errors my customer's make, but when they have an error of some type, they instinctively close the app and reopen it, which is overwriting the old log file rather than appending to it, so I can't retrieve their error. This just started happening one or two releases of B4A ago, not certain when as I just realized this when a few customers have sent me their log files. I have not changed this code whatsoever and it has always worked in the past.

 
Upvote 0

flyingpole

Member
Licensed User
Longtime User
Please try to reproduce it in a small project and upload the project.
I found the issue. An errant character changed a loop count in another routine that deleted old files and was deleting the log file between calls to logevent. Sorry for bothering you and thanks as always for the quick response!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…