2016-12-14 15:34:44 +00:00
|
|
|
#include "ts_env.h"
|
|
|
|
|
|
|
|
TsEnv g_env;
|
|
|
|
|
|
|
|
TsEnv::TsEnv()
|
|
|
|
{}
|
|
|
|
|
|
|
|
TsEnv::~TsEnv()
|
|
|
|
{}
|
|
|
|
|
2017-03-27 17:10:26 +00:00
|
|
|
bool TsEnv::init(bool load_config)
|
2016-12-14 15:34:44 +00:00
|
|
|
{
|
|
|
|
EXLOG_LEVEL(EX_LOG_LEVEL_INFO);
|
|
|
|
|
|
|
|
ex_exec_file(m_exec_file);
|
|
|
|
|
|
|
|
m_exec_path = m_exec_file;
|
|
|
|
ex_dirname(m_exec_path);
|
|
|
|
|
2017-03-27 17:10:26 +00:00
|
|
|
if (!load_config)
|
2017-03-12 05:02:38 +00:00
|
|
|
return true;
|
|
|
|
|
2017-03-26 19:12:15 +00:00
|
|
|
// check development flag file, if exists, run in development mode for trace and debug.
|
|
|
|
ex_wstr dev_flag_file = m_exec_path;
|
|
|
|
ex_path_join(dev_flag_file, false, L"dev_mode", NULL);
|
2016-12-14 15:34:44 +00:00
|
|
|
|
2017-03-26 19:12:15 +00:00
|
|
|
ex_wstr base_path = m_exec_path;
|
|
|
|
ex_wstr log_path;
|
|
|
|
ex_wstr conf_file;
|
2016-12-14 15:34:44 +00:00
|
|
|
|
2017-03-26 19:12:15 +00:00
|
|
|
if (ex_is_file_exists(dev_flag_file.c_str()))
|
2016-12-14 15:34:44 +00:00
|
|
|
{
|
2017-03-27 17:10:26 +00:00
|
|
|
EXLOGW("===== DEVELOPMENT MODE =====\n");
|
2017-03-26 19:12:15 +00:00
|
|
|
|
|
|
|
ex_path_join(base_path, true, L"..", L"..", L"..", L"..", L"server", NULL);
|
|
|
|
conf_file = base_path;
|
2017-03-27 17:10:26 +00:00
|
|
|
ex_path_join(conf_file, false, L"share", L"etc", L"web.ini", NULL);
|
2017-03-26 19:12:15 +00:00
|
|
|
|
|
|
|
log_path = base_path;
|
|
|
|
ex_path_join(log_path, false, L"share", L"log", NULL);
|
2016-12-14 15:34:44 +00:00
|
|
|
}
|
2017-03-26 19:12:15 +00:00
|
|
|
else // not in development mode
|
2016-12-14 15:34:44 +00:00
|
|
|
{
|
|
|
|
base_path = m_exec_path;
|
2017-03-26 19:12:15 +00:00
|
|
|
ex_path_join(base_path, true, L"..", NULL);
|
2016-12-14 15:34:44 +00:00
|
|
|
|
2017-05-24 09:28:28 +00:00
|
|
|
// #ifdef EX_OS_WIN32
|
2016-12-14 15:34:44 +00:00
|
|
|
conf_file = base_path;
|
2017-05-24 09:28:28 +00:00
|
|
|
ex_path_join(conf_file, false, L"data", L"etc", L"web.ini", NULL);
|
2017-03-26 19:12:15 +00:00
|
|
|
log_path = base_path;
|
2017-05-24 09:28:28 +00:00
|
|
|
ex_path_join(log_path, false, L"data", L"log", NULL);
|
|
|
|
// #else
|
|
|
|
// conf_file = L"/etc/teleport/web.ini";
|
|
|
|
// log_path = L"/var/log/teleport";
|
|
|
|
//
|
|
|
|
// #endif
|
2016-12-14 15:34:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 19:12:15 +00:00
|
|
|
m_www_path = base_path;
|
|
|
|
ex_path_join(m_www_path, false, L"www", NULL);
|
|
|
|
|
2016-12-14 15:34:44 +00:00
|
|
|
if (!ex_is_file_exists(conf_file.c_str()))
|
|
|
|
{
|
2017-03-26 21:11:47 +00:00
|
|
|
EXLOGE(L"[tpweb] `%s` not found.\n", conf_file.c_str());
|
2016-12-14 15:34:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExIniFile cfg;
|
|
|
|
if (!cfg.LoadFromFile(conf_file))
|
|
|
|
{
|
2017-01-16 13:28:13 +00:00
|
|
|
EXLOGE("[tpweb] can not load web.ini.\n");
|
2016-12-14 15:34:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ex_wstr log_file;
|
|
|
|
ExIniSection* ps = cfg.GetDumySection();
|
2017-05-24 09:28:28 +00:00
|
|
|
if (!ps->GetStr(L"log-file", log_file))
|
2016-12-14 15:34:44 +00:00
|
|
|
{
|
|
|
|
EXLOG_FILE(L"tpweb.log", log_path.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ex_remove_white_space(log_file);
|
|
|
|
if (log_file[0] == L'"' || log_file[0] == L'\'')
|
|
|
|
log_file.erase(0, 1);
|
|
|
|
if (log_file[ log_file.length() - 1 ] == L'"' || log_file[log_file.length() - 1] == L'\'')
|
|
|
|
log_file.erase(log_file.length() - 1, 1);
|
|
|
|
|
2017-03-26 19:12:15 +00:00
|
|
|
log_path = log_file;
|
2016-12-14 15:34:44 +00:00
|
|
|
ex_dirname(log_path);
|
|
|
|
ex_wstr file_name;
|
|
|
|
file_name.assign(log_file, log_path.length() + 1, log_file.length());
|
|
|
|
|
|
|
|
EXLOG_FILE(file_name.c_str(), log_path.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
int log_level = EX_LOG_LEVEL_INFO;
|
2017-05-24 09:28:28 +00:00
|
|
|
if (ps->GetInt(L"log-level", log_level))
|
2016-12-14 15:34:44 +00:00
|
|
|
{
|
|
|
|
EXLOGV("[tpweb] log-level: %d\n", log_level);
|
|
|
|
EXLOG_LEVEL(log_level);
|
|
|
|
}
|
|
|
|
|
2017-05-24 09:28:28 +00:00
|
|
|
int debug_mode = 0;
|
|
|
|
if (ps->GetInt(L"debug-mode", debug_mode))
|
|
|
|
{
|
|
|
|
EXLOGV("[tpweb] debug-mode: %d\n", debug_mode);
|
|
|
|
// EXLOG_LEVEL(log_level);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (1 == debug_mode) {
|
|
|
|
EXLOG_LEVEL(EX_LOG_LEVEL_DEBUG);
|
|
|
|
}
|
|
|
|
|
2016-12-14 15:34:44 +00:00
|
|
|
return true;
|
|
|
|
}
|