Mana
Loading...
Searching...
No Matches
chatlogger.cpp
Go to the documentation of this file.
1/*
2 * The Mana World
3 * Copyright (C) 2009-2012 The Mana Developers
4 *
5 * This file is part of The Mana World.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include "chatlogger.h"
23
24#include <iostream>
25#include <dirent.h>
26
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <sys/time.h>
30
31#ifdef _WIN32
32#include <windows.h>
33#elif defined __APPLE__
34#include <Carbon/Carbon.h>
35#endif
36
37#include "configuration.h"
38
39#include "utils/stringutils.h"
40
41static std::string getDateString()
42{
43 time_t t = time(nullptr);
44 struct tm *now = localtime(&t);
45 char buffer[11];
46 strftime(buffer, sizeof(buffer), "%Y-%m-%d", now);
47 return buffer;
48}
49
50static std::string &secureName(std::string &name)
51{
52 const size_t sz = name.length();
53 for (size_t f = 0; f < sz; f ++)
54 {
55 const unsigned char ch = name[f];
56 if ((ch < '0' || ch > '9') &&
57 (ch < 'a' || ch > 'z') &&
58 (ch < 'A' || ch > 'Z') &&
59 ch != '-' &&
60 ch != '+' &&
61 ch != '=' &&
62 ch != '.' &&
63 ch != ',' &&
64 ch != ')' &&
65 ch != '(' &&
66 ch != '[' &&
67 ch != ']' &&
68 ch != '#')
69 {
70 name[f] = '_';
71 }
72 }
73 return name;
74}
75
76static void makeDir(const std::string &dir)
77{
78#ifdef _WIN32
79 mkdir(dir.c_str());
80#else
81 mkdir(dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP);
82#endif
83}
84
85
86ChatLogger::~ChatLogger() = default;
87
88void ChatLogger::setLogFile(const std::string &logFilename)
89{
90 if (mLogFile.is_open())
91 mLogFile.close();
92
93 mLogFile.open(logFilename, std::ios_base::app);
94
95 if (!mLogFile.is_open())
96 {
97 std::cout << "Warning: error while opening " << logFilename <<
98 " for writing.\n";
99 }
100}
101
102void ChatLogger::setLogDir(const std::string &logDir)
103{
104 mLogDir = logDir;
105
106 if (mLogFile.is_open())
107 mLogFile.close();
108
109 DIR *dir = opendir(mLogDir.c_str());
110 if (!dir)
111 makeDir(mLogDir);
112 else
113 closedir(dir);
114}
115
116void ChatLogger::log(std::string str)
117{
118 std::string dateStr = getDateString();
119 if (!mLogFile.is_open() || dateStr != mLogDate)
120 {
121 mLogDate = dateStr;
122 setLogFile(strprintf("%s/%s/#General_%s.log",
123 mLogDir.c_str(),
124 mServerName.c_str(),
125 dateStr.c_str()));
126 }
127
128 removeColors(str);
129 mLogFile << str << std::endl;
130}
131
132void ChatLogger::log(std::string name, std::string str)
133{
134 std::ofstream logFile;
135 logFile.open(strprintf("%s/%s/%s_%s.log",
136 mLogDir.c_str(),
137 mServerName.c_str(),
138 secureName(name).c_str(),
139 getDateString().c_str())
140 .c_str(),
141 std::ios_base::app);
142
143 if (!logFile.is_open())
144 return;
145
146 removeColors(str);
147 logFile << str << std::endl;
148}
149
150void ChatLogger::setServerName(const std::string &serverName)
151{
152 mServerName = serverName;
153 if (mServerName.empty() && !config.servers.empty())
154 mServerName = config.servers.front().hostname;
155
156 if (mLogFile.is_open())
157 mLogFile.close();
158
159 secureName(mServerName);
160 if (!mLogDir.empty())
161 {
162 DIR *dir = opendir((mLogDir + "/" + mServerName).c_str());
163 if (!dir)
164 makeDir(mLogDir + "/" + mServerName);
165 else
166 closedir(dir);
167 }
168}
void setServerName(const std::string &serverName)
void setLogFile(const std::string &logFilename)
Sets the file to log to and opens it.
std::string mServerName
Definition chatlogger.h:50
void setLogDir(const std::string &logDir)
std::string mLogDate
Definition chatlogger.h:51
std::string mLogDir
Definition chatlogger.h:49
void log(std::string str)
Enters a message in the log.
std::ofstream mLogFile
Definition chatlogger.h:48
Config config
Global settings (config.xml)
Definition client.cpp:97
bool mkdir(const std::string &path)
Creates a directory in the write path.
Definition filesystem.h:120
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.
std::string & removeColors(std::string &msg)
Removes colors from a string.
ServerInfos servers