Poco :: format错误

时间:2014-06-03 09:02:50

标签: c++ poco-libraries

我正在使用POCO(v 1.4.6p4)日志框架,但我在格式化字符串时遇到了一些问题:

int MyClass::MyMethod(unsigned short number) {  
  Logger::get("MyLog").information(Poco::format("Log a number [%u]", number));
}

我得到:

Log a number [ERRFMT]

抛出Poco :: BadCastException 挖掘源代码我注意到异常被抛入Any类:

template <typename ValueType>
ValueType AnyCast(const Any& operand)
  /// AnyCast operator used to extract a copy of the ValueType from an const Any&.
  ///
  /// Example Usage: 
  ///     MyType tmp = AnyCast<MyType>(anAny).
  /// Will throw a BadCastException if the cast fails.
  /// Dont use an AnyCast in combination with references, i.e. MyType& tmp = ... or const MyType& = ...
  /// Some compilers will accept this code although a copy is returned. Use the RefAnyCast in
  /// these cases.
{
    ValueType* result = AnyCast<ValueType>(const_cast<Any*>(&operand));
    if (!result) throw BadCastException("Failed to convert between const Any types");
    return *result;
}

有人能告诉我哪里错了吗?

1 个答案:

答案 0 :(得分:3)

您必须为无符号短片添加modifier(h):

Poco::format("Log a number [%hu]", number)

这种烦恼来自于Any的严格提取值 - 您必须要求确切的类型,否则您只能获得异常或空指针。