Legends-of-Azeroth-Pandaria-5.4.8
128 строк · 4.0 Кб
1/*
2* This file is part of the Pandaria 5.4.8 Project. See THANKS file for Copyright information
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#include "AppenderFile.h"
19#include "Log.h"
20#include "LogMessage.h"
21#include "StringConvert.h"
22#include "Util.h"
23#include <algorithm>
24
25AppenderFile::AppenderFile(uint8 id, std::string const& name, LogLevel level, AppenderFlags flags, std::vector<std::string_view> const& args) :
26Appender(id, name, level, flags),
27logfile(nullptr),
28_logDir(sLog->GetLogsDir()),
29_maxFileSize(0),
30_fileSize(0)
31{
32if (args.size() < 4)
33throw InvalidAppenderArgsException(Trinity::StringFormat("Log::CreateAppenderFromConfig: Missing file name for appender %s", name.c_str()));
34
35_fileName.assign(args[3]);
36
37std::string mode = "a";
38if (4 < args.size())
39mode.assign(args[4]);
40
41if (flags & APPENDER_FLAGS_USE_TIMESTAMP)
42{
43size_t dot_pos = _fileName.find_last_of('.');
44if (dot_pos != std::string::npos)
45_fileName.insert(dot_pos, sLog->GetLogsTimestamp());
46else
47_fileName += sLog->GetLogsTimestamp();
48}
49
50if (5 < args.size())
51{
52if (Optional<uint32> size = Trinity::StringTo<uint32>(args[5]))
53_maxFileSize = *size;
54else
55throw InvalidAppenderArgsException(Trinity::StringFormat("Log::CreateAppenderFromConfig: Invalid size '%s' for appender %s", std::string(args[5]).c_str(), name.c_str()));
56}
57
58_dynamicName = std::string::npos != _fileName.find("%s");
59_backup = (flags & APPENDER_FLAGS_MAKE_FILE_BACKUP) != 0;
60
61if (!_dynamicName)
62logfile = OpenFile(_fileName, mode, (mode == "w") && _backup);
63}
64
65AppenderFile::~AppenderFile()
66{
67CloseFile();
68}
69
70void AppenderFile::_write(LogMessage const* message)
71{
72bool exceedMaxSize = _maxFileSize > 0 && (_fileSize.load() + message->Size()) > _maxFileSize;
73
74if (_dynamicName)
75{
76char namebuf[TRINITY_PATH_MAX];
77snprintf(namebuf, TRINITY_PATH_MAX, _fileName.c_str(), message->param1.c_str());
78// always use "a" with dynamic name otherwise it could delete the log we wrote in last _write() call
79FILE* file = OpenFile(namebuf, "a", _backup || exceedMaxSize);
80if (!file)
81return;
82fprintf(file, "%s%s\n", message->prefix.c_str(), message->text.c_str());
83fflush(file);
84_fileSize += uint64(message->Size());
85fclose(file);
86return;
87}
88else if (exceedMaxSize)
89logfile = OpenFile(_fileName, "w", true);
90
91if (!logfile)
92return;
93
94fprintf(logfile, "%s%s\n", message->prefix.c_str(), message->text.c_str());
95fflush(logfile);
96_fileSize += uint64(message->Size());
97}
98
99FILE* AppenderFile::OpenFile(std::string const& filename, std::string const& mode, bool backup)
100{
101std::string fullName(_logDir + filename);
102if (backup)
103{
104CloseFile();
105std::string newName(fullName);
106newName.push_back('.');
107newName.append(LogMessage::getTimeStr(time(nullptr)));
108std::replace(newName.begin(), newName.end(), ':', '-');
109rename(fullName.c_str(), newName.c_str()); // no error handling... if we couldn't make a backup, just ignore
110}
111
112if (FILE* ret = fopen(fullName.c_str(), mode.c_str()))
113{
114_fileSize = ftell(ret);
115return ret;
116}
117
118return nullptr;
119}
120
121void AppenderFile::CloseFile()
122{
123if (logfile)
124{
125fclose(logfile);
126logfile = nullptr;
127}
128}
129