Android Example Persistent log for IDE (developer) or APP (user)

I write for me, and share.
Add functionality to a log and logcolor functions.
Easy to implement.
Source Code for enhance or fix.
CSBuilder, TextWriter & TextReader implementation.



B4X:
' log on ide & toast & permanent file
logger.tlogcolor("HOLA: IS A TOAST AND LOG", Colors.Cyan)
logger.wlogcolor("SIMPLE LOG", Colors.Green)
logger.wlog("------Default Color-------------")

'output to screen
EditText1.Text=logger.ReadLog
logger.ReadList(ListView1)
1 line, create a log to ide, a tostamessage and write to logfile.
2 line, colorlog and write to file.
3 line, a simple logo to ide and file

next read the all-historical log to edittext and ListView
Other functions are in the code (like clear)

With this function you have an ever log of the app.
Simplify lines merging toast and log.
Can do/undo with search replace.
logcolor <=> logger.tlogcolor


TO-DO - IDEAS
Convert to a class and then to a library
check size file.log and reduce when raise this level
send log by mail.
and ...............more things.

B4X:
' EXAMPLES LOGGING

'    logger.wlogcolor("ERROR 1 MAIN - LINE 356", Colors.Red)
'    logger.tlogcolor("TOTAL REGISTROS:" & (Cursor1.RowCount - 1),Colors.Green)
'    logger.wlog("------Default Color-------------")

' EXAMPLES READ AND VIEW
' FOR TEXT AND LIST
'(*) logtext.Text=logger.ReadLog  
'(*) logger.ReadList(ListView1)

' EXAMPLES WRITE LOGS
'(*) logger.writelogfile("TEXT TO SAVE", Colors.Red,"YOURFILE.ooo")
'(*) logger.delete_file("wildcards")  

'(*) MOTHER FUNCTIONS, THE OTHER USES THIS.

Sub Process_Globals
    'default file for simplicity
    Dim file2log= "activity.log" As String
End Sub


' same as logcolor save to file
' MASTER FUNCTION: UNIQUE FUNCTION THAT WRITE THE LOGSFILE--------------------------------------------------
public Sub wlogcolor(TEXT As String, c As Int)
    writelogfile(TEXT , c ,file2log)
    LogColor(TEXT,c)
End Sub

'CHILDS FUNCTION -------------------------------------------------------------------------------------------
' same as logcolor save to file  AND TOAST MESSAGE
public Sub tlogcolor(TEXT As String,colo As Int)
    Dim cbs As CSBuilder

    wlogcolor(TEXT,colo)
    cbs.Initialize.Color(colo).Append(TEXT).PopAll
    ToastMessageShow(cbs,True)
End Sub

' same as log  save to file
public Sub wlog(TEXT As String)
    wlogcolor(TEXT,Colors.LightGray)
End Sub

' same but create custom color for ERRORS

'FILE FUNCTION -------------------------------------------------------------------------------------------
'READ FUNCTION -------------------------------------------------------------------------------------------

' read the log and return CSBUILDER ( Use with textedit multiline )
' Use:
' Dim ed As EditText  
' ed.TEXT=ReadLog
 
public Sub ReadLog As CSBuilder
    Return ReadLogFile(file2log)
End Sub

public Sub ReadLogFile(logfile As String) As CSBuilder
    Private tr As TextReader
    Private csb As CSBuilder
    Private oneline,s  As String
    Dim c As Int

    csb.Initialize
  
    tr.Initialize(File.OpenInput(File.DirDefaultExternal, logfile))
    Dim lista = tr.ReadList As List
    Dim i=lista.Size-1 As Int
    Do While i >= 0
        oneline=lista.Get(i)
        i=i-1
        If oneline.Contains("#") Then
            s = oneline.SubString2(oneline.IndexOf("#")+1,oneline.Length)
            c = oneline.SubString2(0,oneline.IndexOf("#"))
            csb.Color(c).Append(s).Append(CRLF).Pop
        End If
        'else ignore (can rebuild to plain txt)
    Loop
    csb.PopAll
    Return csb
End Sub

' read the log and return CSBUILDER ( Use with listview)
'USE:
' dim l as listview
' Readlist(l)
public Sub Readlist(l As ListView)  As CSBuilder
    Return FillList(file2log,l)
End Sub

'Same with custom file.log
public Sub FillList(logfile As String,l As ListView) As CSBuilder
    Private tr As TextReader
    Private csb As CSBuilder
    Private oneline,s  As String
    Dim c As Int

    csb.Initialize
  
    tr.Initialize(File.OpenInput(File.DirDefaultExternal, logfile))
    Dim lista = tr.ReadList As List
    Dim i=lista.Size-1 As Int
    Do While i >= 0
        oneline=lista.Get(i)
        i=i-1
        If oneline.Contains("#") Then
            s = oneline.SubString2(oneline.IndexOf("#")+1,oneline.Length)
            c = oneline.SubString2(0,oneline.IndexOf("#"))
            csb.Initialize
            l.AddSingleLine(csb.Color(c).Append(s).Pop) ' diferent here
        End If
    Loop

    csb.PopAll
    Return csb
End Sub

'FILE FUNCTION -------------------------------------------------------------------------------------------
'WRITE AND DELETE FUNCTION -------------------------------------------------------------------------------------------

public Sub writelogfile(TEXT As String, c As Int,logfile As String)
    Private tx As TextWriter
    tx.Initialize(File.OpenOutput(File.DirDefaultExternal, logfile, True))
    tx.WriteLine(c&"#"&TEXT&Chr(10)&Chr(13))
    tx.Close
End Sub

public Sub delete_file( comodin As String)
    Dim myfile As String
    Dim allsdcard As List

    allsdcard.Initialize
    allsdcard.AddAll(File.ListFiles(File.DirDefaultExternal))
    For i= allsdcard.Size-1 To 0 Step -1
        myfile = allsdcard.Get(i)
        If  myfile.Contains(comodin) Then
            File.delete(File.DirDefaultExternal,myfile )
        End If
    Next
End Sub


public Sub clean
    Dim myfile As String
    Dim allsdcard As List

    allsdcard.Initialize
    allsdcard.AddAll(File.ListFiles(File.DirDefaultExternal))
  
    For i= allsdcard.Size-1 To 0 Step -1
        myfile = allsdcard.Get(i)
        Log("-----------"&myfile)
        If  myfile.Contains("activity.log") Then      ' SAME AS file2log definition
                File.delete(File.DirDefaultExternal,myfile )
        End If
    Next
  
    're-create one
    wlog("")
  
End Sub

Full attach file.
 

Attachments

  • wlog.zip
    401.1 KB · Views: 182

ELCHARO

Member
Licensed User
I working in lib with timer logs
like:

tlog.init()
(((B4A- ROUTINE)))

tlog.time("Routine")


Results: Routine: 2:30:0000

Thanks.
 
Top