log_option_parser.cpp 4.33 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
#include "log_option_parser.hpp"
#include "utils.hpp"

#include <algorithm>


Log_option_parser::Log_option_parser(const string& option,
		const string& delim0, const string& delim1)
{
	reset(option, delim0, delim1);
	parse_all_option_groups();
}


Log_option_parser::~Log_option_parser()
{
}


void Log_option_parser::reset(const string& option,
		const string& delim0, const string& delim1)
{
	//std::transform(option.begin(), option.end(), _option.begin(), ::tolower);
	_option = option;
	_delim0 = delim0;
	_delim1 = delim1;
	_error_message = ""; 
	_has_error = false;
	_log_option_group_index = -1;
	_log_option_groups.clear();
}

Log_type char_code_to_log_type(char code)
{
	switch(code)
	{
		case 'I':
		case 'i':
			return LOG_TYPE_INFO;
		case 'W':
		case 'w':
			return LOG_TYPE_WARNING;
		case 'E':
		case 'e':
			return LOG_TYPE_ERROR;
		case 'D':
		case 'd':
			return LOG_TYPE_DEBUG;
		default:
			return LOG_TYPE_INVALID; 
	}
	return LOG_TYPE_INVALID; 
}

bool Log_option_parser::parse_single_option_group(string& option_chunk)
{
	string dest;
	if(!str_chop_before(option_chunk, _delim1, dest))
	{
		_has_error = true;
		_error_message = "Ill-formed log option";
		return false;
	}

	Log_option_group log;
	log.dest = LOG_DEST_DEV_NULL;
	log.nb_activation_levels = 0;
	log.current_activation_level = 0;
	log.filename = "";
	if(dest == "syslog")
		log.dest = LOG_DEST_SYSLOG;
	else if(dest == "stderr")
		log.dest = LOG_DEST_STDERR;
	else if(dest == "file")
		log.dest = LOG_DEST_FILE;
	else
	{
		_has_error = true;
		_error_message = "Unsupported log destination found in log option";
		return false;
	}


	//parse the activation levels; ... and filename if applicable
	char sign;
	for(int i=0; i<(int)option_chunk.size(); i++)
	{
		switch(option_chunk[i])
		{
			case '+':
			case '-':
				sign = option_chunk[i];
				if(i+1 == (int)option_chunk.size())
				{
					_has_error = true;
					_error_message = "Il-formed log option. An option level ";
					_error_message += "is expected after \""+sign+'"';
					return false;
				}
				log.activation_levels[log.nb_activation_levels].second = 
					sign == '+' ? ACTIVATION_ENABLED : ACTIVATION_DISABLED;
				log.activation_levels[log.nb_activation_levels++].first = 
					char_code_to_log_type(option_chunk[++i]);
				break;
			case 'I':
			case 'i':
			case 'W':
			case 'w':
			case 'E':
			case 'e':
			case 'D':
			case 'd':
				log.activation_levels[log.nb_activation_levels].second = 
					ACTIVATION_ENABLED;
				log.activation_levels[log.nb_activation_levels++].first = 
					char_code_to_log_type(option_chunk[i]);
				break;
			case ':'://filename coming ....
				log.filename = option_chunk.substr(i+1);
				if(log.dest != LOG_DEST_FILE)
				{
					_has_error = true;
					_error_message = "Il-formed log option. Only file channels have filenames";
					return false;
				}
				goto __LABEL_SINGLE_OPTION_GROUP_DONE;
			default:
				_has_error = true;
				_error_message = "Il-formed log option. Unsupported log level";
				return false;
		}
	}

__LABEL_SINGLE_OPTION_GROUP_DONE:
	if(log.dest == LOG_DEST_FILE && log.filename == "")
	{
		_has_error = true;
		_error_message = "Il-formed log option. File channels require a filename";
		return false;
	}
	_log_option_groups.push_back(log);
	return true;
}

void Log_option_parser::parse_all_option_groups()
{
	//"syslog:+I+W+E-D,stderr:IWED,file:IWED:filename"
	string token;
	string option_copy = _option;
	while(str_chop_before(option_copy, _delim0, token))
		parse_single_option_group(token);
}

Log_dest Log_option_parser::get_next_log_dest()
{
	if(_log_option_group_index + 1 == (int)_log_option_groups.size())
		return LOG_DEST_DEV_NULL;
	return _log_option_groups[++_log_option_group_index].dest;
}


pair<Log_type, Activation> Log_option_parser::get_next_level_activation()
{
	if(_log_option_group_index == -1)
		return pair<Log_type, Activation>(LOG_TYPE_INVALID, ACTIVATION_UNDEFINED);

	int &activation_level_index = 
		_log_option_groups[_log_option_group_index].current_activation_level;
	if(activation_level_index >= 4)
		return pair<Log_type, Activation>(LOG_TYPE_INVALID, ACTIVATION_UNDEFINED);
	return 
		_log_option_groups[_log_option_group_index].
			activation_levels[activation_level_index++];
}

static string _empty = "";
const string& Log_option_parser::get_filename()
{
	if(_log_option_group_index == -1) 
		return _empty;
	return _log_option_groups[_log_option_group_index].filename;
}