easylogging++: allow clipping a common filename prefix

This commit is contained in:
moneromooo-monero 2017-03-25 20:12:32 +00:00
parent 5bab044984
commit 72663f4b83
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
2 changed files with 30 additions and 4 deletions

View file

@ -767,7 +767,11 @@ std::string File::extractPathFromFilename(const std::string& fullPath, const cha
return fullPath.substr(0, lastSlashAt + 1); return fullPath.substr(0, lastSlashAt + 1);
} }
void File::buildStrippedFilename(const char* filename, char buff[], std::size_t limit) { void File::buildStrippedFilename(const char* filename, char buff[], const std::string &commonPrefix, std::size_t limit) {
if (!commonPrefix.empty()) {
if (!strncmp(filename, commonPrefix.c_str(), commonPrefix.size()))
filename += commonPrefix.size();
}
std::size_t sizeOfFilename = strlen(filename); std::size_t sizeOfFilename = strlen(filename);
if (sizeOfFilename >= limit) { if (sizeOfFilename >= limit) {
filename += (sizeOfFilename - limit); filename += (sizeOfFilename - limit);
@ -2394,7 +2398,7 @@ base::type::string_t DefaultLogBuilder::build(const LogMessage* logMessage, bool
if (logFormat->hasFlag(base::FormatFlags::File)) { if (logFormat->hasFlag(base::FormatFlags::File)) {
// File // File
base::utils::Str::clearBuff(buff, base::consts::kSourceFilenameMaxLength); base::utils::Str::clearBuff(buff, base::consts::kSourceFilenameMaxLength);
base::utils::File::buildStrippedFilename(logMessage->file().c_str(), buff); base::utils::File::buildStrippedFilename(logMessage->file().c_str(), buff, ELPP->vRegistry()->getFilenameCommonPrefix());
base::utils::Str::replaceFirstWithEscape(logLine, base::consts::kLogFileFormatSpecifier, std::string(buff)); base::utils::Str::replaceFirstWithEscape(logLine, base::consts::kLogFileFormatSpecifier, std::string(buff));
} }
if (logFormat->hasFlag(base::FormatFlags::FileBase)) { if (logFormat->hasFlag(base::FormatFlags::FileBase)) {
@ -2413,7 +2417,7 @@ base::type::string_t DefaultLogBuilder::build(const LogMessage* logMessage, bool
// Location // Location
char* buf = base::utils::Str::clearBuff(buff, char* buf = base::utils::Str::clearBuff(buff,
base::consts::kSourceFilenameMaxLength + base::consts::kSourceLineMaxLength); base::consts::kSourceFilenameMaxLength + base::consts::kSourceLineMaxLength);
base::utils::File::buildStrippedFilename(logMessage->file().c_str(), buff); base::utils::File::buildStrippedFilename(logMessage->file().c_str(), buff, ELPP->vRegistry()->getFilenameCommonPrefix());
buf = base::utils::Str::addToBuff(buff, buf, bufLim); buf = base::utils::Str::addToBuff(buff, buf, bufLim);
buf = base::utils::Str::addToBuff(":", buf, bufLim); buf = base::utils::Str::addToBuff(":", buf, bufLim);
buf = base::utils::Str::convertAndAddToBuff(logMessage->line(), base::consts::kSourceLineMaxLength, buf, bufLim, buf = base::utils::Str::convertAndAddToBuff(logMessage->line(), base::consts::kSourceLineMaxLength, buf, bufLim,
@ -3072,6 +3076,14 @@ void Loggers::clearCategories(void) {
ELPP->vRegistry()->clearCategories(); ELPP->vRegistry()->clearCategories();
} }
void Loggers::setFilenameCommonPrefix(const std::string &prefix) {
ELPP->vRegistry()->setFilenameCommonPrefix(prefix);
}
const std::string &Loggers::getFilenameCommonPrefix() {
return ELPP->vRegistry()->getFilenameCommonPrefix();
}
// VersionInfo // VersionInfo
const std::string VersionInfo::version(void) { const std::string VersionInfo::version(void) {

View file

@ -1103,7 +1103,7 @@ class File : base::StaticClass {
static std::string extractPathFromFilename(const std::string& fullPath, static std::string extractPathFromFilename(const std::string& fullPath,
const char* seperator = base::consts::kFilePathSeperator); const char* seperator = base::consts::kFilePathSeperator);
/// @brief builds stripped filename and puts it in buff /// @brief builds stripped filename and puts it in buff
static void buildStrippedFilename(const char* filename, char buff[], static void buildStrippedFilename(const char* filename, char buff[], const std::string &commonPrefix = NULL,
std::size_t limit = base::consts::kSourceFilenameMaxLength); std::size_t limit = base::consts::kSourceFilenameMaxLength);
/// @brief builds base filename and puts it in buff /// @brief builds base filename and puts it in buff
static void buildBaseFilename(const std::string& fullPath, char buff[], static void buildBaseFilename(const std::string& fullPath, char buff[],
@ -2495,11 +2495,20 @@ class VRegistry : base::NoCopy, public base::threading::ThreadSafe {
return !base::utils::hasFlag(LoggingFlag::DisableVModules, *m_pFlags); return !base::utils::hasFlag(LoggingFlag::DisableVModules, *m_pFlags);
} }
inline void setFilenameCommonPrefix(const std::string &prefix) {
m_filenameCommonPrefix = prefix;
}
inline const std::string &getFilenameCommonPrefix() const {
return m_filenameCommonPrefix;
}
private: private:
base::type::VerboseLevel m_level; base::type::VerboseLevel m_level;
base::type::EnumType* m_pFlags; base::type::EnumType* m_pFlags;
std::map<std::string, base::type::VerboseLevel> m_modules; std::map<std::string, base::type::VerboseLevel> m_modules;
std::deque<std::pair<std::string, Level>> m_categories; std::deque<std::pair<std::string, Level>> m_categories;
std::string m_filenameCommonPrefix;
}; };
} // namespace base } // namespace base
class LogMessage { class LogMessage {
@ -2735,6 +2744,7 @@ class Storage : base::NoCopy, public base::threading::ThreadSafe {
} }
return it->second; return it->second;
} }
private: private:
base::RegisteredHitCounters* m_registeredHitCounters; base::RegisteredHitCounters* m_registeredHitCounters;
base::RegisteredLoggers* m_registeredLoggers; base::RegisteredLoggers* m_registeredLoggers;
@ -3925,6 +3935,10 @@ class Loggers : base::StaticClass {
static void clearVModules(void); static void clearVModules(void);
/// @brief Clears categories /// @brief Clears categories
static void clearCategories(void); static void clearCategories(void);
/// @brief Sets filename common prefix
static void setFilenameCommonPrefix(const std::string &prefix);
/// @brief Gets filename common prefix
static const std::string &getFilenameCommonPrefix();
}; };
class VersionInfo : base::StaticClass { class VersionInfo : base::StaticClass {
public: public: