ManaPlus
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes
ChatLogger Class Reference

#include <chatlogger.h>

Public Member Functions

 ChatLogger ()
 
 ~ChatLogger ()
 
void log (std::string str)
 
void log (std::string name, std::string str)
 
void loadLast (std::string name, std::list< std::string > &list, const unsigned int n) const
 
std::string getDir () const
 
void setServerName (const std::string &serverName)
 
void setBaseLogDir (const std::string &logDir)
 
void clear ()
 

Static Public Member Functions

static std::string secureName (std::string &str)
 

Private Member Functions

void setLogFile (const std::string &logFilename)
 
void setLogDir (const std::string &logDir)
 

Static Private Member Functions

static void writeTo (std::ofstream &file, const std::string &str)
 

Private Attributes

std::ofstream mLogFile
 
std::string mLogDir
 
std::string mBaseLogDir
 
std::string mServerName
 
std::string mLogFileName
 

Detailed Description

Definition at line 31 of file chatlogger.h.

Constructor & Destructor Documentation

◆ ChatLogger()

ChatLogger::ChatLogger ( )

Constructor.

Definition at line 45 of file chatlogger.cpp.

45  :
46  mLogFile(),
47  mLogDir(),
48  mBaseLogDir(),
49  mServerName(),
50  mLogFileName()
51 {
52 }
std::string mLogFileName
Definition: chatlogger.h:83
std::string mBaseLogDir
Definition: chatlogger.h:81
std::string mServerName
Definition: chatlogger.h:82
std::string mLogDir
Definition: chatlogger.h:80
std::ofstream mLogFile
Definition: chatlogger.h:79

◆ ~ChatLogger()

ChatLogger::~ChatLogger ( )

Destructor, closes log file.

Definition at line 54 of file chatlogger.cpp.

55 {
56  if (mLogFile.is_open())
57  mLogFile.close();
58 }

References mLogFile.

Member Function Documentation

◆ clear()

void ChatLogger::clear ( )

Definition at line 228 of file chatlogger.cpp.

229 {
230  mLogDir.clear();
231  mServerName.clear();
232  mLogFileName.clear();
233 }

References mLogDir, mLogFileName, and mServerName.

◆ getDir()

std::string ChatLogger::getDir ( ) const

Definition at line 124 of file chatlogger.cpp.

125 {
126  std::string date;
127 
128  time_t rawtime;
129  char buffer [81];
130 
131  time(&rawtime);
132  tm *const timeinfo = localtime(&rawtime);
133 
134  strftime(buffer, 79, "%Y-%m/%d", timeinfo);
135 
136  date = strprintf("%s/%s/%s",
137  mBaseLogDir.c_str(),
138  mServerName.c_str(),
139  buffer);
140 
141  return date;
142 }
std::string strprintf(const char *const format,...)

References mBaseLogDir, mServerName, and strprintf().

Referenced by loadLast(), and log().

◆ loadLast()

void ChatLogger::loadLast ( std::string  name,
std::list< std::string > &  list,
const unsigned int  n 
) const

Definition at line 197 of file chatlogger.cpp.

200 {
201  std::ifstream logFile;
202  std::string fileName = strprintf("%s/%s.log",
203  getDir().c_str(),
204  secureName(name).c_str());
205 
206  logFile.open(fileName.c_str(), std::ios::in);
207 
208  if (!logFile.is_open())
209  return;
210 
211  char line[710];
212  unsigned sz = CAST_U32(list.size());
213  while (logFile.getline(line, 700))
214  {
215  list.push_back(line);
216  sz ++;
217  if (sz > n)
218  {
219  list.pop_front();
220  sz --;
221  }
222  }
223 
224  if (logFile.is_open())
225  logFile.close();
226 }
#define CAST_U32
Definition: cast.h:31
static std::string secureName(std::string &str)
Definition: chatlogger.cpp:144
std::string getDir() const
Definition: chatlogger.cpp:124
std::string fileName
Definition: testmain.cpp:39

References CAST_U32, fileName, getDir(), secureName(), and strprintf().

Referenced by ChatTab::loadFromLogFile().

◆ log() [1/2]

void ChatLogger::log ( std::string  name,
std::string  str 
)

Definition at line 107 of file chatlogger.cpp.

109 {
110  const std::string &dateStr = getDir();
111  const std::string logFileName = strprintf("%s/%s.log",
112  dateStr.c_str(), secureName(name).c_str());
113 
114  if (!mLogFile.is_open() || logFileName != mLogFileName)
115  {
116  setLogDir(dateStr);
117  setLogFile(logFileName);
118  }
119 
120  str = removeColors(str);
121  writeTo(mLogFile, str);
122 }
void setLogFile(const std::string &logFilename)
Definition: chatlogger.cpp:60
static void writeTo(std::ofstream &file, const std::string &str)
Definition: chatlogger.cpp:170
void setLogDir(const std::string &logDir)
Definition: chatlogger.cpp:77
std::string removeColors(std::string msg)

References getDir(), mLogFile, mLogFileName, removeColors(), secureName(), setLogDir(), setLogFile(), strprintf(), and writeTo().

◆ log() [2/2]

void ChatLogger::log ( std::string  str)

Enters a message in the log. The message will be timestamped.

Definition at line 91 of file chatlogger.cpp.

92 {
93  const std::string &dateStr = getDir();
94  const std::string logFileName = strprintf(
95  "%s/#General.log", dateStr.c_str());
96  if (!mLogFile.is_open() ||
97  logFileName != mLogFileName)
98  {
99  setLogDir(dateStr);
100  setLogFile(logFileName);
101  }
102 
103  str = removeColors(str);
104  writeTo(mLogFile, str);
105 }

References getDir(), mLogFile, mLogFileName, removeColors(), setLogDir(), setLogFile(), strprintf(), and writeTo().

Referenced by ChatTab::saveToLogFile().

◆ secureName()

std::string ChatLogger::secureName ( std::string &  str)
static

Definition at line 144 of file chatlogger.cpp.

145 {
146  const size_t sz = name.length();
147  for (size_t f = 0; f < sz; f ++)
148  {
149  const unsigned char ch = name[f];
150  if ((ch < '0' || ch > '9') &&
151  (ch < 'a' || ch > 'z') &&
152  (ch < 'A' || ch > 'Z') &&
153  ch != '-' &&
154  ch != '+' &&
155  ch != '=' &&
156  ch != '.' &&
157  ch != ',' &&
158  ch != ')' &&
159  ch != '(' &&
160  ch != '[' &&
161  ch != ']' &&
162  ch != '#')
163  {
164  name[f] = '_';
165  }
166  }
167  return name;
168 }

Referenced by loadLast(), log(), and setServerName().

◆ setBaseLogDir()

void ChatLogger::setBaseLogDir ( const std::string &  logDir)
inline

Definition at line 63 of file chatlogger.h.

64  { mBaseLogDir = logDir; }

References mBaseLogDir.

◆ setLogDir()

void ChatLogger::setLogDir ( const std::string &  logDir)
private

Definition at line 77 of file chatlogger.cpp.

78 {
79  mLogDir = logDir;
80 
81  if (mLogFile.is_open())
82  mLogFile.close();
83 
84  DIR *const dir = opendir(mLogDir.c_str());
85  if (dir == nullptr)
86  mkdir_r(mLogDir.c_str());
87  else
88  closedir(dir);
89 }
int mkdir_r(const char *const pathname)
Create a directory, making leading components first if necessary.
Definition: mkdir.cpp:109

References mkdir_r(), mLogDir, and mLogFile.

Referenced by log().

◆ setLogFile()

void ChatLogger::setLogFile ( const std::string &  logFilename)
private

Sets the file to log to and opens it

Definition at line 60 of file chatlogger.cpp.

61 {
62  if (mLogFile.is_open())
63  mLogFile.close();
64 
65  mLogFile.open(logFilename.c_str(),
66  std::ios_base::app);
67  mLogFileName = logFilename;
68 
69  if (!mLogFile.is_open())
70  {
71  std::cout << "Warning: error while opening " <<
72  logFilename <<
73  " for writing.\n";
74  }
75 }

References mLogFile, and mLogFileName.

Referenced by log().

◆ setServerName()

void ChatLogger::setServerName ( const std::string &  serverName)

Definition at line 176 of file chatlogger.cpp.

177 {
178  mServerName = serverName;
179  if (mServerName.empty())
180  mServerName = config.getStringValue("MostUsedServerName0");
181 
182  if (mLogFile.is_open())
183  mLogFile.close();
184 
186  if (!mLogDir.empty())
187  {
188  const std::string name = pathJoin(mLogDir, mServerName);
189  DIR *const dir = opendir(name.c_str());
190  if (dir == nullptr)
191  mkdir_r(name.c_str());
192  else
193  closedir(dir);
194  }
195 }
std::string getStringValue(const std::string &key) const
Configuration config
std::string pathJoin(std::string str1, const std::string &str2)

References config, Configuration::getStringValue(), mkdir_r(), mLogDir, mLogFile, mServerName, pathJoin(), and secureName().

Referenced by ServerDialog::connectToSelectedServer().

◆ writeTo()

void ChatLogger::writeTo ( std::ofstream &  file,
const std::string &  str 
)
staticprivate

Definition at line 170 of file chatlogger.cpp.

172 {
173  file << str << std::endl;
174 }

Referenced by log().

Field Documentation

◆ mBaseLogDir

std::string ChatLogger::mBaseLogDir
private

Definition at line 81 of file chatlogger.h.

Referenced by getDir(), and setBaseLogDir().

◆ mLogDir

std::string ChatLogger::mLogDir
private

Definition at line 80 of file chatlogger.h.

Referenced by clear(), setLogDir(), and setServerName().

◆ mLogFile

std::ofstream ChatLogger::mLogFile
private

Definition at line 79 of file chatlogger.h.

Referenced by log(), setLogDir(), setLogFile(), setServerName(), and ~ChatLogger().

◆ mLogFileName

std::string ChatLogger::mLogFileName
private

Definition at line 83 of file chatlogger.h.

Referenced by clear(), log(), and setLogFile().

◆ mServerName

std::string ChatLogger::mServerName
private

Definition at line 82 of file chatlogger.h.

Referenced by clear(), getDir(), and setServerName().


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