Skip to content

Commit 78d8e82

Browse files
authored
Allow a QString in a LOG4CXX_ macro when 'Qt support/integration' is ON (#247)
* Update Qt support documentation * Change configuration examples to links * Fix NDC example file name * Improve example code
1 parent 25f900f commit 78d8e82

27 files changed

+362
-32
lines changed

.github/workflows/log4cxx-ubuntu.yml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,30 @@ jobs:
3030
os: ubuntu-20.04
3131
cxx: g++
3232
cc: gcc
33+
fmt: OFF
34+
qt: ON
35+
odbc: OFF
3336
- name: ubuntu20-clang
3437
os: ubuntu-20.04
3538
cxx: clang++
3639
cc: clang
40+
fmt: ON
41+
qt: OFF
42+
odbc: ON
3743
- name: ubuntu22-gcc
3844
os: ubuntu-22.04
3945
cxx: g++
4046
cc: gcc
47+
fmt: OFF
48+
qt: OFF
49+
odbc: OFF
4150
- name: ubuntu22-clang
4251
os: ubuntu-22.04
4352
cxx: clang++
4453
cc: clang
54+
fmt: ON
55+
qt: OFF
56+
odbc: OFF
4557

4658
steps:
4759
- uses: actions/checkout@v3
@@ -51,14 +63,17 @@ jobs:
5163
- name: 'Configure Dependencies'
5264
run: |
5365
sudo apt-get update
54-
sudo apt-get install -y libapr1-dev libaprutil1-dev libfmt-dev unixodbc-dev
66+
sudo apt-get install -y libapr1-dev libaprutil1-dev
67+
if [ ${{ matrix.fmt }} == ON ]; then sudo apt-get install -y libfmt-dev; fi
68+
if [ ${{ matrix.odbc }} == ON ]; then sudo apt-get install -y unixodbc-dev; fi
69+
if [ ${{ matrix.qt }} == ON ]; then sudo apt-get install -y qtbase5-dev; fi
5570
5671
- name: 'run cmake - posix'
5772
run: |
5873
cd main
5974
mkdir build
6075
cd build
61-
cmake -DCMAKE_CXX_COMPILER=${{ matrix.cxx }} -DCMAKE_C_COMPILER=${{ matrix.cc }} -DLOG4CXX_ENABLE_ODBC=ON ..
76+
cmake -DCMAKE_CXX_COMPILER=${{ matrix.cxx }} -DCMAKE_C_COMPILER=${{ matrix.cc }} -DLOG4CXX_ENABLE_ODBC=${{ matrix.odbc }} -DLOG4CXX_QT_SUPPORT=${{ matrix.qt }} ..
6277
cmake --build .
6378
6479
- name: run unit tests

src/cmake/win32_target_environment_path.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ function(get_target_environment_path varName)
88
set(EXPAT_DLL_DIR "${EXPAT_LIB_DIR}/../bin")
99
set(LOG4CXX_DLL_DIR "$<SHELL_PATH:$<TARGET_FILE_DIR:log4cxx>>;")
1010
set(PATH_FOR_TESTS ${CMAKE_PROGRAM_PATH};${APR_DLL_DIR};${APR_UTIL_DLL_DIR};${LOG4CXX_DLL_DIR};${EXPAT_DLL_DIR}\;)
11+
if(LOG4CXX_QT_SUPPORT)
12+
list(APPEND PATH_FOR_TESTS "$<SHELL_PATH:$<TARGET_FILE_DIR:log4cxx-qt>>\;")
13+
endif(LOG4CXX_QT_SUPPORT)
1114
list(REMOVE_DUPLICATES PATH_FOR_TESTS)
1215

1316
# Note: we need to include the APR DLLs on our path so that the tests will run.

src/examples/cpp/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
# limitations under the License.
1616
#
1717

18-
set(ALL_LOG4CXX_EXAMPLES auto-configured console delayedloop stream trivial custom-appender MyApp1 MyApp2)
18+
set(ALL_LOG4CXX_EXAMPLES auto-configured console delayedloop stream ndc-example custom-appender MyApp1 MyApp2)
19+
if(LOG4CXX_QT_SUPPORT)
20+
list(APPEND ALL_LOG4CXX_EXAMPLES MyApp-qt)
21+
endif(LOG4CXX_QT_SUPPORT)
1922
if( WIN32 )
2023
include(win32_target_environment_path)
2124
get_target_environment_path(ESCAPED_PATH)
@@ -34,6 +37,10 @@ foreach(exampleName IN LISTS ALL_LOG4CXX_EXAMPLES)
3437
if(${exampleName} STREQUAL MyApp2)
3538
target_sources(${PROGRAM_NAME} PRIVATE com/foo/config2.cpp com/foo/bar.cpp)
3639
endif()
40+
if(${exampleName} STREQUAL MyApp-qt)
41+
target_sources(${PROGRAM_NAME} PRIVATE com/foo/config-qt.cpp com/foo/bar-qt.cpp)
42+
target_link_libraries(${PROGRAM_NAME} PRIVATE log4cxx-qt)
43+
endif()
3744
if(${exampleName} STREQUAL auto-configured)
3845
target_sources(${PROGRAM_NAME} PRIVATE com/foo/config3.cpp )
3946
endif()

src/examples/cpp/MyApp-qt.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <QCoreApplication>
2+
#include "com/foo/config-qt.h"
3+
#include "com/foo/bar.h"
4+
5+
int main(int argc, char **argv) {
6+
int result = EXIT_SUCCESS;
7+
QCoreApplication app(argc, argv);
8+
com::foo::ConfigureLogging();
9+
try {
10+
auto logger = com::foo::getLogger("MyApp");
11+
LOG4CXX_INFO(logger, QString("Message %1").arg(1));
12+
com::foo::Bar bar;
13+
bar.doIt();
14+
LOG4CXX_INFO(logger, QString("Message %1").arg(2));
15+
}
16+
catch(std::exception&) {
17+
result = EXIT_FAILURE;
18+
}
19+
return result;
20+
}

src/examples/cpp/auto-configured.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
*/
1717
#include "com/foo/config.h"
1818

19-
extern auto rootLogger = com::foo::getLogger();
19+
auto rootLogger = com::foo::getLogger();
2020

21-
static struct ExampleStaticData {
21+
struct ExampleStaticData {
2222
ExampleStaticData() {
2323
LOG4CXX_DEBUG(rootLogger, "static initializer message");
2424
}

src/examples/cpp/com/foo/bar-qt.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "com/foo/bar.h"
2+
#include "com/foo/config-qt.h"
3+
4+
using namespace com::foo;
5+
6+
LoggerPtr Bar::m_logger(getLogger("com.foo.bar"));
7+
8+
void Bar::doIt() {
9+
LOG4CXX_DEBUG(m_logger, QString("Did it again!") << QString(" - again!"));
10+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#include "config-qt.h"
18+
#include <log4cxx/logmanager.h>
19+
#include <log4cxx-qt/configuration.h>
20+
#include <log4cxx/helpers/loglog.h>
21+
#include <QCoreApplication>
22+
#include <QVector>
23+
#include <QFileInfo>
24+
#include <QDir>
25+
26+
namespace com { namespace foo {
27+
28+
// Provide the name of the configuration file to Log4cxx.
29+
// Reload the configuration on a QFileSystemWatcher::fileChanged event.
30+
void ConfigureLogging() {
31+
static struct log4cxx_finalizer {
32+
~log4cxx_finalizer() {
33+
log4cxx::LogManager::shutdown();
34+
}
35+
} finaliser;
36+
QFileInfo app{QCoreApplication::applicationFilePath()};
37+
QString basename{app.baseName()};
38+
QVector<QString> paths =
39+
{ QString(".")
40+
, app.absoluteDir().absolutePath()
41+
};
42+
QVector<QString> names =
43+
{ QString(basename + ".xml")
44+
, QString(basename + ".properties")
45+
, QString("MyApp.properties")
46+
, QString("log4cxx.xml")
47+
, QString("log4cxx.properties")
48+
, QString("log4j.xml")
49+
, QString("log4j.properties")
50+
};
51+
#if defined(_DEBUG)
52+
log4cxx::helpers::LogLog::setInternalDebugging(true);
53+
#endif
54+
log4cxx::qt::Configuration::configureFromFileAndWatch(paths, names);
55+
}
56+
57+
// Retrieve the \c name logger pointer.
58+
auto getLogger(const QString& name) -> LoggerPtr {
59+
return name.isEmpty()
60+
? log4cxx::LogManager::getRootLogger()
61+
: log4cxx::LogManager::getLogger(name.toStdString());
62+
}
63+
64+
// Retrieve the \c name logger pointer.
65+
auto getLogger(const char* name) -> LoggerPtr {
66+
return name
67+
? log4cxx::LogManager::getLogger(name)
68+
: log4cxx::LogManager::getRootLogger();
69+
}
70+
71+
} } // namespace com::foo

src/examples/cpp/com/foo/config-qt.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef COM_FOO_CONFIG_QT_H_
2+
#define COM_FOO_CONFIG_QT_H_
3+
#include <log4cxx-qt/logger.h>
4+
5+
/// Methods specific to foo.com
6+
namespace com { namespace foo {
7+
8+
// Provide the name of the configuration file to Log4cxx.
9+
void ConfigureLogging();
10+
11+
/// The logger pointer we use
12+
using LoggerPtr = log4cxx::LoggerPtr;
13+
14+
/// Retrieve the \c name logger pointer.
15+
extern auto getLogger(const QString& name) -> LoggerPtr;
16+
17+
/// Retrieve the \c name logger pointer.
18+
extern auto getLogger(const char* name = NULL) -> LoggerPtr;
19+
20+
} } // namespace com::foo
21+
#endif // COM_FOO_CONFIG_QT_H_
File renamed without changes.

src/main/cpp-qt/configuration.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717
#include <log4cxx-qt/configuration.h>
18+
#include <log4cxx-qt/transcoder.h>
1819
#include <log4cxx/helpers/loglog.h>
1920
#include <log4cxx/xml/domconfigurator.h>
2021
#include <log4cxx/propertyconfigurator.h>
@@ -107,16 +108,16 @@ Configuration::configureFromFileAndWatch(const QVector<QString>& directories,
107108
QString canidate_str = dir + "/" + fname;
108109
QFile candidate(canidate_str);
109110

110-
QString debugMsg = LOG4CXX_STR("Checking file ");
111-
debugMsg.append(canidate_str);
112-
LogLog::debug(debugMsg.toStdString());
111+
LOG4CXX_DECODE_QSTRING(msg, "Checking file " + canidate_str);
112+
LogLog::debug(msg);
113113
if (candidate.exists())
114114
{
115115
log4cxx::spi::ConfigurationStatus configStatus = tryLoadFile(canidate_str);
116116
if( configStatus == log4cxx::spi::ConfigurationStatus::Configured ){
117117
return {configStatus, canidate_str};
118118
}
119-
LogLog::debug("Unable to load file: trying next");
119+
LOG4CXX_DECODE_QSTRING(failmsg, "Unable to load " + canidate_str + ": trying next");
120+
LogLog::debug(failmsg);
120121
}
121122
}
122123
}

0 commit comments

Comments
 (0)