3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
33 #include "sourcemod.h"
34 #include "sourcemm_api.h"
35 #include "sm_stringutil.h"
37 #include "systems/LibrarySys.h"
39 #include "sm_version.h"
44 * :TODO: This should be creating the log folder if it doesn't exist
47 ConfigResult Logger::OnSourceModConfigChanged(const char *key,
53 if (strcasecmp(key, "Logging") == 0)
57 if (strcasecmp(value, "on") == 0)
60 } else if (strcasecmp(value, "off") == 0) {
63 UTIL_Format(error, maxlength, "Invalid value: must be \"on\" or \"off\"");
64 return ConfigResult_Reject;
67 if (source == ConfigSource_Console)
69 state ? EnableLogging() : DisableLogging();
71 m_InitialState = state;
74 return ConfigResult_Accept;
75 } else if (strcasecmp(key, "LogMode") == 0) {
76 if (strcasecmp(value, "daily") == 0)
78 m_Mode = LoggingMode_Daily;
79 } else if (strcasecmp(value, "map") == 0) {
80 m_Mode = LoggingMode_PerMap;
81 } else if (strcasecmp(value, "game") == 0) {
82 m_Mode = LoggingMode_Game;
84 UTIL_Format(error, maxlength, "Invalid value: must be [daily|map|game]");
85 return ConfigResult_Reject;
88 return ConfigResult_Accept;
91 return ConfigResult_Ignore;
94 void Logger::OnSourceModStartup(bool late)
99 void Logger::OnSourceModAllShutdown()
104 void Logger::OnSourceModLevelChange(const char *mapName)
109 void Logger::_NewMapFile()
116 /* Append "Log file closed" to previous log file */
124 tm *curtime = localtime(&t);
128 g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/L%02d%02d%03d.log", curtime->tm_mon + 1, curtime->tm_mday, i);
129 FILE *fp = fopen(_filename, "r");
137 m_NrmFileName.assign(_filename);
139 FILE *fp = fopen(m_NrmFileName.c_str(), "w");
142 g_SMAPI->ConPrint("[SM] Unexpected fatal logging error. SourceMod logging disabled.\n");
147 strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
148 fprintf(fp, "L %s: SourceMod log file started (file \"L%02d%02d%03d.log\") (Version \"%s\")\n", date, curtime->tm_mon + 1, curtime->tm_mday, i, SVN_FULL_VERSION);
153 void Logger::_CloseFile()
161 if (!m_NrmFileName.empty())
163 fp = fopen(m_NrmFileName.c_str(), "r+");
166 fseek(fp, 0, SEEK_END);
167 LogMessage("Log file closed.");
170 m_NrmFileName.clear();
177 fp = fopen(m_ErrFileName.c_str(), "r+");
180 fseek(fp, 0, SEEK_END);
181 LogError("Error log file session closed.");
184 m_ErrFileName.clear();
187 void Logger::InitLogger(LoggingMode mode)
190 m_Active = m_InitialState;
194 tm *curtime = localtime(&t);
195 m_CurDay = curtime->tm_mday;
198 g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/errors_%04d%02d%02d.log", curtime->tm_year + 1900, curtime->tm_mon + 1, curtime->tm_mday);
199 m_ErrFileName.assign(_filename);
203 case LoggingMode_PerMap:
207 m_DelayedStart = true;
211 case LoggingMode_Daily:
213 g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/L%04d%02d%02d.log", curtime->tm_year + 1900, curtime->tm_mon + 1, curtime->tm_mday);
214 m_NrmFileName.assign(_filename);
215 m_DailyPrintHdr = true;
226 void Logger::CloseLogger()
231 void Logger::LogToOpenFile(FILE *fp, const char *msg, ...)
240 LogToOpenFileEx(fp, msg, ap);
244 void Logger::LogToOpenFileEx(FILE *fp, const char *msg, va_list ap)
252 UTIL_FormatArgs(buffer, sizeof(buffer), msg, ap);
257 tm *curtime = localtime(&t);
258 strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
260 fprintf(fp, "L %s: %s\n", date, buffer);
261 g_SMAPI->ConPrintf("L %s: %s\n", date, buffer);
264 void Logger::LogMessage(const char *vafmt, ...)
271 if (m_Mode == LoggingMode_Game)
275 _PrintToGameLog(vafmt, ap);
282 m_DelayedStart = false;
288 tm *curtime = localtime(&t);
291 if (m_Mode == LoggingMode_PerMap)
293 fp = fopen(m_NrmFileName.c_str(), "a+");
297 fp = fopen(m_NrmFileName.c_str(), "a+");
304 if (m_CurDay != curtime->tm_mday)
307 g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/L%04d%02d%02d.log", curtime->tm_year + 1900, curtime->tm_mon + 1, curtime->tm_mday);
308 m_NrmFileName.assign(_filename);
309 m_CurDay = curtime->tm_mday;
310 m_DailyPrintHdr = true;
312 fp = fopen(m_NrmFileName.c_str(), "a+");
320 m_DailyPrintHdr = false;
321 strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
322 fprintf(fp, "L %s: SourceMod log file session started (file \"L%04d%02d%02d.log\") (Version \"%s\")\n", date, curtime->tm_year + 1900, curtime->tm_mon + 1, curtime->tm_mday, SVN_FULL_VERSION);
326 LogToOpenFileEx(fp, vafmt, ap);
335 g_SMAPI->ConPrint("[SM] Unexpected fatal logging error. SourceMod logging disabled.\n");
339 void Logger::LogError(const char *vafmt, ...)
348 tm *curtime = localtime(&t);
350 if (curtime->tm_mday != m_CurDay)
353 g_SourceMod.BuildPath(Path_SM, _filename, sizeof(_filename), "logs/errors_%04d%02d%02d.log", curtime->tm_year + 1900, curtime->tm_mon + 1, curtime->tm_mday);
354 m_ErrFileName.assign(_filename);
355 m_CurDay = curtime->tm_mday;
356 m_ErrMapStart = false;
359 FILE *fp = fopen(m_ErrFileName.c_str(), "a+");
365 strftime(date, sizeof(date), "%m/%d/%Y - %H:%M:%S", curtime);
366 fprintf(fp, "L %s: SourceMod error session started\n", date);
367 fprintf(fp, "L %s: Info (map \"%s\") (file \"errors_%04d%02d%02d.log\")\n", date, m_CurMapName.c_str(), curtime->tm_year + 1900, curtime->tm_mon + 1, curtime->tm_mday);
368 m_ErrMapStart = true;
372 LogToOpenFileEx(fp, vafmt, ap);
376 g_SMAPI->ConPrint("[SM] Unexpected fatal logging error. SourceMod logging disabled.\n");
382 void Logger::MapChange(const char *mapname)
384 m_CurMapName.assign(mapname);
388 case LoggingMode_Daily:
390 LogMessage("-------- Mapchange to %s --------", mapname);
393 case LoggingMode_PerMap:
407 LogError("Error log file session closed.");
409 m_ErrMapStart = false;
412 void Logger::_PrintToGameLog(const char *fmt, va_list ap)
417 len = vsnprintf(msg, sizeof(msg)-2, fmt, ap);
418 len = (len >= sizeof(msg)) ? (sizeof(msg) - 2) : len;
423 engine->LogPrint(msg);
426 const char *Logger::GetLogFileName(LogType type) const
432 return m_NrmFileName.c_str();
436 return m_ErrFileName.c_str();
445 LoggingMode Logger::GetLoggingMode() const
450 void Logger::EnableLogging()
457 LogMessage("[SM] Logging enabled manually by user.");
460 void Logger::DisableLogging()
466 LogMessage("[SM] Logging disabled manually by user.");
470 void Logger::LogFatal(const char *msg, ...)
472 /* :TODO: make this print all pretty-like
473 * In fact, the pretty log printing function should be abstracted.
474 * It's already implemented twice which is bad.
477 char path[PLATFORM_MAX_PATH];
478 g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "sourcemod_fatal.log");
479 FILE *fp = fopen(path, "at");
482 /* We're just doomed, aren't we... */
488 vfprintf(fp, msg, ap);