Files
poco/Foundation/include/Poco/Message.h
Aleksandar Fabijanic 1850dc16aa Benchmark and FastLogger (#5081)
* fix(SharedLibrary): Missing DLLs not reported #5069

* fix(CMake): not producing proper binary names #5070

* fix(SharedLibrary): disable shared lib tests in static build #5069

* fix(misc): add pdjson links to gitignore, remove unused var in SharedLibrary, harden TaskManagerTest

* fic(ci): separate oracle and sqlserver odbc (out of disk space) (#5075)

* fic(ci): separate oracle and sqlserver odbc (out of disk space)

* use oracle odbc driver

* use oracle free

* ad db user

* postpone adding user after build

* remove default tablespace (does not exist)

* reinstate all ci jobs

* add postgresl odb tests to ci

* remove spurious syminks

* fix gitignore (pdjson)

* Remove VS projects #5076

* chore: revert leftover ODB IP address

* fix(CodeQL): float comparison alerts

* fix: compile errors

* chore: upgrade asan to macos-14 (tryout)

* fix: .gitignore symlinks; XML Makefile wrong pattern

* Optimize PatternFormatter and Timezone performance #5078

PatternFormatter:
- Cache node name (Environment::nodeName()) to avoid repeated syscalls
- Add extractBasename() for efficient %O format specifier
- Add string reserve(128) to reduce reallocations during formatting

Timezone:
- Cache UTC offset to avoid repeated syscalls (8x speedup for %L patterns)
- Auto-detect TZ environment variable changes to invalidate cache
- Add reloadCache() method for explicit cache refresh

Tests:
- Add TimezoneTest::testUtcOffsetCaching()
- Add PatternFormatterTest::testExtractBasename()

* fix: use Path::separatorin extractBasename #5078

* Add Benchmark #5080

* enh(Logging): move constructors for Message and Logger #5078

* chore(AsyncNotificationCenter): eliminate MSVC warnings

* enh(build): c++20 support #5084

* feat(CppUnit): print class name
execute all named tests (not only the first one)
accept test name with class (eg. testrunner LoggerTest::testLogger) #5083

* feat(Benchmark): Add Logger/FastLogger comparison benchmarks and Windows support

- Add LoggerBench.cpp with AsyncChannel vs FastLogger benchmarks
- Add compare.sh (Linux/macOS) and compare.ps1 (Windows) scripts
- Add LOGGER_BENCHMARK.md with cross-platform benchmark results
- Update README.md with Windows build instructions (Ninja, CMAKE_PREFIX_PATH)
- Add error message when -- options are used on Windows (should use /)
- Update CMakeLists.txt and Makefile to include LoggerBench #5080

* feat(FastLogger): #5078
FastLogger provides a Poco-compatible wrapper around the Quill logging
library, offering significant performance improvements over AsyncChannel
through lock-free SPSC queues and backend thread processing.

Key features:
- Drop-in replacement for Poco::Logger with FastLogger::get()
- Support for all standard Poco channels (Console, File, Rotating, etc.)
- XML/properties configuration via FastLoggerConfigurator
- Thread affinity for backend worker on Linux and Windows
- Log file rotation with size and time-based policies

Performance (CPU time - calling thread latency):
- Linux: 31-70x faster than AsyncChannel
- Windows: 23-87x faster than AsyncChannel
- macOS: Limited improvement due to lack of thread affinity support

New files:
- Foundation/include/Poco/FastLogger.h
- Foundation/src/FastLogger.cpp
- Util/include/Poco/Util/FastLoggerConfigurator.h
- Util/src/FastLoggerConfigurator.cpp
- dependencies/quill/ (header-only Quill 7.5.0 library)

* fix(cmake): disable FastLogger on emscripten (not supported) #5078

* feat(FastLogger): add cpuAfinity config parameter #5087

* fix(FastLogger): Fix lock-order-inversion in FastLogger (TSAN) #5078

* fix(cmake): build not stripping release binaries #5085

* fix(PCRE): fails to compile with clang/c++20 #5131

* feat(AsyncChannel): add CPU affinity property #5087

* feat(SpinlockMutex): make it adaptive #5132

* feat(AsyncChannel): add CPU affinity property #5087

* chore: remove leftover file commited by mistake

* feat(build): allow FastLogger to be fully disabled at build time #5078

Build system changes:
- Add POCO_NO_FASTLOGGER compile definition in CMake when ENABLE_FASTLOGGER=OFF
  to prevent Config.h from auto-enabling FastLogger
- Add ifdef guards around FastLogger tests in LoggingTestSuite.cpp
- Exclude FastLoggerTest.cpp and FastLoggerChannelsTest.cpp from CMake build
  when FastLogger is disabled
- Add POCO_NO_FASTLOGGER support to Make build system for Foundation and Util
- Add CI jobs to verify builds work without FastLogger (CMake and Make)

Code changes:
- Add LoggingConfigurator::configure() convenience method for quick logging setup

* fix(ci): testrunner args

* chore(progen): remove leftover script #5076

* fix(test): give ANC a bit more time to process

* fix(ci): set env before test run

* chore(doc): quill license

* feat(Channel): add log(Message&&) #5133

* fix(ci): set env before test run

* fix(TestRunner): don't search children #5083

* feat: lock-free queues #5134

* feat(Benchmark): various comparisons

* chore: cleanup benchmark
2025-12-22 21:06:43 +01:00

309 lines
8.2 KiB
C++

//
// Message.h
//
// Library: Foundation
// Package: Logging
// Module: Message
//
// Definition of the Message class.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Foundation_Message_INCLUDED
#define Foundation_Message_INCLUDED
#include "Poco/Foundation.h"
#include "Poco/Timestamp.h"
#include <map>
namespace Poco {
class Foundation_API Message
/// This class represents a log message that is sent through a
/// chain of log channels.
///
/// A Message contains a priority denoting the severity of the
/// message, a source describing its origin, a text describing
/// its meaning, the time of its creation, and an identifier of
/// the process and thread that created the message.
///
/// Optionally a Message can also contain the source file path
/// and line number of the statement generating the message.
///
/// A Message can also contain any number of named parameters
/// that contain additional information about the event that
/// caused the message.
{
public:
using StringMap = std::map<std::string, std::string>;
enum Priority
{
PRIO_FATAL = 1, /// A fatal error. The application will most likely terminate. This is the highest priority.
PRIO_CRITICAL, /// A critical error. The application might not be able to continue running successfully.
PRIO_ERROR, /// An error. An operation did not complete successfully, but the application as a whole is not affected.
PRIO_WARNING, /// A warning. An operation completed with an unexpected result.
PRIO_NOTICE, /// A notice, which is an information with just a higher priority.
PRIO_INFORMATION, /// An informational message, usually denoting the successful completion of an operation.
PRIO_DEBUG, /// A debugging message.
PRIO_TRACE /// A tracing message. This is the lowest priority.
};
Message();
/// Creates an empty Message.
/// The thread and process ids are set.
Message(const std::string& source, const std::string& text, Priority prio);
/// Creates a Message with the given source, text and priority.
/// The thread and process ids are set.
Message(const std::string& source, std::string&& text, Priority prio);
/// Creates a Message with the given source, text and priority.
/// The text is moved into the message.
/// The thread and process ids are set.
Message(const std::string& source, const std::string& text, Priority prio, const char* file, LineNumber line);
/// Creates a Message with the given source, text, priority,
/// source file path and line.
///
/// The source file path must be a
/// static string with a lifetime that's at least the lifetime
/// of the message object (the string is not copied internally).
/// Usually, this will be the path string obtained from the
/// __FILE__ macro.
///
/// The thread and process ids are set.
Message(const std::string& source, std::string&& text, Priority prio, const char* file, LineNumber line);
/// Creates a Message with the given source, text, priority,
/// source file path and line.
///
/// The text is moved into the message.
///
/// The source file path must be a
/// static string with a lifetime that's at least the lifetime
/// of the message object (the string is not copied internally).
/// Usually, this will be the path string obtained from the
/// __FILE__ macro.
///
/// The thread and process ids are set.
Message(const Message& msg);
/// Creates a Message by copying another one.
Message(Message&& msg) noexcept;
/// Creates a Message by copying another one.
Message(const Message& msg, const std::string& text);
/// Creates a Message by copying all but the text from another message.
~Message();
/// Destroys the Message.
Message& operator = (const Message& msg);
/// Assignment operator.
Message& operator = (Message&& msg) noexcept;
/// Assignment operator.
void swap(Message& msg);
/// Swaps the message with another one.
void setSource(const std::string& src);
/// Sets the source of the message.
const std::string& getSource() const;
/// Returns the source of the message.
void setText(const std::string& text);
/// Sets the text of the message.
const std::string& getText() const;
/// Returns the text of the message.
void setPriority(Priority prio);
/// Sets the priority of the message.
Priority getPriority() const;
/// Returns the priority of the message.
void setTime(const Timestamp& time);
/// Sets the time of the message.
const Timestamp& getTime() const;
/// Returns the time of the message.
void setThread(const std::string& thread);
/// Sets the thread identifier for the message.
const std::string& getThread() const;
/// Returns the thread identifier for the message.
void setTid(long pid);
/// Sets the numeric thread identifier for the message.
long getTid() const;
/// Returns the numeric thread identifier for the message.
long getOsTid() const;
/// Returns the numeric thread identifier for the message.
void setPid(long pid);
/// Sets the process identifier for the message.
long getPid() const;
/// Returns the process identifier for the message.
void setSourceFile(const char* file);
/// Sets the source file path of the statement
/// generating the log message.
///
/// File must be a static string, such as the value of
/// the __FILE__ macro. The string is not copied
/// internally for performance reasons.
const char* getSourceFile() const;
/// Returns the source file path of the code creating
/// the message. May be 0 if not set.
void setSourceLine(LineNumber line);
/// Sets the source file line of the statement
/// generating the log message.
///
/// This is usually the result of the __LINE__
/// macro.
LineNumber getSourceLine() const;
/// Returns the source file line of the statement
/// generating the log message. May be 0
/// if not set.
bool has(const std::string& param) const;
/// Returns true if a parameter with the given name exists.
const std::string& get(const std::string& param) const;
/// Returns a const reference to the value of the parameter
/// with the given name. Throws a NotFoundException if the
/// parameter does not exist.
std::string get(const std::string& param, const std::string& defaultValue) const;
/// Returns the value of the parameter with the given name.
/// If the parameter with the given name does not exist,
/// then defaultValue is returned.
const StringMap& getAll() const;
/// Returns a const reference to all the values
void set(const std::string& param, const std::string& value);
/// Sets the value for a parameter. If the parameter does
/// not exist, then it is created.
const std::string& operator [] (const std::string& param) const;
/// Returns a const reference to the value of the parameter
/// with the given name. Throws a NotFoundException if the
/// parameter does not exist.
std::string& operator [] (const std::string& param);
/// Returns a reference to the value of the parameter with the
/// given name. This can be used to set the parameter's value.
/// If the parameter does not exist, it is created with an
/// empty string value.
protected:
void init();
private:
std::string _source;
std::string _text;
Priority _prio;
Timestamp _time;
long _tid;
long _ostid;
std::string _thread;
long _pid;
const char* _file;
LineNumber _line;
StringMap* _pMap;
};
//
// inlines
//
inline const std::string& Message::getSource() const
{
return _source;
}
inline const std::string& Message::getText() const
{
return _text;
}
inline Message::Priority Message::getPriority() const
{
return _prio;
}
inline const Timestamp& Message::getTime() const
{
return _time;
}
inline const std::string& Message::getThread() const
{
return _thread;
}
inline long Message::getTid() const
{
return _tid;
}
inline long Message::getOsTid() const
{
return _ostid;
}
inline long Message::getPid() const
{
return _pid;
}
inline const char* Message::getSourceFile() const
{
return _file;
}
inline LineNumber Message::getSourceLine() const
{
return _line;
}
inline void swap(Message& m1, Message& m2) noexcept
{
m1.swap(m2);
}
} // namespace Poco
#endif // Foundation_Message_INCLUDED