ilogger.cpp 1.01 KB
Newer Older
Swann Perarnau's avatar
Swann Perarnau committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include "ilogger.hpp"
#include "utils.hpp"
#include <sstream>

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

void ILogger::add_errno_in_log(const string& in_msg, int errnum, string &out_msg)
{
	std::ostringstream oss;
	oss<<in_msg<<"[errno = "<<errno<<"|error message = "<<strerror(errnum)<<"]";
	out_msg = oss.str();
}

bool ILogger::prepare_log(Log_type type, const string& msg_in, 
		string& msg_out, int errnum)
{
	if(!(_log_type & type))
		return false;
	if(errnum)
		add_errno_in_log(msg_in, errnum, msg_out);
	else
		msg_out = msg_in;
	
	std::ostringstream oss;
	oss<<"[user:"<<_ruid<<"] "<<msg_out;
	msg_out = oss.str();
	if(log_destination() != LOG_DEST_SYSLOG)
	{
		char buf[1024];
		time_t current_time;
		struct tm *tmp = NULL;

		current_time = time(NULL);
		tmp = localtime(&current_time);
		strftime(buf, sizeof(buf), "%c", tmp);

		oss.str("");
		oss<<buf<<' '<<*get_hostname()<<' '<<APP_NAME<<"[pid:"
			<<getpid()<<"]|"<<log_type_to_str(type)<<": "<<msg_out;
		msg_out = oss.str();
	}
	return true;
}