2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "sourcemod.h"
16 #include "sourcemm_api.h"
18 #include "systems/LibrarySys.h"
19 #include "sm_version.h"
24 * :TODO: This should be creating the log folder if it doesn't exist
27 void Logger::OnSourceModStartup(bool late)
29 //:TODO: read these options from a file, dont hardcode them
30 InitLogger(LoggingMode_Daily, true);
33 void Logger::OnSourceModAllShutdown()
38 void Logger::_NewMapFile()
50 tm *curtime = localtime(&t);
54 g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/logs_%02d%02d%03d.log", curtime->tm_mon + 1, curtime->tm_mday, i);
55 FILE *fp = fopen(_filename, "r");
63 m_NrmFileName.assign(_filename);
65 FILE *fp = fopen(m_NrmFileName.c_str(), "w");
68 g_SMAPI->ConPrint("[SM] Unexpected fatal logging error. SourceMod logging disabled.\n");
73 strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
74 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);
79 void Logger::_CloseFile()
87 if (!m_NrmFileName.empty())
89 fp = fopen(m_NrmFileName.c_str(), "r+");
92 fseek(fp, 0, SEEK_END);
93 LogMessage("Log file closed.");
96 m_NrmFileName.clear();
103 fp = fopen(m_ErrFileName.c_str(), "r+");
106 fseek(fp, 0, SEEK_END);
107 LogError("Error log file session closed.");
110 m_ErrFileName.clear();
113 void Logger::InitLogger(LoggingMode mode, bool startlogging)
116 m_Active = startlogging;
120 tm *curtime = localtime(&t);
121 m_CurDay = curtime->tm_mday;
124 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);
125 m_ErrFileName.assign(_filename);
129 case LoggingMode_PerMap:
133 m_DelayedStart = true;
137 case LoggingMode_Daily:
139 g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/logs_%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday);
140 m_NrmFileName.assign(_filename);
141 m_DailyPrintHdr = true;
152 void Logger::CloseLogger()
157 void Logger::LogMessage(const char *vafmt, ...)
164 if (m_mode == LoggingMode_HL2)
168 _PrintToHL2Log(vafmt, ap);
175 m_DelayedStart = false;
182 vsnprintf(msg, sizeof(msg), vafmt, ap);
188 tm *curtime = localtime(&t);
189 strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
192 if (m_mode == LoggingMode_PerMap)
194 fp = fopen(m_NrmFileName.c_str(), "a+");
198 fp = fopen(m_NrmFileName.c_str(), "a+");
205 if (m_CurDay != curtime->tm_mday)
208 g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/logs_%02d%02d.log", curtime->tm_mon + 1, curtime->tm_mday);
209 m_NrmFileName.assign(_filename);
210 m_CurDay = curtime->tm_mday;
211 m_DailyPrintHdr = true;
213 fp = fopen(m_NrmFileName.c_str(), "a+");
220 m_DailyPrintHdr = false;
221 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);
223 fprintf(fp, "L %s: %s\n", date, msg);
229 g_SMAPI->ConPrintf("L %s: %s\n", date, msg);
232 g_SMAPI->ConPrint("[SM] Unexpected fatal logging error. SourceMod logging disabled.\n");
236 void Logger::LogError(const char *vafmt, ...)
245 tm *curtime = localtime(&t);
248 strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
250 if (curtime->tm_mday != m_CurDay)
253 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);
254 m_ErrFileName.assign(_filename);
255 m_CurDay = curtime->tm_mday;
256 m_ErrMapStart = false;
262 vsnprintf(msg, sizeof(msg), vafmt, ap);
265 FILE *fp = fopen(m_ErrFileName.c_str(), "a+");
270 fprintf(fp, "L %s: SourceMod error session started\n", date);
271 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);
272 m_ErrMapStart = true;
274 fprintf(fp, "L %s: %s\n", date, msg);
277 g_SMAPI->ConPrint("[SM] Unexpected fatal logging error. SourceMod logging disabled.\n");
282 g_SMAPI->ConPrintf("L %s: %s\n", date, msg);
285 void Logger::MapChange(const char *mapname)
287 m_CurMapName.assign(mapname);
291 case LoggingMode_Daily:
293 LogMessage("-------- Mapchange to %s --------", mapname);
296 case LoggingMode_PerMap:
310 LogError("Error log file session closed.");
312 m_ErrMapStart = false;
315 void Logger::_PrintToHL2Log(const char *fmt, va_list ap)
320 len = vsnprintf(msg, sizeof(msg)-2, fmt, ap);
321 len = (len >= sizeof(msg)) ? (sizeof(msg) - 2) : len;
326 engine->LogPrint(msg);
329 const char *Logger::GetLogFileName(LogType type) const
335 return m_NrmFileName.c_str();
339 return m_ErrFileName.c_str();
348 LoggingMode Logger::GetLoggingMode() const
353 void Logger::EnableLogging()
360 LogMessage("Logging enabled manually by user.");
363 void Logger::DisableLogging()
369 LogMessage("Logging disabled manually by user.");