ealogger
ealogger is a c++ library that provides a blazing fast and easy to use logging mechanism
conversion_pattern.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 CONVERSION_PATTERN_H
17 #define CONVERSION_PATTERN_H
18 
19 #include <iostream>
20 #include <string>
21 
22 #include <ealogger/global.h>
23 
28 namespace ealogger
29 {
56 public:
60  enum PATTERN_TYPE {
61  DT = 0,
62  FILE,
64  LINE,
65  FUNC,
66  HOST,
68  MSG,
69  LVL
70  };
71 
78  ConversionPattern(std::string conv_pattern, const PATTERN_TYPE ptype)
79  : conv_pattern(std::move(conv_pattern)), ptype(ptype){};
80 
81  template <typename T>
88  void replace_conversion_pattern(std::string &msg, T new_value) const
89  {
90  this->replace_conversion_pattern(msg, std::to_string(new_value));
91  }
92 
100  {
101  return this->ptype;
102  }
103 
104 private:
105  std::string conv_pattern;
106  const PATTERN_TYPE ptype;
107 };
108 
111 template <>
124  std::string &msg, std::string new_value) const
125 {
126  // set pos to 0. we use this pos to narrow down our search area
127  // std::cout << "conv pattern: " << this->conv_pattern
128  //<< " new_value: " << new_value << std::endl;
129  size_t pos_in_string = 0;
130  while ((pos_in_string = msg.find(this->conv_pattern, pos_in_string)) !=
131  std::string::npos) {
132  msg.replace(pos_in_string, this->conv_pattern.length(), new_value);
133  pos_in_string += new_value.length();
134  }
135 }
136 }
137 #endif /* ifndef CONVERSION_PATTERN_H */
Definition: conversion_pattern.h:66
ConversionPattern(std::string conv_pattern, const PATTERN_TYPE ptype)
Constructor for the a conversion pattern.
Definition: conversion_pattern.h:78
Definition: conversion_pattern.h:69
Definition: conversion_pattern.h:62
Definition: conversion_pattern.h:61
Global ealogger constants.
Main namespace for ealogger.
Definition: conversion_pattern.h:28
A conversion pattern for message templates.
Definition: conversion_pattern.h:55
PATTERN_TYPE
Identifiers for patterns supported by ealogger.
Definition: conversion_pattern.h:60
Definition: conversion_pattern.h:68
Definition: conversion_pattern.h:67
Definition: conversion_pattern.h:65
Definition: conversion_pattern.h:64
ConversionPattern::PATTERN_TYPE get_pattern_type() const
Get the conversion pattern type.
Definition: conversion_pattern.h:99
Definition: conversion_pattern.h:63
void replace_conversion_pattern(std::string &msg, T new_value) const
Replace conversion pattern with new_value.
Definition: conversion_pattern.h:88