swebb2066 commented on code in PR #548: URL: https://github.com/apache/logging-log4cxx/pull/548#discussion_r2396345480
########## src/main/include/log4cxx/helpers/asyncbuffer.h: ########## @@ -0,0 +1,273 @@ +/* + * 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_ASYNC_BUFFER_H +#define LOG4CXX_ASYNC_BUFFER_H + +#include <log4cxx/helpers/messagebuffer.h> +#include <functional> +#include <vector> + +namespace LOG4CXX_NS +{ + +namespace helpers +{ + +/** + * This class is used by the LOG4CXX_INFO_ASYNC and similar + * macros to support insertion operators. + * The class is not intended for use outside of that context. + */ +class LOG4CXX_EXPORT AsyncBuffer +{ +public: + /** An empty buffer. + */ + AsyncBuffer(); + + /** A new buffer with the content of \c other + */ + AsyncBuffer(AsyncBuffer&& other); + + /** Release resources. + */ + ~AsyncBuffer(); + + /** Append a function to this buffer that will convert \c value to text. + * @param value type must be copy-constructable + * @return this buffer. + */ + template<typename T> + AsyncBuffer& operator<<(const T& value) + { + append([value](LogCharMessageBuffer& msgBuf) + { + msgBuf << value; + }); + return *this; + } + + /** Append a function to this buffer that will convert \c value to text. + * @param value type must be move-constructable + * @return this buffer. + */ + template<typename T> + AsyncBuffer& operator<<(const T&& rvalue) + { + append([value = std::move(rvalue)](LogCharMessageBuffer& msgBuf) + { + msgBuf << value; + }); + return *this; + } + + /** + * Has no item been added to this? + */ + bool empty() const; + + /** + * Add text version of buffered values to \c msg + */ + void renderMessage(LogCharMessageBuffer& msg); + + /** + * Remove all message appenders + */ + void clear(); + +private: + AsyncBuffer(const AsyncBuffer&) = delete; + AsyncBuffer& operator=(const AsyncBuffer&) = delete; + + LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(Private, m_priv) + using MessageBufferAppender = std::function<void(LogCharMessageBuffer&)>; + + /** + * Append \c function to this buffer. + */ + void append(const MessageBufferAppender& f); +}; + +} // namespace helpers +} // namespace LOG4CXX_NS + +/** @addtogroup LoggingMacros Logging macros +@{ +*/ + +#if !defined(LOG4CXX_THRESHOLD) || LOG4CXX_THRESHOLD <= 10000 +/** +Add a new logging event containing \c message to attached appender(s) if \c logger is enabled for <code>DEBUG</code> events. + +\usage +~~~{.cpp} +LOG4CXX_DEBUG_ASYNC(m_log, "AddMesh:" + << " name " << meshName + << " type 0x" << std:: hex << traits.Type + << " materialName " << meshObject.GetMaterialName() + << " visible? " << traits.IsDefaultVisible + << " at " << obj->getBoundingBox().getCenter() + << " +/- " << obj->getBoundingBox().getHalfSize() + ); +~~~ + +@param logger the logger that has the enabled status. +@param message a valid r-value expression of an <code>operator<<(std::ostream&. ...)</code> overload. + +*/ +#define LOG4CXX_DEBUG_ASYNC(logger, message) do { \ Review Comment: A separate PR could introduce new `_FMT_ASYNC` that package the format string and args but I do not know if such macros could retain compile time type checking. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
