1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "System/Platform/Win/win32.h"
#include "System/LogOutput.h"
#include "lib/gml/gmlmut.h"
#include "System/Util.h"
#include "Game/GameVersion.h"
#include "System/Config/ConfigHandler.h"
#include "System/FileSystem/FileSystem.h"
#include "System/Log/DefaultFilter.h"
#include "System/Log/FileSink.h"
#include "System/Log/ILog.h"
#include "System/Log/Level.h"
#include "System/mmgr.h"
#include <string>
#include <set>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cassert>
#include <cstring>
#include <boost/thread/recursive_mutex.hpp>
#ifdef _MSC_VER
#include <windows.h>
#endif
/******************************************************************************/
/******************************************************************************/
CONFIG(std::string, RotateLogFiles).defaultValue("auto");
CONFIG(std::string, LogSections).defaultValue("");
CONFIG(std::string, LogSubsystems).defaultValue(""); // XXX deprecated on 22. August 2011, before the 0.83 release
/******************************************************************************/
/******************************************************************************/
CLogOutput logOutput;
static std::ofstream* filelog = NULL;
static bool initialized = false;
CLogOutput::CLogOutput()
: fileName("")
, filePath("")
{
// multiple infologs can't exist together!
assert(this == &logOutput);
assert(!filelog);
SetFileName("infolog.txt");
bool doRotateLogFiles = false;
std::string rotatePolicy = "auto";
if (configHandler != NULL) {
rotatePolicy = configHandler->GetString("RotateLogFiles");
}
if (rotatePolicy == "always") {
doRotateLogFiles = true;
} else if (rotatePolicy == "never") {
doRotateLogFiles = false;
} else { // auto
#ifdef DEBUG
doRotateLogFiles = true;
#else
doRotateLogFiles = false;
#endif
}
SetLogFileRotating(doRotateLogFiles);
}
CLogOutput::~CLogOutput()
{
End();
}
void CLogOutput::End()
{
GML_STDMUTEX_LOCK_NOPROF(log); // End
SafeDelete(filelog);
//log_file_removeLogFile(filePath.c_str());
}
const std::string& CLogOutput::GetFileName() const
{
return fileName;
}
const std::string& CLogOutput::GetFilePath() const
{
assert(initialized);
return filePath;
}
void CLogOutput::SetFileName(std::string fname)
{
GML_STDMUTEX_LOCK_NOPROF(log); // SetFileName
assert(!initialized);
fileName = fname;
}
std::string CLogOutput::CreateFilePath(const std::string& fileName)
{
return FileSystem::GetCwd() + (char)FileSystem::GetNativePathSeparator() + fileName;
}
void CLogOutput::SetLogFileRotating(bool enabled)
{
assert(!initialized);
rotateLogFiles = enabled;
}
bool CLogOutput::IsLogFileRotating() const
{
return rotateLogFiles;
}
void CLogOutput::RotateLogFile() const
{
if (IsLogFileRotating()) {
if (FileSystem::FileExists(filePath)) {
// logArchiveDir: /absolute/writeable/data/dir/log/
std::string logArchiveDir = filePath.substr(0, filePath.find_last_of("/\\") + 1);
logArchiveDir = logArchiveDir + "log" + (char)FileSystem::GetNativePathSeparator();
const std::string archivedLogFile = logArchiveDir + FileSystem::GetFileModificationDate(filePath) + "_" + fileName;
// create the log archive dir if it does not exist yet
if (!FileSystem::DirExists(logArchiveDir)) {
FileSystem::CreateDirectory(logArchiveDir);
}
// move the old log to the archive dir
const int moveError = rename(filePath.c_str(), archivedLogFile.c_str());
if (moveError != 0) {
// no log here yet
std::cerr << "Failed rotating the log file" << std::endl;
}
}
}
}
void CLogOutput::Initialize()
{
if (initialized) return;
filePath = CreateFilePath(fileName);
RotateLogFile();
/*filelog = new std::ofstream(filePath.c_str());
if (filelog->bad())
SafeDelete(filelog);*/
log_file_addLogFile(filePath.c_str());
initialized = true;
InitializeSections();
/*std::vector<std::string>::iterator pili;
for (pili = preInitLog().begin(); pili != preInitLog().end(); ++pili) {
ToFile(*pili);
}
preInitLog().clear();*/
LOG("LogOutput initialized.");
LOG("Spring %s", SpringVersion::GetFull().c_str());
LOG("Build date/time: %s", SpringVersion::GetBuildTime().c_str());
LOG("Build environment: %s", SpringVersion::GetBuildEnvironment().c_str());
LOG("Compiler: %s", SpringVersion::GetCompiler().c_str());
}
void CLogOutput::InitializeSections()
{
// the new systems (ILog.h) log-sub-systems are called sections:
const std::set<const char*> sections = log_filter_section_getRegisteredSet();
{
std::stringstream logSectionsStr;
logSectionsStr << "Available log sections: ";
int numSec = 0;
std::set<const char*>::const_iterator si;
for (si = sections.begin(); si != sections.end(); ++si) {
if (numSec > 0) {
logSectionsStr << ", ";
}
logSectionsStr << *si;
numSec++;
}
LOG("%s", logSectionsStr.str().c_str());
}
// enabled sections is a superset of the ones specified in the environment
// and the ones specified in the configuration file.
// configHandler cannot be accessed here in unitsync, as it may not exist.
std::string enabledSections = ",";
#if defined(UNITSYNC)
#if defined(DEBUG)
// unitsync logging in debug mode always on
enabledSections += "unitsync,ArchiveScanner,";
#endif
#else
#if defined(DEDICATED)
enabledSections += "DedicatedServer,";
#endif
#if !defined(DEBUG)
// Always show at least INFO level of these sections
enabledSections += "Sound,";
#endif
enabledSections += StringToLower(configHandler->GetString("LogSections")) + ",";
enabledSections += StringToLower(configHandler->GetString("LogSubsystems")) + ","; // XXX deprecated on 22. August 2011, before the 0.83 release
#endif
const char* const envSec = getenv("SPRING_LOG_SECTIONS");
const char* const envSubsys = getenv("SPRING_LOG_SUBSYSTEMS"); // XXX deprecated on 22. August 2011, before the 0.83 release
std::string env;
if (envSec != NULL) {
env += ",";
env += envSec;
}
if (envSubsys != NULL) {
env += ",";
env += envSubsys;
}
if (!env.empty()) {
// this allows to disable all sections from the env var
std::string envSections(StringToLower(env));
if (envSections == std::string("none")) {
enabledSections = "";
} else {
enabledSections += envSections + ",";
}
}
const std::string enabledSectionsLC = StringToLower(enabledSections);
{
std::stringstream enabledLogSectionsStr;
enabledLogSectionsStr << "Enabled log sections: ";
int numSec = 0;
// new log sections
std::set<const char*>::const_iterator si;
for (si = sections.begin(); si != sections.end(); ++si) {
const std::string name = StringToLower(*si);
const bool found = (enabledSectionsLC.find("," + name + ",") != std::string::npos);
if (found) {
if (numSec > 0) {
enabledLogSectionsStr << ", ";
}
#if defined(DEBUG)
log_filter_section_setMinLevel(*si, LOG_LEVEL_DEBUG);
enabledLogSectionsStr << *si << "(LOG_LEVEL_DEBUG)";
#else // defined(DEBUG)
log_filter_section_setMinLevel(*si, LOG_LEVEL_INFO);
enabledLogSectionsStr << *si << "(LOG_LEVEL_INFO)";
#endif // defined(DEBUG)
numSec++;
}
}
LOG("%s", enabledLogSectionsStr.str().c_str());
}
LOG("Enable or disable log sections using the LogSections configuration key");
LOG(" or the SPRING_LOG_SECTIONS environment variable (both comma separated).");
LOG(" Use \"none\" to disable the default log sections.");
}
|