ealogger
ealogger is a c++ library that provides a blazing fast and easy to use logging mechanism
sink.h
Go to the documentation of this file.
1 // ealogger is a simple, asynchronous and powerful logger library for c++
2 // Copyright 2013 - 2016 Christian Rapp
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SINK_H
17 #define SINK_H
18 
23 #include <algorithm>
24 #include <map>
25 #include <memory>
26 #include <mutex>
27 #include <string>
28 
29 #include <iostream>
30 
32 #include <ealogger/global.h>
33 #include <ealogger/logmessage.h>
34 #include <ealogger/utility.h>
35 
36 namespace ealogger
37 {
52 class Sink
53 {
54 public:
79  Sink(std::string msg_template, std::string datetime_pattern, bool enabled,
81  virtual ~Sink();
82 
95  void set_msg_template(std::string msg_template);
106  void set_datetime_pattern(std::string datetime_pattern);
112  void set_enabled(bool enabled);
118  bool get_enabled();
125 
135  void prepare_log_message(const std::shared_ptr<LogMessage> &log_message);
136 
137 protected:
138  // TODO: I think some of these protected members could be moved to private
139  std::string
141  std::string datetime_pattern;
142  bool enabled;
146  // Mutexes for all important members
147  std::mutex mtx_msg_template;
148  std::mutex mtx_datetime_pattern;
149  std::mutex mtx_enabled;
150  std::mutex mtx_min_lvl;
151  std::mutex mtx_conv_pattern;
153  std::vector<ConversionPattern>
156  std::map<ealogger::constants::LOG_LEVEL, std::string>
163  void fill_conv_patterns(bool lock);
174  virtual void write_message(const std::string &msg) = 0;
175 
182  virtual void config_changed() = 0;
183 };
187 }
188 
189 #endif /* SINK_H */
std::mutex mtx_conv_pattern
Definition: sink.h:151
std::string datetime_pattern
Definition: sink.h:141
std::mutex mtx_enabled
Definition: sink.h:149
void set_min_lvl(ealogger::constants::LOG_LEVEL min_lvl)
Set minimum severity for this sink.
Definition: sink.cpp:63
bool get_enabled()
Check if this sink is enabled.
Definition: sink.cpp:57
std::mutex mtx_datetime_pattern
Definition: sink.h:148
Global ealogger constants.
Main namespace for ealogger.
Definition: conversion_pattern.h:28
Sink(std::string msg_template, std::string datetime_pattern, bool enabled, ealogger::constants::LOG_LEVEL min_lvl)
Sink constructor.
Definition: sink.cpp:21
void set_datetime_pattern(std::string datetime_pattern)
Set the date time conversion pattern.
Definition: sink.cpp:45
A sink is an object that writes the log message to a specific target.
Definition: sink.h:52
std::map< ealogger::constants::LOG_LEVEL, std::string > loglevel_lookup
Definition: sink.h:157
virtual void write_message(const std::string &msg)=0
Writes a LogMessage object to the logger sink.
void fill_conv_patterns(bool lock)
Fill Sink::vec_conv_patterns with ConverionPattern depending on Sink::msg_template.
Definition: sink.cpp:152
std::mutex mtx_msg_template
Definition: sink.h:147
std::vector< ConversionPattern > vec_conv_patterns
Definition: sink.h:154
bool enabled
Definition: sink.h:142
void set_msg_template(std::string msg_template)
Set the message template with conversion patterns.
Definition: sink.cpp:38
std::string msg_template
Definition: sink.h:140
virtual void config_changed()=0
This method will be called when the SinkConfig option changes.
Header with utility functions for ealogger.
void prepare_log_message(const std::shared_ptr< LogMessage > &log_message)
Prepare a log message before it is written to the targets.
Definition: sink.cpp:69
void set_enabled(bool enabled)
Enable or disable the sink.
Definition: sink.cpp:51
LOG_LEVEL
An enumaration representing the supported loglevels.
Definition: global.h:58
std::mutex mtx_min_lvl
Definition: sink.h:150
ealogger::constants::LOG_LEVEL min_level
Definition: sink.h:144