00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <windows.h>
00011 #include <cstdlib>
00012
00013 #include <bogotel/Portability.h>
00014 #include <bogotel/Util.h>
00015 #include <bogotel/BgtErrors.h>
00016 #include <bogotel/MsgTransport.h>
00017 #include <bogotel/VoiceDev.h>
00018
00019 #include <log4cpp/Portability.hh>
00020 #include <log4cpp/Category.hh>
00021 #include <log4cpp/PropertyConfigurator.hh>
00022
00023 namespace bogotel {
00024
00025 const std::string CUtil::s_arrstrIniLocation[2] = {
00026 ".\\",
00027 "%BOGOTEL_HOME%\\"
00028 };
00029
00030 CUtil *g_util = new CUtil();
00031
00032 CUtil::CUtil() :
00033 m_iInstanceNum(INVALID_INST_NUM),
00034 m_pLogCat(&log4cpp::Category::getInstance(LOG_CAT_NAME))
00035 {
00036
00037 }
00038
00039 CUtil::~CUtil()
00040 {
00041
00042 }
00043
00044 int CUtil::init()
00045 {
00046 int rc;
00047 int iTemp;
00048
00049 if ((rc = setIniFilename()) != resultSUCCESS) {
00050 return resultERROR;
00051 }
00052
00053 try {
00054 m_propIni.load(m_strIniFilename);
00055 } catch (CPropertiesFailure& f) {
00056 log(3, -1, "Failed to load ini file. %s", f.what());
00057 return resultERROR;
00058 }
00059
00060 try {
00061 std::string strLogCfg = m_propIni.getProperty("Log4cppFilename", "");
00062 if (strLogCfg == "") {
00063 m_pLogCat->getRoot().removeAllAppenders();
00064 } else {
00065 log4cpp::PropertyConfigurator::configure(strLogCfg);
00066 }
00067 } catch(log4cpp::ConfigureFailure& f) {
00068 std::cout << "Configure Problem " << f.what() << std::endl;
00069 std::cout << "Not configuring logging." << std::endl;
00070 }
00071
00072 iTemp = m_propIni.getProperty("PortUsedForInstanceChecking", 49999);
00073 if ((rc = setInstanceNum(iTemp)) != resultSUCCESS) {
00074 return resultERROR;
00075 }
00076
00077 CVoiceDev::s_strWavFilenameBase = m_propIni.getProperty("WavFilenameBase", "");
00078
00079 try {
00080 CVoiceDev::s_propWav2Dtmfs.load( m_strIniFilePath +
00081 m_propIni.getProperty("Wav2DtmfsPropertyFilename", "wav2dtmfs.properties"));
00082 } catch (CPropertiesFailure& f) {
00083
00084 log(9, -1, "Failed to load wav2dtmf file. %s", f.what());
00085 }
00086
00087 return resultSUCCESS;
00088 }
00089
00090 std::string CUtil::getInstanceNum()
00091 {
00092 char szInstanceNum[100];
00093 _itoa(m_iInstanceNum, szInstanceNum, 10);
00094 return std::string(szInstanceNum);
00095 }
00096
00097
00098
00099 int CUtil::setIniFilename()
00100 {
00101 FILE *f;
00102 char szUnexpanded[_MAX_PATH];
00103 char szExpanded[_MAX_PATH];
00104
00105 for (int i = 0; i < sizeof(s_arrstrIniLocation) / sizeof(std::string); i++) {
00106 sprintf(szUnexpanded, "%s", s_arrstrIniLocation[i]);
00107 ::ExpandEnvironmentStrings(szUnexpanded, szExpanded, _MAX_PATH);
00108 m_strIniFilePath = szExpanded;
00109 sprintf(szExpanded, "%s%s", m_strIniFilePath.c_str(), INI_FILENAME);
00110 m_strIniFilename = szExpanded;
00111 if ((f = fopen(m_strIniFilename.c_str(), "rt")) != NULL) {
00112
00113 fclose(f);
00114 return resultSUCCESS;
00115 }
00116 }
00117 return resultERROR;
00118 }
00119
00120
00121 int CUtil::setInstanceNum(u_short usPort)
00122 {
00123 struct sockaddr_in addr;
00124 int fd;
00125 int rc;
00126 WSADATA WSAData;
00127
00128 if ((rc = WSAStartup(MAKEWORD(1,1), &WSAData)) != 0) {
00129 return rc;
00130 }
00131
00132 if ((rc = CMsgTransport::openListenerSocket(&addr, &fd, usPort)) != resultSUCCESS) {
00133 if (rc == resultSOCKET_IN_USE) {
00134
00135 m_iInstanceNum = 2;
00136 return resultSUCCESS;
00137 }
00138 log(3, -1, "CUtil::setInstanceNum(): CMsgTransport::openListenerSocket() failed. rc is %d", rc);
00139 return rc;
00140 }
00141
00142 m_iInstanceNum = 1;
00143
00144 return resultSUCCESS;
00145 }
00146
00147 log4cpp::Priority::PriorityLevel CUtil::logLevel2Priority(int iLevel)
00148 {
00149 switch (iLevel) {
00150 case 0:
00151 case 1:
00152 case 2: return log4cpp::Priority::CRIT;
00153 case 3: return log4cpp::Priority::ERROR;
00154 case 4: return log4cpp::Priority::WARN;
00155 case 5: return log4cpp::Priority::NOTICE;
00156 case 6:
00157 case 7: return log4cpp::Priority::INFO;
00158 case 8:
00159 case 9: return log4cpp::Priority::DEBUG;
00160 default: return log4cpp::Priority::NOTSET;
00161 }
00162 }
00163
00164 void CUtil::log(int iLevel, int iChan, char *szFmt, ...)
00165 {
00166 va_list vl;
00167
00168 char szTemp[2000];
00169
00170 va_start(vl, szFmt);
00171 _vsnprintf(szTemp, sizeof(szTemp), szFmt, vl);
00172 va_end(vl);
00173
00174 (*m_pLogCat) << logLevel2Priority(iLevel) << "CHN=" << iChan << ' ' << szTemp;
00175 }
00176 }
00177