Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LOGCXX-514 #153

Merged
merged 9 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/log4cxx-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
run: |
cd vcpkg
./bootstrap-vcpkg.bat
./vcpkg install apr apr-util --triplet=x64-windows
./vcpkg install apr apr-util fmt --triplet=x64-windows

- name: 'Install zip'
id: install-zip
Expand Down
17 changes: 16 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ elseif(TARGET expat::expat)
set(EXPAT_LIBRARIES expat::expat)
endif()

find_package(fmt 7.1 QUIET)
if(${fmt_FOUND})
option(ENABLE_FMT_LAYOUT "Enable the FMT layout(if libfmt found)" ON)
else()
set(ENABLE_FMT_LAYOUT "OFF")
endif()

# Request C++17, if available
# This *should* fallback to an older standard if it is not available
if( NOT "${CMAKE_CXX_STANDARD}")
Expand Down Expand Up @@ -191,7 +198,7 @@ get_directory_property( ATOMIC_IMPL DIRECTORY src/main/include DEFINITION ATOMIC
get_directory_property( FILESYSTEM_IMPL DIRECTORY src/main/include DEFINITION FILESYSTEM_IMPL )
get_directory_property( STD_MAKE_UNIQUE_IMPL DIRECTORY src/main/include DEFINITION STD_MAKE_UNIQUE_IMPL )

foreach(varName HAS_STD_LOCALE HAS_ODBC HAS_MBSRTOWCS HAS_WCSTOMBS HAS_FWIDE HAS_LIBESMTP HAS_SYSLOG)
foreach(varName HAS_STD_LOCALE HAS_ODBC HAS_MBSRTOWCS HAS_WCSTOMBS HAS_FWIDE HAS_LIBESMTP HAS_SYSLOG HAS_FMT)
if(${varName} EQUAL 0)
set(${varName} "OFF" )
elseif(${varName} EQUAL 1)
Expand Down Expand Up @@ -293,3 +300,11 @@ message(STATUS " ConsoleAppender ................. : ON")
message(STATUS " FileAppender .................... : ON")
message(STATUS " RollingFileAppender ............. : ON")
message(STATUS " MultiprocessRollingFileAppender . : ${LOG4CXX_MULTIPROCESS_ROLLING_FILE_APPENDER}")

message(STATUS "Available layouts:")
message(STATUS " HTMLLayout ...................... : ON")
message(STATUS " JSONLayout ...................... : ON")
message(STATUS " PatternLayout ................... : ON")
message(STATUS " SimpleLayout .................... : ON")
message(STATUS " XMLLayout ....................... : ON")
message(STATUS " FMTLayout ....................... : ${ENABLE_FMT_LAYOUT}")
1 change: 0 additions & 1 deletion src/examples/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ configure_file( custom-appender.xml
COPYONLY )

# Custom handling for format string example, since it utilizes libfmt
find_package(fmt 6.0 QUIET)
if(${fmt_FOUND})
add_executable( format-string format-string.cpp )
target_compile_definitions(format-string PRIVATE ${LOG4CXX_COMPILE_DEFINITIONS} ${APR_COMPILE_DEFINITIONS} ${APR_UTIL_COMPILE_DEFINITIONS} )
Expand Down
10 changes: 10 additions & 0 deletions src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ if()
)
endif()

if(${ENABLE_FMT_LAYOUT})
set(extra_classes ${extra_classes}
fmtlayout.cpp
)
endif()

target_sources(log4cxx
PRIVATE
action.cpp
Expand Down Expand Up @@ -204,6 +210,10 @@ if("${FILESYSTEM_IMPL}" STREQUAL "std::filesystem" OR
target_link_libraries(log4cxx PUBLIC $<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,9.0>>:stdc++fs>)
endif()

if(${ENABLE_FMT_LAYOUT})
target_link_libraries(log4cxx PUBLIC fmt::fmt)
endif()

if(LOG4CXX_ABI_CHECK)
message("Getting dependencies for ABI compatability check...")
# Get the latest version of abi-dumper and abi-compliance-checker
Expand Down
120 changes: 120 additions & 0 deletions src/main/cpp/fmtlayout.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* 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 <log4cxx/logstring.h>
#include <log4cxx/fmtlayout.h>
#include <log4cxx/spi/loggingevent.h>
#include <log4cxx/helpers/stringhelper.h>
#include <log4cxx/helpers/pool.h>
#include <log4cxx/helpers/optionconverter.h>
#include <log4cxx/level.h>
#include <chrono>

#include <fmt/format.h>
#include <fmt/chrono.h>

using namespace log4cxx;
using namespace log4cxx::spi;

struct FMTLayout::FMTLayoutPrivate{
FMTLayoutPrivate(){}

FMTLayoutPrivate(const LogString& pattern) :
conversionPattern(pattern)
{}

LogString conversionPattern;
};

IMPLEMENT_LOG4CXX_OBJECT(FMTLayout)

FMTLayout::FMTLayout() :
m_priv(std::make_unique<FMTLayoutPrivate>())
{}

FMTLayout::FMTLayout(const LogString& pattern) :
m_priv(std::make_unique<FMTLayoutPrivate>(pattern))
{}

FMTLayout::~FMTLayout(){}

void FMTLayout::setConversionPattern(const LogString& pattern)
{
m_priv->conversionPattern = pattern;
helpers::Pool pool;
activateOptions(pool);
}

LogString FMTLayout::getConversionPattern() const
{
return m_priv->conversionPattern;
}

void FMTLayout::setOption(const LogString& option, const LogString& value)
{
if (helpers::StringHelper::equalsIgnoreCase(option,
LOG4CXX_STR("CONVERSIONPATTERN"),
LOG4CXX_STR("conversionpattern")))
{
m_priv->conversionPattern = helpers::OptionConverter::convertSpecialChars(value);
}
}

void FMTLayout::activateOptions(helpers::Pool&)
{

}

void FMTLayout::format(LogString& output,
const spi::LoggingEventPtr& event,
log4cxx::helpers::Pool&) const
{
LogString locationFull = fmt::format("{}({})",
event->getLocationInformation().getFileName(),
event->getLocationInformation().getLineNumber());
LogString ndc;
event->getNDC(ndc);

fmt::format_to(std::back_inserter(output),
m_priv->conversionPattern,
fmt::arg("d", event->getChronoTimeStamp()),
fmt::arg("c", event->getLoggerName()),
fmt::arg("logger", event->getLoggerName()),
fmt::arg("f", event->getLocationInformation().getShortFileName()),
fmt::arg("shortfilename", event->getLocationInformation().getShortFileName()),
fmt::arg("F", event->getLocationInformation().getFileName()),
fmt::arg("filename", event->getLocationInformation().getFileName()),
fmt::arg("l", locationFull),
fmt::arg("location", locationFull),
fmt::arg("L", event->getLocationInformation().getLineNumber()),
fmt::arg("line", event->getLocationInformation().getLineNumber()),
fmt::arg("m", event->getMessage()),
fmt::arg("message", event->getMessage()),
fmt::arg("M", event->getLocationInformation().getMethodName()),
fmt::arg("method", event->getLocationInformation().getMethodName()),
fmt::arg("n", LOG4CXX_EOL),
fmt::arg("newline", LOG4CXX_EOL),
fmt::arg("p", event->getLevel()->toString()),
fmt::arg("level", event->getLevel()->toString()),
fmt::arg("r", event->getTimeStamp()),
fmt::arg("t", event->getThreadName()),
fmt::arg("thread", event->getThreadName()),
fmt::arg("T", event->getThreadUserName()),
fmt::arg("threadname", event->getThreadUserName()),
fmt::arg("x", ndc),
fmt::arg("ndc", ndc)
);
}
12 changes: 10 additions & 2 deletions src/main/cpp/loggingevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

#include <chrono>
#include <log4cxx/spi/loggingevent.h>
#include <log4cxx/ndc.h>

Expand Down Expand Up @@ -68,10 +69,11 @@ struct LoggingEvent::LoggingEventPrivate
ndcLookupRequired(true),
mdcCopyLookupRequired(true),
message(message1),
timeStamp(apr_time_now()),
timeStamp(Date::currentTime()),
locationInfo(locationInfo1),
threadName(getCurrentThreadName()),
threadUserName(getCurrentThreadUserName())
threadUserName(getCurrentThreadUserName()),
chronoTimeStamp(std::chrono::microseconds(timeStamp))
{
}

Expand Down Expand Up @@ -138,6 +140,8 @@ struct LoggingEvent::LoggingEventPrivate
* systems or SetThreadDescription on Windows.
*/
const LogString& threadUserName;

std::chrono::time_point<std::chrono::system_clock> chronoTimeStamp;
};

IMPLEMENT_LOG4CXX_OBJECT(LoggingEvent)
Expand Down Expand Up @@ -432,3 +436,7 @@ const log4cxx::spi::LocationInfo& LoggingEvent::getLocationInformation() const
return m_priv->locationInfo;
}

std::chrono::time_point<std::chrono::system_clock> LoggingEvent::getChronoTimeStamp() const{
return m_priv->chronoTimeStamp;
}

Loading