gelfcpp::GelfMessageBuilder Struct Reference

Builder class for GelfMessage instances. More...

#include <GelfMessageBuilder.hpp>

Public Member Functions

GelfMessageBuilderoperator() (const std::string &message)
 Sets the short_message field. More...
 
template<typename T >
GelfMessageBuilderoperator() (const std::string &field, T value)
 Sets a field to the given value. More...
 
template<typename Decorator >
GelfMessageBuilderoperator() (Decorator decorator)
 Applies a decorator to the message. More...
 

Detailed Description

Builder class for GelfMessage instances.

Allows convenient construction for GelfMessages with additional Decorator support. The builder is implicitly convertible to a GelfMessage and can be directly passed to an Output.

Note
The internal GelfMessage is not copy-constructible, thus
GelfMessage msg = builder;
is invalid! But move-construction from an temporary GelfMessageBuilder is allowed.

Example usage:

GelfMessageBuilder builder;
builder("This is the short_message");
builder("field_1", true)("field_2", "chains are possible too!");
output->Write(builder);

Decorators are also supported. A decorator is a functional having the following syntax:

void operator()(GelfMessage& message)

This allows to dynamically set common values via a globally defined decorator. Common use cases are e.g. adding the current timestamp, setting the "host" field.

Example useage:

auto add_common_fields = [](GelfMessage& message)
{
message.SetTimestamp(std::time(nullptr));
message.SetHost("my-host");
};
builder(add_common_fields);

Member Function Documentation

GelfMessageBuilder& gelfcpp::GelfMessageBuilder::operator() ( const std::string &  message)
inline

Sets the short_message field.

Equivalent to gelf_message.SetMessage(message).

Parameters
messagemessage
Returns
*this , for chaining
See also
GelfMessage::SetMessage()
template<typename T >
GelfMessageBuilder& gelfcpp::GelfMessageBuilder::operator() ( const std::string &  field,
value 
)
inline

Sets a field to the given value.

Equivalent to gelf_message.SetField(field, value).

Template Parameters
Tany supported field type,
See also
GelfMessage::SetField()
Parameters
fieldfield name
valuefield value
Returns
*this , for chaining
See also
GelfMessage::SetField()
template<typename Decorator >
GelfMessageBuilder& gelfcpp::GelfMessageBuilder::operator() ( Decorator  decorator)
inline

Applies a decorator to the message.

Equivalent to decorator(gelf_message).

Template Parameters
Decoratordecorator type
Parameters
decoratordecorator function
Returns
*this , for chaining

The documentation for this struct was generated from the following file: