void PropertyConfigurator::configureFromFile(const QString &rConfigFileName,
                                                 LoggerRepository *pLoggerRepository)
	{
	    QFile file(rConfigFileName);
	    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
	    {
	        LogError e = LOG4QT_ERROR(QT_TR_NOOP("Unable to open property file '%1'"),
                                      CONFIGURATOR_OPENING_FILE_ERROR,
                                      "Log4Qt::PropertyConfigurator");
	        e << rConfigFileName;
	        e.addCausingError(LogError(file.errorString(), file.error()));
	        logger()->error(e);
	        return;
	    }
		Properties properties;
		properties.load(&file);
        if (file.error())
        {
            LogError e = LOG4QT_ERROR(QT_TR_NOOP("Unable to read property file '%1'"),
                                      CONFIGURATOR_READING_FILE_ERROR,
                                      "Log4Qt::PropertyConfigurator");
            e << rConfigFileName;
            e.addCausingError(LogError(file.errorString(), file.error()));
            logger()->error(e);
            return;
        }
		configureFromProperties(properties, pLoggerRepository);
	}
Beispiel #2
0
	int PatternFormatter::parseIntegerOption(const QString &rOption)
	{
		if (rOption.isEmpty())
			return 0;
		
		bool ok;
		int result = rOption.toInt(&ok);
		if (!ok)
		{
	        LogError e = LOG4QT_ERROR(QT_TR_NOOP("Option '%1' cannot be converted into an integer"),
                                      LAYOUT_OPTION_IS_NOT_INTEGER_ERROR,
                                      "Log4Qt::PatterFormatter");
	        e << rOption;
	        logger()->error(e);
		}
		if (result < 0)
		{
	        LogError e = LOG4QT_ERROR(QT_TR_NOOP("Option %1 isn't a positive integer"),
                                      LAYOUT_INTEGER_IS_NOT_POSITIVE_ERROR,
                                      "Log4Qt::PatterFormatter");
	        e << result;
	        logger()->error(e);
			result = 0;
		}
		return result;
	}
void DatabaseAppender::append(const LoggingEvent &rEvent)
{
    DatabaseLayout *databaseLayout = qobject_cast<DatabaseLayout *>(layout());

    if (databaseLayout)
    {
        QSqlRecord record = databaseLayout->formatRecord(rEvent);

        QSqlDatabase database = QSqlDatabase::database(connectionName);
        QSqlQuery query(database);
        if (!query.exec(database.driver()->sqlStatement(QSqlDriver::InsertStatement
                        , tableName, record, false)))
        {
            LogError e = LOG4QT_ERROR(QT_TR_NOOP("Sql query exec error: '%1'"),
                                      APPENDER_EXEC_SQL_QUERY_ERROR,
                                      Q_FUNC_INFO);
            e << query.lastQuery() + " " + query.lastError().text();
            logger()->error(e);
        }
    }
    else
    {
        LogError e = LOG4QT_QCLASS_ERROR(QT_TR_NOOP("Use of appender '%1' with invalid layout"),
                                         APPENDER_INVALID_DATABASE_LAYOUT_ERROR);
        e << name();
        logger()->error(e);
    }
}
Beispiel #4
0
    int OptionConverter::toTarget(const QString &rOption, 
                                  bool *p_ok)
    {
        const QLatin1String java_stdout("system.out"); 
        const QLatin1String cpp_stdout("stdout_target"); 
        const QLatin1String java_stderr("system.err"); 
        const QLatin1String cpp_stderr("stderr_target");
        
        if (p_ok)
            *p_ok = true;
        QString s = rOption.trimmed().toLower();
        if (s == java_stdout || s == cpp_stdout)
            return ConsoleAppender::STDOUT_TARGET;
        if (s == java_stderr || s == cpp_stderr)
            return ConsoleAppender::STDERR_TARGET;

        if (p_ok)
            *p_ok = false;
        LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for a target"), 
                                  CONFIGURATOR_INVALID_OPTION_ERROR,
                                  "Log4Qt::OptionConverter");
        e << rOption;
        logger()->error(e);
        return ConsoleAppender::STDOUT_TARGET;
    }
Beispiel #5
0
 bool OptionConverter::toBoolean(const QString &rOption, 
                                 bool *p_ok)
 {
     const QLatin1String str_true("true");
     const QLatin1String str_enabled("enabled");
     const QLatin1String str_one("1");
     const QLatin1String str_false("false");
     const QLatin1String str_disabled("disabled");
     const QLatin1String str_zero("0");
     
     if (p_ok)
         *p_ok = true;
     QString s = rOption.trimmed().toLower();
     if (s == str_true || s == str_enabled || s == str_one)
         return true;
     if (s == str_false || s == str_disabled || s == str_zero)
         return false;
     
     if (p_ok)
         *p_ok = false;
     LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for a boolean"), 
                               CONFIGURATOR_INVALID_OPTION_ERROR,
                               "Log4Qt::OptionConverter");
     e << rOption;
     logger()->error(e);
     return false;
 }
Beispiel #6
0
		void Factory::doSetObjectProperty(QObject *pObject,
																			const QString &rProperty,
																			const QString &rValue)
		{
				// - Validate property
				// - Get correct property name from meta object
				// - Find specific property setter
				// - If no specfifc propery setter can be found,
				//   find general property setter
				// - Call property setter

				QMetaProperty meta_property;
				if (!validateObjectProperty(meta_property, rProperty, pObject))
						return;

				QString property = QLatin1String(meta_property.name());
				QString type = QLatin1String(meta_property.typeName());
				logger()->debug("Setting property '%1' on object of class '%2' to value '%3'",
												property,
												QLatin1String(pObject->metaObject()->className()),
												rValue);

				QVariant value;
				bool ok = true;
				if (type == QLatin1String("bool"))
						value = OptionConverter::toBoolean(rValue, &ok);
				else if (type == QLatin1String("int"))
						value = OptionConverter::toInt(rValue, &ok);
				else if (type == QLatin1String("Log4Qt::Level"))
						value = QVariant::fromValue(OptionConverter::toLevel(rValue, &ok));
				else if (type == QLatin1String("QString"))
						value = rValue;
				else
				{
						LogError e = LOG4QT_ERROR(QT_TR_NOOP("Cannot convert to type '%1' for property '%2' on object of class '%3'"),
																			CONFIGURATOR_UNKNOWN_TYPE_ERROR,
																			"Log4Qt::Factory");
						e << type
							<< property
							<< QString::fromLatin1(pObject->metaObject()->className());
						logger()->error(e);
						return;
				}
				if (!ok)
						return;

				// Everything is checked and the type is the one of the property.
				// Write should never return false
				if (!meta_property.write(pObject, value))
						logger()->warn("Unxpected error result from QMetaProperty.write()");
		}
Beispiel #7
0
 int OptionConverter::toInt(const QString &rOption, 
                            bool *p_ok)
 {
     int value = rOption.trimmed().toInt(p_ok);
     if (*p_ok)
         return value;
     
     LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for an integer"), 
                               CONFIGURATOR_INVALID_OPTION_ERROR,
                               "Log4Qt::OptionConverter");
     e << rOption;
     logger()->error(e);
     return 0;
 }
Beispiel #8
0
 QString OptionConverter::findAndSubst(const Properties &rProperties,
                                       const QString &rKey)
 {
     QString value = rProperties.property(rKey);
     if (value.isNull())
         return value;
     
     const QString begin_subst = QLatin1String("${");
     const QString end_subst = QLatin1String("}");
     const int begin_length = begin_subst.length();
     const int end_length = end_subst.length();
     
     // Don't return a null string, the null string indicates that the 
     // property key does not exist.
     QString result = QLatin1String("");
     
     int i = 0;
     int begin;
     int end;
     while (i < value.length())
     {
         begin = value.indexOf(begin_subst, i);
         if (begin == -1)
         {
             result += value.mid(i);
             i = value.length();
         }
         else
         {
             result += value.mid(i, begin - i);
             end = value.indexOf(end_subst, i + begin_length);
             if (end == -1)
             {
                 LogError e = LOG4QT_ERROR(QT_TR_NOOP("Missing closing bracket for opening bracket at %1. Invalid subsitution in value %2."), 
                                           CONFIGURATOR_INVALID_SUBSTITUTION_ERROR,
                                           "Log4Qt::OptionConverter");
                 e << begin << value;
                 logger()->error(e);
                 return result;
             }
             else
             {
                 result += findAndSubst(rProperties, value.mid(begin + begin_length, end - begin - end_length - 1));
                 i = end + end_length;
             }
         }
     }
     return result;
 }
Beispiel #9
0
 qint64 OptionConverter::toFileSize(const QString &rOption, 
                                    bool *p_ok)
 {
     // - Search for unit
     // - Convert characters befor unit to int
     // - Error, if
     //   - the conversion failed
     //   - the value < 0
     //   - there is text after the unit characters
     
     if (p_ok)
         *p_ok = false;
     QString s = rOption.trimmed().toLower();
     qint64 f = 1;
     int i;
     i = s.indexOf(QLatin1String("kb"));
     if (i >= 0)
         f = 1024;
     else 
     {
         i = s.indexOf(QLatin1String("mb"));
         if (i >= 0)
             f = 1024 * 1024;
         else 
         {
             i = s.indexOf(QLatin1String("gb"));
             if (i >= 0)
                 f = 1024 * 1024 * 1024;
         }
     }
     if (i < 0)
         i = s.length();
     bool ok;
     qint64 value = s.left(i).toLongLong(&ok);
     if (!ok || value < 0 || s.length() > i + 2)
     {
         LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for a file size"), 
                                   CONFIGURATOR_INVALID_OPTION_ERROR,
                                   "Log4Qt::OptionConverter");
         e << rOption;
         logger()->error(e);
         return 0;
     }
     if (p_ok)
         *p_ok = true;
     return value * f;
 }
Beispiel #10
0
    Level OptionConverter::toLevel(const QString &rOption, 
                                   bool *p_ok)
    {
        bool ok;
        Level level = Level::fromString(rOption.toUpper().trimmed(), &ok);
        if (p_ok)
            *p_ok = ok;
        if (ok)
            return level;

        LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for a level"), 
                                  CONFIGURATOR_INVALID_OPTION_ERROR,
                                  "Log4Qt::OptionConverter");
        e << rOption;
        logger()->error(e);
        return level;
    }
Beispiel #11
0
	void PatternFormatter::parse()
	{	
		enum State {
			LITERAL_STATE,
			ESCAPE_STATE,
			MIN_STATE,
			DOT_STATE,
			MAX_STATE,
			CHARACTER_STATE,
			POSSIBLEOPTION_STATE,
			OPTION_STATE
		};
		
		int i = 0;
		QChar c;
		char ch;
		State state = LITERAL_STATE;
		FormattingInfo formatting_info;
		QString literal;
		int converter_start;
		int option_start;
		while (i < mPattern.length())
		{
	        // i points to the current character
			// c contains the current character
			// ch contains the Latin1 equivalent of the current character
			// i is incremented at the end of the loop to consume the character
			// continue is used to change state without consuming the character
			
			c = mPattern.at(i);
			ch = c.toLatin1();
	        switch (state) 
	        {
	            case LITERAL_STATE:
	            	if (ch == '%')
	            	{
	            		formatting_info.clear();
	            		converter_start = i;
	            		state = ESCAPE_STATE;
	            	} else
	            		literal += c;
	            	break;
	            case ESCAPE_STATE:
	            	if (ch == '%')
	            	{
	            		literal += c;
	            		state = LITERAL_STATE;
	            	}
	            	else if (ch == 'n') 
	            	{
	            		literal += Layout::endOfLine();
	            		state = LITERAL_STATE;
	            	}
	            	else 
	            	{
		            	if (!literal.isEmpty())
		            	{
		            		createLiteralConverter(literal);
		            		literal.clear();
		            	}
		            	if (ch == '-')
		            		formatting_info.mLeftAligned = true;
		            	else if (c.isDigit())
		            	{
		        			formatting_info.mMinLength = c.digitValue(); 
		            		state = MIN_STATE;
		            	}
		            	else if (ch == '.')
		            		state = DOT_STATE;
		            	else
		            	{
		            		state = CHARACTER_STATE;
		            		continue;
		            	}
	            	}
	            	break;
	            case MIN_STATE:
	            	if (!addDigit(c, formatting_info.mMinLength))
	            	{
	            		if (ch == '.')
	            			state = DOT_STATE;
	            		else
	            		{
	            			state = CHARACTER_STATE;
	            			continue;
	            		}
	            	}
	            	break;
	            case DOT_STATE:
	            	if (c.isDigit())
	            	{
	        			formatting_info.mMaxLength = c.digitValue(); 
	            		state = MAX_STATE;
	            	} 
	            	else
	            	{
	                    LogError e = LOG4QT_ERROR(QT_TR_NOOP("Found character '%1' where digit was expected."),
                                                  LAYOUT_EXPECTED_DIGIT_ERROR,
                                                  "Log4Qt::PatternFormatter");
	                    e << QString(c);
	                    logger()->error(e);
	            	}
	            	break;
	            case MAX_STATE:
	            	if (!addDigit(c, formatting_info.mMaxLength))
	            	{
	            		state = CHARACTER_STATE;
	            		continue;
	            	}
	            	break;
	            case CHARACTER_STATE:
	            	if (mIgnoreCharacters.indexOf(c) >= 0)
	            		state = LITERAL_STATE;
	            	else if (mOptionCharacters.indexOf(c) >= 0)
	            		state = POSSIBLEOPTION_STATE;
	            	else if (mConversionCharacters.indexOf(c) >= 0)
	            	{
	            		createConverter(c, formatting_info);
	            		state = LITERAL_STATE;
	            	}	
	            	else
	            	{
	            		logger()->warn("Invalid conversion character '%1' at %2 in pattern '%3'",
	            				       c, i, mPattern);
	            		createLiteralConverter(mPattern.mid(converter_start, i - converter_start + 1));
	            		state = LITERAL_STATE;
	            	}
	            	break;
	            case POSSIBLEOPTION_STATE:
	            	if (ch == '{')
	            	{
	                	option_start = i;
	            		state = OPTION_STATE;
	            	}
	            	else
	            	{
	            		createConverter(mPattern.at(i - 1), 
	                                    formatting_info);
	            		state = LITERAL_STATE;
	            		continue;
	            	}
	            	break;
	            case OPTION_STATE:
	            	if (ch == '}')
	            	{	
	            		createConverter(mPattern.at(option_start - 1), 
	                                    formatting_info,
	                                    mPattern.mid(option_start + 1, i - option_start - 1));
	            		state = LITERAL_STATE;
	            	}
	                break;
	            default:
	            	Q_ASSERT_X(false, "PatternFormatter::parse()", "Unknown parsing state constant");
	        		state = LITERAL_STATE;
	        }
			i++;
		}
	
		if (state != LITERAL_STATE)
		{
			logger()->warn("Unexptected end of pattern '%1'", mPattern);
			if (state == ESCAPE_STATE)
				literal += c;
			else
				literal += mPattern.mid(converter_start);
		}
		
		if (!literal.isEmpty())
			createLiteralConverter(literal);
	}
Beispiel #12
0
		bool Factory::validateObjectProperty(QMetaProperty &rMetaProperty,
																				 const QString &rProperty,
																				 QObject *pObject)
		{
				// Validate:
				// - No null object pointer
				// - No empty property name
				// - Property exists on the object (QT or Java name)
				// - Property is readable
				// - Property is writable

				const char *p_context = "Log4Qt::Factory";
				LogError e = LOG4QT_ERROR(QT_TR_NOOP("Unable to set property value on object"),
																	CONFIGURATOR_PROPERTY_ERROR,
																	p_context);

				if (!pObject)
				{
						LogError ce = LOG4QT_ERROR(QT_TR_NOOP("Invalid null object pointer"),
																			 0,
																			 p_context);
						e.addCausingError(ce);
						logger()->error(e);
						return false;
				}
				if (rProperty.isEmpty())
				{
						LogError ce = LOG4QT_ERROR(QT_TR_NOOP("Invalid empty property name"),
																			 0,
																			 p_context);
						e.addCausingError(ce);
						logger()->error(e);
						return false;
				}
				const QMetaObject *p_meta_object = pObject->metaObject();
				QString property = rProperty;
				int i = p_meta_object->indexOfProperty(property.toLatin1());
				if (i < 0)
				{
						// Try name with lower case first character. Java properties names
						// start upper case
						property[0] = property[0].toLower();
						i = p_meta_object->indexOfProperty(property.toLatin1());
						if (i < 0)
						{
								LogError ce = LOG4QT_ERROR(QT_TR_NOOP("Property '%1' does not exist in class '%2'"),
																					 0,
																					 p_context);
								ce << property
									 << QString::fromLatin1(pObject->metaObject()->className());
								e.addCausingError(ce);
								logger()->error(e);
								return false;
						}
				}
				rMetaProperty = p_meta_object->property(i);
				if (!rMetaProperty.isWritable())
				{
						LogError ce = LOG4QT_ERROR(QT_TR_NOOP("Property '%1' is not writable in class '%2'"),
																			 0,
																			 p_context);
						ce << property
							 << QString::fromLatin1(pObject->metaObject()->className());
						e.addCausingError(ce);
						logger()->error(e);
						return false;
				}

				return true;
		}