Question

I'm using POCO (v 1.4.6p4) logging framework but I have some problem on formatting strings:

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

Here I get:

Log a number [ERRFMT]

And a Poco::BadCastException is thrown.
Digging into the source I noted that the exception is thrown into the Any class:

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;
}

Could someone tell me where I wrong?

Was it helpful?

Solution

You have to add modifier (h) for unsigned short:

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

This annoyance is coming from Any's strictness in extracting values from it - you have to ask for the exact type or else all you can get is an exception or null pointer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top