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 276 277 278 279 280 281
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "System/LogOutput.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/Log/LogUtil.h"
#include "System/Platform/Misc.h"
#include <string>
#include <set>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cassert>
#include <cstring>
#include <boost/thread/recursive_mutex.hpp>
/******************************************************************************/
/******************************************************************************/
CONFIG(bool, RotateLogFiles).defaultValue(false)
.description("rotate logfiles, old logfiles will be moved into the subfolder \"log\".");
CONFIG(std::string, LogSections).defaultValue("")
.description("Comma seperated list of enabled logsections, see infolog.txt / console output for possible values");
CONFIG(int, LogFlushLevel).defaultValue(LOG_LEVEL_ERROR)
.description("Flush the logfile when level of message is above LogFlushLevel. i.e. ERROR is flushed as default, WARNING isn't.");
/******************************************************************************/
/******************************************************************************/
static std::map<std::string, int> GetEnabledSections() {
std::map<std::string, int> sectionLevelMap;
std::string enabledSections = ",";
std::string envSections = ",";
#if defined(UNITSYNC)
#if defined(DEBUG)
// unitsync logging in debug mode always on
// configHandler cannot be accessed here in unitsync, as it may not exist.
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"));
#endif
if (getenv("SPRING_LOG_SECTIONS") != NULL) {
// allow disabling all sections from the env var by setting it to "none"
envSections += getenv("SPRING_LOG_SECTIONS");
envSections = StringToLower(envSections);
if (envSections == "none") {
enabledSections = "";
} else {
enabledSections += envSections;
}
}
enabledSections = StringToLower(enabledSections);
enabledSections = StringStrip(enabledSections, " \t\n\r");
// make the last "section:level" substring findable
if (!enabledSections.empty() && enabledSections.back() != ',')
enabledSections += ",";
// n=1 because <enabledSections> always starts with a ',' (if non-empty)
for (size_t n = 1; n < enabledSections.size(); ) {
const size_t k = enabledSections.find(",", n);
if (k != std::string::npos) {
const std::string& sub = enabledSections.substr(n, k - n);
if (!sub.empty()) {
const size_t sepChr = sub.find(":");
const std::string& logSec = (sepChr != std::string::npos)? sub.substr( 0, sepChr): sub;
const std::string& logLvl = (sepChr != std::string::npos)? sub.substr(sepChr + 1, std::string::npos): "";
if (!logLvl.empty()) {
sectionLevelMap[logSec] = StringToInt(logLvl);
} else {
#if defined(DEBUG)
sectionLevelMap[logSec] = LOG_LEVEL_DEBUG;
#else
sectionLevelMap[logSec] = DEFAULT_LOG_LEVEL;
#endif
}
}
n = k + 1;
} else {
n = k;
}
}
return sectionLevelMap;
}
/**
* @brief initialize the log sections
*
* This writes a list of all available and all enabled sections to the log.
*
* Log sections can be enabled using the configuration key "LogSections",
* or the environment variable "SPRING_LOG_SECTIONS".
*
* Both specify a comma-separated list of sections that should be enabled.
* The lists from both sources are combined, there is no overriding.
*
* A section that is enabled by default, can not be disabled.
*/
static void InitializeLogSections()
{
// the new systems (ILog.h) log-sub-systems are called sections
const std::set<const char*>& registeredSections = log_filter_section_getRegisteredSet();
// enabled sections is a superset of the ones specified in the
// environment and the ones specified in the configuration file.
const std::map<std::string, int>& enabledSections = GetEnabledSections();
std::stringstream availableLogSectionsStr;
std::stringstream enabledLogSectionsStr;
availableLogSectionsStr << "Available log sections: ";
enabledLogSectionsStr << "Enabled log sections: ";
unsigned int numRegisteredSections = 0;
unsigned int numEnabledSections = 0;
for (auto si = registeredSections.begin(); si != registeredSections.end(); ++si) {
numRegisteredSections++;
availableLogSectionsStr << ((numRegisteredSections > 1)? ", ": "");
availableLogSectionsStr << *si;
// enabled sections (keys) are in lower-case
const auto sectionIter = enabledSections.find(StringToLower(*si));
// skip if section is registered but not enabled
if (sectionIter == enabledSections.end())
continue;
// user-specified wanted level for this section
const int sectionLevel = sectionIter->second;
if (sectionLevel >= LOG_LEVEL_NONE)
continue;
// find the nearest lower known log-level (in descending order)
const int logLevel = log_util_getNearestLevel(sectionLevel);
// levels can't go lower than this
if (logLevel < 0)
continue;
log_filter_section_setMinLevel(*si, logLevel);
enabledLogSectionsStr << ((numEnabledSections > 0)? ", ": "");
enabledLogSectionsStr << *si << "(" << log_util_levelToString(logLevel) << ")";
numEnabledSections++;
}
LOG("%s", (availableLogSectionsStr.str()).c_str());
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.");
}
CLogOutput logOutput;
CLogOutput::CLogOutput()
: fileName("")
, filePath("")
{
// multiple infologs can't exist together!
assert(this == &logOutput);
SetFileName("infolog.txt");
}
CLogOutput::~CLogOutput()
{
}
void CLogOutput::SetFileName(std::string fname)
{
assert(!IsInitialized());
fileName = fname;
}
std::string CLogOutput::CreateFilePath(const std::string& fileName)
{
return (FileSystem::EnsurePathSepAtEnd(FileSystem::GetCwd()) + fileName);
}
void CLogOutput::RotateLogFile() const
{
if (!FileSystem::FileExists(filePath))
return;
// logArchiveDir: /absolute/writeable/data/dir/log/
const std::string logArchiveDir = filePath.substr(0, filePath.find_last_of("/\\") + 1) + "log" + 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
if (rename(filePath.c_str(), archivedLogFile.c_str()) != 0) {
// no log here yet
std::cerr << "Failed rotating the log file" << std::endl;
}
}
void CLogOutput::Initialize()
{
assert(configHandler != NULL);
if (IsInitialized())
return;
filePath = CreateFilePath(fileName);
if (configHandler->GetBool("RotateLogFiles"))
RotateLogFile();
log_file_addLogFile(filePath.c_str(), NULL, LOG_LEVEL_ALL, configHandler->GetInt("LogFlushLevel"));
InitializeLogSections();
LOG("LogOutput initialized.");
}
void CLogOutput::LogSystemInfo()
{
LOG("Spring %s", SpringVersion::GetFull().c_str());
LOG("Build Environment: %s", SpringVersion::GetBuildEnvironment().c_str());
LOG("Compiler Version: %s", SpringVersion::GetCompiler().c_str());
LOG("Operating System: %s", Platform::GetOS().c_str());
if (Platform::Is64Bit()) {
LOG("Word Size: 64-bit (native mode)");
} else if (Platform::Is32BitEmulation()) {
LOG("Word Size: 32-bit (emulated)");
} else {
LOG("Word Size: 32-bit (native mode)");
}
}
|