Skip to content

Commit

Permalink
Allow a QString in a LOG4CXX_ macro when 'Qt support/integration' is …
Browse files Browse the repository at this point in the history
…ON (#247)

* Update Qt support documentation

* Change configuration examples to links

* Fix NDC example file name

* Improve example code
  • Loading branch information
swebb2066 authored Aug 9, 2023
1 parent 25f900f commit 78d8e82
Show file tree
Hide file tree
Showing 27 changed files with 362 additions and 32 deletions.
19 changes: 17 additions & 2 deletions .github/workflows/log4cxx-ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,30 @@ jobs:
os: ubuntu-20.04
cxx: g++
cc: gcc
fmt: OFF
qt: ON
odbc: OFF
- name: ubuntu20-clang
os: ubuntu-20.04
cxx: clang++
cc: clang
fmt: ON
qt: OFF
odbc: ON
- name: ubuntu22-gcc
os: ubuntu-22.04
cxx: g++
cc: gcc
fmt: OFF
qt: OFF
odbc: OFF
- name: ubuntu22-clang
os: ubuntu-22.04
cxx: clang++
cc: clang
fmt: ON
qt: OFF
odbc: OFF

steps:
- uses: actions/checkout@v3
Expand All @@ -51,14 +63,17 @@ jobs:
- name: 'Configure Dependencies'
run: |
sudo apt-get update
sudo apt-get install -y libapr1-dev libaprutil1-dev libfmt-dev unixodbc-dev
sudo apt-get install -y libapr1-dev libaprutil1-dev
if [ ${{ matrix.fmt }} == ON ]; then sudo apt-get install -y libfmt-dev; fi
if [ ${{ matrix.odbc }} == ON ]; then sudo apt-get install -y unixodbc-dev; fi
if [ ${{ matrix.qt }} == ON ]; then sudo apt-get install -y qtbase5-dev; fi
- name: 'run cmake - posix'
run: |
cd main
mkdir build
cd build
cmake -DCMAKE_CXX_COMPILER=${{ matrix.cxx }} -DCMAKE_C_COMPILER=${{ matrix.cc }} -DLOG4CXX_ENABLE_ODBC=ON ..
cmake -DCMAKE_CXX_COMPILER=${{ matrix.cxx }} -DCMAKE_C_COMPILER=${{ matrix.cc }} -DLOG4CXX_ENABLE_ODBC=${{ matrix.odbc }} -DLOG4CXX_QT_SUPPORT=${{ matrix.qt }} ..
cmake --build .
- name: run unit tests
Expand Down
3 changes: 3 additions & 0 deletions src/cmake/win32_target_environment_path.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ function(get_target_environment_path varName)
set(EXPAT_DLL_DIR "${EXPAT_LIB_DIR}/../bin")
set(LOG4CXX_DLL_DIR "$<SHELL_PATH:$<TARGET_FILE_DIR:log4cxx>>;")
set(PATH_FOR_TESTS ${CMAKE_PROGRAM_PATH};${APR_DLL_DIR};${APR_UTIL_DLL_DIR};${LOG4CXX_DLL_DIR};${EXPAT_DLL_DIR}\;)
if(LOG4CXX_QT_SUPPORT)
list(APPEND PATH_FOR_TESTS "$<SHELL_PATH:$<TARGET_FILE_DIR:log4cxx-qt>>\;")
endif(LOG4CXX_QT_SUPPORT)
list(REMOVE_DUPLICATES PATH_FOR_TESTS)

# Note: we need to include the APR DLLs on our path so that the tests will run.
Expand Down
9 changes: 8 additions & 1 deletion src/examples/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
# limitations under the License.
#

set(ALL_LOG4CXX_EXAMPLES auto-configured console delayedloop stream trivial custom-appender MyApp1 MyApp2)
set(ALL_LOG4CXX_EXAMPLES auto-configured console delayedloop stream ndc-example custom-appender MyApp1 MyApp2)
if(LOG4CXX_QT_SUPPORT)
list(APPEND ALL_LOG4CXX_EXAMPLES MyApp-qt)
endif(LOG4CXX_QT_SUPPORT)
if( WIN32 )
include(win32_target_environment_path)
get_target_environment_path(ESCAPED_PATH)
Expand All @@ -34,6 +37,10 @@ foreach(exampleName IN LISTS ALL_LOG4CXX_EXAMPLES)
if(${exampleName} STREQUAL MyApp2)
target_sources(${PROGRAM_NAME} PRIVATE com/foo/config2.cpp com/foo/bar.cpp)
endif()
if(${exampleName} STREQUAL MyApp-qt)
target_sources(${PROGRAM_NAME} PRIVATE com/foo/config-qt.cpp com/foo/bar-qt.cpp)
target_link_libraries(${PROGRAM_NAME} PRIVATE log4cxx-qt)
endif()
if(${exampleName} STREQUAL auto-configured)
target_sources(${PROGRAM_NAME} PRIVATE com/foo/config3.cpp )
endif()
Expand Down
20 changes: 20 additions & 0 deletions src/examples/cpp/MyApp-qt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <QCoreApplication>
#include "com/foo/config-qt.h"
#include "com/foo/bar.h"

int main(int argc, char **argv) {
int result = EXIT_SUCCESS;
QCoreApplication app(argc, argv);
com::foo::ConfigureLogging();
try {
auto logger = com::foo::getLogger("MyApp");
LOG4CXX_INFO(logger, QString("Message %1").arg(1));
com::foo::Bar bar;
bar.doIt();
LOG4CXX_INFO(logger, QString("Message %1").arg(2));
}
catch(std::exception&) {
result = EXIT_FAILURE;
}
return result;
}
4 changes: 2 additions & 2 deletions src/examples/cpp/auto-configured.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
#include "com/foo/config.h"

extern auto rootLogger = com::foo::getLogger();
auto rootLogger = com::foo::getLogger();

static struct ExampleStaticData {
struct ExampleStaticData {
ExampleStaticData() {
LOG4CXX_DEBUG(rootLogger, "static initializer message");
}
Expand Down
10 changes: 10 additions & 0 deletions src/examples/cpp/com/foo/bar-qt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "com/foo/bar.h"
#include "com/foo/config-qt.h"

using namespace com::foo;

LoggerPtr Bar::m_logger(getLogger("com.foo.bar"));

void Bar::doIt() {
LOG4CXX_DEBUG(m_logger, QString("Did it again!") << QString(" - again!"));
}
71 changes: 71 additions & 0 deletions src/examples/cpp/com/foo/config-qt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config-qt.h"
#include <log4cxx/logmanager.h>
#include <log4cxx-qt/configuration.h>
#include <log4cxx/helpers/loglog.h>
#include <QCoreApplication>
#include <QVector>
#include <QFileInfo>
#include <QDir>

namespace com { namespace foo {

// Provide the name of the configuration file to Log4cxx.
// Reload the configuration on a QFileSystemWatcher::fileChanged event.
void ConfigureLogging() {
static struct log4cxx_finalizer {
~log4cxx_finalizer() {
log4cxx::LogManager::shutdown();
}
} finaliser;
QFileInfo app{QCoreApplication::applicationFilePath()};
QString basename{app.baseName()};
QVector<QString> paths =
{ QString(".")
, app.absoluteDir().absolutePath()
};
QVector<QString> names =
{ QString(basename + ".xml")
, QString(basename + ".properties")
, QString("MyApp.properties")
, QString("log4cxx.xml")
, QString("log4cxx.properties")
, QString("log4j.xml")
, QString("log4j.properties")
};
#if defined(_DEBUG)
log4cxx::helpers::LogLog::setInternalDebugging(true);
#endif
log4cxx::qt::Configuration::configureFromFileAndWatch(paths, names);
}

// Retrieve the \c name logger pointer.
auto getLogger(const QString& name) -> LoggerPtr {
return name.isEmpty()
? log4cxx::LogManager::getRootLogger()
: log4cxx::LogManager::getLogger(name.toStdString());
}

// Retrieve the \c name logger pointer.
auto getLogger(const char* name) -> LoggerPtr {
return name
? log4cxx::LogManager::getLogger(name)
: log4cxx::LogManager::getRootLogger();
}

} } // namespace com::foo
21 changes: 21 additions & 0 deletions src/examples/cpp/com/foo/config-qt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef COM_FOO_CONFIG_QT_H_
#define COM_FOO_CONFIG_QT_H_
#include <log4cxx-qt/logger.h>

/// Methods specific to foo.com
namespace com { namespace foo {

// Provide the name of the configuration file to Log4cxx.
void ConfigureLogging();

/// The logger pointer we use
using LoggerPtr = log4cxx::LoggerPtr;

/// Retrieve the \c name logger pointer.
extern auto getLogger(const QString& name) -> LoggerPtr;

/// Retrieve the \c name logger pointer.
extern auto getLogger(const char* name = NULL) -> LoggerPtr;

} } // namespace com::foo
#endif // COM_FOO_CONFIG_QT_H_
File renamed without changes.
9 changes: 5 additions & 4 deletions src/main/cpp-qt/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/
#include <log4cxx-qt/configuration.h>
#include <log4cxx-qt/transcoder.h>
#include <log4cxx/helpers/loglog.h>
#include <log4cxx/xml/domconfigurator.h>
#include <log4cxx/propertyconfigurator.h>
Expand Down Expand Up @@ -107,16 +108,16 @@ Configuration::configureFromFileAndWatch(const QVector<QString>& directories,
QString canidate_str = dir + "/" + fname;
QFile candidate(canidate_str);

QString debugMsg = LOG4CXX_STR("Checking file ");
debugMsg.append(canidate_str);
LogLog::debug(debugMsg.toStdString());
LOG4CXX_DECODE_QSTRING(msg, "Checking file " + canidate_str);
LogLog::debug(msg);
if (candidate.exists())
{
log4cxx::spi::ConfigurationStatus configStatus = tryLoadFile(canidate_str);
if( configStatus == log4cxx::spi::ConfigurationStatus::Configured ){
return {configStatus, canidate_str};
}
LogLog::debug("Unable to load file: trying next");
LOG4CXX_DECODE_QSTRING(failmsg, "Unable to load " + canidate_str + ": trying next");
LogLog::debug(failmsg);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/cpp/domconfigurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ spi::ConfigurationStatus DOMConfigurator::configure(const std::wstring& filename
}
#endif

#if LOG4CXX_UNICHAR_API
#if LOG4CXX_UNICHAR_API || LOG4CXX_LOGCHAR_IS_UNICHAR
spi::ConfigurationStatus DOMConfigurator::configure(const std::basic_string<UniChar>& filename)
{
File file(filename);
Expand Down Expand Up @@ -894,7 +894,7 @@ spi::ConfigurationStatus DOMConfigurator::configureAndWatch(const std::wstring&
}
#endif

#if LOG4CXX_UNICHAR_API
#if LOG4CXX_UNICHAR_API || LOG4CXX_LOGCHAR_IS_UNICHAR
spi::ConfigurationStatus DOMConfigurator::configureAndWatch(const std::basic_string<UniChar>& filename)
{
return configureAndWatch(filename, FileWatchdog::DEFAULT_DELAY);
Expand Down Expand Up @@ -958,7 +958,7 @@ spi::ConfigurationStatus DOMConfigurator::configureAndWatch(const std::wstring&
}
#endif

#if LOG4CXX_UNICHAR_API
#if LOG4CXX_UNICHAR_API || LOG4CXX_LOGCHAR_IS_UNICHAR
spi::ConfigurationStatus DOMConfigurator::configureAndWatch(const std::basic_string<UniChar>& filename, long delay)
{
File file(filename);
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ File::File(const wchar_t* name1)
}
#endif

#if LOG4CXX_UNICHAR_API
#if LOG4CXX_UNICHAR_API || LOG4CXX_LOGCHAR_IS_UNICHAR
File::File(const std::basic_string<UniChar>& name1)
: m_priv(std::make_unique<FilePrivate>(decodeLS(name1)))
{
Expand Down
10 changes: 8 additions & 2 deletions src/main/cpp/hexdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <log4cxx/log4cxx.h>
/* Prevent error C2491: 'std::numpunct<_Elem>::id': definition of dllimport static data member not allowed */
#if defined(_MSC_VER) && (LOG4CXX_UNICHAR_API || LOG4CXX_LOGCHAR_IS_UNICHAR)
#define __FORCE_INSTANCE
#endif
#include <log4cxx/hexdump.h>
#include <log4cxx/log4cxx.h>
#include <sstream>
Expand All @@ -32,8 +38,8 @@ LogString log4cxx::hexdump(const void* bytes, uint32_t len, HexdumpFlags flags){
const wchar_t fill_char = L'0';
const wchar_t space_fill_char = L' ';
#else
const char fill_char = '0';
const char space_fill_char = ' ';
const logchar fill_char = '0';
const logchar space_fill_char = ' ';
#endif

if(flags & HexdumpFlags::AddStartingNewline){
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void Level::toString(std::wstring& dst) const

#endif

#if LOG4CXX_UNICHAR_API
#if LOG4CXX_UNICHAR_API || LOG4CXX_LOGCHAR_IS_UNICHAR
LevelPtr Level::toLevel(const std::basic_string<UniChar>& sArg)
{
return toLevel(sArg, Level::getDebug());
Expand Down
4 changes: 2 additions & 2 deletions src/main/cpp/messagebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include <log4cxx/log4cxx.h>
/* Prevent error C2491: 'std::numpunct<_Elem>::id': definition of dllimport static data member not allowed */
#if defined(_MSC_VER) && LOG4CXX_UNICHAR_API
#if defined(_MSC_VER) && (LOG4CXX_UNICHAR_API || LOG4CXX_LOGCHAR_IS_UNICHAR)
#define __FORCE_INSTANCE
#endif
#include <log4cxx/helpers/messagebuffer.h>
Expand Down Expand Up @@ -587,7 +587,7 @@ const std::basic_string<log4cxx::UniChar>& MessageBuffer::str(std::basic_ostream

#endif // LOG4CXX_WCHAR_T_API

#if LOG4CXX_UNICHAR_API
#if LOG4CXX_UNICHAR_API || LOG4CXX_LOGCHAR_IS_UNICHAR
struct UniCharMessageBuffer::UniCharMessageBufferPrivate : public StringOrStream<UniChar> {};

UniCharMessageBuffer::UniCharMessageBuffer() :
Expand Down
4 changes: 4 additions & 0 deletions src/main/cpp/timebasedrollingpolicy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ const std::string TimeBasedRollingPolicy::createFile(const std::string& fileName

if (stat == APR_SUCCESS)
{
#ifdef WIN32
snprintf(szUid, MAX_FILE_LEN, "%p", uid);
#else
snprintf(szUid, MAX_FILE_LEN, "%u", uid);
#endif
}

log4cxx::filesystem::path path(fileName);
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/transcoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ void Transcoder::encode(unsigned int sv, std::wstring& dst)



#if LOG4CXX_UNICHAR_API
#if LOG4CXX_UNICHAR_API || LOG4CXX_LOGCHAR_IS_UNICHAR
void Transcoder::decode(const std::basic_string<UniChar>& src, LogString& dst)
{
#if LOG4CXX_LOGCHAR_IS_UNICHAR
Expand Down
22 changes: 22 additions & 0 deletions src/main/include/log4cxx-qt/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef _LOG4CXX_QT_LOGGER_H
#define _LOG4CXX_QT_LOGGER_H
#include <log4cxx/logger.h>
#include <log4cxx-qt/messagebuffer.h>
#endif // _LOG4CXX_QT_LOGGER_H
Loading

0 comments on commit 78d8e82

Please sign in to comment.