TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Appender.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef APPENDER_H
19 #define APPENDER_H
20 
21 #include <unordered_map>
22 #include <stdexcept>
23 #include <string>
24 #include <time.h>
25 #include <type_traits>
26 #include <vector>
27 #include <utility>
28 #include "Define.h"
29 
30 // Values assigned have their equivalent in enum ACE_Log_Priority
32 {
40 };
41 
42 const uint8 MaxLogLevels = 6;
43 
45 {
50 };
51 
53 {
58  APPENDER_FLAGS_USE_TIMESTAMP = 0x08, // only used by FileAppender
59  APPENDER_FLAGS_MAKE_FILE_BACKUP = 0x10 // only used by FileAppender
60 };
61 
63 {
64  LogMessage(LogLevel _level, std::string const& _type, std::string&& _text)
65  : level(_level), type(_type), text(std::forward<std::string>(_text)), mtime(time(NULL))
66  { }
67 
68  LogMessage(LogMessage const& /*other*/) = delete;
69  LogMessage& operator=(LogMessage const& /*other*/) = delete;
70 
71  static std::string getTimeStr(time_t time);
72  std::string getTimeStr();
73 
74  LogLevel const level;
75  std::string const type;
76  std::string const text;
77  std::string prefix;
78  std::string param1;
79  time_t mtime;
80 
82  uint32 Size() const
83  {
84  return static_cast<uint32>(prefix.size() + text.size());
85  }
86 };
87 
89 {
90  public:
91  Appender(uint8 _id, std::string const& name, LogLevel level = LOG_LEVEL_DISABLED, AppenderFlags flags = APPENDER_FLAGS_NONE);
92  virtual ~Appender();
93 
94  uint8 getId() const;
95  std::string const& getName() const;
96  virtual AppenderType getType() const = 0;
97  LogLevel getLogLevel() const;
98  AppenderFlags getFlags() const;
99 
100  void setLogLevel(LogLevel);
101  void write(LogMessage* message);
102  static const char* getLogLevelString(LogLevel level);
103  virtual void setRealmId(uint32 /*realmId*/) { }
104 
105  private:
106  virtual void _write(LogMessage const* /*message*/) = 0;
107 
109  std::string name;
112 };
113 
114 typedef std::unordered_map<uint8, Appender*> AppenderMap;
115 
116 typedef std::vector<char const*> ExtraAppenderArgs;
117 typedef Appender*(*AppenderCreatorFn)(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, ExtraAppenderArgs extraArgs);
118 typedef std::unordered_map<uint8, AppenderCreatorFn> AppenderCreatorMap;
119 
120 template<class AppenderImpl>
121 Appender* CreateAppender(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, ExtraAppenderArgs extraArgs)
122 {
123  return new AppenderImpl(id, name, level, flags, std::forward<ExtraAppenderArgs>(extraArgs));
124 }
125 
126 class TC_COMMON_API InvalidAppenderArgsException : public std::length_error
127 {
128 public:
129  explicit InvalidAppenderArgsException(std::string const& message) : std::length_error(message) { }
130 };
131 
132 #endif
std::unordered_map< uint8, AppenderCreatorFn > AppenderCreatorMap
Definition: Appender.h:118
std::string const type
Definition: Appender.h:75
std::string prefix
Definition: Appender.h:77
Definition: Appender.h:35
Definition: Appender.h:34
Definition: Appender.h:55
AppenderType
Definition: Appender.h:44
Definition: Appender.h:36
Definition: Appender.h:37
LogLevel const level
Definition: Appender.h:74
std::string const text
Definition: Appender.h:76
std::unordered_map< uint8, Appender * > AppenderMap
Definition: Appender.h:114
STL namespace.
arena_t NULL
Definition: jemalloc_internal.h:624
uint32 Size() const
@ Returns size of the log message content in bytes
Definition: Appender.h:82
std::vector< char const * > ExtraAppenderArgs
Definition: Appender.h:116
Definition: Appender.h:38
uint8 id
Definition: Appender.h:108
Definition: Appender.h:46
AppenderFlags
Definition: Appender.h:52
Definition: Appender.h:57
Definition: Appender.h:56
Definition: Appender.h:62
Definition: Appender.h:126
Definition: Appender.h:33
Vector2int16 & operator=(const Any &a)
uint32_t uint32
Definition: Define.h:150
LogLevel
Definition: Appender.h:31
virtual void setRealmId(uint32)
Definition: Appender.h:103
Definition: Appender.h:58
std::string name
Definition: Appender.h:109
#define TC_COMMON_API
Definition: Define.h:116
AppenderFlags flags
Definition: Appender.h:111
Definition: Appender.h:54
Definition: Appender.h:59
const uint8 MaxLogLevels
Definition: Appender.h:42
Definition: Appender.h:39
InvalidAppenderArgsException(std::string const &message)
Definition: Appender.h:129
Definition: Appender.h:47
LogLevel level
Definition: Appender.h:110
uint8_t uint8
Definition: Define.h:152
time_t mtime
Definition: Appender.h:79
Definition: Appender.h:48
uint8 flags
Definition: DisableMgr.cpp:44
std::string param1
Definition: Appender.h:78
Definition: Appender.h:88
Appender * CreateAppender(uint8 id, std::string const &name, LogLevel level, AppenderFlags flags, ExtraAppenderArgs extraArgs)
Definition: Appender.h:121
LogMessage(LogLevel _level, std::string const &_type, std::string &&_text)
Definition: Appender.h:64
Definition: Appender.h:49