TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
AppenderFile Class Reference

#include <AppenderFile.h>

Public Types

typedef std::integral_constant
< AppenderType, APPENDER_FILE >
::type 
TypeIndex
 

Public Member Functions

 AppenderFile (uint8 id, std::string const &name, LogLevel level, AppenderFlags flags, ExtraAppenderArgs extraArgs)
 
 ~AppenderFile ()
 
FILE * OpenFile (std::string const &name, std::string const &mode, bool backup)
 
AppenderType getType () const override
 
- Public Member Functions inherited from Appender
 Appender (uint8 _id, std::string const &name, LogLevel level=LOG_LEVEL_DISABLED, AppenderFlags flags=APPENDER_FLAGS_NONE)
 
virtual ~Appender ()
 
uint8 getId () const
 
std::string constgetName () const
 
LogLevel getLogLevel () const
 
AppenderFlags getFlags () const
 
void setLogLevel (LogLevel)
 
void write (LogMessage *message)
 
virtual void setRealmId (uint32)
 

Private Member Functions

void CloseFile ()
 
void _write (LogMessage const *message) override
 

Private Attributes

FILE * logfile
 
std::string _fileName
 
std::string _logDir
 
bool _dynamicName
 
bool _backup
 
uint64 _maxFileSize
 
std::atomic< uint64_fileSize
 

Additional Inherited Members

- Static Public Member Functions inherited from Appender
static const char * getLogLevelString (LogLevel level)
 

Member Typedef Documentation

typedef std::integral_constant<AppenderType, APPENDER_FILE>::type AppenderFile::TypeIndex

Constructor & Destructor Documentation

AppenderFile::AppenderFile ( uint8  id,
std::string const name,
LogLevel  level,
AppenderFlags  flags,
ExtraAppenderArgs  extraArgs 
)
27  :
28  Appender(id, name, level, flags),
29  logfile(NULL),
30  _logDir(sLog->GetLogsDir()),
31  _maxFileSize(0),
32  _fileSize(0)
33 {
34  if (extraArgs.empty())
35  throw InvalidAppenderArgsException(Trinity::StringFormat("Log::CreateAppenderFromConfig: Missing file name for appender %s\n", name.c_str()));
36 
37  _fileName = extraArgs[0];
38 
39  char const* mode = "a";
40  if (extraArgs.size() > 1)
41  mode = extraArgs[1];
42 
44  {
45  size_t dot_pos = _fileName.find_last_of(".");
46  if (dot_pos != std::string::npos)
47  _fileName.insert(dot_pos, sLog->GetLogsTimestamp());
48  else
49  _fileName += sLog->GetLogsTimestamp();
50  }
51 
52  if (extraArgs.size() > 2)
53  _maxFileSize = atoi(extraArgs[2]);
54  else
55  _maxFileSize = 0;
56 
57  _dynamicName = std::string::npos != _fileName.find("%s");
59 
60  if (!_dynamicName)
61  logfile = OpenFile(_fileName, mode, !strcmp(mode, "w") && _backup);
62 }
std::string _logDir
Definition: AppenderFile.h:39
std::string _fileName
Definition: AppenderFile.h:38
arena_t NULL
Definition: jemalloc_internal.h:624
#define sLog
Definition: Log.h:154
FILE * OpenFile(std::string const &name, std::string const &mode, bool backup)
Definition: AppenderFile.cpp:98
bool _backup
Definition: AppenderFile.h:41
Definition: Appender.h:126
uint64 _maxFileSize
Definition: AppenderFile.h:42
Definition: Appender.h:58
std::string name
Definition: Appender.h:109
FILE * logfile
Definition: AppenderFile.h:37
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28
AppenderFlags flags
Definition: Appender.h:111
Definition: Appender.h:59
Appender(uint8 _id, std::string const &name, LogLevel level=LOG_LEVEL_DISABLED, AppenderFlags flags=APPENDER_FLAGS_NONE)
Definition: Appender.cpp:40
std::atomic< uint64 > _fileSize
Definition: AppenderFile.h:43
LogLevel level
Definition: Appender.h:110
bool _dynamicName
Definition: AppenderFile.h:40

+ Here is the call graph for this function:

AppenderFile::~AppenderFile ( )
65 {
66  CloseFile();
67 }
void CloseFile()
Definition: AppenderFile.cpp:120

+ Here is the call graph for this function:

Member Function Documentation

void AppenderFile::_write ( LogMessage const message)
overrideprivatevirtual

Implements Appender.

70 {
71  bool exceedMaxSize = _maxFileSize > 0 && (_fileSize.load() + message->Size()) > _maxFileSize;
72 
73  if (_dynamicName)
74  {
75  char namebuf[TRINITY_PATH_MAX];
76  snprintf(namebuf, TRINITY_PATH_MAX, _fileName.c_str(), message->param1.c_str());
77  // always use "a" with dynamic name otherwise it could delete the log we wrote in last _write() call
78  FILE* file = OpenFile(namebuf, "a", _backup || exceedMaxSize);
79  if (!file)
80  return;
81  fprintf(file, "%s%s\n", message->prefix.c_str(), message->text.c_str());
82  fflush(file);
83  _fileSize += uint64(message->Size());
84  fclose(file);
85  return;
86  }
87  else if (exceedMaxSize)
88  logfile = OpenFile(_fileName, "w", true);
89 
90  if (!logfile)
91  return;
92 
93  fprintf(logfile, "%s%s\n", message->prefix.c_str(), message->text.c_str());
94  fflush(logfile);
95  _fileSize += uint64(message->Size());
96 }
std::string _fileName
Definition: AppenderFile.h:38
#define snprintf
Definition: Common.h:76
FMT_API int fprintf(std::FILE *f, CStringRef format, ArgList args)
uint64_t uint64
Definition: g3dmath.h:170
#define TRINITY_PATH_MAX
Definition: Define.h:68
FILE * OpenFile(std::string const &name, std::string const &mode, bool backup)
Definition: AppenderFile.cpp:98
bool _backup
Definition: AppenderFile.h:41
uint64 _maxFileSize
Definition: AppenderFile.h:42
FILE * logfile
Definition: AppenderFile.h:37
std::atomic< uint64 > _fileSize
Definition: AppenderFile.h:43
bool _dynamicName
Definition: AppenderFile.h:40

+ Here is the call graph for this function:

void AppenderFile::CloseFile ( )
private
121 {
122  if (logfile)
123  {
124  fclose(logfile);
125  logfile = NULL;
126  }
127 }
arena_t NULL
Definition: jemalloc_internal.h:624
FILE * logfile
Definition: AppenderFile.h:37

+ Here is the caller graph for this function:

AppenderType AppenderFile::getType ( ) const
inlineoverridevirtual

Implements Appender.

32 { return TypeIndex::value; }
const FieldDescriptor value
Definition: descriptor.h:1522
FILE * AppenderFile::OpenFile ( std::string const name,
std::string const mode,
bool  backup 
)
99 {
100  std::string fullName(_logDir + filename);
101  if (backup)
102  {
103  CloseFile();
104  std::string newName(fullName);
105  newName.push_back('.');
106  newName.append(LogMessage::getTimeStr(time(NULL)));
107  std::replace(newName.begin(), newName.end(), ':', '-');
108  rename(fullName.c_str(), newName.c_str()); // no error handling... if we couldn't make a backup, just ignore
109  }
110 
111  if (FILE* ret = fopen(fullName.c_str(), mode.c_str()))
112  {
113  _fileSize = ftell(ret);
114  return ret;
115  }
116 
117  return NULL;
118 }
std::string _logDir
Definition: AppenderFile.h:39
std::string getTimeStr()
Definition: Appender.cpp:35
arena_t NULL
Definition: jemalloc_internal.h:624
std::string replace(const std::string &s, const std::string &pattern, const std::string &replacement)
Definition: stringutils.cpp:97
void CloseFile()
Definition: AppenderFile.cpp:120
std::atomic< uint64 > _fileSize
Definition: AppenderFile.h:43

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

bool AppenderFile::_backup
private
bool AppenderFile::_dynamicName
private
std::string AppenderFile::_fileName
private
std::atomic<uint64> AppenderFile::_fileSize
private
std::string AppenderFile::_logDir
private
uint64 AppenderFile::_maxFileSize
private
FILE* AppenderFile::logfile
private

The documentation for this class was generated from the following files: