Skip to content

Commit

Permalink
fix some log issue
Browse files Browse the repository at this point in the history
  • Loading branch information
PearCoding committed Jun 19, 2024
1 parent 36c58ff commit 445f584
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 35 deletions.
36 changes: 10 additions & 26 deletions src/runtime/ExternalProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <sys/wait.h>
#include <unistd.h>

#include <fstream>

#elif defined(IG_OS_WINDOWS)
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
Expand Down Expand Up @@ -38,8 +40,6 @@ class ExternalProcessInternal {
mutable int exit_code;

int stdIn[2];
// int stdOut[2];
int tmpOut;

inline ExternalProcessInternal(const std::string& name, const Path& exe, const std::vector<std::string>& parameters, const Path& logFile)
: exePath(exe)
Expand All @@ -48,7 +48,6 @@ class ExternalProcessInternal {
, pid(-1)
, exit_code(-1)
, stdIn{ InvalidPipe, InvalidPipe }
, tmpOut{ InvalidPipe }
{
IG_UNUSED(name);
}
Expand All @@ -58,9 +57,6 @@ class ExternalProcessInternal {
if (stdIn[PipeWrite] != InvalidPipe)
close(stdIn[PipeWrite]);

if (tmpOut != InvalidPipe)
close(tmpOut);

// Remove empty logs
if (std::filesystem::file_size(logFile) == 0)
std::filesystem::remove(logFile);
Expand All @@ -74,7 +70,7 @@ class ExternalProcessInternal {
return false;
}

tmpOut = open(logFile.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
int tmpOut = open(logFile.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (tmpOut < 0) {
IG_LOG(L_ERROR) << "Getting file handle for temporary file for process " << exePath << " failed: " << std::strerror(errno) << std::endl;
return false;
Expand Down Expand Up @@ -129,6 +125,7 @@ class ExternalProcessInternal {
// Close unnecessary handles
close(stdIn[PipeRead]);
stdIn[PipeRead] = InvalidPipe;
close(tmpOut);

// Check for error
if (pid == -1) {
Expand Down Expand Up @@ -216,26 +213,13 @@ class ExternalProcessInternal {
if (pid == -1)
return {};

std::string output;
while (true) {
char c;
int result = read(tmpOut, &c, 1);
if (result < 0) {
IG_LOG(L_ERROR) << "read for " << exePath << " (" << pid << " | " << logFile << ") failed: " << std::strerror(errno) << std::endl;
break;
} else if (result == 0) {
// EOF
break;
}

output += c;
try {
std::stringstream stream;
stream << std::ifstream(logFile).rdbuf();
return stream.str();
} catch (...) {
return {};
}

// close(stdOut[PipeRead]);
// stdOut[PipeRead] = InvalidPipe;
close(tmpOut);
tmpOut = InvalidPipe;
return output;
}
};

Expand Down
21 changes: 12 additions & 9 deletions src/runtime/shader/ShaderTaskManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ class ShaderTaskManagerInternal {
return;

proc.Proc->waitForFinish();
int exitCode = proc.Proc->exitCode();
const std::string log = proc.Proc->receiveOnce();
const int exitCode = proc.Proc->exitCode();

const auto dur = std::chrono::steady_clock::now() - proc.Start;

Expand All @@ -186,24 +187,26 @@ class ShaderTaskManagerInternal {

// All good -> recompile to get data from cache!
ptr = mInternalCompiler->compile(proc.Work.Script, proc.Work.Function);
} else {
// Dump shader into tmp folder
const Path tmpFile = std::filesystem::temp_directory_path() / "Ignis" / (whitespace_escaped(proc.Work.reasonableID()) + ".art");
dumpShader(tmpFile, proc.Work.Script);

IG_LOG(L_ERROR) << "Finished compilation of '" << proc.Work.Name << "' for group '" << proc.Work.ID << "' with exit code " << exitCode << " (" << dur << ")." << std::endl
<< "Dump of shader is available at " << tmpFile << std::endl;
}

mWorkMutex.lock();
mResultMap[proc.Work.ID] = Result{
.Log = proc.Proc->receiveOnce(),
.Log = log,
.Ptr = ptr
};
mWorkMutex.unlock();

delete proc.Proc;
proc.Proc = nullptr;

if (exitCode != EXIT_SUCCESS) {
// Dump shader into tmp folder
const Path tmpFile = std::filesystem::temp_directory_path() / "Ignis" / (whitespace_escaped(proc.Work.reasonableID()) + ".art");
dumpShader(tmpFile, proc.Work.Script);

IG_LOG(L_ERROR) << "Finished compilation of '" << proc.Work.Name << "' for group '" << proc.Work.ID << "' with exit code " << exitCode << " (" << dur << ")." << std::endl
<< "Dump of shader is available at " << tmpFile << std::endl;
}
}
};

Expand Down

0 comments on commit 445f584

Please sign in to comment.