HiveLoggerMT5
- librerie
-
Sergey Chekhovskikh
👋 Hi there! I write robots, systems, and other stuff in Python and MQL5.
📊 My workday is a marathon of creating neural networks and algorithmic trading robots, which I make trade through MetaTrader. I teach them to play the market just like I taught my dog to balance a treat on his nose. - Versione: 1.6
HiveLoggerMT5 is a compiled logging library for MQL5 programs.
One call writes a short human-readable line to the Experts journal and the full event as one JSON line into a local file inside the terminal sandbox.
An expert, service or script leaves a machine-readable trace that any JSON-aware tool can filter and analyze later.
No DLLs, no network access, no external dependencies.
Two journals in one call
Each accepted event produces:
- a one-line entry in the Experts journal — level, stable event name, short message — for the operator watching the chart;
- a full JSONL record in the log file for agents, collectors and post-analysis.
Record fields: v (format version), time_local , time_local_ms (millisecond timestamp), source (program name), level , event , message , account , chart_id , command_id . One event is one physical line terminated with LF, so a partially written tail never corrupts earlier records and a collector can resume after a restart. The command_id field links all events of one command into a chain.
Connection in three steps
- The terminal installs the purchased library into MQL5\Libraries\Market\ .
- Save the header below as MQL5\Include\HiveLoggerMT5.mqh .
- Include the header in your program: open a session in OnInit , log events, close in OnDeinit .
#include <HiveLoggerMT5.mqh>
int OnInit()
{
if(!HiveOpenDefault())
return INIT_FAILED;
HiveLogInfo("session.start","logger ready");
return INIT_SUCCEEDED;
}
void OnTick()
{
HiveLogDebug("tick.processed","...");
}
void OnDeinit(const int reason)
{
HiveLogInfo("session.stop",IntegerToString(reason));
HiveClose();
} The full buyer header (save as MQL5\Include\HiveLoggerMT5.mqh ):
//+------------------------------------------------------------------+
//| HiveLoggerMT5.mqh |
//| HiveLoggerMT5 - MQL5 Market edition, customer header |
//| |
//| The single file a customer needs besides the binary: |
//| 1. Copy this file to MQL5\Include\HiveLoggerMT5.mqh |
//| 2. Copy HiveLoggerMT5.ex5 to MQL5\Libraries\Market\ |
//| (keep the Market subfolder name; the #import below uses it) |
//| 3. #include <HiveLoggerMT5.mqh> and call the Hive* functions. |
//| |
//| Flat C-style API over the binary library: full functional parity |
//| with the class API 1.6.1 except the sink interface, which stays |
//| a GitHub-edition feature (pointers do not pass through #import). |
//| The JSONL output is byte-identical to the class API, so the |
//| tools/hivelog.py CLI reads it without changes. |
//+------------------------------------------------------------------+
#ifndef HIVELOGGERMT5_MQH
#define HIVELOGGERMT5_MQH
// Log levels: pass as int to HiveLog/HiveSetMinLevel; the library
// validates the range.
enum ENUM_HIVE_LEVEL
{
HIVE_DEBUG=0,
HIVE_INFO=1,
HIVE_WARN=2,
HIVE_ERROR=3
};
// Write status codes returned by HiveWriteStatus (1.6.1 semantics):
// SHORT means a partial packet write or a discarded packet (the lost
// lines are counted in HiveDroppedLines); FLUSH means every byte was
// written but the flush was not confirmed. Both survive HiveClose and
// are reset by any HiveOpen with a valid path, even a failed one; a
// repeated Open on the open logger and an invalid path keep them.
// The MARKET_ prefix avoids clashing with the HIVE_WRITE_* enum members
// of the class edition (Include/HiveLogger/HiveLogger.mqh) if a buyer
// mixes both editions in one project.
#define HIVE_MARKET_WRITE_OK 0
#define HIVE_MARKET_WRITE_SHORT 1
#define HIVE_MARKET_WRITE_FLUSH 2
#import "Market\HiveLoggerMT5.ex5"
bool HiveOpen(const string folder,const string prefix);
bool HiveLog(const int level,const string event_type,const string message,
const string command_id);
bool HiveSetMinLevel(const int level);
bool HiveSetMaxFileBytes(const long bytes);
bool HiveSetRetentionFiles(const int files);
bool HiveSetRetentionAgeDays(const int days);
bool HiveSetRetentionTotalBytes(const long bytes);
bool HiveSetPreciseTime(const bool on);
bool HiveSetBuffering(const bool on,const int lines,const long bytes);
int HiveFlush(void);
int HiveBufferedLines(void);
int HiveDroppedLines(void);
int HiveWriteStatus(void);
int HiveWriteError(void);
string HivePath(void);
void HiveClose(void);
#import
// Inline wrappers with the common defaults. The #import prototypes
// above carry no default arguments (not guaranteed through #import),
// so these compile-time helpers keep call sites short. They never
// enter the .ex5.
bool HiveOpenDefault(void)
{
return HiveOpen("HiveLogger","");
}
bool HiveLogDebug(const string event_type,const string message,
const string command_id="")
{
return HiveLog(HIVE_DEBUG,event_type,message,command_id);
}
bool HiveLogInfo(const string event_type,const string message,
const string command_id="")
{
return HiveLog(HIVE_INFO,event_type,message,command_id);
}
bool HiveLogWarn(const string event_type,const string message,
const string command_id="")
{
return HiveLog(HIVE_WARN,event_type,message,command_id);
}
bool HiveLogError(const string event_type,const string message,
const string command_id="")
{
return HiveLog(HIVE_ERROR,event_type,message,command_id);
}
// Default packet limits match the class API: 64 lines, 32 KiB.
bool HiveSetBufferingDefault(const bool on)
{
return HiveSetBuffering(on,64,32768);
}
#endif // HIVELOGGERMT5_MQH
API overview
| Function | Purpose |
|---|---|
| HiveOpen(folder, prefix) / HiveOpenDefault() | Open a session; default folder HiveLogger , default prefix empty. Invalid paths (absolute, .. , : ) return false . |
| HiveLog(level, event_type, message, command_id) | One event to both journals. Returns false if the file write failed; filtered-out events return true without file I/O. |
| HiveLogDebug/Info/Warn/Error(event, message, command_id) | Level convenience wrappers. |
| HiveSetMinLevel(level) | Level filter, HIVE_DEBUG < HIVE_INFO < HIVE_WARN < HIVE_ERROR . |
| HiveSetMaxFileBytes(bytes) | File rotation by size; segments get -0 , -1 , ... suffixes. |
| HiveSetRetentionFiles(n) / HiveSetRetentionAgeDays(d) / HiveSetRetentionTotalBytes(b) | Retention limits applied at HiveOpen ; the open file is never deleted, but its size counts toward the total. |
| HiveSetBuffering(on, lines, bytes) / HiveFlush() | Buffered writes (defaults 64 lines / 32768 bytes, applied from the next HiveOpen ); explicit flush returns the number of lines written. |
| HiveBufferedLines() / HiveDroppedLines() | Queue depth and cumulative count of lines lost to buffer limits. |
| HiveSetPreciseTime(on) | Millisecond timestamps. on (default) calibrates once at the next Open ; off skips the calibration wait and logs seconds only. |
| HiveWriteStatus() / HiveWriteError() | Last write/flush failure: HIVE_MARKET_WRITE_OK/SHORT/FLUSH plus the OS error. Survives HiveClose ; reset by any HiveOpen with a valid path. |
| HivePath() | Relative path of the current file inside MQL5\Files , empty while closed. |
| HiveClose() | Flushes and closes the session. |
Behavior notes
- One session per program; each program gets its own library context, so several experts log independently.
- A repeated HiveOpen on the open logger keeps the current file and returns true ; switch the folder through HiveClose first.
- The log directory is created by the terminal if missing.
- In the Strategy Tester the library writes into the tester sandbox; millisecond precision relies on terminal time, which the tester virtualizes.
- The JSONL schema is stable and documented; the language of message content is whatever the caller passes.
Requirements
MetaTrader 5 terminal with Market support. The library performs no trading operations and reads no account state for its behavior; it works on any account type.
