Skip to content

Commit

Permalink
Restore CFString code
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-webb committed Aug 4, 2023
1 parent 80e012d commit 3cae7a1
Show file tree
Hide file tree
Showing 18 changed files with 341 additions and 38 deletions.
13 changes: 13 additions & 0 deletions src/main/cpp/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ File::File(const QString& name)
}
#endif

#if LOG4CXX_CFSTRING_API
static LogString decodeLS(const CFStringRef& src)
{
LogString dst;
Transcoder::decode(src, dst);
return dst;
}
File::File(const CFStringRef& name)
: m_priv(std::make_unique<FilePrivate>(decodeLS(name)))
{
}
#endif

File::File(const File& src)
: m_priv(std::make_unique<FilePrivate>(src.m_priv->path, src.m_priv->autoDelete))
{
Expand Down
19 changes: 19 additions & 0 deletions src/main/cpp/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,25 @@ void Level::toString(std::basic_string<UniChar>& dst) const

#endif

#if LOG4CXX_CFSTRING_API
LevelPtr Level::toLevel(const CFStringRef& sArg)
{
return toLevel(sArg, Level::getDebug());
}

LevelPtr Level::toLevel(const CFStringRef& sArg, const LevelPtr& defaultLevel)
{
LogString s;
Transcoder::decode(sArg, s);
return toLevelLS(s, defaultLevel);
}

void Level::toString(CFStringRef& dst) const
{
dst = Transcoder::encode(name);
}
#endif

LevelPtr Level::toLevelLS(const LogString& sArg, const LevelPtr& defaultLevel)
{
const LogString trimmed(StringHelper::trim(sArg));
Expand Down
4 changes: 1 addition & 3 deletions src/main/cpp/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,9 +1058,7 @@ void Logger::addEvent(const LevelPtr& level, QString&& message, const LocationIn

void Logger::getName(QString& rv) const
{
LOG4CXX_DECODE_QSTRING(msg, message);
auto event = std::make_shared<LoggingEvent>(m_priv->name, level, location, std::move(msg));
callAppenders(event, p);
rv = Transcoder::encode(m_priv->name);
}

void Logger::qtrace(const QString& msg, const spi::LocationInfo& location) const
Expand Down
21 changes: 21 additions & 0 deletions src/main/cpp/logmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,27 @@ LoggerPtr LogManager::exists(const std::basic_string<UniChar>& name)
}
#endif

#if LOG4CXX_CFSTRING_API
LoggerPtr LogManager::getLogger(const CFStringRef& name)
{
LOG4CXX_DECODE_CFSTRING(n, name);
return getLoggerLS(n);
}

LoggerPtr LogManager::getLogger(const CFStringRef& name,
const spi::LoggerFactoryPtr& factory)
{
LOG4CXX_DECODE_CFSTRING(n, name);
return getLoggerLS(n, factory);
}

LoggerPtr LogManager::exists(const CFStringRef& name)
{
LOG4CXX_DECODE_CFSTRING(n, name);
return existsLS(n);
}
#endif

LoggerPtr LogManager::existsLS(const LogString& name)
{
return getLoggerRepository()->exists(name);
Expand Down
9 changes: 7 additions & 2 deletions src/main/cpp/logstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,14 @@ ulogstream::ulogstream(const std::basic_string<Ch>& loggerName,
const log4cxx::LevelPtr& level) : logstream_base(log4cxx::Logger::getLogger(loggerName), level), stream(0)
{
}
#endif

#if LOG4CXX_UNICHAR_API
#if LOG4CXX_CFSTRING_API
ulogstream::ulogstream(const CFStringRef& loggerName,
const log4cxx::LevelPtr& level)
: logstream_base(log4cxx::Logger::getLogger(loggerName), level), stream(0)
{
}
#endif

ulogstream::ulogstream(const log4cxx::LoggerPtr& logger,
const log4cxx::LevelPtr& level) : logstream_base(logger, level), stream(0)
Expand Down
48 changes: 48 additions & 0 deletions src/main/cpp/mdc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include <log4cxx/helpers/transcoder.h>
#include <log4cxx/helpers/threadspecificdata.h>

#if LOG4CXX_CFSTRING_API
#include <CoreFoundation/CFString.h>
#endif

using namespace log4cxx;
using namespace log4cxx::helpers;

Expand Down Expand Up @@ -222,3 +226,47 @@ std::basic_string<log4cxx::UniChar> MDC::remove(const std::basic_string<log4cxx:
}
#endif

#if LOG4CXX_CFSTRING_API
MDC::MDC(const CFStringRef& key1, const CFStringRef& value)
{
Transcoder::decode(key1, key);
LOG4CXX_DECODE_CFSTRING(v, value);
putLS(key, v);
}

CFStringRef MDC::get(const CFStringRef& key)
{
LOG4CXX_DECODE_CFSTRING(lkey, key);
LogString lvalue;

if (get(lkey, lvalue))
{
LOG4CXX_ENCODE_CFSTRING(value, lvalue);
return value;
}

return CFSTR("");
}

void MDC::put(const CFStringRef& key, const CFStringRef& value)
{
LOG4CXX_DECODE_CFSTRING(lkey, key);
LOG4CXX_DECODE_CFSTRING(lvalue, value);
putLS(lkey, lvalue);
}


CFStringRef MDC::remove(const CFStringRef& key)
{
LOG4CXX_DECODE_CFSTRING(lkey, key);
LogString lvalue;

if (remove(lkey, lvalue))
{
LOG4CXX_ENCODE_CFSTRING(value, lvalue);
return value;
}

return CFSTR("");
}
#endif
43 changes: 16 additions & 27 deletions src/main/cpp/messagebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,39 +765,30 @@ CharMessageBuffer& MessageBuffer::operator<<(const QString& msg)

#endif // LOG4CXX_QSTRING_API

#if LOG4CXX_UNICHAR_API && LOG4CXX_CFSTRING_API
#if LOG4CXX_CFSTRING_API
#include <CoreFoundation/CFString.h>
#include <vector>

#if LOG4CXX_UNICHAR_API
UniCharMessageBuffer& UniCharMessageBuffer::operator<<(const CFStringRef& msg)
{
if (m_priv->stream == 0)
{
m_priv->buf.append(msg.toUtf8().constData());
}
else
{
*m_priv->stream << msg.toUtf8().constData();
}

return *this;
}

UniCharMessageBuffer& UniCharMessageBuffer::operator<<(const QString& msg)
{
const UniChar* chars = msg.utf16();

if (chars != 0)
size_t length = CFStringGetLength(msg);
if (0 < length)
{
return operator<<(chars);
std::vector<unsigned short> tmp(length);
CFStringGetCharacters(msg, CFRangeMake(0, length), &tmp[0]);
if (m_priv->stream)
{
*m_priv->stream << std::basic_string<UniChar>(&tmp[0], tmp.size());
}
else
{
m_priv->buf.append(&tmp[0], tmp.size());
}
}

return *this;
}
elif LOG4CXX_CFSTRING_API
#include <CoreFoundation/CFString.h>
#include <vector>

#else
CharMessageBuffer& CharMessageBuffer::operator<<(const CFStringRef& msg)
{
LOG4CXX_DECODE_CFSTRING(tmp, msg);
Expand All @@ -819,7 +810,5 @@ CharMessageBuffer& MessageBuffer::operator<<(const CFStringRef& msg)
return m_priv->cbuf << tmp;
}
#endif // LOG4CXX_WCHAR_T_API

#endif // LOG4CXX_UNICHAR_API
#endif // LOG4CXX_CFSTRING_API


55 changes: 55 additions & 0 deletions src/main/cpp/ndc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,59 @@ bool NDC::peek(std::basic_string<UniChar>& dst)

#endif

#if LOG4CXX_CFSTRING_API
NDC::NDC(const CFStringRef& message)
{
push(message);
}

void NDC::push(const CFStringRef& message)
{
LOG4CXX_DECODE_CFSTRING(msg, message);
pushLS(msg);
}

bool NDC::pop(CFStringRef& dst)
{
ThreadSpecificData* data = ThreadSpecificData::getCurrentData();

if (data != 0)
{
Stack& stack = data->getStack();

if (!stack.empty())
{
dst = Transcoder::encode(getMessage(stack.top()));
stack.pop();
data->recycle();
return true;
}

data->recycle();
}

return false;
}

bool NDC::peek(CFStringRef& dst)
{
ThreadSpecificData* data = ThreadSpecificData::getCurrentData();

if (data != 0)
{
Stack& stack = data->getStack();

if (!stack.empty())
{
dst = Transcoder::encode(getMessage(stack.top()));
return true;
}

data->recycle();
}

return false;
}

#endif

8 changes: 6 additions & 2 deletions src/main/cpp/transcoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
#endif
#include <log4cxx/private/log4cxx_private.h>

#if LOG4CXX_CFSTRING_API
#include <CoreFoundation/CFString.h>
#endif

using namespace log4cxx;
using namespace log4cxx::helpers;

Expand Down Expand Up @@ -650,7 +654,7 @@ QString Transcoder::encode(const LogString& src)
#if LOG4CXX_CFSTRING_API
void Transcoder::decode(const CFStringRef& src, LogString& dst)
{
auto length = CFStringGetLength(src);
auto length = CFStringGetLength(src);
#if defined(_DEBUG)
Pool pool;
LogString msg(LOG4CXX_STR("Transcoder::decodeCFString"));
Expand All @@ -673,7 +677,7 @@ void Transcoder::decode(const CFStringRef& src, LogString& dst)

CFStringRef Transcoder::encode(const LogString& src)
{
std::basic_string<unsigned short> tmp;
std::basic_string<unsigned short> tmp;
for (auto ch : src)
encodeUTF16(ch, tmp);
return CFStringCreateWithCharacters(kCFAllocatorDefault, tmp.data(), tmp.size());
Expand Down
6 changes: 6 additions & 0 deletions src/main/include/log4cxx/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ class LOG4CXX_EXPORT File
*/
File(const QString& path);
#endif
#if LOG4CXX_CFSTRING_API
/**
* Construct a new instance. Use setPath to specify path using a LogString.
* @param path file path.
*/
File(const CFStringRef& path);
#endif /**
* Copy constructor.
*/
File(const File& src);
Expand Down
10 changes: 10 additions & 0 deletions src/main/include/log4cxx/helpers/messagebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ class LOG4CXX_EXPORT UniCharMessageBuffer
UniCharMessageBuffer& operator<<(const QString& msg);
#endif

#if LOG4CXX_CFSTRING_API
/**
* Appends a string into the buffer and
* fixes the buffer to use char characters.
* @param msg message to append.
* @return encapsulated CharMessageBuffer.
*/
UniCharMessageBuffer& operator<<(const CFStringRef& msg);
#endif

/**
* Insertion operator for STL manipulators such as std::fixed.
* @param manip manipulator.
Expand Down
21 changes: 17 additions & 4 deletions src/main/include/log4cxx/helpers/transcoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ class LOG4CXX_EXPORT Transcoder

#endif

#if LOG4CXX_QSTRING_API
static void decode(const QString& src, LogString& dst);
static QString encode(const LogString& src);
#endif

#if LOG4CXX_UNICHAR_API || LOG4CXX_LOGCHAR_IS_UNICHAR
static void decode(const std::basic_string<UniChar>& src, LogString& dst);
Expand All @@ -161,6 +157,16 @@ class LOG4CXX_EXPORT Transcoder

#endif

#if LOG4CXX_QSTRING_API
static void decode(const QString& src, LogString& dst);
static QString encode(const LogString& src);
#endif

#if LOG4CXX_CFSTRING_API
static void decode(const CFStringRef& src, LogString& dst);
static CFStringRef encode(const LogString& src);
#endif


enum { LOSSCHAR = 0x3F };

Expand Down Expand Up @@ -221,6 +227,13 @@ class LOG4CXX_EXPORT Transcoder
#define LOG4CXX_ENCODE_QSTRING(var, src) \
QString var = log4cxx::helpers::Transcoder::encode(src)

#define LOG4CXX_DECODE_CFSTRING(var, src) \
log4cxx::LogString var; \
log4cxx::helpers::Transcoder::decode(src, var)

#define LOG4CXX_ENCODE_CFSTRING(var, src) \
CFStringRef var = log4cxx::helpers::Transcoder::encode(src)


#if LOG4CXX_LOGCHAR_IS_WCHAR

Expand Down
Loading

0 comments on commit 3cae7a1

Please sign in to comment.