Improve logging
diff --git a/src/fuzzers/cpp/PatternLayoutFuzzer.cpp b/src/fuzzers/cpp/PatternLayoutFuzzer.cpp
index ddb44a5..e81fb99 100644
--- a/src/fuzzers/cpp/PatternLayoutFuzzer.cpp
+++ b/src/fuzzers/cpp/PatternLayoutFuzzer.cpp
@@ -20,6 +20,8 @@
#include <unistd.h> // for `readlink`, `chdir`
#include <libgen.h> // for `dirname`
#include <limits.h> // for `PATH_MAX`
+#include <iomanip> // for `put_time()`
+#include <ctime> // for `time()`
#include <fuzzer/FuzzedDataProvider.h>
#include <log4cxx/propertyconfigurator.h>
#include <log4cxx/appenderskeleton.h>
@@ -68,10 +70,17 @@
} // namespace
+static std::time_t logTime;
+static std::tm* logTimeBuffer;
+#define LOG(stream) \
+ logTime = std::time(nullptr); \
+ logTimeBuffer = std::localtime(&logTime); \
+ stream << std::put_time(logTimeBuffer, "%Y-%m-%d %H:%M:%S") << " [" << __FILE_NAME__ << ":" << __LINE__ << "] "
+
static void findExecutablePath(char* buffer) {
ssize_t length = readlink("/proc/self/exe", buffer, PATH_MAX);
if (length == -1) {
- std::cerr << "ERROR: Failed to find the executable path" << std::endl;
+ LOG(std::cerr) << "ERROR: Failed to find the executable path" << std::endl;
exit(EXIT_FAILURE);
}
buffer[length] = '\0';
@@ -82,33 +91,22 @@
findExecutablePath(executablePath);
char* executableHome = dirname(executablePath);
if (chdir(executableHome) != 0) {
- std::cerr << "DEBUG: Executable path: " << executablePath << std::endl;
- std::cerr << "DEBUG: Executable home: " << executableHome << std::endl;
- std::cerr << "ERROR: Failed to `chdir()` the executable path" << std::endl;
+ LOG(std::cerr) << "DEBUG: Executable path: " << executablePath << std::endl;
+ LOG(std::cerr) << "DEBUG: Executable home: " << executableHome << std::endl;
+ LOG(std::cerr) << "ERROR: Failed to `chdir()` the executable path" << std::endl;
}
}
static int INITIALIZED = 0;
static void init() {
-
- // Initialize only once
if (INITIALIZED != 0) {
return;
}
- std::cout << "DEBUG: Initializing" << std::endl;
-
- // Report the effective Git commit ID
- std::cout << "INFO: Produced using the Git commit ID: " << GIT_COMMIT_ID << std::endl;
-
- // Load the configuration
+ LOG(std::cout) << "INFO: Produced using the Git commit ID: " << GIT_COMMIT_ID << std::endl;
chdirExecutableHome();
PropertyConfigurator::configure("PatternLayoutFuzzer.properties");
-
- // Toggle initialization flag
- std::cout << "DEBUG: Initialized" << std::endl;
INITIALIZED = 1;
-
}
#define MAX_STRING_LENGTH 30