Пример #1
0
std::string CSqlLogger::get_log_filepath()
{
    std::string log_filepath;

    MOOON_ASSERT(!_dbinfo->alias.empty());
    if (_dbinfo->alias.empty())
    {
        MYLOG_ERROR("alias empty: %s\n", _dbinfo->str().c_str());
    }
    else
    {
        const std::string& log_dirpath = get_log_dirpath(_dbinfo->alias);
        if (!sys::CDirUtils::exist(log_dirpath))
        {
            MYLOG_INFO("to create sqllog dir[%s]: %s\n", log_dirpath.c_str(), _dbinfo->str().c_str());
            sys::CDirUtils::create_directory_recursive(log_dirpath.c_str(), DIRECTORY_DEFAULT_PERM);
        }

        const time_t now = time(NULL);
        if (now == _log_file_timestamp)
        {
            ++_log_file_suffix;
        }
        else
        {
            _log_file_suffix = 0;
            _log_file_timestamp = now;
        }

        log_filepath = utils::CStringUtils::format_string("%s/sql.%013" PRId64".%06d", log_dirpath.c_str(), static_cast<int64_t>(now), _log_file_suffix);
    }

    return log_filepath;
}
Пример #2
0
void CSqlLogger::rotate_log()
{
    if (_log_fd != -1)
    {
        if (-1 == fsync(_log_fd))
        {
            const int errcode = errno;
            MYLOG_ERROR("fsync %s error: (%d)%s\n", _log_filepath.c_str(), errcode, sys::Error::to_string(errcode).c_str());
        }

        close(_log_fd);
        _log_fd = -1;
    }
    if (_log_filepath.empty())
    {
        _log_filepath = get_last_log_filepath();
        MYLOG_INFO("%s\n", _log_filepath.c_str());
    }
    else
    {
        _log_filepath = get_log_filepath();
        MYLOG_INFO("%s\n", _log_filepath.c_str());
    }

    if (_log_filepath.empty())
    {
    }
    else
    {
        _log_fd = open(_log_filepath.c_str(), O_WRONLY|O_CREAT|O_APPEND, FILE_DEFAULT_PERM); // O_EXCL
        if (-1 == _log_fd)
        {
            MYLOG_ERROR("[%s] create %s error: %s\n", _dbinfo->str().c_str(), _log_filepath.c_str(), sys::Error::to_string().c_str());
        }
        else
        {
            int log_file_size = static_cast<int>(sys::CFileUtils::get_file_size(_log_fd));
            MYLOG_INFO("[%s] create %s ok: %d\n", _dbinfo->str().c_str(), _log_filepath.c_str(), log_file_size);
        }
    }
}
Пример #3
0
std::string CSqlLogger::get_last_log_filepath()
{
    std::vector<std::string>* subdir_names = NULL;
    std::vector<std::string>* link_names = NULL;
    std::vector<std::string> file_names;
    std::string log_filename;
    const std::string& log_dirpath = get_log_dirpath(_dbinfo->alias);

    // 如果目录不存在,则自动创建
    if (!sys::CDirUtils::exist(log_dirpath))
    {
        MYLOG_INFO("to create sqllog dir[%s]: %s\n", log_dirpath.c_str(), _dbinfo->str().c_str());
        sys::CDirUtils::create_directory_recursive(log_dirpath.c_str(), DIRECTORY_DEFAULT_PERM);
    }

    sys::CDirUtils::list(log_dirpath, subdir_names, &file_names, link_names);
    if (!file_names.empty())
    {
        std::sort(file_names.begin(), file_names.end());
        for (int i=static_cast<int>(file_names.size()-1); i>=0; --i)
        {
            if (is_sql_log_filename(file_names[i]))
            {
                log_filename = file_names[i];
                break;
            }
        }
    }

    if (log_filename.empty())
    {
        MYLOG_INFO("no history log file: %s\n", _dbinfo->str().c_str());
        return get_log_filepath();
    }
    else
    {
        std::string last_log_filepath = log_dirpath + std::string("/") + log_filename;
        return last_log_filepath;
    }
}
Пример #4
0
bool CMainHelper::run()
{
    mooon::db_proxy::CConfigLoader* config_loader = mooon::db_proxy::CConfigLoader::get_singleton();
    mooon::sys::CThreadEngine monitor(mooon::sys::bind(&mooon::db_proxy::CConfigLoader::monitor, config_loader));

    try
    {
        MYLOG_INFO("thrift will listen on port[%u]\n", mooon::argument::port->value());
        _thrift_server.serve(mooon::argument::port->value());
        return true;
    }
    catch (apache::thrift::TException& tx)
    {
        fprintf(stderr, "thrift exception: %s\n", tx.what());
        return false;
    }
}
Пример #5
0
MOOON_NAMESPACE_USE

static void foo()
{
    for (int i=0; i<100000; ++i)
    {
        MYLOG_DEBUG("[%d]MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM", i);
        MYLOG_DETAIL("[%d]KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK", i+1);
        MYLOG_INFO("[%d]BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", i+2);
        MYLOG_ERROR("[%d]TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT", i+3);
        MYLOG_WARN("[%d]PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP", i+4);
        MYLOG_TRACE("[%d]AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", i+5);
        MYLOG_STATE("[%d]ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ", i+6);

        //sys::CUtils::millisleep(10);
    }
}
Пример #6
0
bool ISMGManager::UpdateISMGInfo() {
	std::list<ISMGInfo *> lst;
	if (true == m_dbService->getAllISMGInfo(lst)) {
		for (std::list<ISMGInfo *>::iterator it = lst.begin(); 
			it != lst.end(); 
			++it) {
			ISMGInfo *pInfo = *it;
			if (NULL != pInfo) {
				m_mapISMG.insert(std::make_pair(pInfo->SP_Id, pInfo));
			}
		}
		
		MYLOG_INFO("Update ISMGInfo success");
		return true;
	} else {
		MYLOG_ERROR("Update ISMGParam failed");
		return false;
	}
}
Пример #7
0
CVirtualHost* CHostManager::add_host(const char* domain_name, uint32_t domain_name_length)
{
    int times = 0;
    uint32_t factor = util::CStringUtil::hash(domain_name, domain_name_length);
	CVirtualHost* host = new CVirtualHost(domain_name);

    for (;;)
    {
        uint32_t index = factor % sizeof(_host_table)/sizeof(CVirtualHost*);
        if (NULL == _host_table[index])
        {
            _host_table[index] = host;
			MYLOG_INFO("Host: %.*s added.\n", domain_name_length, domain_name);
            return host;
        }

        ++factor;
        if (++times == sizeof(_host_table)/sizeof(CVirtualHost*)-1)
            break;
    }

	return NULL;
}
Пример #8
0
bool ISMGManager::UpdateISMGParam() {
	std::list<ISMGParam *> lst;
	if (true == m_dbService->getAllISMGParam(lst)) {
		for (std::list<ISMGParam *>::iterator it = lst.begin();
			it != lst.end();
			++it) {
			ISMGParam *pParam = *it;
			if (NULL != pParam) {
				ISMGInfo *pInfo = getISMGInfo(pParam->SP_Id);
				if (NULL != pInfo) {
					pInfo->UpdateParam(pParam);
				} else {
					delete pParam;
				}
			}
		}

		MYLOG_INFO("Update ISMGParam success");
		return true;
	} else {
		MYLOG_ERROR("Update ISMGParam failed");
		return false;
	}
}
Пример #9
0
bool CConfigLoader::load_update(const Json::Value& json, struct UpdateInfo* update_info_array[])
{
    for (int i=0; i<static_cast<int>(json.size()); ++i)
    {
        struct UpdateInfo* update_info = new struct UpdateInfo(json[i]);

        if (!update_info->check())
        {
            delete update_info;
            return false;
        }
        else
        {
            MYLOG_INFO("%s\n", update_info->str().c_str());
            if (!add_update_info(update_info, update_info_array))
            {
                delete update_info;
                return false;
            }
        }
    }

    return true;
}
Пример #10
0
bool CConfigLoader::load_query(const Json::Value& json, struct QueryInfo* query_info_array[])
{
    for (int i=0; i<static_cast<int>(json.size()); ++i)
    {
        struct QueryInfo* query_info = new struct QueryInfo(json[i]);

        if (!query_info->check())
        {
            delete query_info;
            return false;
        }
        else
        {
            MYLOG_INFO("%s\n", query_info->str().c_str());
            if (!add_query_info(query_info, query_info_array))
            {
                delete query_info;
                return false;
            }
        }
    }

    return true;
}
Пример #11
0
int main(int argc, char* argv[])
{
    try
    {
        pid_t pid;
        ::mooon::sys::g_logger = new sys::CSafeLogger(".", "test.log");
        sys::g_logger->set_single_filesize(20240000);
        sys::g_logger->set_backup_number(10);

        MYLOG_INFO("hello");
        MYLOG_ERROR("world");

        for (int i=0; i<10; ++i)
        {
            pid = fork();
            if (-1 == pid)
            {
                fprintf(stderr, "fork error: %m\n");
                exit(1);
            }
            else if (0 == pid)
            {
                // 子进程
                sys::CThreadEngine thread1(sys::bind(&foo));
                sys::CThreadEngine thread2(sys::bind(&foo));
                sys::CThreadEngine thread3(sys::bind(&foo));
                sys::CThreadEngine thread4(sys::bind(&foo));
                sys::CThreadEngine thread5(sys::bind(&foo));

                thread1.join();
                thread2.join();
                thread3.join();
                thread4.join();
                thread5.join();
                exit(0);
            }
        }

        // 等待所有子进程结束
        while (true)
        {
            int status = -1;
            pid = wait(&status);
            if (pid > 0)
            {
                printf("process[%u] exit with %d\n", pid, status);
            }
            else
            {
                if (ECHILD == errno)
                {
                    break;
                }
                else
                {
                    printf("unknown error when waiting child\n");
                    break;
                }
            }
        }
    }
    catch (sys::CSyscallException& syscall_ex)
    {
        fprintf(stderr, "%s\n", syscall_ex.str().c_str());
        exit(1);
    }

    return 0;
}
Пример #12
0
bool CConfigLoader::load(const std::string& filepath)
{
    Json::Reader reader;
    Json::Value root;
    std::ifstream fs(filepath.c_str());
    struct DbInfo* db_info_array[MAX_DB_CONNECTION] = { NULL };
    struct QueryInfo* query_info_array[MAX_SQL_TEMPLATE] = { NULL };
    struct UpdateInfo* update_info_array[MAX_SQL_TEMPLATE] = { NULL };

    if (_md5_sum.empty())
    {
        MYLOG_INFO("loading %s\n", filepath.c_str());
    }
    else
    {
        MYLOG_DETAIL("loading %s\n", filepath.c_str());
    }
    if (!fs)
    {
        MYLOG_ERROR("load %s failed: %s\n", filepath.c_str(), strerror(errno));
        return false;
    }
    if (!reader.parse(fs, root))
    {
        MYLOG_ERROR("parse %s failed: %s\n", filepath.c_str(), reader.getFormattedErrorMessages().c_str());
        return false;
    }

    // 检查文件是否有修改过
    std::string md5_sum = utils::CMd5Helper::lowercase_md5("%s", root.toStyledString().c_str());
    if (md5_sum == _md5_sum)
    {
        MYLOG_DETAIL("not changed: (%s)%s\n", md5_sum.c_str(), filepath.c_str());
        return true; // 未发生变化
    }

    init_db_info_array(db_info_array);
    init_query_info_array(query_info_array);
    init_update_info_array(update_info_array);
    if (!load_database(root["database"], db_info_array))
        return false;
    if (!load_query(root["query"], query_info_array))
        return false;
    if (!load_update(root["update"], update_info_array))
        return false;

    int i; // 加写锁
    sys::WriteLockHelper write_lock(_read_write_lock);

    release_db_info_array(_db_info_array);
    release_query_info_array(_query_info_array);
    release_update_info_array(_update_info_array);
    for (i=0; i<MAX_DB_CONNECTION; ++i)
    {
        if (db_info_array[i] != NULL)
        {
            // 启动时即连接一下,以早期发现配置等问题
            _db_info_array[i] = new struct DbInfo(*db_info_array[i]);
            sys::DBConnection* db_connection = init_db_connection(i, false);
            if (db_connection != NULL)
            {
                delete db_connection;
                db_connection = NULL;
            }
        }
    }
    for (i=0; i<MAX_SQL_TEMPLATE; ++i)
    {
        if (query_info_array[i] != NULL)
            _query_info_array[i] = new struct QueryInfo(*query_info_array[i]);
        if (update_info_array[i] != NULL)
            _update_info_array[i] = new struct UpdateInfo(*update_info_array[i]);
    }

    _md5_sum = md5_sum;
    MYLOG_INFO("loaded %s[%s] successfully\n", filepath.c_str(), _md5_sum.c_str());
    return true;
}
int tcpserver::init_server( std::string ip, int port, int max_epoll_count, notify_callback callback )
{
	m_timeout_check_interval_seconds = 10;
	m_fd_timeout_threshold = 300;
	//m_increaseid=0;
	m_clientcount=0;
	m_unify_id=0;
	MYLOG_INFO("ready to start  tcpserver");
	int result = 0;
	m_port = port;

	m_epoll_maxsize = max_epoll_count;
	if (callback == NULL)
	{
		return -1;
	}
	
	m_notify_callback = callback;

	// 监听端口
	m_listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	int reuse = 1;
	if (setsockopt(m_listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) 
	{
		MYLOG_ERROR("set socket reuse failed");
		return -1;
	}

	char ip_address[100]={0};
	sprintf(ip_address, "0.0.0.0");
	struct sockaddr_in addr;
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = inet_addr(ip_address);
	addr.sin_port = htons(port);

	if (bind(m_listen_fd, (const struct sockaddr*)&addr, (socklen_t)sizeof(addr)) == -1) 
	{
		MYLOG_ERROR("failed to bind socket fd to ip and port=%d",m_port);
		return -1;
	}

	if (listen(m_listen_fd, SOMAXCONN)== -1)
	{
		MYLOG_ERROR("failed to listen on port=%d",m_port);
		return -1;
	}

	result = pipe(m_pipes);
	if (result == -1)
	{
		MYLOG_ERROR("failed to create epoll pipes");
		return -1;
	}
	m_pipe_read = m_pipes[0];
	m_pipe_write = m_pipes[1];

	m_array_epoll_data = (STU_EPOLL_DATA **)new char[sizeof(STU_EPOLL_DATA *) * m_epoll_maxsize];
	for (int i=0; i<m_epoll_maxsize; i++)
	{
		STU_EPOLL_DATA *pstu = new STU_EPOLL_DATA;
		pstu->status = enum_socket_close;
		m_array_epoll_data[i] = pstu;
	}

	pthread_t threadid;
	result = pthread_create(&threadid, NULL, tcpserver::epoll_reactor, this);
	if (result == -1)
	{
		MYLOG_ERROR("failed to create http server epoll read send thread");
		return -1;
	}

	MYLOG_INFO("tcpserver start finished");

	return 0;



	
}