-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGSPy.cpp
More file actions
77 lines (68 loc) · 2.57 KB
/
Copy pathGSPy.cpp
File metadata and controls
77 lines (68 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "GSPy.h"
#include "GSPy_Error.h"
#include "PythonManager.h"
#include <string>
#include "Logger.h"
#include "ConfigManager.h"
extern "C" void GSPy(int methodID, int* status, double* inargs, double* outargs)
{
try {
// Initialize the logger once using the new ConfigManager
static bool logger_initialized = false;
if (!logger_initialized) {
std::string log_filename = GetLogFilename();
int log_level = GetLogLevel(); // Get from config file
InitLogger(log_filename, static_cast<LogLevel>(log_level));
SetLogLevelFromInt(log_level); // Apply log level atomically
logger_initialized = true;
}
LogDebug("GSPy called with MethodID: " + std::to_string(methodID));
*status = 0;
std::string errorMessage;
switch (methodID)
{
case 0: // Initialize
if (!InitializePython(errorMessage)) {
SendErrorToGoldSim(errorMessage, status, outargs);
}
break;
case 1: // Calculate
ExecuteCalculation(inargs, outargs, errorMessage);
if (!errorMessage.empty()) {
LogDebug("Sending error to GoldSim: " + errorMessage);
SendErrorToGoldSim(errorMessage, status, outargs);
LogDebug("Error sent to GoldSim successfully");
}
break;
case 2: // Report Version
LogInfo("Reporting version to GoldSim: " + std::string(GSPY_VERSION));
outargs[0] = GSPY_VERSION_DOUBLE;
break;
case 3: // Report Arguments
// We need to initialize to read the config to get argument counts
if (!InitializePython(errorMessage)) {
SendErrorToGoldSim(errorMessage, status, outargs);
return;
}
outargs[0] = static_cast<double>(GetNumberOfInputs());
outargs[1] = static_cast<double>(GetNumberOfOutputs());
break;
case 99: // Cleanup
FinalizePython();
break;
default:
*status = 1;
break;
}
}
catch (const std::exception& e) {
LogError(std::string("C++ exception caught in GSPy: ") + e.what());
std::string errorMsg = std::string("C++ exception: ") + e.what();
SendErrorToGoldSim(errorMsg, status, outargs);
}
catch (...) {
LogError("Unknown C++ exception caught in GSPy");
std::string errorMsg = "Unknown C++ exception in GSPy";
SendErrorToGoldSim(errorMsg, status, outargs);
}
}