/*
 * LSC_Logger.cpp
 *
 *  Created on: 2014-4-10
 *      Author: sj
 */

#include "LSC_Logger.h"

/*
 * no use...
 * just for test ACE_Object_Manager::at_exit.
 */
void LSC_ProcessExitLog::cleanup (void *param) {
	ACE_UNUSED_ARG(param);

	LOGN("Process Exit!\n");
}

LSC_Logger::LSC_Logger(){
	curr_log_leave = LM_INFO;
	ACE_Object_Manager::instance()->at_exit(new LSC_ProcessExitLog());
}

LSC_Logger::~LSC_Logger() {
	LOGI("~LSC_Logger()\n");
}

int LSC_Logger::open(string process_name,string log_leave) {
    ACE_LOG_MSG->open(process_name.c_str(), ACE_Log_Msg::SYSLOG|ACE_Log_Msg::STDERR);
	/*
	 * log leave setting
	 */
	return set_log_leave(log_leave);
}

int LSC_Logger::set_log_leave(string log_leave) {
	if (log_leave == "D") {/*debug*/
		ACE_LOG_MSG->priority_mask(
				LM_DEBUG | LM_INFO | LM_NOTICE | LM_WARNING | LM_ERROR,
				ACE_Log_Msg::PROCESS);
		ACE_LOG_MSG->priority_mask(
				LM_DEBUG | LM_INFO | LM_NOTICE | LM_WARNING | LM_ERROR,
				ACE_Log_Msg::THREAD);
		curr_log_leave = LM_DEBUG;
	} else if (log_leave == "I") {/*info*/
		ACE_LOG_MSG->priority_mask(LM_INFO | LM_NOTICE | LM_WARNING | LM_ERROR,
				ACE_Log_Msg::PROCESS);
		ACE_LOG_MSG->priority_mask(LM_INFO | LM_NOTICE | LM_WARNING | LM_ERROR,
				ACE_Log_Msg::THREAD);
		curr_log_leave = LM_INFO;
	} else if (log_leave == "N") {/*notice*/
		ACE_LOG_MSG->priority_mask(LM_NOTICE | LM_WARNING | LM_ERROR,
				ACE_Log_Msg::PROCESS);
		ACE_LOG_MSG->priority_mask(LM_NOTICE | LM_WARNING | LM_ERROR,
				ACE_Log_Msg::THREAD);
		curr_log_leave = LM_NOTICE;
	} else if (log_leave == "W") {/*warning*/
		ACE_LOG_MSG->priority_mask(LM_WARNING | LM_ERROR, ACE_Log_Msg::PROCESS);
		ACE_LOG_MSG->priority_mask(LM_WARNING | LM_ERROR, ACE_Log_Msg::THREAD);
		curr_log_leave = LM_WARNING;
	} else if (log_leave == "E") {/*error*/
		ACE_LOG_MSG->priority_mask(LM_ERROR, ACE_Log_Msg::PROCESS);
		ACE_LOG_MSG->priority_mask(LM_ERROR, ACE_Log_Msg::THREAD);
		curr_log_leave = LM_ERROR;
	} else {
		LOGW("Unknown log leave:%s.\n", log_leave.c_str());
		return -1;
	}

//	LOGI("Now log leave:%s.\n",log_leave.c_str());
	return 0;
}

ACE_Log_Priority LSC_Logger::get_log_leave() {
	return curr_log_leave;
}

