Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:17 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Managing Log Files.

Author Message
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 08:50pm 24 Jan 2018
Copy link to clipboard 
Print this post

Hi All,

Just downloaded the log file from one of my MM+'s.

About a 15 minute download over TCP via MMedit.
It's just a CSV that can load straight into Excel.

Nothing flash about the routine writing the file...


  Quote  Sub Logdata

If DoLog=1 And LogDone=0 Then

If Dir$("SpaLog.csv", File)="" Then
OPEN "SpaLog.csv" FOR Append AS #2
Print #2, "Date,Time,TmpScAmb.,TmpScPan,TmpScCur,TmpScInp,TmpScOut,FlowRate,SlrVlt,FlowRate"
Close #2
End If

If Dir$("HeatLog.csv", File)="" Then
OPEN "HeatLog.csv" FOR Append AS #3
Print #3, "Date,Time,TmpChAmb,TmpChRof,TmpChLng,TmpChCel,TmpChBed,TmpChOf1,TmpChOf2"
Close #3
End If

OPEN "SpaLog.csv" FOR Append AS #2
Print #2, Date$;",";Time$;",";TmpScAmb;",";TmpScPan;",";TmpScCur;",";TmpScInp;",";TmpScOut;",";FlowRate;",";SlrVlt;",";FlowRate*(TmpScOut-TmpScInp)*70.2
Close #2

OPEN "HeatLog.csv" FOR Append AS #3
Print #3, Date$;",";Time$;",";TmpChAmb;",";TmpChRof;",";TmpChLng;",";TmpChCel;",";TmpChBed;",";TmpChOf1;",";TmpChOf2
Close #3
LogDone=
1
End If



What I'd like to do is have it roll over to a new file at midnight at the end of each month.

Or more so rename it to say HeatLog1712 for example. then start a new one.

Does anyone have an existing example or some thoughts they'd care to share.

Thanks

Phil.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 09:18pm 24 Jan 2018
Copy link to clipboard 
Print this post

Something like this
  Quote   thisday$ = DATE$
PRINT thisday$

filename$ =
"HeatLog"+MID$(thisday$,9,2)+MID$(thisday$,4,2)+MID$(thisday$,1,2)+".csv"
PRINT filename$



you can 'append' to a new file and it will be created as required.

Jim
VK7JH
MMedit
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 10:07pm 24 Jan 2018
Copy link to clipboard 
Print this post

Thanks Jim,

That's a starting point.

I'm thinking monthly log files so could use,

  Quote   thisday$ = DATE$
filename$ =
"HeatLog"+MID$(thisday$,9,2)+MID$(thisday$,4,2)+".csv"

PRINT filename$


That would give me HeatLog1801.csv, ...1802 etc.

What I would like is to keep the current one as just HeatLog.csv & then rename it to the above when a new one is required.

Not sure about RENAME though.
It was missing from the MM+ last I looked, the manual had an error carrying it's presence & syntax over from the Maximite, which did support rename.

Phil.
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 10:20pm 24 Jan 2018
Copy link to clipboard 
Print this post

It would be easier to leave it as the current month. That way whenever you go to log it will use the current month in the filename.

'Define variables somewhere

Sub Logdata
If DoLog=1 And LogDone=0 Then
thisday$ = DATE$
fileSpa$ = "SpaLog"+MID$(thisday$,9,2)+MID$(thisday$,4,2)+".csv"
fileHeat$ = "HeatLog"+MID$(thisday$,9,2)+MID$(thisday$,4,2)+".csv"
PRINT fileSpa$, " ", fileHeat$

If Dir$(fileSpa$, File)="" Then
OPEN fileSpa$ FOR Append AS #2
Print #2, "Date,Time,TmpScAmb.,TmpScPan,TmpScCur,TmpScInp,TmpScOut,FlowRate,SlrVlt,FlowRate"
Close #2

'Do same as previous code but use fileSpa$ and fileHeat$ as the filenames
Edited by Azure 2018-01-26
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 10:29pm 24 Jan 2018
Copy link to clipboard 
Print this post

If you can recognise when a new month starts (easy enough to do), you could write two logs.
One with the year/month in it and a separate one for current month.
At the start of a new month, delete the current month file and let it start again.

Jim

VK7JH
MMedit
 
erbp
Senior Member

Joined: 03/05/2016
Location: Australia
Posts: 195
Posted: 10:42pm 24 Jan 2018
Copy link to clipboard 
Print this post

The MM+ manual for v5.4 lists the NAME command which will do what you want - see below.

NAME old$ AS new$
Rename a file or a directory from ‘old$’ to ‘new$’. Both are strings.
A directory path can be used in both 'old$' and 'new$'. If the paths differ the
file specified in 'old$' will be moved to the path specified in 'new$' with the file
name as specified.


I've not used it myself so cannot vouch that it works, but since it is in the 5.4 document I would expect that it would.

Phil.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 11:34pm 24 Jan 2018
Copy link to clipboard 
Print this post

Hi Phill.

This is what I use in one of my programs, to log any events to a DAILY log file, that changes automatically to a new filename each day.

The ACTLOG(ActivityLog) if/then, is part of the main message processing loop, and ACTLOG is a flag set in the options page so you can turn logging off an on just by checking this flag as part of the normal run through the main loop.

  Quote   IF ACTLOG THEN 'Log this message to today's logfile
T2$=Right$(DATE$,2)
T1$=
Left$(DATE$,2)
MONTH=
VAL(MID$(DATE$,4,2))
LOGFILE$=T1$+MTH$(MONTH)+T2$+
".LOG"
IF DEBUG THEN Print "Append to log: "+LOGFILE$
Chdir "\LOG"
Do While FILE_OPEN_FLAG:Loop
FILE_OPEN_FLAG=
1
Open LOGFILE$ for Append as #4
Print #4,L$
Close #4
FILE_OPEN_FLAG=
0
ENDIF


The beauty of OPEN FOR APPEND, is that if the file exists, it will be appended to. If it does NOT exist, MMBASIC will create it, and start writing to it which makes it nice and simple.

I also have two other subs which are called whenever something unexpected happens(ERRLOG - ErrorsLog), or whenever the user changes anything(SYSLOG - SystemLog). These are MONTHLY logs rather then daily ones, as they don't(or shouldn't!) see as much activity as the daily ones do. T1$ is the data(or message in this case) that you want added to the log, and this is passed to the sub as part of the call to the sub in the normal fashion, then the sub takes total care of logging that message to the current file etc, etc, etc.

  Quote  SUB WRITE_ERRLOG(T1$)
Local TXT$,MREF$,FILE$
Chdir "\ERRLOG"
Do While FILE_OPEN_FLAG:Loop
FILE_OPEN_FLAG=
1
MREF$=MTH$(
VAL(MID$(DATE$,4,2)))+"_"+MID$(DATE$,7) 'Build a reference for current month
FILE$=MREF$+".LOG"
Open FILE$ for Append as #4
TXT$=
"["+DATE$+", "+time$+"> - "+T1$
Print #4,TXT$
IF DEBUG THEN Print FILE$+" - "+TXT$
Close #4
FILE_OPEN_FLAG=
0
END SUB

SUB WRITE_SYSLOG(T1$)
Local TXT$,MREF$,FILE$
Chdir "\SYSLOG"
Do While FILE_OPEN_FLAG:Loop
FILE_OPEN_FLAG=
1
MREF$=MTH$(
VAL(MID$(DATE$,4,2)))+"_"+MID$(DATE$,7) 'Build a reference for current month
FILE$=MREF$+".LOG"
Open FILE$ for Append as #4
TXT$=
"["+DATE$+", "+time$+"> - "+T1$
Print #4,TXT$
IF DEBUG THEN Print FILE$+" - "+TXT$
Close #4
FILE_OPEN_FLAG=
0
END SUB


I hope those code-snippets help.

EDIT: I should mention that MTH$ is a simple array consisting of:

  Quote   Dim MTH$(12) LENGTH 3


...and the array is pre-loaded with these details:

  Quote   MTH$(1)="JAN":MTH$(2)="FEB":MTH$(3)="MAR":MTH$(4)="APR":MTH$(5)="MAY"
MTH$(
6)="JUN":MTH$(7)="JUL":MTH$(8)="AUG":MTH$(9)="SEP":MTH$(10)="OCT"
MTH$(
11)="NOV":MTH$(12)="DEC"


EDIT: This is the kind of output files the above two routines produce:





The daily logs are pretty much the same format, but the filename is something like 27JUN17.LOG kind of thing.Edited by Grogster 2018-01-26
Smoke makes things work. When the smoke gets out, it stops!
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 04:36am 25 Jan 2018
Copy link to clipboard 
Print this post

  erbp said   The MM+ manual for v5.4 lists the NAME command which will do what you want - see below.

I've not used it myself so cannot vouch that it works, but since it is in the 5.4 document I would expect that it would.


Yeah it's been in the manual in error for many versions,
but just checked again on a MM+ running 5.4 and it does now exist & works fine.

At this stage I just have a preference to keep sizes down by archiving complete months.

@Grogster,

I should be able to use some of yours once I setup a trigger flag to check the rollover.

Looking for the days rollover should be easy.
Just look for the bold parts of the string & set a flag.

01-01-2018 00:00:00

Phil.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 05:11am 25 Jan 2018
Copy link to clipboard 
Print this post


Just looking for midnight can give grief if you miss the time-slot for any reason.

This method calculates the filename every time you call the logdata sub and by keeping track of the old filename, it can tell if the month has rolled over without any concern about when the first record gets written to the new file.

  Quote  SUB logdata

thisday$ =
DATE$
thisMonth$ =
MID$(thisday$,9,2)+MID$(thisday$,4,2)+".csv"
IF thisMonth$ <> oldMonth$ THEN
' we have the first write to the new month
' do any end of month stuff here.
' You can write to the end of the old month or the start of the new.
oldMonth$ = thisMonth$ ' only call the start of month once
ENDIF

OPEN "HeatLog"+thisMonth$ FOR APPEND AS #2
PRINT #2, '''
CLOSE #2

OPEN "SpaLog"+thisMonth$ FOR APPEND AS #2
PRINT #2, '''
CLOSE #2


END SUB



It is good for regular writes and things like error logs which hopefully don't occur too often.

You can use the rollover condition to reset monthly maximums etc.

You will have to decide which month a reading exactly on midnight goes in.

Jim
VK7JH
MMedit
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 05:25am 25 Jan 2018
Copy link to clipboard 
Print this post

Bit Rusty on my code here, been months since I looked at it, but this is where I'd like to intergrate it.


  Quote   Dim Integer LogInt=15,DoLog,LogDone,Timer_L,Time_L 'Log every 15 minutes

SetTick 30000,ChkTime,2 'Checks time every 30 seconds

Do
If Touch(x) <> -1 Then CheckTouch

If DoLog=1 Then LogData

UpdateLCD
Loop

Sub ChkTime

If Val(Mid$(Time$,4,2)) Mod LogInt=0 And LogDone=0 Then
DoLog=
1
Else If Val(Mid$(Time$,4,2)) Mod LogInt<>0 And LogDone=1 Then
DoLog=
0:LogDone=0
End If

End Sub


Sub Logdata

If DoLog=1 And LogDone=0 Then

If Dir$("SpaLog.csv", File)="" Then
OPEN "SpaLog.csv" FOR Append AS #2
Print #2, "Date,Time,TmpScAmb.,TmpScPan,TmpScCur,TmpScInp,TmpScOut,FlowRate,SlrVlt,FlowRate"
Close #2
End If

If Dir$("HeatLog.csv", File)="" Then
OPEN "HeatLog.csv" FOR Append AS #3
Print #3, "Date,Time,TmpChAmb,TmpChRof,TmpChLng,TmpChCel,TmpChBed,TmpChOf1,TmpChOf2"
Close #3
End If

OPEN "SpaLog.csv" FOR Append AS #2
Print #2, Date$;",";Time$;",";TmpScAmb;",";TmpScPan;",";TmpScCur;",";TmpScInp;",";TmpScOut;",";FlowRate;",";SlrVlt;",";FlowRate*(TmpScOut-TmpScInp)*70.2
Close #2

OPEN "HeatLog.csv" FOR Append AS #3
Print #3, Date$;",";Time$;",";TmpChAmb;",";TmpChRof;",";TmpChLng;",";TmpChCel;",";TmpChBed;",";TmpChOf1;",";TmpChOf2
Close #3
LogDone=
1
End If

End Sub




Phil.
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 05:58am 25 Jan 2018
Copy link to clipboard 
Print this post

I use the following format:
YYYYMMDDHHmmss

According to needs i use more or less of that format.
You can make files per second/minute/hour/day/month/year depending on how many characters you use.
Even when you change it during use it will still sort easy.


Microblocks. Build with logic.
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 07:51am 25 Jan 2018
Copy link to clipboard 
Print this post

  Phil23 said  ...
  Quote  Sub ChkTime

If Val(Mid$(Time$,4,2)) Mod LogInt=0 And LogDone=0 Then
DoLog=
1
Else If Val(Mid$(Time$,4,2)) Mod LogInt<>0 And LogDone=1 Then
DoLog=
0:LogDone=0
End If

End Sub



That is a very strange use of MOD (value MOD bool). It would normally be another AND or similar logical comparison.

The code I suggested earlier will start a new file automatically when the month rolls over, you do not need to check if it has changed. When it changes the file won't be there and it will create it and add the csv header like you originally had.
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 09:55pm 25 Jan 2018
Copy link to clipboard 
Print this post

  Azure said  
  Phil23 said  FONT=Courier]Sub ChkTime

If Val(Mid$(Time$,4,2)) Mod LogInt=0 And LogDone=0 Then
DoLog=
1


That is a very strange use of MOD (value MOD bool). It would normally be another AND or similar logical comparison.there and it will create it and add the csv header like you originally had.


I was looking for a way of being able to easily change the logging interval, so went with dividing the minutes by the interval.

Figured it was only every going to be interval that were fractions of 60 minutes.

Phil.Edited by Phil23 2018-01-27
 
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 02:34am 26 Jan 2018
Copy link to clipboard 
Print this post

@Phil23
Short answer, that evaluation is probably not doing what you expect.
You need to post more of the code to get a useful suggestion for changes.

Longer Answer:
I looked up the MMBasic Manual to try and use the correct MMBasic terminology.
The evaluation mixes arithmetic and logical operations, it is not clear what the precedence of arithmetic vs logical operators is (they are normally not mixed).
If MOD has a lower precedence than AND then the following is true:
LogInt=0 evaluates and returns a logical comparison (0 for false, 1 for true)
LogDone=0 evaluates returns a logical comparison (0 for false, 1 for true)
LogInt=0 AND LogDone=0 evaluates and returns a bitwise comparison
between the 0 or 1's above, so the result will still be 0 or 1.

So the evaluation becomes "Val() MOD [0 or 1], which I don't think is what you are intending.

If MOD has a higher precedence than AND then it will be evaluated differently again.
Edited by Azure 2018-01-27
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025