summaryrefslogtreecommitdiff
path: root/source/slang-capture-replay/capture_utility.cpp
diff options
context:
space:
mode:
authorkaizhangNV <149626564+kaizhangNV@users.noreply.github.com>2024-05-08 09:13:45 -0700
committerGitHub <noreply@github.com>2024-05-08 09:13:45 -0700
commit4f2330d059ab5943ddf33bfed37be6a0378d43a8 (patch)
tree9e3a0790deee6248343d49a644ab9f0be7909901 /source/slang-capture-replay/capture_utility.cpp
parenteb3970897049269602cc18cee644e437c0aff928 (diff)
capture/replay: interface implementation 1 (#4122)
* capture/replay: interface implementation 1 - Add global session, filesystem, and session capture interface classes: GlobalSessionCapture for IGlobalSession FileSystemCapture for ISlangFileSystemExt SessionCapture for ISession - Add environment variables to enable it The 2 variables are SLANG_CAPTURE_LAYER and SLANG_CAPTURE_LOG_LEVEL SLANG_CAPTURE_LAYER: In slang_createGlobalSession(), after the compiling/loading stdlib, we will check the capture environment variable, if it's set to 1, we will create a GlobalSessionCapture object and return to user code. SLANG_CAPTURE_LOG_LEVEL: This is to set the log level, user can choose the loglevel to debug. (We can remove this when the feature is fully implemented). - Update premake file and cmake file to add the capture/replay source folder * Fix Windows build error Fix windows build error by adding the "SLANG_MCALL" keyword. Change to use Slang::ComPtr for those captured object pointers to simplify the resource management. Use __func__ macro to print the function name in the log.
Diffstat (limited to 'source/slang-capture-replay/capture_utility.cpp')
-rw-r--r--source/slang-capture-replay/capture_utility.cpp82
1 files changed, 82 insertions, 0 deletions
diff --git a/source/slang-capture-replay/capture_utility.cpp b/source/slang-capture-replay/capture_utility.cpp
new file mode 100644
index 000000000..550edb64f
--- /dev/null
+++ b/source/slang-capture-replay/capture_utility.cpp
@@ -0,0 +1,82 @@
+
+#include <cstring>
+#include <string>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <mutex>
+
+#include "capture_utility.h"
+
+constexpr const char* kCaptureLayerEnvVar = "SLANG_CAPTURE_LAYER";
+constexpr const char* kCaptureLayerLogLevel = "SLANG_CAPTURE_LOG_LEVEL";
+
+namespace SlangCapture
+{
+ static thread_local unsigned int g_logLevel = LogLevel::Silent;
+
+ static bool getEnvironmentVariable(const char* name, std::string& out)
+ {
+#ifdef _WIN32
+ char* envVar = nullptr;
+ size_t sz = 0;
+ if (_dupenv_s(&envVar, &sz, name) == 0 && envVar != nullptr)
+ {
+ out = envVar;
+ free(envVar);
+ }
+#else
+ if (const char* envVar = std::getenv(name))
+ {
+ out = envVar;
+ }
+#endif
+ return out.empty() == false;
+ }
+
+ bool isCaptureLayerEnabled()
+ {
+ std::string envVarStr;
+ if(getEnvironmentVariable(kCaptureLayerEnvVar, envVarStr))
+ {
+ if (envVarStr == "1")
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ void setLogLevel()
+ {
+ // We only want to set the log level once
+ if (g_logLevel != LogLevel::Silent)
+ {
+ return;
+ }
+
+ std::string envVarStr;
+ if (getEnvironmentVariable(kCaptureLayerLogLevel, envVarStr))
+ {
+ char* end = nullptr;
+ unsigned int logLevel = std::strtol(envVarStr.c_str(), &end, 10);
+ if (end && (*end == 0))
+ {
+ g_logLevel = std::min((unsigned int)(LogLevel::Verbose), logLevel);
+ return;
+ }
+ }
+ }
+
+ void slangCaptureLog(LogLevel logLevel, const char* fmt, ...)
+ {
+ if (logLevel > g_logLevel)
+ {
+ return;
+ }
+
+ va_list args;
+ va_start(args, fmt);
+ vfprintf(stdout, fmt, args);
+ va_end(args);
+ }
+}