//--------------------------------------------------------------------------- // Code //--------------------------------------------------------------------------- int main(int argc, char* argv[]) { XnStatus nRetVal = XN_STATUS_OK; printf("Starting sensor server...\n"); const XnChar* strConfigDir = "."; if (argc >= 2) { strConfigDir = argv[1]; } XnChar strConfigFile[XN_FILE_MAX_PATH]; nRetVal = XnSensorServerGetGlobalConfigFile(strConfigDir, strConfigFile, XN_FILE_MAX_PATH); XN_CHECK_RC(nRetVal, "Resolving global config file"); #if (XN_PLATFORM == XN_PLATFORM_LINUX_X86 || XN_PLATFORM == XN_PLATFORM_LINUX_ARM || XN_PLATFORM == XN_PLATFORM_MACOSX) xnLogSetOutputFolder("/var/log/primesense/XnSensorServer/"); #endif nRetVal = XnFormatsInitFromINIFile(strConfigFile); XN_CHECK_RC(nRetVal, "Initializing DDK"); printf("Running...\n"); nRetVal = XnSensorServerRun(strConfigFile); XN_CHECK_RC(nRetVal, "starting sensor server"); printf("\nShutting down sensor server...\n"); XnDDKShutdown(); return 0; }
XN_C_API XnStatus xnLogCreateFile(const XnChar* csFileName, XN_FILE_HANDLE* phFile) { XnStatus nRetVal = XN_STATUS_OK; // set log directory if (g_logData.m_strLogDir[0] == '\0') { nRetVal = xnLogSetOutputFolder(XN_LOG_DIR_NAME); XN_IS_STATUS_OK(nRetVal); } if (g_logData.m_strTime[0] == '\0') { time_t currtime; time(&currtime); strftime(g_logData.m_strTime, sizeof(g_logData.m_strTime)-1, "%Y_%m_%d__%H_%M_%S", localtime(&currtime)); } XN_PROCESS_ID nProcID = 0; xnOSGetCurrentProcessID(&nProcID); // create full path file name - add process start time and process ID XnChar strFilePath[XN_FILE_MAX_PATH]; sprintf(strFilePath, "%s%s_%u.%s", g_logData.m_strLogDir, g_logData.m_strTime, nProcID, csFileName); // and open the file nRetVal = xnOSOpenFile(strFilePath, XN_OS_FILE_WRITE | XN_OS_FILE_TRUNCATE, phFile); XN_IS_STATUS_OK(nRetVal); return (XN_STATUS_OK); }
XN_C_API XnStatus xnLogInitFromINIFile(const XnChar* cpINIFileName, const XnChar* cpSectionName) { XnStatus nRetVal = XN_STATUS_OK; XnInt32 nTemp; // read filters xnLogReadMasksFromINI(cpINIFileName, cpSectionName, "LogMasks", xnLogBCSetMaskState); xnLogReadMasksFromINI(cpINIFileName, cpSectionName, "DumpMasks", xnDumpSetMaskState); //Test if log redirection is needed XnChar strLogPath[XN_FILE_MAX_PATH] = {0}; nRetVal = xnOSReadStringFromINI(cpINIFileName, cpSectionName, "LogPath", strLogPath, XN_FILE_MAX_PATH); if (nRetVal == XN_STATUS_OK) { nRetVal = xnLogSetOutputFolder(strLogPath); XN_IS_STATUS_OK(nRetVal); } nRetVal = xnOSReadIntFromINI(cpINIFileName, cpSectionName, "Verbosity", &nTemp); if (nRetVal == XN_STATUS_OK) { nRetVal = xnLogSetMaskMinSeverity(XN_LOG_MASK_ALL, (XnLogSeverity)nTemp); XN_IS_STATUS_OK(nRetVal); } // configure writers nRetVal = xnOSReadIntFromINI(cpINIFileName, cpSectionName, "LogToConsole", &nTemp); if (nRetVal == XN_STATUS_OK) { nRetVal = xnLogSetConsoleOutput(nTemp); XN_IS_STATUS_OK(nRetVal); } nRetVal = xnOSReadIntFromINI(cpINIFileName, cpSectionName, "LogToFile", &nTemp); if (nRetVal == XN_STATUS_OK) { nRetVal = xnLogSetFileOutput(nTemp); XN_IS_STATUS_OK(nRetVal); } #if XN_PLATFORM == XN_PLATFORM_ANDROID_ARM nRetVal = xnOSReadIntFromINI(cpINIFileName, cpSectionName, "LogToAndroidLog", &nTemp); if (nRetVal == XN_STATUS_OK) { nRetVal = xnLogSetAndroidOutput(nTemp); XN_IS_STATUS_OK(nRetVal); } #endif nRetVal = xnOSReadIntFromINI(cpINIFileName, cpSectionName, "LogLineInfo", &nTemp); if (nRetVal == XN_STATUS_OK) { nRetVal = xnLogSetLineInfo(nTemp); XN_IS_STATUS_OK(nRetVal); } return XN_STATUS_OK; }
XN_C_API XnStatus XN_C_DECL xnLogCreateNewFile(const XnChar* strName, XnBool bSessionBased, XnChar* csFullPath, XnUInt32 nPathBufferSize, XN_FILE_HANDLE* phFile) { XnStatus nRetVal = XN_STATUS_OK; LogData& logData = LogData::GetInstance(); // set log directory if (logData.strLogDir[0] == '\0') { nRetVal = xnLogSetOutputFolder(XN_LOG_DIR_NAME); XN_IS_STATUS_OK(nRetVal); } if (logData.strSessionTimestamp[0] == '\0') { time_t currtime; time(&currtime); strftime(logData.strSessionTimestamp, sizeof(logData.strSessionTimestamp)-1, "%Y_%m_%d__%H_%M_%S", localtime(&currtime)); } XN_PROCESS_ID nProcID = 0; xnOSGetCurrentProcessID(&nProcID); // create full path file name - add process start time and process ID XnUInt32 nPathSize = 0; XnUInt32 nCharsWritten = 0; nRetVal = xnOSStrFormat(csFullPath, nPathBufferSize - nPathSize, &nCharsWritten, "%s", logData.strLogDir); XN_IS_STATUS_OK(nRetVal); nPathSize += nCharsWritten; if (bSessionBased) { nRetVal = xnOSStrFormat(csFullPath + nPathSize, nPathBufferSize - nPathSize, &nCharsWritten, "%s_%u.", logData.strSessionTimestamp, nProcID); XN_IS_STATUS_OK(nRetVal); nPathSize += nCharsWritten; } nRetVal = xnOSStrFormat(csFullPath + nPathSize, nPathBufferSize - nPathSize, &nCharsWritten, "%s", strName); XN_IS_STATUS_OK(nRetVal); nPathSize += nCharsWritten; // and open the file nRetVal = xnOSOpenFile(csFullPath, XN_OS_FILE_WRITE | XN_OS_FILE_TRUNCATE, phFile); XN_IS_STATUS_OK(nRetVal); return (XN_STATUS_OK); }
OniStatus Context::initialize() { XnBool repositoryOverridden = FALSE; XnChar repositoryFromINI[XN_FILE_MAX_PATH] = {0}; m_initializationCounter++; if (m_initializationCounter > 1) { xnLogVerbose(XN_MASK_ONI_CONTEXT, "Initialize: Already initialized"); return ONI_STATUS_OK; } XnStatus rc; XnChar strModulePath[XN_FILE_MAX_PATH]; rc = xnOSGetModulePathForProcAddress(reinterpret_cast<void*>(&dummyFunctionToTakeAddress), strModulePath); if (rc != XN_STATUS_OK) { m_errorLogger.Append("Couldn't get the OpenNI shared library module's path: %s", xnGetStatusString(rc)); return OniStatusFromXnStatus(rc); } XnChar strBaseDir[XN_FILE_MAX_PATH]; rc = xnOSGetDirName(strModulePath, strBaseDir, XN_FILE_MAX_PATH); if (rc != XN_STATUS_OK) { // Very unlikely to happen, but just in case. m_errorLogger.Append("Couldn't get the OpenNI shared library module's directory: %s", xnGetStatusString(rc)); return OniStatusFromXnStatus(rc); } s_valid = TRUE; // Read configuration file XnChar strOniConfigurationFile[XN_FILE_MAX_PATH]; XnBool configurationFileExists = FALSE; // Search the module directory for OpenNI.ini. xnOSStrCopy(strOniConfigurationFile, strBaseDir, XN_FILE_MAX_PATH); rc = xnOSAppendFilePath(strOniConfigurationFile, ONI_CONFIGURATION_FILE, XN_FILE_MAX_PATH); if (rc == XN_STATUS_OK) { xnOSDoesFileExist(strOniConfigurationFile, &configurationFileExists); } #ifdef ONI_PLATFORM_ANDROID_OS xnLogSetMaskMinSeverity(XN_LOG_MASK_ALL, (XnLogSeverity)0); xnLogSetAndroidOutput(TRUE); #endif if (configurationFileExists) { // First, we should process the log related configuration as early as possible. XnInt32 nValue; XnChar strLogPath[XN_FILE_MAX_PATH] = {0}; //Test if log redirection is needed rc = xnOSReadStringFromINI(strOniConfigurationFile, "Log", "LogPath", strLogPath, XN_FILE_MAX_PATH); if (rc == XN_STATUS_OK) { rc = xnLogSetOutputFolder(strLogPath); if (rc != XN_STATUS_OK) { xnLogWarning(XN_MASK_ONI_CONTEXT, "Failed to set log output folder: %s", xnGetStatusString(rc)); } else { xnLogVerbose(XN_MASK_ONI_CONTEXT, "Log directory redirected to: %s", strLogPath); } } rc = xnOSReadIntFromINI(strOniConfigurationFile, "Log", "Verbosity", &nValue); if (rc == XN_STATUS_OK) { xnLogSetMaskMinSeverity(XN_LOG_MASK_ALL, (XnLogSeverity)nValue); } rc = xnOSReadIntFromINI(strOniConfigurationFile, "Log", "LogToConsole", &nValue); if (rc == XN_STATUS_OK) { xnLogSetConsoleOutput(nValue == 1); } rc = xnOSReadIntFromINI(strOniConfigurationFile, "Log", "LogToFile", &nValue); if (rc == XN_STATUS_OK) { xnLogSetFileOutput(nValue == 1); } // Then, process the other device configurations. rc = xnOSReadStringFromINI(strOniConfigurationFile, "Device", "Override", m_overrideDevice, XN_FILE_MAX_PATH); if (rc != XN_STATUS_OK) { xnLogVerbose(XN_MASK_ONI_CONTEXT, "No override device in configuration file"); } rc = xnOSReadStringFromINI(strOniConfigurationFile, "Drivers", "Repository", repositoryFromINI, XN_FILE_MAX_PATH); if (rc == XN_STATUS_OK) { repositoryOverridden = TRUE; } xnLogVerbose(XN_MASK_ONI_CONTEXT, "Configuration has been read from '%s'", strOniConfigurationFile); } else { xnLogVerbose(XN_MASK_ONI_CONTEXT, "Couldn't find configuration file '%s'", strOniConfigurationFile); } xnLogVerbose(XN_MASK_ONI_CONTEXT, "OpenNI %s", ONI_VERSION_STRING); // Resolve the drive path based on the module's directory. XnChar strDriverPath[XN_FILE_MAX_PATH]; xnOSStrCopy(strDriverPath, strBaseDir, XN_FILE_MAX_PATH); if (repositoryOverridden) { xnLogVerbose(XN_MASK_ONI_CONTEXT, "Extending the driver path by '%s', as configured in file '%s'", repositoryFromINI, strOniConfigurationFile); rc = xnOSAppendFilePath(strDriverPath, repositoryFromINI, XN_FILE_MAX_PATH); } else { rc = xnOSAppendFilePath(strDriverPath, ONI_DEFAULT_DRIVERS_REPOSITORY, XN_FILE_MAX_PATH); } if (rc != XN_STATUS_OK) { m_errorLogger.Append("The driver path gets too long"); return OniStatusFromXnStatus(rc); } xnLogVerbose(XN_MASK_ONI_CONTEXT, "Using '%s' as driver path", strDriverPath); rc = loadLibraries(strDriverPath); if (rc == XN_STATUS_OK) { m_errorLogger.Clear(); } return OniStatusFromXnStatus(rc); }