![]() |
Forum Index : Microcontroller and PC projects : Managing Log Files.
Author | Message | ||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
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... 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: AustraliaPosts: 6283 |
Something like this you can 'append' to a new file and it will be created as required. Jim VK7JH MMedit |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Thanks Jim, That's a starting point. I'm thinking monthly log files so could use, 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: AustraliaPosts: 446 |
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 |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
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: AustraliaPosts: 195 |
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 ZealandPosts: 9610 |
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. 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. I hope those code-snippets help. EDIT: I should mention that MTH$ is a simple array consisting of: ...and the array is pre-loaded with these details: 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. Smoke makes things work. When the smoke gets out, it stops! |
||||
Phil23 Guru ![]() Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
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: AustraliaPosts: 6283 |
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. 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: AustraliaPosts: 1667 |
Bit Rusty on my code here, been months since I looked at it, but this is where I'd like to intergrate it. Phil. |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
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: AustraliaPosts: 446 |
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: AustraliaPosts: 1667 |
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. |
||||
Azure![]() Guru ![]() Joined: 09/11/2017 Location: AustraliaPosts: 446 |
@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. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |