Added null checks and some more startup logs for runtime

pull/14/head
Akshay Joshi 2018-09-17 15:16:48 +05:30
parent a2258c83e9
commit 003889f15e
2 changed files with 40 additions and 17 deletions

View File

@ -201,7 +201,8 @@ Server::Server(quint16 port, QString key, QString logFileName)
// Get the current path // Get the current path
PyObject* sysPath = PySys_GetObject((char*)"path"); PyObject* sysPath = PySys_GetObject((char*)"path");
if (sysPath != NULL)
{
// Add new additional path elements // Add new additional path elements
Logger::GetLogger()->Log("Adding new additional path elements"); Logger::GetLogger()->Log("Adding new additional path elements");
for (i = path_list.size() - 1; i >= 0 ; --i) for (i = path_list.size() - 1; i >= 0 ; --i)
@ -216,19 +217,39 @@ Server::Server(quint16 port, QString key, QString logFileName)
#endif #endif
#endif #endif
} }
}
else
Logger::GetLogger()->Log("Unable to get the current path.");
// Redirect stderr // Redirect stderr
Logger::GetLogger()->Log("Redirecting stderr..."); Logger::GetLogger()->Log("Redirecting stderr...");
PyObject *sys = PyImport_ImportModule("sys"); PyObject *sys = PyImport_ImportModule("sys");
if (sys != NULL)
{
PyObject *err = NULL;
#ifdef PYTHON2 #ifdef PYTHON2
PyObject *err = PyFile_FromString(m_logFileName.toUtf8().data(), (char *)"w"); err = PyFile_FromString(m_logFileName.toUtf8().data(), (char *)"w");
#else #else
FILE *log = fopen(m_logFileName.toUtf8().data(), (char *)"w"); FILE *log = fopen(m_logFileName.toUtf8().data(), (char *)"w");
if (log != NULL)
{
int fd = fileno(log); int fd = fileno(log);
PyObject *err = PyFile_FromFd(fd, NULL, (char *)"w", -1, NULL, NULL, NULL, 0); err = PyFile_FromFd(fd, NULL, (char *)"w", -1, NULL, NULL, NULL, 0);
}
else
Logger::GetLogger()->Log(QString("Failed to open log file: %1").arg(m_logFileName));
#endif #endif
QFile(m_logFileName).setPermissions(QFile::ReadOwner|QFile::WriteOwner); QFile(m_logFileName).setPermissions(QFile::ReadOwner|QFile::WriteOwner);
if (err != NULL)
{
PyObject_SetAttrString(sys, "stderr", err); PyObject_SetAttrString(sys, "stderr", err);
Logger::GetLogger()->Log("stderr redirected successfully.");
}
else
Logger::GetLogger()->Log(QString("Failed to get the file pointer of: %1 ").arg(m_logFileName));
}
else
Logger::GetLogger()->Log("Failed to import 'sys' module.");
} }
Server::~Server() Server::~Server()
@ -300,7 +321,7 @@ void Server::run()
} }
// Set the port number and key, and force SERVER_MODE off. // Set the port number and key, and force SERVER_MODE off.
Logger::GetLogger()->Log("Set the port number and key, and force SERVER_MODE off"); Logger::GetLogger()->Log("Set the port number, key and force SERVER_MODE off");
PyRun_SimpleString(QString("PGADMIN_PORT = %1").arg(m_port).toLatin1()); PyRun_SimpleString(QString("PGADMIN_PORT = %1").arg(m_port).toLatin1());
PyRun_SimpleString(QString("PGADMIN_KEY = '%1'").arg(m_key).toLatin1()); PyRun_SimpleString(QString("PGADMIN_KEY = '%1'").arg(m_key).toLatin1());
PyRun_SimpleString(QString("SERVER_MODE = False").toLatin1()); PyRun_SimpleString(QString("SERVER_MODE = False").toLatin1());
@ -317,7 +338,7 @@ void Server::run()
char* n_argv[] = { m_appfile_utf8.data() }; char* n_argv[] = { m_appfile_utf8.data() };
PySys_SetArgv(1, n_argv); PySys_SetArgv(1, n_argv);
Logger::GetLogger()->Log("Server::run: PyRun_SimpleFile launching application server..."); Logger::GetLogger()->Log("PyRun_SimpleFile launching application server...");
PyObject* PyFileObject = PyFile_FromString(m_appfile_utf8.data(), (char *)"r"); PyObject* PyFileObject = PyFile_FromString(m_appfile_utf8.data(), (char *)"r");
int ret = PyRun_SimpleFile(PyFile_AsFile(PyFileObject), m_appfile_utf8.data()); int ret = PyRun_SimpleFile(PyFile_AsFile(PyFileObject), m_appfile_utf8.data());
if (ret != 0) if (ret != 0)
@ -339,7 +360,7 @@ void Server::run()
wchar_t* n_argv[] = { wcAppName }; wchar_t* n_argv[] = { wcAppName };
PySys_SetArgv(1, n_argv); PySys_SetArgv(1, n_argv);
Logger::GetLogger()->Log("Server::run: PyRun_SimpleFile launching application server..."); Logger::GetLogger()->Log("PyRun_SimpleFile launching application server...");
if (PyRun_SimpleFile(cp, m_appfile_utf8.data()) != 0) if (PyRun_SimpleFile(cp, m_appfile_utf8.data()) != 0)
{ {
Logger::GetLogger()->Log("Failed to launch the application server, server thread exiting."); Logger::GetLogger()->Log("Failed to launch the application server, server thread exiting.");

View File

@ -263,10 +263,11 @@ int main(int argc, char * argv[])
Logger::GetLogger()->Log("Starting pgAdmin4 server..."); Logger::GetLogger()->Log("Starting pgAdmin4 server...");
while (done != true) while (done != true)
{ {
QString msg = QString(QWidget::tr("Initializing server object, port:%1, key:%2, logfile:%3")).arg(port).arg(key).arg(logFileName); QString msg = QString(QWidget::tr("Creating server object, port:%1, key:%2, logfile:%3")).arg(port).arg(key).arg(logFileName);
Logger::GetLogger()->Log(msg); Logger::GetLogger()->Log(msg);
server = new Server(port, key, logFileName); server = new Server(port, key, logFileName);
Logger::GetLogger()->Log("Initializing server...");
if (!server->Init()) if (!server->Init())
{ {
splash->finish(NULL); splash->finish(NULL);
@ -282,6 +283,7 @@ int main(int argc, char * argv[])
exit(1); exit(1);
} }
Logger::GetLogger()->Log("Server initialized.");
Logger::GetLogger()->Log("Starting Server Thread..."); Logger::GetLogger()->Log("Starting Server Thread...");
server->start(); server->start();