Loading…

Log Rotation Helper

A log file nobody ever rotates grows forever. On anything that logs every tick - and a debugging session usually should - that is a multi-gigabyte file within weeks, one the terminal is slow to open and Windows is slow to back up. This header checks a file's size against a threshold you set and, once it is crossed, renames the file aside with a timestamp and leaves the name free for whatever writes to it next. It deliberately does not write log lines itself and does not include Logger.mqh or any other header in this catalogue - every CodeBase submission has to stand alone, so pair this with your own FileOpen/FileWrite calls (or with the separate Logger.mqh submission, included independently in your own project). Call LogRotateCheck() before opening your log file each session, or periodically from OnTimer, and let it decide whether a rotation is due. Two rotations landing in the same second would otherwise collide on the same timestamped filename, silently overwriting one with the other. This header checks for that and appends "_2", "_3" and so on until it finds a free name, so a burst of activity never loses a log segment. Archive filenames use a fixed-width timestamp (yyyyMMdd_HHmmss), which means they sort into chronological order as plain strings - the pruning function does not need to parse dates back out of anything, it just sorts the filenames and deletes the oldest ones beyond the cap you gave it. Passing maxArchives = 0 is a legitimate setting, not a bug: it means "rotate to cap the active file's size, keep no history at all." Notes and limitations - Close any file handle your own code holds on filename before calling LogRotateCheck(). Renaming a file that is still open corrupts it on some Windows configurations, and this header has no way to detect that from inside the program on either platform - it is the caller's responsibility. - Operates entirely in the sandboxed Files folder. FILE_COMMON is not used, so this does not coordinate rotation across multiple terminal instances sharing one common folder - each terminal manages its own files. - The timestamp has one-second resolution. Two rotations within the same second are handled correctly (see the collision-avoidance behaviour above), but do not rely on the filename alone to order events more finely than a second - read the file's own timestamps or contents for that. - This header never writes to the active log file and never opens it for anything but reading its size. All writing is the caller's responsibility.

Script · MT5 · v1.0 · Free

About Log Rotation Helper

Log Rotation Helper is a script for MetaTrader 5. It has 1 setting, such as Base name. MQL source code included.

Files in the download: LogRotate.mq5.mqh, LogRotate.txt, LogRotateDemo.ex5, LogRotateDemo.mq5.

Settings

SettingDefaultWhat it does
Base namelograte_demo.logSandbox file used for the demonstration

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Trading Sessions · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria