More consistency of some sort...
Oh god, what has possessed me to do this?
3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
16 #include "sourcemod.h"
17 #include "sourcemm_api.h"
19 #include "systems/LibrarySys.h"
20 #include "sm_version.h"
25 * :TODO: This should be creating the log folder if it doesn't exist
28 void Logger::OnSourceModStartup(bool late)
30 //:TODO: read these options from a file, dont hardcode them
31 InitLogger(LoggingMode_Daily, true);
34 void Logger::OnSourceModAllShutdown()
39 void Logger::_NewMapFile()
51 tm *curtime = localtime(&t);
55 g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/logs_%02d%02d%03d.log", curtime->tm_mon + 1, curtime->tm_mday, i);
56 FILE *fp = fopen(_filename, "r");
64 m_NrmFileName.assign(_filename);
66 FILE *fp = fopen(m_NrmFileName.c_str(), "w");
69 g_SMAPI->ConPrint("[SM] Unexpected fatal logging error. SourceMod logging disabled.\n");
74 strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
75 fprintf(fp, "L %s: SourceMod log file started (file \"logs_%02d%02d%03d.log\") (Version \"%s\")\n", date, curtime->tm_mon + 1, curtime->tm_mday, i, SVN_FULL_VERSION);
80 void Logger::_CloseFile()
88 if (!m_NrmFileName.empty())
90 fp = fopen(m_NrmFileName.c_str(), "r+");
93 fseek(fp, 0, SEEK_END);
94 LogMessage("Log file closed.");
97 m_NrmFileName.clear();
104 fp = fopen(m_ErrFileName.c_str(), "r+");
107 fseek(fp, 0, SEEK_END);
108 LogError("Error log file session closed.");
111 m_ErrFileName.clear();
114 void Logger::InitLogger(LoggingMode mode, bool startlogging)
117 m_Active = startlogging;
121 tm *curtime = localtime(&t);
122 m_CurDay = curtime->tm_mday;
125 g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/errors_%02d%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday, curtime->tm_year - 100);
126 m_ErrFileName.assign(_filename);
130 case LoggingMode_PerMap:
134 m_DelayedStart = true;
138 case LoggingMode_Daily:
140 g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/logs_%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday);
141 m_NrmFileName.assign(_filename);
142 m_DailyPrintHdr = true;
153 void Logger::CloseLogger()
158 void Logger::LogMessage(const char *vafmt, ...)
165 if (m_mode == LoggingMode_HL2)
169 _PrintToHL2Log(vafmt, ap);
176 m_DelayedStart = false;
183 vsnprintf(msg, sizeof(msg), vafmt, ap);
189 tm *curtime = localtime(&t);
190 strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
193 if (m_mode == LoggingMode_PerMap)
195 fp = fopen(m_NrmFileName.c_str(), "a+");
199 fp = fopen(m_NrmFileName.c_str(), "a+");
206 if (m_CurDay != curtime->tm_mday)
209 g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/logs_%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday);
210 m_NrmFileName.assign(_filename);
211 m_CurDay = curtime->tm_mday;
212 m_DailyPrintHdr = true;
214 fp = fopen(m_NrmFileName.c_str(), "a+");
221 m_DailyPrintHdr = false;
222 fprintf(fp, "L %s: SourceMod log file session started (file \"logs_%02d%02d.log\") (Version \"%s\")\n", date, curtime->tm_mon + 1, curtime->tm_mday, SVN_FULL_VERSION);
224 fprintf(fp, "L %s: %s\n", date, msg);
230 g_SMAPI->ConPrintf("L %s: %s\n", date, msg);
233 g_SMAPI->ConPrint("[SM] Unexpected fatal logging error. SourceMod logging disabled.\n");
237 void Logger::LogError(const char *vafmt, ...)
246 tm *curtime = localtime(&t);
249 strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
251 if (curtime->tm_mday != m_CurDay)
254 g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/errors_%02d%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday, curtime->tm_year - 100);
255 m_ErrFileName.assign(_filename);
256 m_CurDay = curtime->tm_mday;
257 m_ErrMapStart = false;
263 vsnprintf(msg, sizeof(msg), vafmt, ap);
266 FILE *fp = fopen(m_ErrFileName.c_str(), "a+");
271 fprintf(fp, "L %s: SourceMod error session started\n", date);
272 fprintf(fp, "L %s: Info (map \"%s\") (log file \"errors_%02d%02d%02d.log\")\n", date, m_CurMapName.c_str(), curtime->tm_mon + 1, curtime->tm_mday, curtime->tm_year - 100);
273 m_ErrMapStart = true;
275 fprintf(fp, "L %s: %s\n", date, msg);
278 g_SMAPI->ConPrint("[SM] Unexpected fatal logging error. SourceMod logging disabled.\n");
283 g_SMAPI->ConPrintf("L %s: %s\n", date, msg);
286 void Logger::MapChange(const char *mapname)
288 m_CurMapName.assign(mapname);
292 case LoggingMode_Daily:
294 LogMessage("-------- Mapchange to %s --------", mapname);
297 case LoggingMode_PerMap:
311 LogError("Error log file session closed.");
313 m_ErrMapStart = false;
316 void Logger::_PrintToHL2Log(const char *fmt, va_list ap)
321 len = vsnprintf(msg, sizeof(msg)-2, fmt, ap);
322 len = (len >= sizeof(msg)) ? (sizeof(msg) - 2) : len;
327 engine->LogPrint(msg);
330 const char *Logger::GetLogFileName(LogType type) const
336 return m_NrmFileName.c_str();
340 return m_ErrFileName.c_str();
349 LoggingMode Logger::GetLoggingMode() const
354 void Logger::EnableLogging()
361 LogMessage("Logging enabled manually by user.");
364 void Logger::DisableLogging()
370 LogMessage("Logging disabled manually by user.");