Skip to content

Commit ae61fbd

Browse files
committed
Add ISO-formatted timestamps to file logging output
1 parent 8eaa0c8 commit ae61fbd

File tree

2 files changed

+69
-35
lines changed

2 files changed

+69
-35
lines changed

Source/Utils/Utils.cpp

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,100 @@
11
#include "Utils.h"
22

33
#ifdef _WIN32
4-
#include <windows.h>
54
#include <intrin.h>
5+
#include <windows.h>
66
#else
77
#include <dlfcn.h>
88
#endif
99

1010
#include <cctype>
1111

12-
std::string OELogger::getModuleName() {
12+
std::string OELogger::getModuleName()
13+
{
1314
#ifdef _WIN32
1415
HMODULE hModule = nullptr;
1516
// Get handle to the module containing the current instruction pointer
16-
GetModuleHandleEx(
17+
GetModuleHandleEx (
1718
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
18-
(LPCTSTR)(void*)_ReturnAddress(), // Cast return address properly
19+
(LPCTSTR) (void*) _ReturnAddress(), // Cast return address properly
1920
&hModule);
20-
21+
2122
WCHAR modulePath[MAX_PATH + 256];
22-
if (GetModuleFileNameW(hModule, modulePath, (DWORD)(MAX_PATH + 256))) {
23+
if (GetModuleFileNameW (hModule, modulePath, (DWORD) (MAX_PATH + 256)))
24+
{
2325
// Convert WCHAR to std::string using UTF-8 encoding
24-
int sizeNeeded = WideCharToMultiByte(CP_UTF8, 0, modulePath, -1, NULL, 0, NULL, NULL);
25-
std::string result(sizeNeeded, 0);
26-
WideCharToMultiByte(CP_UTF8, 0, modulePath, -1, &result[0], sizeNeeded, NULL, NULL);
27-
return formatModuleName(result);
26+
int sizeNeeded = WideCharToMultiByte (CP_UTF8, 0, modulePath, -1, NULL, 0, NULL, NULL);
27+
std::string result (sizeNeeded, 0);
28+
WideCharToMultiByte (CP_UTF8, 0, modulePath, -1, &result[0], sizeNeeded, NULL, NULL);
29+
return formatModuleName (result);
2830
}
2931
return "[unknown]";
3032
#else
3133
// macOS/Linux implementation
3234
Dl_info info;
33-
if (dladdr(reinterpret_cast<void*>(__builtin_return_address(0)), &info)) {
34-
if (info.dli_fname) {
35-
return formatModuleName(std::string(info.dli_fname));
35+
if (dladdr (reinterpret_cast<void*> (__builtin_return_address (0)), &info))
36+
{
37+
if (info.dli_fname)
38+
{
39+
return formatModuleName (std::string (info.dli_fname));
3640
}
3741
}
3842
return "[unknown]";
3943
#endif
4044
}
4145

42-
std::string OELogger::formatModuleName(const std::string& path) {
43-
size_t lastSlash = path.find_last_of("/\\");
44-
std::string basename = path.substr(lastSlash + 1);
45-
46-
// Remove .exe or .dll extension on Windows
47-
#ifdef _WIN32
48-
size_t lastDot = basename.find_last_of('.');
49-
if (lastDot != std::string::npos) {
50-
std::string ext = basename.substr(lastDot);
51-
if (_stricmp(ext.c_str(), ".exe") == 0 || _stricmp(ext.c_str(), ".dll") == 0) {
52-
basename = basename.substr(0, lastDot);
46+
std::string OELogger::formatModuleName (const std::string& path)
47+
{
48+
size_t lastSlash = path.find_last_of ("/\\");
49+
std::string basename = path.substr (lastSlash + 1);
50+
51+
// Remove .exe or .dll extension on Windows
52+
#ifdef _WIN32
53+
size_t lastDot = basename.find_last_of ('.');
54+
if (lastDot != std::string::npos)
55+
{
56+
std::string ext = basename.substr (lastDot);
57+
if (_stricmp (ext.c_str(), ".exe") == 0 || _stricmp (ext.c_str(), ".dll") == 0)
58+
{
59+
basename = basename.substr (0, lastDot);
5360
}
5461
}
55-
#endif
62+
#endif
5663

5764
std::string formatted;
58-
for (size_t i = 0; i < basename.length(); ++i) {
65+
for (size_t i = 0; i < basename.length(); ++i)
66+
{
5967
char ch = basename[i];
60-
if (std::isupper(ch)) {
61-
if (i > 0) {
68+
if (std::isupper (ch))
69+
{
70+
if (i > 0)
71+
{
6272
formatted += '-';
6373
}
64-
formatted += std::tolower(ch);
65-
} else {
74+
formatted += std::tolower (ch);
75+
}
76+
else
77+
{
6678
formatted += ch;
6779
}
6880
}
6981
return "[" + formatted + "]";
82+
}
83+
84+
std::string OELogger::getCurrentTimeIso()
85+
{
86+
// Get current time with millisecond precision
87+
auto now = std::chrono::system_clock::now();
88+
auto ms = std::chrono::duration_cast<std::chrono::milliseconds> (now.time_since_epoch()) % 1000;
89+
90+
// Convert to time_t for std::put_time
91+
std::time_t time_now = std::chrono::system_clock::to_time_t (now);
92+
std::tm bt = *std::localtime (&time_now);
93+
94+
// Format as ISO 8601 with milliseconds
95+
std::ostringstream oss;
96+
oss << std::put_time (&bt, "%Y-%m-%dT%H:%M:%S");
97+
oss << '.' << std::setfill ('0') << std::setw (3) << ms.count();
98+
99+
return oss.str();
70100
}

Source/Utils/Utils.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
#define UTIL_H_DEFINED
2626

2727
#include <chrono>
28+
#include <ctime>
2829
#include <fstream>
2930
#include <functional>
31+
#include <iomanip>
3032
#include <iostream>
3133
#include <map>
3234
#include <mutex>
35+
#include <sstream>
3336
#include <string>
3437

3538
#include "../Processors/PluginManager/PluginAPI.h"
@@ -69,6 +72,7 @@ class PLUGIN_API OELogger
6972
template <typename... Args>
7073
void LOGFile (Args&&... args)
7174
{
75+
logFile << "[" << getCurrentTimeIso() << "]";
7276
(logFile << ... << args);
7377
logFile << std::endl;
7478
}
@@ -83,7 +87,8 @@ class PLUGIN_API OELogger
8387
}
8488

8589
static std::string getModuleName();
86-
static std::string formatModuleName(const std::string& path);
90+
static std::string formatModuleName (const std::string& path);
91+
static std::string getCurrentTimeIso();
8792

8893
private:
8994
std::mutex mt;
@@ -93,9 +98,8 @@ class PLUGIN_API OELogger
9398
~OELogger() = default;
9499

95100
// Disable copy and move
96-
OELogger(const OELogger&) = delete;
97-
OELogger& operator=(const OELogger&) = delete;
98-
101+
OELogger (const OELogger&) = delete;
102+
OELogger& operator= (const OELogger&) = delete;
99103
};
100104

101105
/* Expose the Logger instance to plugins */
@@ -107,7 +111,7 @@ extern "C" PLUGIN_API OELogger& getOELogger();
107111

108112
/* Log Buffer -- related logs i.e. inside process() method */
109113
#define LOGB(...) \
110-
getOELogger().LOGFile (getOELogger().getModuleName(),"[buffer] ", __VA_ARGS__);
114+
getOELogger().LOGFile (getOELogger().getModuleName(), "[buffer] ", __VA_ARGS__);
111115

112116
/* Log Console -- gets printed to the GUI Debug Console */
113117
#define LOGC(...) \

0 commit comments

Comments
 (0)