Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions FileXT/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,27 @@

// Debug logging macros
#ifndef NDEBUG
#define LOG(...) log(__VA_ARGS__)
#define LOG(format, ...) log("FileXT: " format "\n", ##__VA_ARGS__)
#else
#define LOG(...)
#endif

#ifdef ENABLE_LOG_VERBOSE
#define LOG_VERBOSE(...) log("FileXT: ", __VA_ARGS__);
#define LOG_VERBOSE(format, ...) log("FileXT [VERBOSE]: " format "\n", ##__VA_ARGS__);
#else
#define LOG_VERBOSE(...)
#endif

#define LOG_CRITICAL(...) logTrace(__LINE__, __FILE__, "FileXT: CRITICAL ERROR: ", __VA_ARGS__);
#define LOG_CRITICAL(format, ...) logTrace(__LINE__, __FILE__, "FileXT [CRITICAL ERROR]: " format " [%s] [line: %d]\n", ##__VA_ARGS__);

#ifndef NDEBUG
#define LOG_G(...) log_g(__VA_ARGS__)
#else
#define LOG_G(...)
#endif

// Forward Decls
extern std::unique_ptr<FILE, decltype(&std::fclose)> gpFile;
std::filesystem::path& getLogPath();

template <typename ...Args>
void log(const char* format, Args&&...args) {
Expand All @@ -36,17 +41,34 @@ void log(const char* format, Args&&...args) {
FILE* pFile = gpFile.get();
#endif

if (pFile != nullptr)
{
if (pFile != nullptr) {
std::fprintf(pFile, format, args...);
std::fflush(pFile);
}
}

template <typename ...Args>
inline void logTrace(int line, const char* fileName, const char* format, Args&&...args) {
size_t bufSize = std::snprintf(NULL, 0, "%s [%s] [line: %d]\n", format, fileName, line);
std::vector<char> buf(bufSize + 1, '\0');
std::snprintf(buf.data(), bufSize, "%s [%s] [line: %d]\n", format, fileName, line);
log(buf.data(), args...);
}
log(format, args..., fileName, line);
}

// Guaranteed to print somewhere, even without a log file.
// Windows: Visual Studio output window
// Linux: Terminal window
#ifndef NDEBUG
template <typename ...Args>
inline void log_g(const char* format, Args...args) {
#if defined(_WIN32)
std::vector<char> buf;

size_t bufSize = std::snprintf(nullptr, 0, format, args...);
buf.resize(++bufSize);

std::snprintf(buf.data(), bufSize, format, args...);
::OutputDebugStringA(buf.data());
#else
fprintf(stderr, format, args...);
fflush(stderr);
#endif
}
#endif
29 changes: 17 additions & 12 deletions FileXT/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@

using namespace std;

// Forward Declerations
bool checkFileName(string& fileName);
string getDllFolder();
std::filesystem::path& getLogPath();

// Globals
filext::filemgr gFileMgr;
std::unique_ptr<FILE, decltype(&std::fclose)> gpFile(std::fopen(getLogPath().u8string().c_str(), "w"), &std::fclose);

bool checkFileName(string& fileName);
string getDllFolder();

const std::string& GetAndEnsureStorageDir()
{
// keep this static path internal so it doesn't get used too early or modified externally.
Expand Down Expand Up @@ -58,7 +60,7 @@ __attribute__((constructor))
#endif
static void Entry()
{
LOG_VERBOSE("FileXT Dll entry\n");
LOG_VERBOSE("FileXT Dll entry");
std::string const& storageDirectory = GetAndEnsureStorageDir();
}

Expand Down Expand Up @@ -98,7 +100,7 @@ extern "C"

// Macro for asserting correct argument count
#define ASSERT_EXT_ARGC(argc, argcNeeded) if (argc != argcNeeded) { \
LOG("Wrong arg count, received: %i, expected: %i\n", argc, argcNeeded); \
LOG("Wrong arg count, received: %i, expected: %i", argc, argcNeeded); \
return FILEXT_ERROR_WRONG_ARG_COUNT; \
}

Expand Down Expand Up @@ -128,7 +130,7 @@ FILEXT_EXPORT int FILEXT_CALL RVExtensionArgs(char* output, int outputSize, cons
fileName = string(argv[1]);
fileName.erase(0, 1);
fileName.pop_back();
//LOG("Argc: %i\n", argc);
//LOG("Argc: %i", argc);

// Check file name
// Bail if file name is wrong
Expand All @@ -137,7 +139,7 @@ FILEXT_EXPORT int FILEXT_CALL RVExtensionArgs(char* output, int outputSize, cons
fileName = storageDirectory + fileName;
}

LOG_VERBOSE("RVExtensionArgs: function: %s, fileName: %s, outputSize: %i\n", functionName, fileName.c_str(), outputSize);
LOG_VERBOSE("RVExtensionArgs: function: %s, fileName: %s, outputSize: %i", functionName, fileName.c_str(), outputSize);

// Resolve function name

Expand Down Expand Up @@ -176,7 +178,7 @@ FILEXT_EXPORT int FILEXT_CALL RVExtensionArgs(char* output, int outputSize, cons
reset = 0;
}
int retInt = gFileMgr.get(fileName, argv[2], strOut, outputSize-4, (bool)reset); // Just to be safe, reduce size a bit
LOG(" Returning string of size: %i\n", (unsigned int)strOut.size());
LOG(" Returning string of size: %i", (unsigned int)strOut.size());
ASSERT_BUFFER_SIZE((int)outputSize -1, (int)strOut.size());
strcpy(output, strOut.c_str());
return retInt;
Expand Down Expand Up @@ -208,7 +210,7 @@ FILEXT_EXPORT int FILEXT_CALL RVExtensionArgs(char* output, int outputSize, cons
for (const auto& entry : filesystem::directory_iterator(storageDirectory)) {
string fileName = entry.path().filename().string();
vectorFileNamesSQF.push_back(sqf::value(fileName));
LOG("File: %s\n", fileName.c_str());
LOG("File: %s", fileName.c_str());
}

sqf::value fileNamesSQFArray(vectorFileNamesSQF);
Expand Down Expand Up @@ -297,8 +299,8 @@ std::string getDllFolder()
(LPCSTR)getDllFolder,
&hm))
{
LOG("error: GetModuleHandle returned %i", GetLastError());
return std::string("");
LOG_G("Fatal Error: GetModuleHandle returned %i", GetLastError());
throw new std::runtime_error("Fatal Error: GetModuleHandle failed.");
}

GetModuleFileNameA(hm, path, sizeof(path));
Expand All @@ -310,7 +312,10 @@ std::string getDllFolder()
return p;
#else
Dl_info dl_info;
dladdr((void*)getDllFolder, &dl_info);
if (dladdr((void*)getDllFolder, &dl_info) == 0) {
LOG_G("Fatal Error: dladdr returned 0");
throw new std::runtime_error("Fatal Error: dladdr returned 0");
}
std::string p = std::string(dl_info.dli_fname);

// Remove DLL name from the path
Expand Down
Loading